From 43866a0807f447c0ab292a7b834d1d6469b3847b Mon Sep 17 00:00:00 2001 From: codeking Date: Wed, 12 Mar 2025 02:09:18 +0100 Subject: [PATCH] Update and refactor existing codebase --- core/controllers/ConfigurationController.py | 8 +++--- core/controllers/SessionStateController.py | 2 +- core/controllers/SystemStateController.py | 2 +- core/models/Configuration.py | 27 ++++++++++---------- core/models/session/SessionState.py | 28 ++++++++++----------- core/models/system/SystemState.py | 28 ++++++++++----------- 6 files changed, 47 insertions(+), 48 deletions(-) diff --git a/core/controllers/ConfigurationController.py b/core/controllers/ConfigurationController.py index 5dfde4b..98424a9 100644 --- a/core/controllers/ConfigurationController.py +++ b/core/controllers/ConfigurationController.py @@ -35,7 +35,7 @@ class ConfigurationController: configuration = ConfigurationController.get_or_new() configuration.connection = connection - configuration.save(configuration) + configuration.save() @staticmethod def get_auto_sync_enabled(): @@ -52,7 +52,7 @@ class ConfigurationController: configuration = ConfigurationController.get_or_new() configuration.auto_sync_enabled = enable_auto_sync - configuration.save(configuration) + configuration.save() @staticmethod def get_last_synced_at(): @@ -69,8 +69,8 @@ class ConfigurationController: configuration = ConfigurationController.get_or_new() configuration.last_synced_at = datetime.now(timezone.utc) - Configuration.save(configuration) + configuration.save() @staticmethod def update_or_create(configuration): - Configuration.save(configuration) + configuration.save() diff --git a/core/controllers/SessionStateController.py b/core/controllers/SessionStateController.py index 415ff76..ab4f1fa 100644 --- a/core/controllers/SessionStateController.py +++ b/core/controllers/SessionStateController.py @@ -23,4 +23,4 @@ class SessionStateController: @staticmethod def update_or_create(session_state): - SessionState.save(session_state) + session_state.save() diff --git a/core/controllers/SystemStateController.py b/core/controllers/SystemStateController.py index bab3104..56673f7 100644 --- a/core/controllers/SystemStateController.py +++ b/core/controllers/SystemStateController.py @@ -23,4 +23,4 @@ class SystemStateController: @staticmethod def update_or_create(system_state): - SystemState.save(system_state) + system_state.save() diff --git a/core/models/Configuration.py b/core/models/Configuration.py index 4870923..3272cc0 100644 --- a/core/models/Configuration.py +++ b/core/models/Configuration.py @@ -4,7 +4,7 @@ from dataclasses_json import dataclass_json, config from datetime import datetime from json import JSONDecodeError from marshmallow import fields -from typing import Optional +from typing import Optional, Self from zoneinfo import ZoneInfo import dataclasses_json import json @@ -39,6 +39,18 @@ class Configuration: ) ) + def save(self: Self): + + config_file_contents = f'{self.to_json(indent=4)}\n' + os.makedirs(Constants.HV_CONFIG_HOME, exist_ok=True) + + config_file_path = f'{Constants.HV_CONFIG_HOME}/config.json' + + with open(config_file_path, 'w') as config_file: + + config_file.write(config_file_contents) + config_file.close() + @staticmethod def get(): @@ -57,19 +69,6 @@ class Configuration: return configuration - @staticmethod - def save(configuration): - - config_file_contents = f'{configuration.to_json(indent=4)}\n' - os.makedirs(Constants.HV_CONFIG_HOME, exist_ok=True) - - config_file_path = f'{Constants.HV_CONFIG_HOME}/config.json' - - with open(config_file_path, 'w') as config_file: - - config_file.write(config_file_contents) - config_file.close() - @staticmethod def _iso_format(datetime_instance: datetime): datetime_instance = datetime_instance.replace(tzinfo=ZoneInfo('UTC')) diff --git a/core/models/session/SessionState.py b/core/models/session/SessionState.py index b63fd1f..b1a38c4 100644 --- a/core/models/session/SessionState.py +++ b/core/models/session/SessionState.py @@ -3,6 +3,7 @@ from dataclasses import dataclass, field from dataclasses_json import config, Exclude, dataclass_json from json import JSONDecodeError from pathlib import Path +from typing import Self import json import os import psutil @@ -22,6 +23,19 @@ class SessionState: def get_state_path(self): return SessionState.__get_state_path(self.id) + def save(self: Self): + + session_state_file_contents = f'{self.to_json(indent=4)}\n' + os.makedirs(self.get_state_path(), exist_ok=True, mode=0o700) + + session_state_file_path = f'{self.get_state_path()}/state.json' + Path(session_state_file_path).touch(exist_ok=True, mode=0o600) + + with open(session_state_file_path, 'w') as session_state_file: + + session_state_file.write(session_state_file_contents) + session_state_file.close() + @staticmethod def find_by_id(id: int): @@ -44,20 +58,6 @@ class SessionState: def exists(id: int): return os.path.isdir(SessionState.__get_state_path(id)) and re.match(r'^\d+$', str(id)) - @staticmethod - def save(session_state): - - session_state_file_contents = f'{session_state.to_json(indent=4)}\n' - os.makedirs(SessionState.__get_state_path(session_state.id), exist_ok=True, mode=0o700) - - session_state_file_path = f'{SessionState.__get_state_path(session_state.id)}/state.json' - Path(session_state_file_path).touch(exist_ok=True, mode=0o600) - - with open(session_state_file_path, 'w') as session_state_file: - - session_state_file.write(session_state_file_contents) - session_state_file.close() - @staticmethod def dissolve(id: int): diff --git a/core/models/system/SystemState.py b/core/models/system/SystemState.py index 0b24f30..ceaeb31 100644 --- a/core/models/system/SystemState.py +++ b/core/models/system/SystemState.py @@ -3,6 +3,7 @@ from dataclasses import dataclass from dataclasses_json import dataclass_json from json import JSONDecodeError from pathlib import Path +from typing import Self import json import os import pathlib @@ -12,6 +13,19 @@ import pathlib class SystemState: profile_id: int + def save(self: Self): + + system_state_file_contents = f'{self.to_json(indent=4)}\n' + os.makedirs(SystemState.__get_state_path(), exist_ok=True, mode=0o700) + + system_state_file_path = f'{SystemState.__get_state_path()}/system.json' + Path(system_state_file_path).touch(exist_ok=True, mode=0o600) + + with open(system_state_file_path, 'w') as system_state_file: + + system_state_file.write(system_state_file_contents) + system_state_file.close() + @staticmethod def get(): @@ -32,20 +46,6 @@ class SystemState: def exists(): return os.path.isfile(f'{SystemState.__get_state_path()}/system.json') - @staticmethod - def save(system_state): - - system_state_file_contents = f'{system_state.to_json(indent=4)}\n' - os.makedirs(SystemState.__get_state_path(), exist_ok=True, mode=0o700) - - system_state_file_path = f'{SystemState.__get_state_path()}/system.json' - Path(system_state_file_path).touch(exist_ok=True, mode=0o600) - - with open(system_state_file_path, 'w') as system_state_file: - - system_state_file.write(system_state_file_contents) - system_state_file.close() - @staticmethod def dissolve():