Update and refactor existing codebase

This commit is contained in:
codeking 2024-09-20 02:41:41 +02:00
parent c54427790a
commit d8d54d88d1
10 changed files with 26 additions and 25 deletions

View file

@ -6,7 +6,8 @@ import os
@dataclass(frozen=True) @dataclass(frozen=True)
class Constants: class Constants:
CLIENT_VERSION: Final[str] = '1.0.0' SP_CLIENT_VERSION: Final[str] = '1.0.0'
HOME: Final[str] = os.path.expanduser('~') HOME: Final[str] = os.path.expanduser('~')
CONFIG_HOME: Final[str] = os.environ.get('XDG_CONFIG_HOME') or os.path.join(HOME, '.config') CONFIG_HOME: Final[str] = os.environ.get('XDG_CONFIG_HOME') or os.path.join(HOME, '.config')
@ -24,6 +25,6 @@ class Constants:
SP_SESSION_STATE_HOME: Final[str] = SP_STATE_HOME + '/sessions' SP_SESSION_STATE_HOME: Final[str] = SP_STATE_HOME + '/sessions'
SP_STORAGE_DATABASE: Final[str] = SP_DATA_HOME + '/storage.db' SP_STORAGE_DATABASE_PATH: Final[str] = SP_DATA_HOME + '/storage.db'
SP_API_BASE_URL: Final[str] = 'https://api.simplifiedprivacy.is/api/v1' SP_API_BASE_URL: Final[str] = 'https://api.simplifiedprivacy.is/api/v1'

View file

@ -37,7 +37,7 @@ class ApplicationVersionController:
applications = ApplicationController.get_all() applications = ApplicationController.get_all()
application_versions = [] application_versions = []
for application_code in [application.code for application in applications]: for application_code in (application.code for application in applications):
application_version_subset = WebServiceApiService.get_application_versions(application_code, proxies) application_version_subset = WebServiceApiService.get_application_versions(application_code, proxies)

View file

@ -21,7 +21,7 @@ class ClientController:
@staticmethod @staticmethod
def get_version(): def get_version():
return ClientVersionController.get(Constants.CLIENT_VERSION) return ClientVersionController.get(Constants.SP_CLIENT_VERSION)
@staticmethod @staticmethod
def can_be_updated(): def can_be_updated():

View file

@ -25,7 +25,7 @@ class ConfigurationController:
configuration = ConfigurationController.get() configuration = ConfigurationController.get()
if configuration is None or configuration.connection not in ['system', 'tor']: if configuration is None or configuration.connection not in ('system', 'tor'):
raise UnknownConnectionTypeError('The preferred connection type could not be determined.') raise UnknownConnectionTypeError('The preferred connection type could not be determined.')
return configuration.connection return configuration.connection

View file

@ -89,12 +89,12 @@ class ConnectionController:
if profile.connection.is_unprotected(): if profile.connection.is_unprotected():
if not ConnectionController.system_uses_wireguard_interface(): if not ConnectionController.system_uses_wireguard_interface():
if not force: if not force:
raise ConnectionUnprotectedError('Connection unprotected while the system is not using a WireGuard interface.') raise ConnectionUnprotectedError('Connection unprotected while the system is not using a WireGuard interface.')
else: else:
ProfileController.disable(profile) ProfileController.disable(profile)
if profile.connection.code == 'tor': if profile.connection.code == 'tor':

View file

