add dependencies, config
This commit is contained in:
@@ -7,8 +7,15 @@ authors = [
|
||||
{ name = "WorldTeacher", email = "coding_contact@pm.me" }
|
||||
]
|
||||
requires-python = ">=3.13"
|
||||
dependencies = []
|
||||
dependencies = [
|
||||
"omegaconf>=2.3.0",
|
||||
]
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[dependency-groups]
|
||||
test = [
|
||||
"pytest>=8.3.4",
|
||||
]
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
def hello() -> str:
|
||||
return "Hello from komconfig!"
|
||||
from .config import Settings as KomConfig
|
||||
|
||||
248
src/komconfig/config.py
Normal file
248
src/komconfig/config.py
Normal file
@@ -0,0 +1,248 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Optional
|
||||
import os
|
||||
from omegaconf import OmegaConf
|
||||
from omegaconf import DictConfig
|
||||
|
||||
|
||||
@dataclass
|
||||
class Komga:
|
||||
"""Komga server settings."""
|
||||
|
||||
url: str
|
||||
user: str
|
||||
password: str
|
||||
media_path: str
|
||||
|
||||
def getattr(self, name):
|
||||
return getattr(self, name)
|
||||
|
||||
def _setattr(self, name, value):
|
||||
setattr(self, name, value)
|
||||
|
||||
|
||||
@dataclass
|
||||
class General:
|
||||
"""General application settings."""
|
||||
|
||||
log_level: str
|
||||
log_file: str
|
||||
komga: Komga
|
||||
|
||||
def getattr(self, name):
|
||||
return getattr(self, name)
|
||||
|
||||
def _setattr(self, name, value):
|
||||
setattr(self, name, value)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Aria2:
|
||||
"""Aria2 settings."""
|
||||
|
||||
host: str
|
||||
port: int
|
||||
secret: str
|
||||
timeout: int
|
||||
kill_after_completion: bool
|
||||
|
||||
def getattr(self, name):
|
||||
return getattr(self, name)
|
||||
|
||||
def _setattr(self, name, value):
|
||||
setattr(self, name, value)
|
||||
|
||||
|
||||
@dataclass
|
||||
class KomGrabber:
|
||||
"""KomGrabber settings."""
|
||||
|
||||
download_location: str
|
||||
aria2: Aria2
|
||||
|
||||
def getattr(self, name):
|
||||
return getattr(self, name)
|
||||
|
||||
def _setattr(self, name, value):
|
||||
setattr(self, name, value)
|
||||
|
||||
|
||||
@dataclass
|
||||
class KomTagger:
|
||||
"""KomTagger settings."""
|
||||
|
||||
failed_location: str
|
||||
success_location: str
|
||||
|
||||
def getattr(self, name):
|
||||
return getattr(self, name)
|
||||
|
||||
def _setattr(self, name, value):
|
||||
setattr(self, name, value)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ComicVine:
|
||||
"""ComicVine settings."""
|
||||
|
||||
api_key: str
|
||||
url: str
|
||||
|
||||
def getattr(self, name):
|
||||
return getattr(self, name)
|
||||
|
||||
def _setattr(self, name, value):
|
||||
setattr(self, name, value)
|
||||
|
||||
|
||||
@dataclass
|
||||
class MangaDex:
|
||||
"""MangaDex settings."""
|
||||
|
||||
url: str
|
||||
username: str
|
||||
password: str
|
||||
|
||||
def getattr(self, name):
|
||||
return getattr(self, name)
|
||||
|
||||
def _setattr(self, name, value):
|
||||
setattr(self, name, value)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ComicsOrg:
|
||||
"""Comics.org settings."""
|
||||
|
||||
path: str
|
||||
|
||||
def getattr(self, name):
|
||||
return getattr(self, name)
|
||||
|
||||
def _setattr(self, name, value):
|
||||
setattr(self, name, value)
|
||||
|
||||
|
||||
@dataclass
|
||||
class API:
|
||||
"""API settings."""
|
||||
|
||||
comicvine: ComicVine
|
||||
mangadex: MangaDex
|
||||
comicsorg: ComicsOrg
|
||||
|
||||
@property
|
||||
def comicvine(self):
|
||||
return self.comicvine
|
||||
|
||||
@property
|
||||
def mangadex(self):
|
||||
return self.mangadex
|
||||
|
||||
@property
|
||||
def comicsorg(self):
|
||||
return self.comicsorg
|
||||
|
||||
def getattr(self, name):
|
||||
return getattr(self, name)
|
||||
|
||||
def _setattr(self, name, value):
|
||||
setattr(self, name, value)
|
||||
|
||||
|
||||
class Settings:
|
||||
"""A class to handle the configuration of the application. After initializing, it will try to load the config file and store it for future access. Any changes made can be saved to the file using the .save() method. Changes are used in real time in the app, if a restart is required, the Application will show a window.
|
||||
|
||||
Raises:
|
||||
RuntimeError: Configuration not loaded
|
||||
|
||||
KeyError: Invalid option
|
||||
|
||||
"""
|
||||
|
||||
_config: Optional[DictConfig] = None
|
||||
|
||||
def __init__(self, config_path: str = "~/.config/KomSuite/config.yaml"):
|
||||
"""
|
||||
Loads the configuration file and stores it for future access.
|
||||
|
||||
Args:
|
||||
config_path (str): Path to the YAML configuration file.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: Configuration file not found
|
||||
"""
|
||||
if not os.path.exists(os.path.expanduser(config_path)):
|
||||
raise FileNotFoundError(f"Configuration file not found: {config_path}")
|
||||
self._config = OmegaConf.load(os.path.expanduser(config_path))
|
||||
self.config_path = config_path
|
||||
|
||||
def save(self):
|
||||
"""
|
||||
Saves the current configuration to the file.
|
||||
|
||||
Args:
|
||||
config_path (str): Path to the YAML configuration file.
|
||||
"""
|
||||
OmegaConf.save(self._config, self.config_path)
|
||||
|
||||
@property
|
||||
def general(self):
|
||||
return General(**self._config.general)
|
||||
|
||||
@property
|
||||
def general_attr(self, name):
|
||||
return getattr(self.general, name)
|
||||
|
||||
@general_attr.setter
|
||||
def general_attr(self, name, value):
|
||||
self.general._setattr(name, value)
|
||||
|
||||
@property
|
||||
def komgrabber(self):
|
||||
return KomGrabber(**self._config.komgrabber)
|
||||
|
||||
@property
|
||||
def komgrabber_attr(self, name):
|
||||
return getattr(self.komgrabber, name)
|
||||
|
||||
@komgrabber_attr.setter
|
||||
def komgrabber_attr(self, name, value):
|
||||
self.komgrabber._setattr(name, value)
|
||||
|
||||
@property
|
||||
def komtagger(self):
|
||||
return KomTagger(**self._config.komtagger)
|
||||
|
||||
def komtagger_attr(self, name):
|
||||
return getattr(self.komtagger, name)
|
||||
|
||||
def set_komtagger_attr(self, name, value):
|
||||
OmegaConf.update(self._config, f"komtagger.{name}", value)
|
||||
|
||||
def set_komgrabber_attr(self, name, value):
|
||||
OmegaConf.update(self._config, f"komgrabber.{name}", value)
|
||||
|
||||
def set_general_attr(self, name, value):
|
||||
OmegaConf.update(self._config, f"general.{name}", value)
|
||||
|
||||
def set_api_attr(self, name, value):
|
||||
OmegaConf.update(self._config, f"api.{name}", value)
|
||||
|
||||
@property
|
||||
def save_path(self):
|
||||
return self._config.save_path
|
||||
|
||||
@save_path.setter
|
||||
def save_path(self, value: str):
|
||||
self._config.save_path = value
|
||||
|
||||
def load_config(self, path, filename):
|
||||
return OmegaConf.load(os.path.join(path, filename))
|
||||
|
||||
@property
|
||||
def api(self):
|
||||
return API(**self._config.api)
|
||||
|
||||
def dict(self):
|
||||
return OmegaConf.to_container(self._config)
|
||||
Reference in New Issue
Block a user