summaryrefslogtreecommitdiff
path: root/wrappers/python/eduvpncommon/main.py
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-03-23 10:42:49 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-03-23 10:42:49 +0100
commited6073f2c2c6600063f2e5062937b7a2a1162eb2 (patch)
treed43109bb55546fc4815824579c4879b51e4c3605 /wrappers/python/eduvpncommon/main.py
parente9597dc652e8ca99141b0d66bfca3e24f233d430 (diff)
Python: Implement state callbacks using decorators
Diffstat (limited to 'wrappers/python/eduvpncommon/main.py')
-rw-r--r--wrappers/python/eduvpncommon/main.py67
1 files changed, 59 insertions, 8 deletions
diff --git a/wrappers/python/eduvpncommon/main.py b/wrappers/python/eduvpncommon/main.py
index ac13344..9a4931f 100644
--- a/wrappers/python/eduvpncommon/main.py
+++ b/wrappers/python/eduvpncommon/main.py
@@ -1,21 +1,72 @@
-from . import lib, VPNStateChange, GetDataError
+from . import lib, VPNStateChange, GetDataError, GetPtrString
from ctypes import *
+from enum import Enum
+import functools
-@VPNStateChange
-def state_change(old, new, data):
- print(f"Python: State change {old.decode()} {new.decode()} DATA {data.decode()}")
+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
# state_callback: The callback to trigger whenever a state is changed, FIXME: Remove whenever this wrapper has implemented callbacks using function decorations
def Register(name, config_directory, state_callback):
- name_bytes = name.encode('utf-8')
- dir_bytes = config_directory.encode('utf-8')
- lib.Register(name_bytes, dir_bytes, state_callback)
+ name_bytes = name.encode("utf-8")
+ dir_bytes = config_directory.encode("utf-8")
+ ptr_err = lib.Register(name_bytes, dir_bytes, state_callback)
+ err_string = GetPtrString(ptr_err)
+ return err_string
+
+
+class EduVPN(object):
+ def __init__(self, name, config_directory):
+ self.event_handler = EventHandler()
+ self.name = name
+ self.config_directory = config_directory
+
+ 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) == ""
+
+ @property
+ def event(self):
+ return self.event_handler
+
+ def callback(self, old_state, new_state, data):
+ self.event.run(old_state, new_state, data)
+
+
+class EventHandler(object):
+ def __init__(self):
+ self.handlers = {}
+
+ def on(self, state, state_type):
+ def wrapped_f(func):
+ if (state, state_type) not in self.handlers:
+ 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):
+ if (state, state_type) not in self.handlers:
+ return
+ for func in self.handlers[(state, state_type)]:
+ func(data)
+
+ def run(self, old_state, new_state, data):
+ if old_state == new_state:
+ 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
-