@ -86,13 +86,13 @@ class ProfileController:
if profile.is_system_profile(): if profile.is_system_profile():
profiles = ProfileController.get_all() subjects = ProfileController.get_all().values()
for key, value in profiles.items(): for subject in subjects:
if type(value).__name__ == 'SessionProfile': if subject.is_session_profile():
if value.connection.is_unprotected() and ProfileController.is_enabled(value) and not force: if subject.connection.is_unprotected() and ProfileController.is_enabled(subject) and not force:
raise ConnectionUnprotectedError('Disabling this system connection would leave one or more sessions exposed.') raise ConnectionUnprotectedError('Disabling this system connection would leave one or more sessions exposed.')
if SystemStateController.exists(): if SystemStateController.exists():
@ -174,7 +174,7 @@ class ProfileController:
@staticmethod @staticmethod
def attach_proxy_configuration(profile: Union[SessionProfile, SystemProfile]): def attach_proxy_configuration(profile: Union[SessionProfile, SystemProfile]):
if type(profile).__name__ != 'SessionProfile': if not profile.is_session_profile():
return None return None
if profile.subscription is None: if profile.subscription is None:
@ -188,7 +188,7 @@ class ProfileController:
@staticmethod @staticmethod
def get_proxy_configuration(profile: Union[SessionProfile, SystemProfile]): def get_proxy_configuration(profile: Union[SessionProfile, SystemProfile]):
if type(profile).__name__ != 'SessionProfile': if not profile.is_session_profile():
return None return None
profile.get_proxy_configuration() profile.get_proxy_configuration()

View file

@ -7,7 +7,7 @@ class Model:
@staticmethod @staticmethod
def _create_table_if_not_exists(table_name: str, table_definition: str, drop_existing: bool = False): def _create_table_if_not_exists(table_name: str, table_definition: str, drop_existing: bool = False):
connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE) connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE_PATH)
cursor = connection.cursor() cursor = connection.cursor()
if drop_existing: if drop_existing:
@ -24,7 +24,7 @@ class Model:
if parameters is None: if parameters is None:
parameters = [] parameters = []
connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE) connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE_PATH)
cursor = connection.cursor() cursor = connection.cursor()
cursor.row_factory = row_factory cursor.row_factory = row_factory
@ -37,7 +37,7 @@ class Model:
@staticmethod @staticmethod
def _query_exists(query: str, parameters): def _query_exists(query: str, parameters):
connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE) connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE_PATH)
cursor = connection.cursor() cursor = connection.cursor()
response = cursor.execute('SELECT EXISTS(' + query + ')', parameters).fetchone() response = cursor.execute('SELECT EXISTS(' + query + ')', parameters).fetchone()
@ -51,7 +51,7 @@ class Model:
if parameters is None: if parameters is None:
parameters = [] parameters = []
connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE) connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE_PATH)
cursor = connection.cursor() cursor = connection.cursor()
cursor.row_factory = row_factory cursor.row_factory = row_factory
@ -64,7 +64,7 @@ class Model:
@staticmethod @staticmethod
def _insert_many(query: str, tuple_factory, items): def _insert_many(query: str, tuple_factory, items):
connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE) connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE_PATH)
cursor = connection.cursor() cursor = connection.cursor()
cursor.executemany(query, map(tuple_factory, items)) cursor.executemany(query, map(tuple_factory, items))

View file

@ -22,7 +22,7 @@ class Subscription:
) )
def is_active(self): def is_active(self):
return self.expires_at is not None and self.expires_at > datetime.now(tz=timezone.utc) return self.expires_at is not None and self.expires_at > datetime.now(timezone.utc)
@staticmethod @staticmethod
def from_iso_format(datetime_string: str): def from_iso_format(datetime_string: str):

View file

@ -38,7 +38,7 @@ class SubscriptionPlan(Model):
features_proxy = False features_proxy = False
features_wireguard = False features_wireguard = False
if type(connection).__name__ == 'SessionConnection': if connection.is_session_connection():
if connection.masked: if connection.masked:
features_proxy = True features_proxy = True
@ -61,7 +61,7 @@ class SubscriptionPlan(Model):
features_proxy = False features_proxy = False
features_wireguard = False features_wireguard = False
if type(connection).__name__ == 'SessionConnection': if connection.is_session_connection():
if connection.masked: if connection.masked:
features_proxy = True features_proxy = True

View file

@ -172,7 +172,7 @@ if __name__ == '__main__':
manage_subscription_input = None manage_subscription_input = None
while manage_subscription_input not in ['1', '2', '3', '']: while manage_subscription_input not in ('1', '2', '3', ''):
print('Please select from the following:\n') print('Please select from the following:\n')