Cleanup settings magic

This commit is contained in:
Stein Magnus Jodal 2010-08-17 01:23:37 +02:00
parent e021863fd8
commit 7afc74d80b
3 changed files with 45 additions and 24 deletions

View File

@ -2,8 +2,6 @@ import sys
if not (2, 6) <= sys.version_info < (3,):
sys.exit(u'Mopidy requires Python >= 2.6, < 3')
from mopidy import settings as raw_settings
def get_version():
return u'0.1.0a4'
@ -27,13 +25,6 @@ class MopidyException(Exception):
class SettingsError(MopidyException):
pass
class Settings(object):
def __getattr__(self, attr):
if attr.isupper() and not hasattr(raw_settings, attr):
raise SettingsError(u'Setting "%s" is not set.' % attr)
value = getattr(raw_settings, attr)
if type(value) != bool and not value:
raise SettingsError(u'Setting "%s" is empty.' % attr)
return value
settings = Settings()
from mopidy import settings as default_settings_module
from mopidy.utils.settings import SettingsProxy
settings = SettingsProxy(default_settings_module)

View File

@ -7,11 +7,6 @@ Available settings and their default values.
file called ``~/.mopidy/settings.py`` and redefine settings there.
"""
# Absolute import needed to import ~/.mopidy/settings.py and not ourselves
from __future__ import absolute_import
import os
import sys
#: List of playback backends to use. See :mod:`mopidy.backends` for all
#: available backends.
#:
@ -172,10 +167,3 @@ SPOTIFY_USERNAME = u''
#:
#: Used by :mod:`mopidy.backends.libspotify`.
SPOTIFY_PASSWORD = u''
# Import user specific settings
dotdir = os.path.expanduser(u'~/.mopidy/')
settings_file = os.path.join(dotdir, u'settings.py')
if os.path.isfile(settings_file):
sys.path.insert(0, dotdir)
from settings import *

42
mopidy/utils/settings.py Normal file
View File

@ -0,0 +1,42 @@
# Absolute import needed to import ~/.mopidy/settings.py and not ourselves
from __future__ import absolute_import
from copy import copy
import os
import sys
from mopidy import SettingsError
class SettingsProxy(object):
def __init__(self, default_settings_module):
self.default_settings = self._get_settings_dict_from_module(
default_settings_module)
self.local_settings = self._get_local_settings()
self.raw_settings = copy(self.default_settings)
self.raw_settings.update(self.local_settings)
def _get_local_settings(self):
dotdir = os.path.expanduser(u'~/.mopidy/')
settings_file = os.path.join(dotdir, u'settings.py')
if os.path.isfile(settings_file):
sys.path.insert(0, dotdir)
import settings as local_settings_module
return self._get_settings_dict_from_module(local_settings_module)
def _get_settings_dict_from_module(self, module):
settings = filter(lambda (key, value): self._is_setting(key),
module.__dict__.iteritems())
return dict(settings)
def _is_setting(self, name):
return name.isupper()
def __getattr__(self, attr):
if not self._is_setting(attr):
return
if attr not in self.raw_settings:
raise SettingsError(u'Setting "%s" is not set.' % attr)
value = self.raw_settings[attr]
if type(value) != bool and not value:
raise SettingsError(u'Setting "%s" is empty.' % attr)
return value