Move and rename expand_path to mopidy.utils.path
Also switches a bit move of mopidy.utils.settings over to module imports and double spaces between functions.
This commit is contained in:
parent
6cc57701f9
commit
dda5e5261a
@ -1,11 +1,20 @@
|
||||
import glib
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
import string
|
||||
import sys
|
||||
import urllib
|
||||
|
||||
logger = logging.getLogger('mopidy.utils.path')
|
||||
|
||||
XDG_DIRS = {
|
||||
'XDG_CACHE_DIR': glib.get_user_cache_dir(),
|
||||
'XDG_DATA_DIR': glib.get_user_data_dir(),
|
||||
'XDG_MUSIC_DIR': glib.get_user_special_dir(glib.USER_DIRECTORY_MUSIC),
|
||||
}
|
||||
|
||||
|
||||
def get_or_create_folder(folder):
|
||||
folder = os.path.expanduser(folder)
|
||||
if os.path.isfile(folder):
|
||||
@ -16,6 +25,7 @@ def get_or_create_folder(folder):
|
||||
os.makedirs(folder, 0755)
|
||||
return folder
|
||||
|
||||
|
||||
def get_or_create_file(filename):
|
||||
filename = os.path.expanduser(filename)
|
||||
if not os.path.isfile(filename):
|
||||
@ -23,6 +33,7 @@ def get_or_create_file(filename):
|
||||
open(filename, 'w')
|
||||
return filename
|
||||
|
||||
|
||||
def path_to_uri(*paths):
|
||||
path = os.path.join(*paths)
|
||||
path = path.encode('utf-8')
|
||||
@ -30,6 +41,7 @@ def path_to_uri(*paths):
|
||||
return 'file:' + urllib.pathname2url(path)
|
||||
return 'file://' + urllib.pathname2url(path)
|
||||
|
||||
|
||||
def uri_to_path(uri):
|
||||
if sys.platform == 'win32':
|
||||
path = urllib.url2pathname(re.sub('^file:', '', uri))
|
||||
@ -37,6 +49,7 @@ def uri_to_path(uri):
|
||||
path = urllib.url2pathname(re.sub('^file://', '', uri))
|
||||
return path.encode('latin1').decode('utf-8') # Undo double encoding
|
||||
|
||||
|
||||
def split_path(path):
|
||||
parts = []
|
||||
while True:
|
||||
@ -47,6 +60,13 @@ def split_path(path):
|
||||
break
|
||||
return parts
|
||||
|
||||
|
||||
def expand_path(path):
|
||||
path = os.path.expanduser(path)
|
||||
path = os.path.abspath(path)
|
||||
return string.Template(path).safe_substitute(XDG_DIRS)
|
||||
|
||||
|
||||
def find_files(path):
|
||||
if os.path.isfile(path):
|
||||
if not isinstance(path, unicode):
|
||||
@ -73,6 +93,7 @@ def find_files(path):
|
||||
filename = filename.decode('latin1')
|
||||
yield filename
|
||||
|
||||
|
||||
# FIXME replace with mock usage in tests.
|
||||
class Mtime(object):
|
||||
def __init__(self):
|
||||
|
||||
@ -3,24 +3,17 @@ from __future__ import absolute_import
|
||||
|
||||
import copy
|
||||
import getpass
|
||||
import glib
|
||||
import logging
|
||||
import os
|
||||
import pprint
|
||||
import string
|
||||
import sys
|
||||
|
||||
from mopidy import SettingsError, SETTINGS_PATH, SETTINGS_FILE
|
||||
from mopidy.utils.log import indent
|
||||
from mopidy.utils import log
|
||||
from mopidy.utils import path
|
||||
|
||||
logger = logging.getLogger('mopidy.utils.settings')
|
||||
|
||||
XDG_DIRS = {
|
||||
'XDG_CACHE_DIR': glib.get_user_cache_dir(),
|
||||
'XDG_DATA_DIR': glib.get_user_data_dir(),
|
||||
'XDG_MUSIC_DIR': glib.get_user_special_dir(glib.USER_DIRECTORY_MUSIC),
|
||||
}
|
||||
|
||||
|
||||
class SettingsProxy(object):
|
||||
def __init__(self, default_settings_module):
|
||||
@ -67,7 +60,7 @@ class SettingsProxy(object):
|
||||
if not value:
|
||||
return value
|
||||
if attr.endswith('_PATH') or attr.endswith('_FILE'):
|
||||
value = self.expandpath(value)
|
||||
value = path.expand_path(value)
|
||||
return value
|
||||
|
||||
def __setattr__(self, attr, value):
|
||||
@ -76,17 +69,12 @@ class SettingsProxy(object):
|
||||
else:
|
||||
super(SettingsProxy, self).__setattr__(attr, value)
|
||||
|
||||
def expandpath(self, value):
|
||||
value = os.path.expanduser(value)
|
||||
value = os.path.abspath(value)
|
||||
return string.Template(value).safe_substitute(XDG_DIRS)
|
||||
|
||||
def validate(self, interactive):
|
||||
if interactive:
|
||||
self._read_missing_settings_from_stdin(self.current, self.runtime)
|
||||
if self.get_errors():
|
||||
logger.error(u'Settings validation errors: %s',
|
||||
indent(self.get_errors_as_string()))
|
||||
log.indent(self.get_errors_as_string()))
|
||||
raise SettingsError(u'Settings validation failed.')
|
||||
|
||||
def _read_missing_settings_from_stdin(self, current, runtime):
|
||||
@ -210,11 +198,11 @@ def format_settings_list(settings):
|
||||
for (key, value) in sorted(settings.current.iteritems()):
|
||||
default_value = settings.default.get(key)
|
||||
masked_value = mask_value_if_secret(key, value)
|
||||
lines.append(u'%s: %s' % (key, indent(
|
||||
lines.append(u'%s: %s' % (key, log.indent(
|
||||
pprint.pformat(masked_value), places=2)))
|
||||
if value != default_value and default_value is not None:
|
||||
lines.append(u' Default: %s' %
|
||||
indent(pformat(default_value), places=4))
|
||||
log.indent(pformat(default_value), places=4))
|
||||
if errors.get(key) is not None:
|
||||
lines.append(u' Error: %s' % errors[key])
|
||||
return '\n'.join(lines)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user