diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3ef04704..267e31da 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,7 +6,7 @@ repos: entry: bash -c "poetry run mypy ." language: system - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.2.1 + rev: v0.3.3 hooks: - id: ruff - id: ruff-format diff --git a/ixmp4/conf/base.py b/ixmp4/conf/base.py index d8ab5f29..2ff75e7a 100644 --- a/ixmp4/conf/base.py +++ b/ixmp4/conf/base.py @@ -9,8 +9,6 @@ class PlatformInfo(pydantic.BaseModel): class Config(Protocol): - def list_platforms(self) -> list[PlatformInfo]: - ... + def list_platforms(self) -> list[PlatformInfo]: ... - def get_platform(self, key: str) -> PlatformInfo: - ... + def get_platform(self, key: str) -> PlatformInfo: ... diff --git a/ixmp4/conf/logging/debug.conf b/ixmp4/conf/logging/debug.conf deleted file mode 100644 index 1499ac15..00000000 --- a/ixmp4/conf/logging/debug.conf +++ /dev/null @@ -1,24 +0,0 @@ - -[loggers] -keys = root - -[handlers] -keys = console - -[formatters] -keys = generic - -[logger_root] -level = DEBUG -handlers = console -qualname = - -[handler_console] -class = StreamHandler -args = (sys.stderr,) -level = NOTSET -formatter = generic - -[formatter_generic] -format = [%(levelname)s] %(asctime)s - %(name)s: %(message)s -datefmt = %H:%M:%S diff --git a/ixmp4/conf/logging/debug.json b/ixmp4/conf/logging/debug.json new file mode 100644 index 00000000..6613e2ef --- /dev/null +++ b/ixmp4/conf/logging/debug.json @@ -0,0 +1,21 @@ +{ + "version": 1, + "disable_existing_loggers": false, + "formatters":{ + "generic": { + "format": "[%(levelname)s] %(asctime)s - %(name)s: %(message)s", + "datefmt": "%H:%M:%S" + } + }, + "root": { + "level": "DEBUG", + "handlers": ["console"] + }, + "handlers":{ + "console":{ + "class": "logging.StreamHandler", + "level": "NOTSET", + "formatter": "generic" + } + } +} diff --git a/ixmp4/conf/logging/development.conf b/ixmp4/conf/logging/development.conf deleted file mode 100644 index 139fd0c1..00000000 --- a/ixmp4/conf/logging/development.conf +++ /dev/null @@ -1,24 +0,0 @@ - -[loggers] -keys = root - -[handlers] -keys = console - -[formatters] -keys = generic - -[logger_root] -level = INFO -handlers = console -qualname = - -[handler_console] -class = StreamHandler -args = (sys.stderr,) -level = NOTSET -formatter = generic - -[formatter_generic] -format = [%(levelname)s] %(asctime)s - %(name)s: %(message)s -datefmt = %H:%M:%S diff --git a/ixmp4/conf/logging/development.json b/ixmp4/conf/logging/development.json new file mode 100644 index 00000000..97d7a328 --- /dev/null +++ b/ixmp4/conf/logging/development.json @@ -0,0 +1,21 @@ +{ + "version": 1, + "disable_existing_loggers": false, + "formatters":{ + "generic":{ + "format": "[%(levelname)s] %(asctime)s - %(name)s: %(message)s", + "datefmt": "%H:%M:%S" + } + }, + "root":{ + "level": "INFO", + "handlers": ["console"] + }, + "handlers":{ + "console":{ + "class": "logging.StreamHandler", + "level": "NOTSET", + "formatter": "generic" + } + } +} diff --git a/ixmp4/conf/logging/production.conf b/ixmp4/conf/logging/production.conf deleted file mode 100644 index 38fc6e7c..00000000 --- a/ixmp4/conf/logging/production.conf +++ /dev/null @@ -1,30 +0,0 @@ - -[loggers] -keys = root,ixmp4_core - -[handlers] -keys = console - -[formatters] -keys = generic - -[logger_root] -level = NOTSET -handlers = -qualname = - -[logger_ixmp4_core] -level = INFO -handlers = console -qualname = ixmp4 - - -[handler_console] -class = StreamHandler -args = (sys.stderr,) -level = NOTSET -formatter = generic - -[formatter_generic] -format = [%(levelname)s] %(asctime)s - %(name)s: %(message)s -datefmt = %H:%M:%S diff --git a/ixmp4/conf/logging/production.json b/ixmp4/conf/logging/production.json new file mode 100644 index 00000000..261c1842 --- /dev/null +++ b/ixmp4/conf/logging/production.json @@ -0,0 +1,23 @@ +{ + "version": 1, + "disable_existing_loggers": false, + "formatters": { + "generic": { + "format": "[%(levelname)s] %(asctime)s - %(name)s: %(message)s", + "datefmt": "%H:%M:%S" + } + }, + "loggers": { + "ixmp4": { + "level": "INFO", + "handlers": ["console"] + } + }, + "handlers": { + "console": { + "class": "logging.StreamHandler", + "level": "NOTSET", + "formatter": "generic" + } + } +} diff --git a/ixmp4/conf/logging/server.conf b/ixmp4/conf/logging/server.conf deleted file mode 100644 index 87baeb58..00000000 --- a/ixmp4/conf/logging/server.conf +++ /dev/null @@ -1,73 +0,0 @@ - -[loggers] -keys = root,sqlalchemy,fastapi,httpx,uvicorn,access,watchfiles - -[handlers] -keys = access,debug,error,console - -[formatters] -keys = generic - -[logger_sqlalchemy] -level = NOTSET -handlers = debug,error -qualname = sqlalchemy - -[logger_watchfiles] -level = ERROR -handlers = error,debug -qualname = watchfiles.main - -[logger_fastapi] -level = NOTSET -handlers = debug,error,console -qualname = fastapi - -[logger_uvicorn] -level = NOTSET -handlers = debug,error,console -qualname = uvicorn - -[logger_access] -level = NOTSET -handlers = access -qualname = uvicorn.access - -[logger_httpx] -level = NOTSET -handlers = debug,error -qualname = httpx - - -[logger_root] -level = NOTSET -handlers = debug,error -qualname = - -[handler_access] -class = handlers.RotatingFileHandler -args=(os.getenv("IXMP4_ACCESS_LOG"), "a", 250000) -level = INFO -formatter = generic - -[handler_debug] -class = handlers.RotatingFileHandler -args=(os.getenv("IXMP4_DEBUG_LOG"), "a", 250000) -level = DEBUG -formatter = generic - -[handler_error] -class = handlers.RotatingFileHandler -args=(os.getenv("IXMP4_ERROR_LOG"), "a", 250000) -level = WARN -formatter = generic - -[handler_console] -class = StreamHandler -args = (sys.stderr,) -level = INFO -formatter = generic - -[formatter_generic] -format = [%(levelname)s] %(asctime)s - %(name)s: %(message)s -datefmt = %H:%M:%S diff --git a/ixmp4/conf/logging/server.json b/ixmp4/conf/logging/server.json new file mode 100644 index 00000000..f693ad15 --- /dev/null +++ b/ixmp4/conf/logging/server.json @@ -0,0 +1,71 @@ +{ + "version": 1, + "disable_existing_loggers": false, + "formatters":{ + "generic": { + "format": "[%(levelname)s] %(asctime)s - %(name)s: %(message)s", + "datefmt": "%H:%M:%S" + } + }, + "loggers": { + "fastapi": { + "level": "NOTSET", + "handlers": ["debug","error","console"] + }, + "httpx": { + "level": "NOTSET", + "handlers": ["debug","error"] + }, + "sqlalchemy": { + "level": "NOTSET", + "handlers": ["debug","error"] + }, + "uvicorn": { + "level": "NOTSET", + "handlers": ["debug","error","console"] + }, + "uvicorn.access": { + "level": "NOTSET", + "handlers": ["access"] + }, + "watchfiles.main": { + "level": "ERROR", + "handlers": ["error","debug"] + } + }, + "handlers": { + "access": { + "class": "logging.handlers.RotatingFileHandler", + "level": "INFO", + "formatter": "generic", + "filename": "os.getenv('IXMP4_ACCESS_LOG')", + "mode": "a", + "maxBytes": 250000 + }, + "console": { + "class": "logging.StreamHandler", + "level": "INFO", + "formatter": "generic" + }, + "debug": { + "class": "logging.handlers.RotatingFileHandler", + "level": "DEBUG", + "formatter": "generic", + "filename": "os.getenv('IXMP4_DEBUG_LOG')", + "mode": "a", + "maxBytes": 250000 + }, + "error": { + "class": "logging.handlers.RotatingFileHandler", + "level": "WARN", + "formatter": "generic", + "filename": "os.getenv('IXMP4_ERROR_LOG')", + "mode": "a", + "maxBytes": 250000 + } + }, + "root": { + "level": "NOTSET", + "handlers": ["debug","error"] + } +} diff --git a/ixmp4/conf/settings.py b/ixmp4/conf/settings.py index 51425146..ac6e07bd 100644 --- a/ixmp4/conf/settings.py +++ b/ixmp4/conf/settings.py @@ -1,3 +1,4 @@ +import json import logging import logging.config import os @@ -22,9 +23,9 @@ class Settings(BaseSettings): - mode: Literal["production"] | Literal["development"] | Literal[ - "debug" - ] = "production" + mode: Literal["production"] | Literal["development"] | Literal["debug"] = ( + "production" + ) storage_directory: Path = Field( "~/.local/share/ixmp4/", json_schema_extra={"env": "ixmp4_dir"} ) @@ -144,8 +145,10 @@ def configure_logging(self, config: str): os.environ.setdefault("IXMP4_DEBUG_LOG", str(debug_file.absolute())) os.environ.setdefault("IXMP4_ERROR_LOG", str(error_file.absolute())) - logging_config = here / f"logging/{config}.conf" - logging.config.fileConfig(logging_config, disable_existing_loggers=False) + logging_config = here / f"logging/{config}.json" + with open(logging_config) as file: + config_dict = json.load(file) + logging.config.dictConfig(config_dict) def check_credentials(self): if self.default_credentials is not None: diff --git a/ixmp4/data/abstract/base.py b/ixmp4/data/abstract/base.py index 358a7526..6a4fe0b5 100644 --- a/ixmp4/data/abstract/base.py +++ b/ixmp4/data/abstract/base.py @@ -37,45 +37,38 @@ class BaseModel(Protocol, metaclass=BaseMeta): class BaseRepository(Protocol): - def __init__(self, *args, **kwargs) -> None: - ... + def __init__(self, *args, **kwargs) -> None: ... class Retriever(BaseRepository, Protocol): - def get(self, *args, **kwargs) -> BaseModel: - ... + def get(self, *args, **kwargs) -> BaseModel: ... class Creator(BaseRepository, Protocol): - def create(self, *args, **kwargs) -> BaseModel: - ... + def create(self, *args, **kwargs) -> BaseModel: ... class Deleter(BaseRepository, Protocol): - def delete(self, *args, **kwargs) -> None: - ... + def delete(self, *args, **kwargs) -> None: ... class Lister(BaseRepository, Protocol): - def list(self, *args, **kwargs) -> list: - ... + def list(self, *args, **kwargs) -> list: ... class Tabulator(BaseRepository, Protocol): - def tabulate(self, *args, **kwargs) -> pd.DataFrame: - ... + def tabulate(self, *args, **kwargs) -> pd.DataFrame: ... class Enumerator(Lister, Tabulator, Protocol): - def enumerate(self, *args, table: bool = False, **kwargs) -> list | pd.DataFrame: - ... + def enumerate( + self, *args, table: bool = False, **kwargs + ) -> list | pd.DataFrame: ... class BulkUpserter(BaseRepository, Protocol): - def bulk_upsert(self, *args, **kwargs) -> None: - ... + def bulk_upsert(self, *args, **kwargs) -> None: ... class BulkDeleter(BaseRepository, Protocol): - def bulk_delete(self, *args, **kwargs) -> None: - ... + def bulk_delete(self, *args, **kwargs) -> None: ... diff --git a/ixmp4/data/abstract/iamc/measurand.py b/ixmp4/data/abstract/iamc/measurand.py index c4a17380..53b34139 100644 --- a/ixmp4/data/abstract/iamc/measurand.py +++ b/ixmp4/data/abstract/iamc/measurand.py @@ -30,11 +30,9 @@ class MeasurandRepository( base.Enumerator, Protocol, ): - def create(self, variable_name: str, unit__id: int) -> Measurand: - ... + def create(self, variable_name: str, unit__id: int) -> Measurand: ... - def get(self, variable_name: str, unit__id: int) -> Measurand: - ... + def get(self, variable_name: str, unit__id: int) -> Measurand: ... def get_or_create(self, variable_name: str, unit__id: int) -> Measurand: try: @@ -44,10 +42,8 @@ def get_or_create(self, variable_name: str, unit__id: int) -> Measurand: def list( self, - ) -> list[Measurand]: - ... + ) -> list[Measurand]: ... def tabulate( self, - ) -> pd.DataFrame: - ... + ) -> pd.DataFrame: ... diff --git a/ixmp4/data/api/optimization/indexset.py b/ixmp4/data/api/optimization/indexset.py index 9caea44c..477bd308 100644 --- a/ixmp4/data/api/optimization/indexset.py +++ b/ixmp4/data/api/optimization/indexset.py @@ -17,9 +17,13 @@ class IndexSet(base.BaseModel): id: int name: str - elements: StrictFloat | StrictInt | list[ - StrictFloat | StrictInt | StrictStr - ] | StrictStr | None + elements: ( + StrictFloat + | StrictInt + | StrictStr + | list[StrictFloat | StrictInt | StrictStr] + | None + ) run__id: int created_at: datetime | None diff --git a/ixmp4/db/migrations/versions/97ba231770e2_create_optimization_scalar_table.py b/ixmp4/db/migrations/versions/97ba231770e2_create_optimization_scalar_table.py index 0d4ae576..e269528a 100644 --- a/ixmp4/db/migrations/versions/97ba231770e2_create_optimization_scalar_table.py +++ b/ixmp4/db/migrations/versions/97ba231770e2_create_optimization_scalar_table.py @@ -6,6 +6,7 @@ Create Date: 2023-11-14 15:59:19.314252 """ + import sqlalchemy as sa from alembic import op diff --git a/ixmp4/db/migrations/versions/c71efc396d2b_initial_migration.py b/ixmp4/db/migrations/versions/c71efc396d2b_initial_migration.py index 912a575e..0b6697cc 100644 --- a/ixmp4/db/migrations/versions/c71efc396d2b_initial_migration.py +++ b/ixmp4/db/migrations/versions/c71efc396d2b_initial_migration.py @@ -6,6 +6,7 @@ Create Date: 2023-04-26 15:37:46.677955 """ + import sqlalchemy as sa from alembic import op diff --git a/ixmp4/db/migrations/versions/da1fba23f206_create_optimization_indexset_table.py b/ixmp4/db/migrations/versions/da1fba23f206_create_optimization_indexset_table.py index c50a1bd3..f0f3fb87 100644 --- a/ixmp4/db/migrations/versions/da1fba23f206_create_optimization_indexset_table.py +++ b/ixmp4/db/migrations/versions/da1fba23f206_create_optimization_indexset_table.py @@ -6,6 +6,7 @@ Create Date: 2023-08-30 12:29:50.037072 """ + import sqlalchemy as sa from alembic import op from sqlalchemy.dialects import postgresql diff --git a/ixmp4/server/rest/optimization/indexset.py b/ixmp4/server/rest/optimization/indexset.py index 4b9f7a81..f0ec76fc 100644 --- a/ixmp4/server/rest/optimization/indexset.py +++ b/ixmp4/server/rest/optimization/indexset.py @@ -21,9 +21,9 @@ class IndexSetInput(BaseModel): class ElementsInput(BaseModel): - elements: StrictFloat | StrictInt | list[ - StrictFloat | StrictInt | StrictStr - ] | StrictStr + elements: ( + StrictFloat | StrictInt | StrictStr | list[StrictFloat | StrictInt | StrictStr] + ) @autodoc