Beispiel #1
0
def flatten_config(config):
    """Given a ConfigParser, flatten the values into a dict-of-dicts
    representation where each section gets its own dictionary of values.
    """
    out = confit.OrderedDict()
    for section in config.sections():
        sec_dict = out[section] = confit.OrderedDict()
        for option in config.options(section):
            sec_dict[option] = config.get(section, option, True)
    return out
Beispiel #2
0
def transform_data(data):
    """Given a dict-of-dicts representation of legacy config data, tweak
    the data into a new form. This new form is suitable for dumping as
    YAML.
    """
    out = confit.OrderedDict()

    for section, pairs in data.items():
        if section == 'beets':
            # The "main" section. In the new config system, these values
            # are in the "root": no section at all.
            for key, value in pairs.items():
                value = transform_value(value)

                if key.startswith('import_'):
                    # Importer config is now under an "import:" key.
                    if 'import' not in out:
                        out['import'] = confit.OrderedDict()
                    out['import'][key[7:]] = value

                elif key == 'plugins':
                    # Renamed plugins.
                    plugins = value.split()
                    new_plugins = [PLUGIN_NAMES.get(p, p) for p in plugins]
                    out['plugins'] = ' '.join(new_plugins)

                elif key == 'replace':
                    # YAMLy representation for character replacements.
                    replacements = confit.OrderedDict()
                    for pat, repl in grouper(2, value.split()):
                        if repl == '<strip>':
                            repl = ''
                        replacements[pat] = repl
                    out['replace'] = replacements

                elif key == 'pluginpath':
                    # Used to be a colon-separated string. Now a list.
                    out['pluginpath'] = value.split(':')

                else:
                    out[key] = value

        elif pairs:
            # Other sections (plugins, etc).
            sec_out = out[section] = confit.OrderedDict()
            for key, value in pairs.items():

                # Standardized "auto" option.
                if key in AUTO_KEYS:
                    key = 'auto'

                # Unnecessary : hack in queries.
                if section == 'paths':
                    key = key.replace('_', ':')

                # Changed option names for importfeeds plugin.
                if section == 'importfeeds':
                    if key.startswith(IMPORTFEEDS_PREFIX):
                        key = key[len(IMPORTFEEDS_PREFIX):]

                sec_out[key] = transform_value(value)

    return out