Update and refactor existing codebase

This commit is contained in:
codeking 2025-03-12 02:09:18 +01:00
parent ac125b9cb7
commit 43866a0807
6 changed files with 47 additions and 48 deletions

View file

@ -35,7 +35,7 @@ class ConfigurationController:
configuration = ConfigurationController.get_or_new() configuration = ConfigurationController.get_or_new()
configuration.connection = connection configuration.connection = connection
configuration.save(configuration) configuration.save()
@staticmethod @staticmethod
def get_auto_sync_enabled(): def get_auto_sync_enabled():
@ -52,7 +52,7 @@ class ConfigurationController:
configuration = ConfigurationController.get_or_new() configuration = ConfigurationController.get_or_new()
configuration.auto_sync_enabled = enable_auto_sync configuration.auto_sync_enabled = enable_auto_sync
configuration.save(configuration) configuration.save()
@staticmethod @staticmethod
def get_last_synced_at(): def get_last_synced_at():
@ -69,8 +69,8 @@ class ConfigurationController:
configuration = ConfigurationController.get_or_new() configuration = ConfigurationController.get_or_new()
configuration.last_synced_at = datetime.now(timezone.utc) configuration.last_synced_at = datetime.now(timezone.utc)
Configuration.save(configuration) configuration.save()
@staticmethod @staticmethod
def update_or_create(configuration): def update_or_create(configuration):
Configuration.save(configuration) configuration.save()

View file

@ -23,4 +23,4 @@ class SessionStateController:
@staticmethod @staticmethod
def update_or_create(session_state): def update_or_create(session_state):
SessionState.save(session_state) session_state.save()

View file

@ -23,4 +23,4 @@ class SystemStateController:
@staticmethod @staticmethod
def update_or_create(system_state): def update_or_create(system_state):
SystemState.save(system_state) system_state.save()

View file

@ -4,7 +4,7 @@ from dataclasses_json import dataclass_json, config
from datetime import datetime from datetime import datetime
from json import JSONDecodeError from json import JSONDecodeError
from marshmallow import fields from marshmallow import fields
from typing import Optional from typing import Optional, Self
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
import dataclasses_json import dataclasses_json
import 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 @staticmethod
def get(): def get():
@ -57,19 +69,6 @@ class Configuration:
return 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 @staticmethod
def _iso_format(datetime_instance: datetime): def _iso_format(datetime_instance: datetime):
datetime_instance = datetime_instance.replace(tzinfo=ZoneInfo('UTC')) datetime_instance = datetime_instance.replace(tzinfo=ZoneInfo('UTC'))

View file

@ -3,6 +3,7 @@ from dataclasses import dataclass, field
from dataclasses_json import config, Exclude, dataclass_json from dataclasses_json import config, Exclude, dataclass_json
from json import JSONDecodeError from json import JSONDecodeError
from pathlib import Path from pathlib import Path
from typing import Self
import json import json
import os import os
import psutil import psutil
@ -22,6 +23,19 @@ class SessionState:
def get_state_path(self): def get_state_path(self):
return SessionState.__get_state_path(self.id) 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 @staticmethod
def find_by_id(id: int): def find_by_id(id: int):
@ -44,20 +58,6 @@ class SessionState:
def exists(id: int): def exists(id: int):
return os.path.isdir(SessionState.__get_state_path(id)) and re.match(r'^\d+$', str(id)) 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 @staticmethod
def dissolve(id: int): def dissolve(id: int):

View file

@ -3,6 +3,7 @@ from dataclasses import dataclass
from dataclasses_json import dataclass_json from dataclasses_json import dataclass_json
from json import JSONDecodeError from json import JSONDecodeError
from pathlib import Path from pathlib import Path
from typing import Self
import json import json
import os import os
import pathlib import pathlib
@ -12,6 +13,19 @@ import pathlib
class SystemState: class SystemState:
profile_id: int 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 @staticmethod
def get(): def get():
@ -32,20 +46,6 @@ class SystemState:
def exists(): def exists():
return os.path.isfile(f'{SystemState.__get_state_path()}/system.json') 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 @staticmethod
def dissolve(): def dissolve():