From 175d6dbb992696e5b8db96ede33de5371d286f2d Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Fri, 22 Mar 2024 13:02:46 +0100 Subject: Python: Format and fix linting errors --- wrappers/python/eduvpn_common/event.py | 7 +- wrappers/python/eduvpn_common/loader.py | 135 +++++++++++++++++++------------- wrappers/python/eduvpn_common/main.py | 29 ++----- wrappers/python/eduvpn_common/types.py | 14 +--- 4 files changed, 93 insertions(+), 92 deletions(-) (limited to 'wrappers') diff --git a/wrappers/python/eduvpn_common/event.py b/wrappers/python/eduvpn_common/event.py index efe3310..2eb528a 100644 --- a/wrappers/python/eduvpn_common/event.py +++ b/wrappers/python/eduvpn_common/event.py @@ -1,4 +1,3 @@ -from enum import IntEnum from typing import Any, Callable, Dict, List, Tuple from eduvpn_common.state import State, StateType @@ -41,7 +40,7 @@ class EventHandler(object): try: # Get the method method = getattr(cls, method_name) - except Exception as e: + except Exception: # Unable to get a value, go to the next continue @@ -81,9 +80,7 @@ class EventHandler(object): self.handlers[(state, state_type)] = [] self.handlers[(state, state_type)].append(func) - def run_state( - self, state: State, other_state: State, state_type: StateType, data: str - ) -> bool: + def run_state(self, state: State, other_state: State, state_type: StateType, data: str) -> bool: """The function that runs the callback for a specific event :param state: State: The state of the event :param other_state: State: The other state of the event diff --git a/wrappers/python/eduvpn_common/loader.py b/wrappers/python/eduvpn_common/loader.py index ca0fd20..360ec04 100644 --- a/wrappers/python/eduvpn_common/loader.py +++ b/wrappers/python/eduvpn_common/loader.py @@ -1,13 +1,12 @@ import pathlib -from collections import defaultdict from ctypes import CDLL, c_char_p, c_int, c_void_p, cdll from eduvpn_common import __version__ from eduvpn_common.types import ( BoolError, + DataError, ProxyReady, ProxySetup, - DataError, ReadRxBytes, TokenGetter, TokenSetter, @@ -31,7 +30,7 @@ def load_lib() -> CDLL: try: lib = cdll.LoadLibrary(libfile) # Otherwise, library should have been copied to the lib/ folder - except: + except Exception: lib = cdll.LoadLibrary(str(pathlib.Path(__file__).parent / "lib" / libfile)) return lib @@ -52,64 +51,94 @@ def initialize_functions(lib: CDLL) -> None: lib.FreeString.argtypes, lib.FreeString.restype = [c_void_p], None lib.DiscoOrganizations.argtypes, lib.DiscoOrganizations.restype = [c_int], DataError lib.DiscoServers.argtypes, lib.DiscoServers.restype = [c_int], DataError - lib.GetConfig.argtypes, lib.GetConfig.restype = [ - c_int, - c_int, - c_char_p, - c_int, - c_int, - ], DataError - lib.AddServer.argtypes, lib.AddServer.restype = [ - c_int, - c_int, + lib.GetConfig.argtypes, lib.GetConfig.restype = ( + [ + c_int, + c_int, + c_char_p, + c_int, + c_int, + ], + DataError, + ) + lib.AddServer.argtypes, lib.AddServer.restype = ( + [ + c_int, + c_int, + c_char_p, + c_int, + ], c_char_p, - c_int, - ], c_char_p + ) lib.CurrentServer.argtypes, lib.CurrentServer.restype = [], DataError - lib.RemoveServer.argtypes, lib.RemoveServer.restype = [ - c_int, + lib.RemoveServer.argtypes, lib.RemoveServer.restype = ( + [ + c_int, + c_char_p, + ], c_char_p, - ], c_char_p + ) lib.ServerList.argtypes, lib.ServerList.restype = [], DataError - lib.Register.argtypes, lib.Register.restype = [ - c_char_p, - c_char_p, - c_char_p, - VPNStateChange, - c_int, - ], c_void_p + lib.Register.argtypes, lib.Register.restype = ( + [ + c_char_p, + c_char_p, + c_char_p, + VPNStateChange, + c_int, + ], + c_void_p, + ) lib.RenewSession.argtypes, lib.RenewSession.restype = [c_int], c_void_p - lib.SetTokenHandler.argtypes, lib.SetTokenHandler.restype = [ - TokenGetter, - TokenSetter, - ], c_void_p + lib.SetTokenHandler.argtypes, lib.SetTokenHandler.restype = ( + [ + TokenGetter, + TokenSetter, + ], + c_void_p, + ) lib.Cleanup.argtypes, lib.Cleanup.restype = [c_int], c_void_p lib.SetProfileID.argtypes, lib.SetProfileID.restype = [c_char_p], c_void_p lib.CookieNew.argtypes, lib.CookieNew.restype = [], c_int lib.CookieReply.argtypes, lib.CookieReply.restype = [c_int, c_char_p], c_void_p lib.CookieCancel.argtypes, lib.CookieCancel.restype = [c_int], c_void_p lib.CookieDelete.argtypes, lib.CookieDelete.restype = [c_int], c_void_p - lib.SetSecureLocation.argtypes, lib.SetSecureLocation.restype = [ - c_char_p, - c_char_p, - ], c_void_p - lib.SetState.argtypes, lib.SetState.restype = [ - c_int, - ], c_void_p - lib.InState.argtypes, lib.InState.restype = [ - c_int, - ], BoolError - lib.StartFailover.argtypes, lib.StartFailover.restype = [ - c_int, - c_char_p, - c_int, - ReadRxBytes, - ], BoolError - lib.StartProxyguard.argtypes, lib.StartProxyguard.restype = [ - c_int, - c_char_p, - c_int, - c_char_p, - ProxySetup, - ProxyReady, - ], c_void_p + lib.SetSecureLocation.argtypes, lib.SetSecureLocation.restype = ( + [ + c_char_p, + c_char_p, + ], + c_void_p, + ) + lib.SetState.argtypes, lib.SetState.restype = ( + [ + c_int, + ], + c_void_p, + ) + lib.InState.argtypes, lib.InState.restype = ( + [ + c_int, + ], + BoolError, + ) + lib.StartFailover.argtypes, lib.StartFailover.restype = ( + [ + c_int, + c_char_p, + c_int, + ReadRxBytes, + ], + BoolError, + ) + lib.StartProxyguard.argtypes, lib.StartProxyguard.restype = ( + [ + c_int, + c_char_p, + c_int, + c_char_p, + ProxySetup, + ProxyReady, + ], + c_void_p, + ) diff --git a/wrappers/python/eduvpn_common/main.py b/wrappers/python/eduvpn_common/main.py index 3a91ebb..84ae9ab 100644 --- a/wrappers/python/eduvpn_common/main.py +++ b/wrappers/python/eduvpn_common/main.py @@ -1,9 +1,11 @@ import ctypes import json from enum import IntEnum -from typing import Any, Callable, Iterator, Optional +from typing import Any, Callable, Iterator +from eduvpn_common.event import EventHandler from eduvpn_common.loader import initialize_functions, load_lib +from eduvpn_common.state import State from eduvpn_common.types import ( ProxyReady, ProxySetup, @@ -15,10 +17,6 @@ from eduvpn_common.types import ( encode_args, ) -from eduvpn_common.event import EventHandler -from eduvpn_common.state import State - - global_object = None @@ -134,7 +132,6 @@ class EduVPN(object): """Register the Go shared library. This makes sure the FSM is initialized and that we can call Go functions - :param handler: Optional[Callable]: (Default value = None): The handler that runs state transitions :param debug: bool: (Default value = False): Whether or not we want to enable debug logging """ @@ -293,9 +290,7 @@ class EduVPN(object): :raises WrappedError: An error by the Go library """ # Set the location by country code - location_err = self.go_function( - self.lib.SetSecureLocation, org_id, country_code - ) + location_err = self.go_function(self.lib.SetSecureLocation, org_id, country_code) # If there is a location event, set it so that the wait callback finishes # And so that the Go code can move to the next state @@ -305,9 +300,7 @@ class EduVPN(object): def set_token_handler(self, getter: Callable, setter: Callable) -> None: self.token_setter = setter self.token_getter = getter - handler_err = self.go_function( - self.lib.SetTokenHandler, token_getter, token_setter - ) + handler_err = self.go_function(self.lib.SetTokenHandler, token_getter, token_setter) if handler_err: forwardError(handler_err) @@ -328,9 +321,7 @@ class EduVPN(object): if renew_err: forwardError(renew_err) - def start_failover( - self, gateway: str, wg_mtu: int, readrxbytes: ReadRxBytes - ) -> bool: + def start_failover(self, gateway: str, wg_mtu: int, readrxbytes: ReadRxBytes) -> bool: dropped, dropped_err = self.go_cookie_function( self.lib.StartFailover, gateway, @@ -365,9 +356,7 @@ class EduVPN(object): @TokenSetter -def token_setter( - server_id: ctypes.c_char_p, server_type: ctypes.c_int, tokens: ctypes.c_char_p -): +def token_setter(server_id: ctypes.c_char_p, server_type: ctypes.c_int, tokens: ctypes.c_char_p): global global_object if global_object is None: return @@ -409,9 +398,7 @@ def state_callback(old_state: int, new_state: int, data: str) -> int: global global_object if global_object is None: return 0 - handled = global_object.event_handler.run( - State(old_state), State(new_state), data.decode("utf-8") - ) + handled = global_object.event_handler.run(State(old_state), State(new_state), data.decode("utf-8")) if handled: return 1 return 0 diff --git a/wrappers/python/eduvpn_common/types.py b/wrappers/python/eduvpn_common/types.py index 3375258..837ead6 100644 --- a/wrappers/python/eduvpn_common/types.py +++ b/wrappers/python/eduvpn_common/types.py @@ -1,16 +1,4 @@ -from ctypes import ( - CDLL, - CFUNCTYPE, - POINTER, - Structure, - c_char, - c_char_p, - c_int, - c_ulonglong, - c_size_t, - c_void_p, - cast, -) +from ctypes import CDLL, CFUNCTYPE, POINTER, Structure, c_char, c_char_p, c_int, c_size_t, c_ulonglong, c_void_p, cast from typing import Any, Iterator, List, Tuple -- cgit v1.2.3