Пример #1
0
def _load(files, defaults, overrides):
    parser = configparser.RawConfigParser()

    # TODO: simply return path to config file for defaults so we can load it
    # all in the same way?
    logger.info('Loading config from builtin defaults')
    for default in defaults:
        if isinstance(default, compat.text_type):
            default = default.encode('utf-8')
        parser.readfp(io.BytesIO(default))

    # Load config from a series of config files
    files = [path.expand_path(f) for f in files]
    for name in files:
        if os.path.isdir(name):
            for filename in os.listdir(name):
                filename = os.path.join(name, filename)
                if os.path.isfile(filename) and filename.endswith('.conf'):
                    _load_file(parser, filename)
        else:
            _load_file(parser, name)

    # If there have been parse errors there is a python bug that causes the
    # values to be lists, this little trick coerces these into strings.
    parser.readfp(io.BytesIO())

    raw_config = {}
    for section in parser.sections():
        raw_config[section] = dict(parser.items(section))

    logger.info('Loading config from command line options')
    for section, key, value in overrides:
        raw_config.setdefault(section, {})[key] = value

    return raw_config
Пример #2
0
def main():
    settings = load()
    if not settings:
        return

    config = convert(settings)

    known = [
        'spotify', 'scrobbler', 'mpd', 'mpris', 'local', 'stream', 'http']
    extensions = [e for e in ext.load_extensions() if e.ext_name in known]

    print b'Converted config:\n'
    print config_lib.format(config, extensions)

    conf_file = path.expand_path(b'$XDG_CONFIG_DIR/mopidy/mopidy.conf')
    if os.path.exists(conf_file):
        print '%s exists, exiting.' % conf_file
        sys.exit(1)

    print 'Write new config to %s? [yN]' % conf_file,
    if raw_input() != 'y':
        print 'Not saving, exiting.'
        sys.exit(0)

    serialized_config = config_lib.format(config, extensions, display=False)
    with io.open(conf_file, 'wb') as filehandle:
        filehandle.write(serialized_config)
    print 'Done.'
Пример #3
0
def main():
    settings = load()
    if not settings:
        return

    config = convert(settings)

    known = ["spotify", "scrobbler", "mpd", "mpris", "local", "stream", "http"]
    extensions = [e for e in ext.load_extensions() if e.ext_name in known]

    print(b"Converted config:\n")
    print(config_lib.format(config, extensions))

    conf_file = path.expand_path(b"$XDG_CONFIG_DIR/mopidy/mopidy.conf")
    if os.path.exists(conf_file):
        print("%s exists, exiting." % conf_file)
        sys.exit(1)

    print("Write new config to %s? [yN]" % conf_file, end=" ")
    if raw_input() != "y":
        print("Not saving, exiting.")
        sys.exit(0)

    serialized_config = config_lib.format(config, extensions, display=False)
    with io.open(conf_file, "wb") as filehandle:
        filehandle.write(serialized_config)
    print("Done.")
Пример #4
0
def load():
    settings_file = path.expand_path(b'$XDG_CONFIG_DIR/mopidy/settings.py')
    print 'Checking %s' % settings_file

    setting_globals = {}
    try:
        execfile(settings_file, setting_globals)
    except Exception as e:
        print 'Problem loading settings: %s' % e
    return setting_globals
Пример #5
0
def load():
    settings_file = path.expand_path(b"$XDG_CONFIG_DIR/mopidy/settings.py")
    print("Checking %s" % settings_file)

    setting_globals = {}
    try:
        execfile(settings_file, setting_globals)
    except Exception as e:
        print("Problem loading settings: %s" % e)
    return setting_globals
Пример #6
0
def _load(files, defaults, overrides):
    parser = configparser.RawConfigParser()

    files = [path.expand_path(f) for f in files]
    sources = ['builtin defaults'] + files + ['command line options']
    logger.info('Loading config from: %s', ', '.join(sources))

    # TODO: simply return path to config file for defaults so we can load it
    # all in the same way?
    for default in defaults:
        if isinstance(default, unicode):
            default = default.encode('utf-8')
        parser.readfp(io.BytesIO(default))

    # Load config from a series of config files
    for filename in files:
        try:
            with io.open(filename, 'rb') as filehandle:
                parser.readfp(filehandle)
        except configparser.MissingSectionHeaderError as e:
            logging.warning('%s does not have a config section, not loaded.',
                            filename)
        except configparser.ParsingError as e:
            linenos = ', '.join(str(lineno) for lineno, line in e.errors)
            logger.warning('%s has errors, line %s has been ignored.',
                            filename, linenos)
        except IOError:
            # TODO: if this is the initial load of logging config we might not
            # have a logger at this point, we might want to handle this better.
            logger.debug('Config file %s not found; skipping', filename)

    # If there have been parse errors there is a python bug that causes the
    # values to be lists, this little trick coerces these into strings.
    parser.readfp(io.BytesIO())

    raw_config = {}
    for section in parser.sections():
        raw_config[section] = dict(parser.items(section))

    for section, key, value in overrides or []:
        raw_config.setdefault(section, {})[key] = value

    return raw_config
Пример #7
0
def _load(files, defaults, overrides):
    parser = configparser.RawConfigParser()

    files = [path.expand_path(f) for f in files]
    sources = ['builtin defaults'] + files + ['command line options']
    logger.info('Loading config from: %s', ', '.join(sources))

    # TODO: simply return path to config file for defaults so we can load it
    # all in the same way?
    for default in defaults:
        if isinstance(default, unicode):
            default = default.encode('utf-8')
        parser.readfp(io.BytesIO(default))

    # Load config from a series of config files
    for filename in files:
        try:
            with io.open(filename, 'rb') as filehandle:
                parser.readfp(filehandle)
        except configparser.MissingSectionHeaderError as e:
            logging.warning('%s does not have a config section, not loaded.',
                            filename)
        except configparser.ParsingError as e:
            linenos = ', '.join(str(lineno) for lineno, line in e.errors)
            logger.warning('%s has errors, line %s has been ignored.',
                           filename, linenos)
        except IOError:
            # TODO: if this is the initial load of logging config we might not
            # have a logger at this point, we might want to handle this better.
            logger.debug('Config file %s not found; skipping', filename)

    # If there have been parse errors there is a python bug that causes the
    # values to be lists, this little trick coerces these into strings.
    parser.readfp(io.BytesIO())

    raw_config = {}
    for section in parser.sections():
        raw_config[section] = dict(parser.items(section))

    for section, key, value in overrides:
        raw_config.setdefault(section, {})[key] = value

    return raw_config