-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path_cffi_api_util.py
497 lines (394 loc) · 16.9 KB
/
_cffi_api_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import os, warnings
from functools import partial
import numpy as np
# UTF8 under testing
codec = 'UTF8'
interface_classes = set()
warn_wrong_case = False
def set_case_insensitive_attributes(use=True, warn=False):
'''
This function is provided to allow easier migration from `win32com.client`.
When used with late bindings, `win32com` allows using mixed-case names for
any of the COM-related items. When migrating or testing with DSS Python,
users can then use this function to continue using the same code, optionally
emitting warnings when the canonical casing is different from the one used.
Note that there is a small overhead for allowing case-insensitive names,
thus is not recommended to continue using it after migration/adjustments to
the user code.
'''
if use:
global warn_wrong_case
warn_wrong_case = warn
if warn_wrong_case:
Base.__getattr__ = Base._getattr_case_check
Base.__setattr__ = Base._setattr_case_check
else:
Base.__getattr__ = Base._getattr
Base.__setattr__ = Base._setattr
elif Base.__getattr__ == Base._getattr or Base.__getattr__ == Base._getattr_case_check:
del Base.__setattr__
del Base.__getattr__
class DSSException(Exception):
def __str__(self):
return f'(#{self.args[0]}) {self.args[1]}'
# For backwards compatibility
DssException = DSSException
use_com_compat = set_case_insensitive_attributes
class CtxLib:
'''
Exposes a CFFI Lib object pre-binding the DSSContext (`ctx`) object to the
`ctx_*` functions.
'''
def __init__(self, ctx, lib):
done = set()
# First, process all `ctx_*`` functions
for name, value in vars(lib).items():
is_ctx = name.startswith('ctx_')
if not is_ctx and not name.startswith('Batch_Create'):
continue
# Keep the basic management functions alone
if name not in ('ctx_New', 'ctx_Dispose', 'ctx_Get_Prime', 'ctx_Set_Prime', ):
if is_ctx:
name = name[4:]
setattr(self, name, partial(value, ctx))
else:
setattr(self, name, value)
done.add(name)
# Then the remaining fields
for name, value in vars(lib).items():
if name.startswith('ctx_') or name in done:
continue
setattr(self, name, value)
class Base(object):
__slots__ = [
'_lib',
'_api_util',
'_get_string',
'_get_float64_array',
'_get_float64_gr_array',
'_get_int32_array',
'_get_int32_gr_array',
'_get_int8_array',
'_get_int8_gr_array',
'_get_string_array',
'_prepare_float64_array',
'_prepare_int32_array',
'_prepare_string_array',
'_errorPtr',
]
def __init__(self, api_util):
self._lib = api_util.lib
self._api_util = api_util
self._get_string = api_util.get_string
self._get_float64_array = api_util.get_float64_array
self._get_float64_gr_array = api_util.get_float64_gr_array
self._get_int32_array = api_util.get_int32_array
self._get_int32_gr_array = api_util.get_int32_gr_array
self._get_int8_array = api_util.get_int8_array
self._get_int8_gr_array = api_util.get_int8_gr_array
self._get_string_array = api_util.get_string_array
self._prepare_float64_array = api_util.prepare_float64_array
self._prepare_int32_array = api_util.prepare_int32_array
self._prepare_string_array = api_util.prepare_string_array
self._errorPtr = self._lib.Error_Get_NumberPtr()
cls = type(self)
if cls not in interface_classes:
interface_classes.add(cls)
cls._dss_original_attributes = {a for a in dir(self) if not a.startswith('_')}
lowercase_map = {a.lower(): a for a in cls._dss_original_attributes}
cls._dss_attributes = lowercase_map
def _check_for_error(self, result=None):
'''Checks for an OpenDSS error. Raises an exception if any, otherwise returns the `result` parameter.'''
if self._errorPtr[0]:
error_num = self._errorPtr[0]
self._errorPtr[0] = 0
raise DSSException(error_num, self._get_string(self._lib.Error_Get_Description()))
return result
# for backwards compatibility
CheckForError = _check_for_error
def _getattr(self, key):
if key.startswith('_'):
return object.__getattribute__(self, key)
key = self.__class__._dss_attributes.get(key.lower(), key)
return object.__getattribute__(self, key)
def _getattr_case_check(self, key):
if key.startswith('_'):
return object.__getattribute__(self, key)
correct_key = self.__class__._dss_attributes.get(key.lower(), key)
if key != correct_key:
warnings.warn('Wrong case for getter {}.{}: {}'.format(self.__class__.__name__, correct_key, key))
return object.__getattribute__(self, correct_key)
def _setattr(self, key, value):
key = self.__class__._dss_attributes.get(key.lower(), key)
object.__setattr__(self, key, value)
def _setattr_case_check(self, key, value):
correct_key = self.__class__._dss_attributes.get(key.lower(), key)
if key != correct_key:
warnings.warn('Wrong case for setter {}.{}: {}'.format(self.__class__.__name__, correct_key, key))
key = self.__class__._dss_attributes.get(key.lower(), key)
object.__setattr__(self, key, value)
def _decode_and_free_string(self, s) -> str:
res = self._ffi.string(s).decode(self._api_util.codec)
self._lib.DSS_Dispose_String(s)
self._check_for_error()
return res
class CffiApiUtil(object):
def __init__(self, ffi, lib, ctx=None):
self.codec = codec
self.ctx = ctx
self.ffi = ffi
self.lib_unpatched = lib
if ctx is None:
self.lib = lib
else:
self.lib = CtxLib(ctx, lib)
self.init_buffers()
def __delete__(self):
if self.ctx is None:
return
if self.lib.ctx_Get_Prime() != self.ctx:
self.lib.ctx_Dispose(self.ctx)
def init_buffers(self):
tmp_string_pointers = (self.ffi.new('char****'), self.ffi.new('int32_t**'))
tmp_float64_pointers = (self.ffi.new('double***'), self.ffi.new('int32_t**'))
tmp_int32_pointers = (self.ffi.new('int32_t***'), self.ffi.new('int32_t**'))
tmp_int8_pointers = (self.ffi.new('int8_t***'), self.ffi.new('int32_t**'))
# reorder pointers so data pointers are first, count pointers last
ptr_args = [
ptr
for ptrs in zip(tmp_string_pointers, tmp_float64_pointers, tmp_int32_pointers, tmp_int8_pointers)
for ptr in ptrs
]
self.lib.DSS_GetGRPointers(*ptr_args)
# we don't need to keep the extra indirections
self.gr_string_pointers = (tmp_string_pointers[0][0], tmp_string_pointers[1][0])
self.gr_float64_pointers = (tmp_float64_pointers[0][0], tmp_float64_pointers[1][0])
self.gr_int32_pointers = (tmp_int32_pointers[0][0], tmp_int32_pointers[1][0])
self.gr_int8_pointers = (tmp_int8_pointers[0][0], tmp_int8_pointers[1][0])
def clear_buffers(self):
self.lib.DSS_DisposeGRData()
self.lib.DSS_ResetStringBuffer()
self.init_buffers()
def get_string(self, b):
if b != self.ffi.NULL:
return self.ffi.string(b).decode(self.codec)
return u''
def get_float64_array(self, func, *args):
ptr = self.ffi.new('double**')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
res = np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 8), dtype=np.float64).copy()
self.lib.DSS_Dispose_PDouble(ptr)
if cnt[3] != 0:
# If the last element is filled, we have a matrix. Otherwise, the
# matrix feature is disabled or the result is indeed a vector
return res.reshape((cnt[2], cnt[3]))
return res
def get_float64_gr_array(self):
ptr, cnt = self.gr_float64_pointers
if cnt[3] != 0:
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 8), dtype=np.float64).copy().reshape((cnt[2], cnt[3]))
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 8), dtype=np.float64).copy()
def get_int32_array(self, func, *args):
ptr = self.ffi.new('int32_t**')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
res = np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 4), dtype=np.int32).copy()
self.lib.DSS_Dispose_PInteger(ptr)
if cnt[3] != 0:
# If the last element is filled, we have a matrix. Otherwise, the
# matrix feature is disabled or the result is indeed a vector
return res.reshape((cnt[2], cnt[3]))
return res
def get_ptr_array(self, func, *args):
ptr = self.ffi.new('void***')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
res = np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * np.dtype(np.uintp).itemsize), dtype=np.uintp).copy()
self.lib.DSS_Dispose_PPointer(ptr)
return res
def get_int32_gr_array(self):
ptr, cnt = self.gr_int32_pointers
if cnt[3] != 0:
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 4), dtype=np.int32).copy().reshape((cnt[2], cnt[3]))
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 4), dtype=np.int32).copy()
def get_int8_array(self, func, *args):
ptr = self.ffi.new('int8_t**')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
res = np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 1), dtype=np.int8).copy()
self.lib.DSS_Dispose_PByte(ptr)
if cnt[3] != 0:
# If the last element is filled, we have a matrix. Otherwise, the
# matrix feature is disabled or the result is indeed a vector
return res.reshape((cnt[2], cnt[3]))
return res
def get_int8_gr_array(self):
ptr, cnt = self.gr_int8_pointers
if cnt[3] != 0:
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 1), dtype=np.int8).copy().reshape((cnt[2], cnt[3]))
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 1), dtype=np.int8).copy()
def get_string_array(self, func, *args):
ptr = self.ffi.new('char***')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
if not cnt[0]:
res = []
else:
actual_ptr = ptr[0]
if actual_ptr == self.ffi.NULL:
res = []
else:
codec = self.codec
str_ptrs = self.ffi.unpack(actual_ptr, cnt[0])
#res = [(str(self.ffi.string(str_ptr).decode(codec)) if (str_ptr != self.ffi.NULL) else None) for str_ptr in str_ptrs]
res = [(self.ffi.string(str_ptr).decode(codec) if (str_ptr != self.ffi.NULL) else u'') for str_ptr in str_ptrs]
self.lib.DSS_Dispose_PPAnsiChar(ptr, cnt[1])
return res
def get_string_array2(self, func, *args): # for compatibility with OpenDSSDirect.py
ptr = self.ffi.new('char***')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
if not cnt[0]:
res = []
else:
actual_ptr = ptr[0]
if actual_ptr == self.ffi.NULL:
res = []
else:
codec = self.codec
res = [(str(self.ffi.string(actual_ptr[i]).decode(codec)) if (actual_ptr[i] != self.ffi.NULL) else '') for i in range(cnt[0])]
if res == [u'']:
# most COM methods return an empty array as an
# array with an empty string
res = []
if len(res) == 1 and res[0].lower() == 'none':
res = []
self.lib.DSS_Dispose_PPAnsiChar(ptr, cnt[1])
return res
def get_float64_array2(self, func, *args):
ptr = self.ffi.new('double**')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
if not cnt[0]:
res = []
else:
res = self.ffi.unpack(ptr[0], cnt[0])
self.lib.DSS_Dispose_PDouble(ptr)
return res
def get_float64_gr_array2(self):
ptr, cnt = self.gr_float64_pointers
return self.ffi.unpack(ptr[0], cnt[0])
def get_int32_array2(self, func, *args):
ptr = self.ffi.new('int32_t**')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
if not cnt[0]:
res = None
else:
res = self.ffi.unpack(ptr[0], cnt[0])
self.lib.DSS_Dispose_PInteger(ptr)
return res
def get_int32_gr_array2(self):
ptr, cnt = self.gr_int32_pointers
return self.ffi.unpack(ptr[0], cnt[0])
def get_int8_array2(self, func, *args):
ptr = self.ffi.new('int8_t**')
cnt = self.ffi.new('int32_t[4]')
func(ptr, cnt, *args)
if not cnt[0]:
res = None
else:
res = self.ffi.unpack(ptr[0], cnt[0])
self.lib.DSS_Dispose_PByte(ptr)
return res
def get_int8_gr_array2(self):
ptr, cnt = self.gr_int8_pointers
return self.ffi.unpack(ptr[0], cnt[0])
def prepare_float64_array(self, value):
if type(value) is not np.ndarray or value.dtype != np.float64:
value = np.array(value, dtype=np.float64)
ptr = self.ffi.cast('double*', self.ffi.from_buffer(value.data))
cnt = value.size
return value, ptr, cnt
def prepare_int32_array(self, value):
if type(value) is not np.ndarray or value.dtype != np.int32:
value = np.array(value, dtype=np.int32)
ptr = self.ffi.cast('int32_t*', self.ffi.from_buffer(value.data))
cnt = value.size
return value, ptr, cnt
def prepare_string_array(self, value):
if value is None:
raise ValueError("Value cannot be None!")
ptrs = []
value_enc = []
codec = self.codec
for v in value:
if type(v) is not bytes:
v = v.encode(codec)
value_enc.append(v)
ptrs.append(self.ffi.new("char[]", v))
# Need to keep reference to every pointer to they don't get
# garbage collected too early
return value_enc or value, ptrs, len(ptrs)
class Iterable(Base):
__slots__ = [
'_Get_First',
'_Get_Next',
'_Get_Count',
'_Get_AllNames',
'_Get_Name',
'_Set_Name',
'_Get_idx',
'_Set_idx'
]
def __init__(self, api_util):
Base.__init__(self, api_util)
prefix = type(self).__name__[1:]
self._Get_First = getattr(self._lib, '{}_Get_First'.format(prefix))
self._Get_Next = getattr(self._lib, '{}_Get_Next'.format(prefix))
self._Get_Count = getattr(self._lib, '{}_Get_Count'.format(prefix))
self._Get_AllNames = getattr(self._lib, '{}_Get_AllNames'.format(prefix))
self._Get_Name = getattr(self._lib, '{}_Get_Name'.format(prefix))
self._Set_Name = getattr(self._lib, '{}_Set_Name'.format(prefix))
self._Get_idx = getattr(self._lib, '{}_Get_idx'.format(prefix))
self._Set_idx = getattr(self._lib, '{}_Set_idx'.format(prefix))
@property
def First(self):
'''Sets the first object of this type active. Returns 0 if none.'''
return self.CheckForError(self._Get_First())
@property
def Next(self):
'''(read-only) Sets next object of this type active. Returns 0 if no more.'''
return self.CheckForError(self._Get_Next())
@property
def Count(self):
'''Number of objects of this type'''
return self.CheckForError(self._Get_Count())
def __len__(self):
return self.CheckForError(self._Get_Count())
def __iter__(self):
idx = self.CheckForError(self._Get_First())
while idx != 0:
yield self
idx = self.CheckForError(self._Get_Next())
@property
def AllNames(self):
'''Array of all names of this object type'''
return self.CheckForError(self._get_string_array(self._Get_AllNames))
@property
def Name(self):
'''Gets the current name or sets the active object of this type by name'''
return self._get_string(self.CheckForError(self._Get_Name()))
@Name.setter
def Name(self, Value):
if type(Value) is not bytes:
Value = Value.encode(self._api_util.codec)
self.CheckForError(self.CheckForError(self._Set_Name(Value)))
@property
def idx(self):
'''Gets the current index or sets the active object of this type by index'''
return self.CheckForError(self._Get_idx())
@idx.setter
def idx(self, Value):
self.CheckForError(self._Set_idx(Value))