Ejemplo n.º 1
0
Archivo: io.py Proyecto: snazy/nessie
def write_to_file(config: confuse.Configuration) -> None:
    """Write updated config to file."""
    config_filename = os.path.join(config.config_dir(),
                                   confuse.CONFIG_FILENAME)
    with open(config_filename, "w", encoding="UTF-8") as f:
        f.write(config.dump())
Ejemplo n.º 2
0
class PrescConfig:
    """
    Wrapper around a confuse Configuration object.

    This is used for managing config options in PRESC, including the global config.

    Attributes
    ----------
    from_config : PrescConfig
        A PrescConfig instance to override. If None, the config is initialized to the default settings.
    """
    def __init__(self, from_config=None):
        if from_config:
            self._config = LocalConfig(from_config.settings)
        else:
            self._config = Configuration("PRESC", read=False)
            self.reset_defaults()

    def reset_defaults(self):
        """Reset all options to their defaults."""
        self._config.clear()
        self.update_from_file(DEFAULT_CONFIG_PATH)

    def update_from_file(self, file_path):
        """Override current settings with those in the given YAML file."""
        self._config.set_file(str(file_path))

    def set(self, settings):
        """Update one or more config options.

        These should be specified in a dict, either mirroring the nested
        structure of the configuration file, or as flat key-value pairs using
        dots to indicate nested namespaces.

        Examples
        --------
        ``config.set({"report": {"title": "My Report", "author": "Me"}})``
        ``config.set({"report.title": "My Report", "report.author": "Me"})``

        """
        if not isinstance(settings, dict):
            raise PrescError("Config settings must be specified in a dict")
        self._config.set_args(settings, dots=True)

    @property
    def settings(self):
        """Access the underlying confuse object."""
        return self._config

    def dump(self):
        """Dump the current config in YAML format."""
        return self._config.dump()

    # Make option access work on the PrescConfig:
    def __getitem__(self, key):
        return self._config.__getitem__(key)

    def get(self, template=None):
        # If template is None, defer to the underlying default arg.
        template_arg = {}
        if template:
            template_arg["template"] = template
        return self._config.get(**template_arg)

    def flatten(self):
        return self._config.flatten()
def update_config(config: confuse.Configuration):
    """Update the config file."""
    open(config.user_config_path(), 'w').write(config.dump())
Ejemplo n.º 4
0
def to_dict(config: confuse.Configuration) -> dict:
    """Convert confuse Configuration object to dict."""
    yaml_config = config.dump(redact=True)
    dct = yaml.safe_load(StringIO(yaml_config))
    return dct
Ejemplo n.º 5
0
def update_config(config: confuse.Configuration):
    open(config.user_config_path(), 'w').write(config.dump())