Source code for spinningdiskanalyzer.parameters

"""
Manages image analysis parameters.

Settings are stored in a file named `parameters.yaml` in the `analysis`
subfolder of the folder containing the input images. If that file does
not yet exist, it will be created and initialized with default values.
The user may edit that file manually for a given analysis run.

After calling [](#init), read or modify parameter values with the
[](#get) and [](#set) functions.
"""

from . import meta

from ruamel.yaml import YAML
from pathlib     import Path
from shutil      import copy2 as copy
from appdirs     import user_config_dir
from logging     import getLogger
from typing      import Any

values = {}
yaml   = YAML()
file   = None
log    = getLogger(__name__)


[docs] def init(folder: Path): """ Loads configuration from `parameters.yaml` in given `folder`. If that file does not yet exist, it will be created and initialized with default values for the parameters. """ global file, values file = folder/'parameters.yaml' if not file.exists(): defaults = defaults_file() log.debug(f'Initializing parameters file with defaults: {defaults}') copy(defaults, file) values = yaml.load(file)
[docs] def save(): """Saves configuration to file, preserving comments from first load.""" with file.open('w', encoding='UTF-8-sig') as stream: yaml.dump(values, stream)
[docs] def get(section: str, name: str) -> Any: """Returns the value of the `name`d parameter from the given `section`.""" return values[section][name]
[docs] def set(section: str, name: str, value: Any): """Set the value of the `name`d parameter from the given `section`.""" values[section][name] = value
[docs] def defaults_file() -> Path: """ Returns the configuration file with the default parameter values. The default values are stored in a file named `parameters.yaml`. The primary copy is distributed with this application and stored along with the executable. Typically, files in this location would be read-only, so we provide the user with a copy in their config folder instead, as they can then edit it. """ config_folder = Path(user_config_dir(appname=meta.title, appauthor='')) config_file = config_folder/'parameters.yaml' if not config_file.exists(): log.debug('User configuration file does not exist yet.') here = Path(__file__).parent primary_copy = here/config_file.name log.debug(f'Initiliazing with primary copy: {primary_copy}') config_folder.mkdir(parents=True, exist_ok=True) copy(primary_copy, config_file) return config_file