Skip to content

config

Configuration Options

C

Bases: C

Names used in config object

Source code in mmg_toolbox/tkguis/misc/config.py
class C(config.C):
    """Names used in config object"""
    processing_directory = 'processing_directory'
    notebook_directory = 'notebook_directory'
    recent_data_directories = 'recent_data_directories'
    small_screen_height = 'small_screen_height'
    text_size = 'text_size'
    text_size_small = 'text_size_small'
    plot_size = 'plot_size'
    image_size = 'image_size'
    plot_max_percent = 'plot_max_percent'
    plot_dpi = 'plot_dpi'
    plot_title = 'plot_title'
    default_colormap = 'default_colormap'
    current_dir = 'current_dir'
    current_proc = 'current_proc'
    current_nb = 'current_nb'

check_config_filename(config_filename)

Check config filename is writable, raise OSError if not

Source code in mmg_toolbox/tkguis/misc/config.py
def check_config_filename(config_filename: str | None) -> str:
    """Check config filename is writable, raise OSError if not"""
    if config_filename is None:
        config_filename = CONFIG_FILE
    return check_file_access(config_filename)

default_config(beamline=None)

Returns the default beamline config dict

Source code in mmg_toolbox/tkguis/misc/config.py
def default_config(beamline: str | None = None) -> dict:
    """Returns the default beamline config dict"""
    cfg = CONFIG.copy()
    if beamline is None:
        beamline = get_beamline()
    if beamline in config.BEAMLINE_CONFIG:
        cfg.update(config.BEAMLINE_CONFIG[beamline])
    return cfg

get_config(config_filename=None, beamline=None)

merge loaded config into default beamline config and return the config dict

Source code in mmg_toolbox/tkguis/misc/config.py
def get_config(config_filename: str | None = None, beamline: str | None = None) -> dict:
    """merge loaded config into default beamline config and return the config dict"""
    config_filename = check_config_filename(config_filename)
    user_config = load_config(config_filename)
    cfg = default_config(beamline)
    if beamline and user_config.get(C.beamline) and beamline != user_config.get(C.beamline):
        # default config overrides user config when changing beamline
        user_config.update(cfg)
        return user_config
    cfg.update(user_config)
    return cfg

load_config(config_filename=CONFIG_FILE)

Loads a config dict from file, by default from the default location

Source code in mmg_toolbox/tkguis/misc/config.py
def load_config(config_filename: str = CONFIG_FILE) -> dict:
    """Loads a config dict from file, by default from the default location"""
    if os.path.isfile(config_filename):
        with open(config_filename, 'r') as f:
            return json.load(f)
    return {}

reset_config(cfg)

Reset config dict in place with default values of beamline

Source code in mmg_toolbox/tkguis/misc/config.py
def reset_config(cfg: dict) -> None:
    """Reset config dict in place with default values of beamline"""
    beamline = cfg.get(C.beamline, None)
    cfg.clear()
    cfg.update(default_config(beamline))

save_config(cfg)

Save the config dict into the file location referenced in config['config_file']

Source code in mmg_toolbox/tkguis/misc/config.py
def save_config(cfg: dict):
    """Save the config dict into the file location referenced in config['config_file']"""
    config_filename = cfg.get(C.conf_file, CONFIG_FILE)
    with open(config_filename, 'w') as f:
        json.dump(cfg, f)
    print('Saved config to {}'.format(config_filename))

save_config_as(config_filename=None, **kwargs)

Save the config dict into a new file location

Source code in mmg_toolbox/tkguis/misc/config.py
def save_config_as(config_filename: str | None = None, **kwargs):
    """Save the config dict into a new file location"""
    cfg = get_config(config_filename)
    cfg.update(kwargs)
    cfg[C.conf_file] = config_filename
    save_config(cfg)