diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-08-15 14:41:28 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-08-15 14:41:28 +0200 |
| commit | fec6ea1eba9cee325bbd9d82aa71b8ebf5ef90b0 (patch) | |
| tree | dec0894380a2de8721a1e7733d75053bcc5181fc /wrappers | |
| parent | c5e85ba79d4d091af9873f1fb0e7415c3b17b9f8 (diff) | |
Refactor: Use constants for state callbacks instead of strings
Diffstat (limited to 'wrappers')
| -rw-r--r-- | wrappers/python/main.py | 12 | ||||
| -rw-r--r-- | wrappers/python/src/__init__.py | 2 | ||||
| -rw-r--r-- | wrappers/python/src/event.py | 19 | ||||
| -rw-r--r-- | wrappers/python/src/main.py | 15 | ||||
| -rw-r--r-- | wrappers/python/src/state.py | 23 | ||||
| -rw-r--r-- | wrappers/python/tests.py | 3 |
6 files changed, 47 insertions, 27 deletions
diff --git a/wrappers/python/main.py b/wrappers/python/main.py index 58e8b95..1ab29cc 100644 --- a/wrappers/python/main.py +++ b/wrappers/python/main.py @@ -1,4 +1,5 @@ import eduvpn_common.main as eduvpn +from eduvpn_common.state import State, StateType import webbrowser import json import sys @@ -27,22 +28,20 @@ def ask_profile_input(total: int) -> int: def setup_callbacks(_eduvpn: eduvpn.EduVPN) -> None: # The callback that starst OAuth # It needs to open the URL in the web browser - @_eduvpn.event.on("OAuth_Started", eduvpn.StateType.Enter) + @_eduvpn.event.on(State.OAUTH_STARTED, StateType.Enter) def oauth_initialized(old_state: str, url: str) -> None: print(f"Got OAuth URL {url}, old state: {old_state}") webbrowser.open(url) - @_eduvpn.event.on("Ask_Location", eduvpn.StateType.Enter) + @_eduvpn.event.on(State.ASK_LOCATION, StateType.Enter) def ask_location(old_state: str, locations: str): print("Locations: ", locations) _eduvpn.set_secure_location("NL") # The callback which asks the user for a profile - @_eduvpn.event.on("Ask_Profile", eduvpn.StateType.Enter) + @_eduvpn.event.on(State.ASK_PROFILE, StateType.Enter) def ask_profile(old_state: str, profiles: str): - print( - "Multiple profiles found, you need to select a profile, old state: {old_state}" - ) + print("Multiple profiles found, you need to select a profile:") # Parse the profiles as JSON data = json.loads(profiles) @@ -94,6 +93,7 @@ if __name__ == "__main__": # Set the internal FSM state to connected try: + _eduvpn.set_connecting() _eduvpn.set_connected() except Exception as e: print("Failed to set connected:", e) diff --git a/wrappers/python/src/__init__.py b/wrappers/python/src/__init__.py index be06525..c0b6679 100644 --- a/wrappers/python/src/__init__.py +++ b/wrappers/python/src/__init__.py @@ -43,7 +43,7 @@ class DataError(Structure): _fields_ = [("data", c_void_p), ("error", c_void_p)] -VPNStateChange = CFUNCTYPE(None, c_char_p, c_char_p, c_char_p, c_char_p) +VPNStateChange = CFUNCTYPE(None, c_char_p, c_int, c_int, c_char_p) # Exposed functions # We have to use c_void_p instead of c_char_p to free it properly diff --git a/wrappers/python/src/event.py b/wrappers/python/src/event.py index 778ce5e..0803dee 100644 --- a/wrappers/python/src/event.py +++ b/wrappers/python/src/event.py @@ -1,19 +1,14 @@ from . import VPNStateChange from enum import Enum from typing import Callable - - -class StateType(Enum): - Enter = 1 - Leave = 2 - Wait = 3 +from .state import StateType EDUVPN_CALLBACK_PROPERTY = "_eduvpn_property_callback" # A state transition decorator for classes # To use this, make sure to register the class with `register_class_callbacks` -def class_state_transition(state: str, state_type: StateType) -> Callable: +def class_state_transition(state: int, state_type: StateType) -> Callable: def wrapper(func): setattr(func, EDUVPN_CALLBACK_PROPERTY, (state, state_type)) return func @@ -45,7 +40,7 @@ class EventHandler(object): else: self.remove_event(state, state_type, method) - def remove_event(self, state: str, state_type: StateType, func: Callable): + def remove_event(self, state: int, state_type: StateType, func: Callable): for key, values in self.handlers.copy().items(): if key == (state, state_type): values.remove(func) @@ -54,13 +49,13 @@ class EventHandler(object): else: self.handlers[key] = values - def add_event(self, state: str, state_type: StateType, func: Callable): + def add_event(self, state: int, state_type: StateType, func: Callable): if (state, state_type) not in self.handlers: self.handlers[(state, state_type)] = [] self.handlers[(state, state_type)].append(func) # A decorator for standalone functions - def on(self, state: str, state_type: StateType) -> Callable: + def on(self, state: int, state_type: StateType) -> Callable: def wrapped_f(func): self.add_event(state, state_type, func) return func @@ -68,14 +63,14 @@ class EventHandler(object): return wrapped_f def run_state( - self, state: str, other_state: str, state_type: StateType, data: str + self, state: int, other_state: int, state_type: StateType, data: str ) -> None: if (state, state_type) not in self.handlers: return for func in self.handlers[(state, state_type)]: func(other_state, data) - def run(self, old_state: str, new_state: str, data: str) -> None: + def run(self, old_state: int, new_state: int, data: str) -> None: if old_state == new_state: return diff --git a/wrappers/python/src/main.py b/wrappers/python/src/main.py index ac44073..8440c7d 100644 --- a/wrappers/python/src/main.py +++ b/wrappers/python/src/main.py @@ -1,7 +1,8 @@ from . import lib, VPNStateChange, encode_args, decode_res from typing import Optional, Tuple import threading -from .event import StateType, EventHandler +from .event import EventHandler +from .state import State, StateType import json eduvpn_objects = {} @@ -25,7 +26,7 @@ def state_callback(name, old_state, new_state, data): name = name.decode() if name not in eduvpn_objects: return - eduvpn_objects[name].callback(old_state.decode(), new_state.decode(), data.decode()) + eduvpn_objects[name].callback(State(old_state), State(new_state), data.decode()) class EduVPN(object): @@ -41,13 +42,13 @@ class EduVPN(object): self.profile_event: Optional[threading.Event] = None self.location_event: Optional[threading.Event] = None - @self.event.on("Ask_Profile", StateType.Wait) - def wait_profile_event(old_state: str, profiles: str): + @self.event.on(State.ASK_PROFILE, StateType.Wait) + def wait_profile_event(old_state: int, profiles: str): if self.profile_event: self.profile_event.wait() - @self.event.on("Ask_Location", StateType.Wait) - def wait_location_event(old_state: str, locations: str): + @self.event.on(State.ASK_LOCATION, StateType.Wait) + def wait_location_event(old_state: int, locations: str): if self.location_event: self.location_event.wait() @@ -172,7 +173,7 @@ class EduVPN(object): def event(self) -> EventHandler: return self.event_handler - def callback(self, old_state: str, new_state: str, data: str) -> None: + def callback(self, old_state: State, new_state: State, data: str) -> None: self.event.run(old_state, new_state, data) def set_profile(self, profile_id: str) -> None: diff --git a/wrappers/python/src/state.py b/wrappers/python/src/state.py new file mode 100644 index 0000000..cd5bd90 --- /dev/null +++ b/wrappers/python/src/state.py @@ -0,0 +1,23 @@ +from enum import IntEnum + + +class StateType(IntEnum): + Enter = 1 + Leave = 2 + Wait = 3 + + +class State(IntEnum): + DEREGISTERED = 0 + NO_SERVER = 1 + ASK_LOCATION = 2 + SEARCH_SERVER = 3 + LOADING_SERVER = 4 + CHOSEN_SERVER = 5 + OAUTH_STARTED = 6 + AUTHORIZED = 7 + REQUEST_CONFIG = 8 + ASK_PROFILE = 9 + HAS_CONFIG = 10 + CONNECTING = 11 + CONNECTED = 12 diff --git a/wrappers/python/tests.py b/wrappers/python/tests.py index cbec370..5cf5c25 100644 --- a/wrappers/python/tests.py +++ b/wrappers/python/tests.py @@ -2,6 +2,7 @@ import unittest import eduvpn_common.main as eduvpn +from eduvpn_common.state import State, StateType import webbrowser import sys import os @@ -20,7 +21,7 @@ class ConfigTests(unittest.TestCase): # This can throw an exception _eduvpn.register() - @_eduvpn.event.on("OAuth_Started", eduvpn.StateType.Enter) + @_eduvpn.event.on(State.OAUTH_STARTED, StateType.Enter) def oauth_initialized(old_state, url): login_eduvpn(url) |
