diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-03-22 14:42:20 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-03-22 14:42:20 +0100 |
| commit | 22f9d7bafc60f56259e74df98e544b5820cfca5b (patch) | |
| tree | fdc4886d035d04623666cb3132f3054bda77e889 /wrappers/python | |
| parent | eb5a1f2e9d47530c1896f49a2c4e7ffc82bcce4f (diff) | |
Save a local copy of the disco list
Diffstat (limited to 'wrappers/python')
| -rw-r--r-- | wrappers/python/eduvpncommon/__init__.py | 20 | ||||
| -rw-r--r-- | wrappers/python/eduvpncommon/main.py | 7 |
2 files changed, 26 insertions, 1 deletions
diff --git a/wrappers/python/eduvpncommon/__init__.py b/wrappers/python/eduvpncommon/__init__.py index b79c980..ff329a0 100644 --- a/wrappers/python/eduvpncommon/__init__.py +++ b/wrappers/python/eduvpncommon/__init__.py @@ -19,10 +19,30 @@ _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)] + VPNStateChange = CFUNCTYPE(None, c_char_p, c_char_p, c_char_p) # Exposed functions lib.Register.argtypes, lib.Register.restype = [c_char_p, c_char_p, VPNStateChange], None +lib.GetOrganizationsList.argtypes, lib.GetOrganizationsList.restype = [], DataError +lib.GetServersList.argtypes, lib.GetServersList.restype = [], DataError # We have to use c_void_p instead of c_char_p to free it properly # 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 + lib.FreeString(ptr) + if string: + return string.decode() + return "" + + +def GetDataError(data_error): + data = GetPtrString(data_error.data) + error = GetPtrString(data_error.error) + return data, error diff --git a/wrappers/python/eduvpncommon/main.py b/wrappers/python/eduvpncommon/main.py index 3bb0c48..ac13344 100644 --- a/wrappers/python/eduvpncommon/main.py +++ b/wrappers/python/eduvpncommon/main.py @@ -1,4 +1,4 @@ -from . import lib, VPNStateChange +from . import lib, VPNStateChange, GetDataError from ctypes import * @VPNStateChange @@ -14,3 +14,8 @@ def Register(name, config_directory, state_callback): dir_bytes = config_directory.encode('utf-8') lib.Register(name_bytes, dir_bytes, state_callback) +def GetDiscoServers(): + servers, serversErr = GetDataError(lib.GetServersList()) + organizations, organizationsErr = GetDataError(lib.GetOrganizationsList()) + return servers, serversErr, organizations, organizationsErr + |
