Skip to content

Commit

Permalink
fix: changes to fix lint mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
Killg0d committed Dec 27, 2024
1 parent d9bc842 commit ecec2ff
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/apps/backend/modules/config/config_service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import configparser
import os
from typing import Any
from pathlib import Path
from modules.error.custom_errors import MissingKeyError
from modules.common.types import ErrorCode
import json


def get_parent_directory(directory: str, levels: int) -> Path:
parent_dir = Path(directory)
Expand All @@ -14,11 +11,11 @@ def get_parent_directory(directory: str, levels: int) -> Path:
return parent_dir

class ConfigService:
_config = None
_config: configparser.ConfigParser = configparser.ConfigParser()
config_path = get_parent_directory(__file__, 6) / "config"

@staticmethod
def load_config():
def load_config()->None:
config = configparser.ConfigParser()
default_config = ConfigService.config_path / "default.ini"
app_env = os.environ.get('APP_ENV', "development")
Expand All @@ -37,7 +34,8 @@ def load_config():
print("config:", config_dict)

@staticmethod
def __ensure_all_sections_exist(config: configparser.ConfigParser, default_config_path: Path):
def __ensure_all_sections_exist(config: configparser.ConfigParser,
default_config_path: Path)->None:
default_config = configparser.ConfigParser()
default_config.read(default_config_path)

Expand All @@ -46,10 +44,10 @@ def __ensure_all_sections_exist(config: configparser.ConfigParser, default_confi
config.add_section(section)

@staticmethod
def __load_environment_variables(config: configparser.ConfigParser):
def __load_environment_variables(config: configparser.ConfigParser)->None:
env_config = configparser.ConfigParser()
custom_env_file = ConfigService.config_path / "custom-environment-variables.ini"

if custom_env_file.exists():
env_config.read(custom_env_file)
config.read(custom_env_file)
Expand All @@ -63,16 +61,17 @@ def __load_environment_variables(config: configparser.ConfigParser):
config[section][key] = env_var

@staticmethod
def get_value(*, key: str, section: str = 'DEFAULT') -> Any:
def get_value(*, key: str, section: str = 'DEFAULT') -> str:
try:
value = ConfigService._config.get(section, key, fallback=None)
return value if value else None
except (configparser.NoOptionError, configparser.NoSectionError):
raise MissingKeyError(missing_key=key, error_code=ErrorCode.MISSING_KEY)
except (configparser.NoOptionError, configparser.NoSectionError) as exc:
raise MissingKeyError(missing_key=key, error_code=ErrorCode.MISSING_KEY) from exc

@staticmethod
def has_value(*, key: str, section: str = 'DEFAULT') -> bool:
if ConfigService._config.has_option(section, key):
value = ConfigService._config.get(section, key, fallback=None)
return bool(value)
return False
return bool(value)
return False

0 comments on commit ecec2ff

Please sign in to comment.