|
| 1 | +import etgtools |
| 2 | +import etgtools.tweaker_tools as tools |
| 3 | + |
| 4 | +from etgtools.extractors import ClassDef, CppMethodDef_cffi, ParamDef |
| 5 | + |
1 | 6 | def run(module):
|
2 |
| - # TODO: replicate functions added in etg/sip/stream.py |
3 |
| - pass |
| 7 | + # Include a C++ class that can wrap a Python file-like object so it can |
| 8 | + # be used as a wxInputStream |
| 9 | + c = ClassDef(name='wxPyInputStream') |
| 10 | + c.includeCppCode('src/cffi/stream_input.cpp') |
| 11 | + module.addItem(c) |
| 12 | + c.addItem(CppMethodDef_cffi( |
| 13 | + c.name, isCtor=True, |
| 14 | + pyArgs=etgtools.ArgsString('(WL_Self self, WL_Object file)'), |
| 15 | + pyBody="""\ |
| 16 | + with wrapper_lib.get_refcounted_handle(file) as handle: |
| 17 | + self._cpp_obj = call(handle) |
| 18 | + """, |
| 19 | + cReturnType='void*', |
| 20 | + cArgsString='(void *handle)', |
| 21 | + cBody="return new WL_CLASS_NAME(handle);", |
| 22 | + originalCppArgs=etgtools.ArgsString('(void *handle)'), |
| 23 | + )) |
| 24 | + |
| 25 | + # Add the following code to the class body to initialize the callback |
| 26 | + module.addCdef_cffi("""\ |
| 27 | + size_t (*PyInputStream_OnSysRead)(void*, void*, size_t); |
| 28 | + int (*PyInputStream_IsSeekable)(void*); |
| 29 | + size_t (*PyInputStream_TellI)(void*); |
| 30 | + size_t (*PyInputStream_SeekI)(void*, size_t, int); |
| 31 | + """) |
| 32 | + c.pyCode_cffi = """\ |
| 33 | + @ffi.callback('size_t(*)(void*, void*, size_t)') |
| 34 | + def _PyInputStream_OnSysRead(handle, buffer, bufsize): |
| 35 | + file = ffi.from_handle(handle) |
| 36 | + data = file.read(bufsize) |
| 37 | + ffi.cast('char*', buffer)[0:len(data)] = data |
| 38 | + return len(data) |
| 39 | + clib.PyInputStream_OnSysRead = _PyInputStream_OnSysRead |
| 40 | + |
| 41 | + @ffi.callback('int(*)(void*)') |
| 42 | + def _PyInputStream_IsSeekable(handle): |
| 43 | + file = ffi.from_handle(handle) |
| 44 | + return hasattr(file, 'seek') |
| 45 | + clib.PyInputStream_IsSeekable = _PyInputStream_IsSeekable |
| 46 | +
|
| 47 | + @ffi.callback('size_t(*)(void*)') |
| 48 | + def _PyInputStream_TellI(handle): |
| 49 | + file = ffi.from_handle(handle) |
| 50 | + return file.tell() |
| 51 | + clib.PyInputStream_TellI = _PyInputStream_TellI |
| 52 | +
|
| 53 | + @ffi.callback('size_t(*)(void*, size_t, int)') |
| 54 | + def _PyInputStream_SeekI(handle, pos, whence): |
| 55 | + file = ffi.from_handle(handle) |
| 56 | + return file.seek(pos, whence) |
| 57 | + clib.PyInputStream_SeekI = _PyInputStream_SeekI |
| 58 | + """ |
| 59 | + |
0 commit comments