Skip to content

Commit

Permalink
added logging and settings creator
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhroom committed Aug 30, 2024
1 parent 244faaf commit 8e60cf6
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion rascal2/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import logging
import pathlib
import platform
from os import PathLike

from rascal2.core import Settings

SOURCE_PATH = pathlib.Path(__file__).parent
STATIC_PATH = SOURCE_PATH / "static"
Expand Down Expand Up @@ -30,4 +34,49 @@ def path_for(filename: str):
return (IMAGES_PATH / filename).as_posix()


# TODO: Initial global QSetting
def setup_settings(project_path: str | PathLike) -> Settings:
"""Set up the Settings object for the project.
Parameters
----------
project_path : str or PathLike
The path to the current RasCAL-2 project.
Returns
-------
Settings
If a settings.json file already exists in the
RasCAL-2 project, returns a Settings object with
the settings defined there. Otherwise, returns a
(global) default Settings object.
"""
filepath = pathlib.Path(project_path, "settings.json")
if filepath.is_file():
json = pathlib.Path(filepath).read_text()
return Settings.model_validate_json(json)
return Settings()


def setup_logging(logpath: str | PathLike, level: int = logging.INFO) -> logging.Logger:
"""Set up logging for the project.
The default logging path and level are defined in the settings.
Parameters
----------
logpath : str | PathLike
The path to where the log file will be written.
file_level : int, default logging.INFO
The debug level for the logger.
"""
logpath = pathlib.Path(logpath)
logger = logging.getLogger(logpath.stem)
logging.setLevel(level)

# TODO add console print handler when console is added
log_filehandler = logging.FileHandler(logpath)
logger.addHandler(log_filehandler)

return logger

0 comments on commit 8e60cf6

Please sign in to comment.