diff options
Diffstat (limited to 'wrappers/python/eduvpncommon/main.py')
| -rw-r--r-- | wrappers/python/eduvpncommon/main.py | 49 |
1 files changed, 36 insertions, 13 deletions
diff --git a/wrappers/python/eduvpncommon/main.py b/wrappers/python/eduvpncommon/main.py index 9a4931f..9e1f25e 100644 --- a/wrappers/python/eduvpncommon/main.py +++ b/wrappers/python/eduvpncommon/main.py @@ -1,12 +1,13 @@ from . import lib, VPNStateChange, GetDataError, GetPtrString from ctypes import * from enum import Enum -import functools + class StateType(Enum): Enter = 1 Leave = 2 + # Registers the python app with the Go code # name: The name of the app to be registered # url: The url of the server to connect to, FIXME: To be removed @@ -19,19 +20,46 @@ def Register(name, config_directory, state_callback): return err_string +def GetDiscoServers(): + servers, serversErr = GetDataError(lib.GetServersList()) + organizations, organizationsErr = GetDataError(lib.GetOrganizationsList()) + return servers, serversErr, organizations, organizationsErr + + +def Connect(url): + url_bytes = url.encode("utf-8") + data_error = lib.Connect(url_bytes) + return GetDataError(data_error) + + +# This has to be global as otherwise the callback is not alive +callback_function = None + + +def register_callback(eduvpn): + global callback_function + callback_function = VPNStateChange( + lambda old_state, new_state, data: eduvpn.callback( + old_state.decode(), new_state.decode(), data.decode() + ) + ) + + class EduVPN(object): def __init__(self, name, config_directory): self.event_handler = EventHandler() self.name = name self.config_directory = config_directory + register_callback(self) def register(self) -> bool: - closure = VPNStateChange( - lambda old_state, new_state, data: self.callback( - old_state.decode(), new_state.decode(), data.decode() - ) - ) - return Register(self.name, self.config_directory, closure) == "" + return Register(self.name, self.config_directory, callback_function) == "" + + def get_disco(self): + return GetDiscoServers() + + def connect(self, url): + return Connect(url) @property def event(self): @@ -51,6 +79,7 @@ class EventHandler(object): self.handlers[(state, state_type)] = [] self.handlers[(state, state_type)].append(func) return func + return wrapped_f def run_state(self, state, state_type, data): @@ -64,9 +93,3 @@ class EventHandler(object): return self.run_state(old_state, StateType.Leave, data) self.run_state(new_state, StateType.Enter, data) - - -def GetDiscoServers(): - servers, serversErr = GetDataError(lib.GetServersList()) - organizations, organizationsErr = GetDataError(lib.GetOrganizationsList()) - return servers, serversErr, organizations, organizationsErr |
