def check_old_locations(): dot_mopidy_dir = path.expand_path(b'~/.mopidy') if os.path.isdir(dot_mopidy_dir): logger.warning( 'Old Mopidy dot dir found at %s. Please migrate your config to ' 'the ini-file based config format. See release notes for further ' 'instructions.', dot_mopidy_dir) old_settings_file = path.expand_path(b'$XDG_CONFIG_DIR/mopidy/settings.py') if os.path.isfile(old_settings_file): logger.warning( 'Old Mopidy settings file found at %s. Please migrate your ' 'config to the ini-file based config format. See release notes ' 'for further instructions.', old_settings_file)
def __init__(self, backend, config): super().__init__(backend) ext_config = config[Extension.ext_name] if ext_config["playlists_dir"] is None: self._playlists_dir = Extension.get_data_dir(config) else: self._playlists_dir = path.expand_path(ext_config["playlists_dir"]) if ext_config["base_dir"] is None: self._base_dir = self._playlists_dir else: self._base_dir = path.expand_path(ext_config["base_dir"]) self._default_encoding = ext_config["default_encoding"] self._default_extension = ext_config["default_extension"]
def _get_media_dirs(self, config): for entry in config['file']['media_dirs']: media_dir = {} media_dir_split = entry.split('|', 1) local_path = path.expand_path( media_dir_split[0].encode(FS_ENCODING)) if not local_path: logger.debug( 'Failed expanding path (%s) from file/media_dirs config ' 'value.', media_dir_split[0]) continue elif not os.path.isdir(local_path): logger.warning( '%s is not a directory. Please create the directory or ' 'update the file/media_dirs config value.', local_path) continue media_dir['path'] = local_path if len(media_dir_split) == 2: media_dir['name'] = media_dir_split[1] else: # TODO Mpd client should accept / in dir name media_dir['name'] = media_dir_split[0].replace(os.sep, '+') yield media_dir
def _get_media_dirs(self, config): for entry in config["file"]["media_dirs"]: media_dir = {} media_dir_split = entry.split("|", 1) local_path = path.expand_path(media_dir_split[0]) if local_path is None: logger.debug( "Failed expanding path (%s) from file/media_dirs config " "value.", media_dir_split[0], ) continue elif not local_path.is_dir(): logger.warning( "%s is not a directory. Please create the directory or " "update the file/media_dirs config value.", local_path, ) continue media_dir["path"] = local_path if len(media_dir_split) == 2: media_dir["name"] = media_dir_split[1] else: # TODO Mpd client should accept / in dir name media_dir["name"] = media_dir_split[0].replace(os.sep, "+") yield media_dir
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, bytes): default = default.decode() parser.read_string(default) # Load config from a series of config files for f in files: f = path.expand_path(f) if f.is_dir(): for g in f.iterdir(): if g.is_file() and g.suffix == ".conf": _load_file(parser, g.resolve()) else: _load_file(parser, f.resolve()) 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
def deserialize(self, value): value = value.strip() expanded = path.expand_path(value) validators.validate_required(value, self._required) validators.validate_required(expanded, self._required) if not value or expanded is None: return None return ExpandedPath(value, expanded)
def get_config_dir(cls, config): """Get or create configuration directory for the extension. :param config: the Mopidy config object :return: pathlib.Path """ assert cls.ext_name is not None config_dir_path = (path.expand_path(config["core"]["config_dir"]) / cls.ext_name) path.get_or_create_dir(config_dir_path) return config_dir_path
def get_cache_dir(cls, config): """Get or create cache directory for the extension. Use this directory to cache data that can safely be thrown away. :param config: the Mopidy config object :return: pathlib.Path """ assert cls.ext_name is not None cache_dir_path = (path.expand_path(config["core"]["cache_dir"]) / cls.ext_name) path.get_or_create_dir(cache_dir_path) return cache_dir_path
def get_data_dir(cls, config): """Get or create data directory for the extension. Use this directory to store data that should be persistent. :param config: the Mopidy config object :returns: pathlib.Path """ assert cls.ext_name is not None data_dir_path = (path.expand_path(config["core"]["data_dir"]) / cls.ext_name) path.get_or_create_dir(data_dir_path) return data_dir_path
def create_initial_config_file(args, extensions_data): """Initialize whatever the last config file is with defaults""" config_file = args.config_files[-1] if os.path.exists(path.expand_path(config_file)): return try: default = config_lib.format_initial(extensions_data) path.get_or_create_file(config_file, mkdir=False, content=default) logger.info("Initialized %s with default config", config_file) except IOError as error: logger.warning("Unable to initialize %s with default config: %s", config_file, encoding.locale_decode(error))
def create_initial_config_file(args, extensions_data): """Initialize whatever the last config file is with defaults""" config_file = args.config_files[-1] if os.path.exists(path.expand_path(config_file)): return try: default = config_lib.format_initial(extensions_data) path.get_or_create_file(config_file, mkdir=False, content=default) logger.info('Initialized %s with default config', config_file) except IOError as error: logger.warning('Unable to initialize %s with default config: %s', config_file, encoding.locale_decode(error))
def create_file_structures_and_config(args, extensions_data): path.get_or_create_dir(b'$XDG_DATA_DIR/mopidy') path.get_or_create_dir(b'$XDG_CONFIG_DIR/mopidy') # Initialize whatever the last config file is with defaults config_file = args.config_files[-1] if os.path.exists(path.expand_path(config_file)): return try: default = config_lib.format_initial(extensions_data) path.get_or_create_file(config_file, mkdir=False, content=default) logger.info('Initialized %s with default config', config_file) except IOError as error: logger.warning( 'Unable to initialize %s with default config: %s', config_file, encoding.locale_decode(error))
def create_initial_config_file(args, extensions_data): """Initialize whatever the last config file is with defaults""" config_file = path.expand_path(args.config_files[-1]) if config_file.exists(): return try: default = config_lib.format_initial(extensions_data) path.get_or_create_file( config_file, mkdir=False, content=default.encode(errors="surrogateescape"), ) logger.info(f"Initialized {config_file.as_uri()} with default config") except OSError as exc: logger.warning( f"Unable to initialize {config_file.as_uri()} with default config: {exc}" )
def test_home_dir_expansion(self): self.assertEqual( os.path.expanduser(b'~/foo'), path.expand_path(b'~/foo'))
def test_xdg_subsititution(self): self.assertEqual( GLib.get_user_data_dir() + b'/foo', path.expand_path(b'$XDG_DATA_DIR/foo'))
def test_abspath(self): self.assertEqual(os.path.abspath(b'./foo'), path.expand_path(b'./foo'))
def test_empty_path(self): result = path.expand_path("") assert result == pathlib.Path(".").resolve()
def test_xdg_subsititution_unknown(self): self.assertIsNone( path.expand_path(b'/tmp/$XDG_INVALID_DIR/foo'))
def test_empty_path(self): self.assertEqual(os.path.abspath(b'.'), path.expand_path(b''))
def test_xdg_subsititution(self): expected = GLib.get_user_data_dir() + "/foo" result = path.expand_path("$XDG_DATA_DIR/foo") assert str(result) == expected
def test_home_dir_expansion(self): self.assertEqual(os.path.expanduser(b'~/foo'), path.expand_path(b'~/foo'))
def test_absolute_path(self): self.assertEqual(b'/tmp/foo', path.expand_path(b'/tmp/foo'))
def test_absolute_path(self): result = path.expand_path("/tmp/foo") assert result == pathlib.Path("/tmp/foo")
def test_invalid_utf8_bytes(self): result = path.expand_path(b"ab\xc3\x12") assert result == pathlib.Path("ab\udcc3\x12").resolve()
def test_home_dir_expansion(self): result = path.expand_path("~/foo") assert result == pathlib.Path("~/foo").expanduser()
def _get_data_dir(self): # get or create data director for core data_dir_path = (path.expand_path(self._config["core"]["data_dir"]) / "core") path.get_or_create_dir(data_dir_path) return data_dir_path
def test_xdg_subsititution(self): self.assertEqual(glib.get_user_data_dir() + b'/foo', path.expand_path(b'$XDG_DATA_DIR/foo'))
def test_xdg_subsititution_unknown(self): self.assertIsNone(path.expand_path(b'/tmp/$XDG_INVALID_DIR/foo'))
def test_xdg_subsititution_unknown(self): result = path.expand_path("/tmp/$XDG_INVALID_DIR/foo") assert result is None
def test_abspath(self): result = path.expand_path("./foo") assert result == pathlib.Path("./foo").resolve()