From ed6073f2c2c6600063f2e5062937b7a2a1162eb2 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Wed, 23 Mar 2022 10:42:49 +0100 Subject: Python: Implement state callbacks using decorators --- wrappers/python/eduvpncommon/__init__.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'wrappers/python/eduvpncommon/__init__.py') diff --git a/wrappers/python/eduvpncommon/__init__.py b/wrappers/python/eduvpncommon/__init__.py index ff329a0..073af39 100644 --- a/wrappers/python/eduvpncommon/__init__.py +++ b/wrappers/python/eduvpncommon/__init__.py @@ -3,14 +3,20 @@ from collections import defaultdict import pathlib import platform -_lib_prefixes = defaultdict(lambda: "lib", { - "windows": "", -}) - -_lib_suffixes = defaultdict(lambda: ".so", { - "windows": ".dll", - "darwin": ".dylib", -}) +_lib_prefixes = defaultdict( + lambda: "lib", + { + "windows": "", + }, +) + +_lib_suffixes = defaultdict( + lambda: ".so", + { + "windows": ".dll", + "darwin": ".dylib", + }, +) _os = platform.system().lower() @@ -19,9 +25,10 @@ _libfile = f"{_lib_prefixes[_os]}{_libname}{_lib_suffixes[_os]}" # Library should have been copied to the lib/ folder lib = cdll.LoadLibrary(str(pathlib.Path(__file__).parent / "lib" / _libfile)) + class DataError(Structure): - _fields_ = [('data', c_void_p), - ('error', c_void_p)] + _fields_ = [("data", c_void_p), ("error", c_void_p)] + VPNStateChange = CFUNCTYPE(None, c_char_p, c_char_p, c_char_p) @@ -33,6 +40,7 @@ lib.GetServersList.argtypes, lib.GetServersList.restype = [], DataError # See https://stackoverflow.com/questions/13445568/python-ctypes-how-to-free-memory-getting-invalid-pointer-error lib.FreeString.argtypes, lib.FreeString.restype = [c_void_p], None + def GetPtrString(ptr): if ptr: string = cast(ptr, c_char_p).value -- cgit v1.2.3