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 logging
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
import re
|
import re
|
||||||
|
import string
|
||||||
|
import sys
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
logger = logging.getLogger('mopidy.utils.path')
|
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):
|
def get_or_create_folder(folder):
|
||||||
folder = os.path.expanduser(folder)
|
folder = os.path.expanduser(folder)
|
||||||
if os.path.isfile(folder):
|
if os.path.isfile(folder):
|
||||||
@ -16,6 +25,7 @@ def get_or_create_folder(folder):
|
|||||||
os.makedirs(folder, 0755)
|
os.makedirs(folder, 0755)
|
||||||
return folder
|
return folder
|
||||||
|
|
||||||
|
|
||||||
def get_or_create_file(filename):
|
def get_or_create_file(filename):
|
||||||
filename = os.path.expanduser(filename)
|
filename = os.path.expanduser(filename)
|
||||||
if not os.path.isfile(filename):
|
if not os.path.isfile(filename):
|
||||||
@ -23,6 +33,7 @@ def get_or_create_file(filename):
|
|||||||
open(filename, 'w')
|
open(filename, 'w')
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
|
|
||||||
def path_to_uri(*paths):
|
def path_to_uri(*paths):
|
||||||
path = os.path.join(*paths)
|
path = os.path.join(*paths)
|
||||||
path = path.encode('utf-8')
|
path = path.encode('utf-8')
|
||||||
@ -30,6 +41,7 @@ def path_to_uri(*paths):
|
|||||||
return 'file:' + urllib.pathname2url(path)
|
return 'file:' + urllib.pathname2url(path)
|
||||||
return 'file://' + urllib.pathname2url(path)
|
return 'file://' + urllib.pathname2url(path)
|
||||||
|
|
||||||
|
|
||||||
def uri_to_path(uri):
|
def uri_to_path(uri):
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
path = urllib.url2pathname(re.sub('^file:', '', uri))
|
path = urllib.url2pathname(re.sub('^file:', '', uri))
|
||||||
@ -37,6 +49,7 @@ def uri_to_path(uri):
|
|||||||
path = urllib.url2pathname(re.sub('^file://', '', uri))
|
path = urllib.url2pathname(re.sub('^file://', '', uri))
|
||||||
return path.encode('latin1').decode('utf-8') # Undo double encoding
|
return path.encode('latin1').decode('utf-8') # Undo double encoding
|
||||||
|
|
||||||
|
|
||||||
def split_path(path):
|
def split_path(path):
|
||||||
parts = []
|
parts = []
|
||||||
while True:
|
while True:
|
||||||
@ -47,6 +60,13 @@ def split_path(path):
|
|||||||
break
|
break
|
||||||
return parts
|
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):
|
def find_files(path):
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
if not isinstance(path, unicode):
|
if not isinstance(path, unicode):
|
||||||
@ -73,6 +93,7 @@ def find_files(path):
|
|||||||
filename = filename.decode('latin1')
|
filename = filename.decode('latin1')
|
||||||
yield filename
|
yield filename
|
||||||
|
|
||||||
|
|
||||||
# FIXME replace with mock usage in tests.
|
# FIXME replace with mock usage in tests.
|
||||||
class Mtime(object):
|
class Mtime(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|||||||
@ -3,24 +3,17 @@ from __future__ import absolute_import
|
|||||||
|
|
||||||
import copy
|
import copy
|
||||||
import getpass
|
import getpass
|
||||||
import glib
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import pprint
|
import pprint
|
||||||
import string
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from mopidy import SettingsError, SETTINGS_PATH, SETTINGS_FILE
|
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')
|
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):
|
class SettingsProxy(object):
|
||||||
def __init__(self, default_settings_module):
|
def __init__(self, default_settings_module):
|
||||||
@ -67,7 +60,7 @@ class SettingsProxy(object):
|
|||||||
if not value:
|
if not value:
|
||||||
return value
|
return value
|
||||||
if attr.endswith('_PATH') or attr.endswith('_FILE'):
|
if attr.endswith('_PATH') or attr.endswith('_FILE'):
|
||||||
value = self.expandpath(value)
|
value = path.expand_path(value)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def __setattr__(self, attr, value):
|
def __setattr__(self, attr, value):
|
||||||
@ -76,17 +69,12 @@ class SettingsProxy(object):
|
|||||||
else:
|
else:
|
||||||
super(SettingsProxy, self).__setattr__(attr, value)
|
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):
|
def validate(self, interactive):
|
||||||
if interactive:
|
if interactive:
|
||||||
self._read_missing_settings_from_stdin(self.current, self.runtime)
|
self._read_missing_settings_from_stdin(self.current, self.runtime)
|
||||||
if self.get_errors():
|
if self.get_errors():
|
||||||
logger.error(u'Settings validation errors: %s',
|
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.')
|
raise SettingsError(u'Settings validation failed.')
|
||||||
|
|
||||||
def _read_missing_settings_from_stdin(self, current, runtime):
|
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()):
|
for (key, value) in sorted(settings.current.iteritems()):
|
||||||
default_value = settings.default.get(key)
|
default_value = settings.default.get(key)
|
||||||
masked_value = mask_value_if_secret(key, value)
|
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)))
|
pprint.pformat(masked_value), places=2)))
|
||||||
if value != default_value and default_value is not None:
|
if value != default_value and default_value is not None:
|
||||||
lines.append(u' Default: %s' %
|
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:
|
if errors.get(key) is not None:
|
||||||
lines.append(u' Error: %s' % errors[key])
|
lines.append(u' Error: %s' % errors[key])
|
||||||
return '\n'.join(lines)
|
return '\n'.join(lines)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user