1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00

Settings now don't fiddle with sys.path

This commit is contained in:
sametmax 2014-06-22 12:42:21 +07:00
parent 1a0426881a
commit 391df055f9

View File

@ -14,6 +14,15 @@ try:
except (AttributeError): except (AttributeError):
pass # privilege does't work on several plateform pass # privilege does't work on several plateform
try:
from runpy import run_path
except ImportError:
# python-2.6 or earlier - use simplier less-optimized execfile()
def run_path(file_path):
mod_globals = {'__file__': file_path}
execfile(file_path, mod_globals)
return mod_globals
def drop_privileges(user=None, group=None, wait=5): def drop_privileges(user=None, group=None, wait=5):
""" """
@ -72,17 +81,25 @@ class SettingsContainer(object):
return cls._instance return cls._instance
def update_with_module(self, module): def update_with_dict(self, dict):
""" """
Update settings with values from the given module. Update settings with values from the given mapping object.
(Taking only variable with uppercased name) (Taking only variable with uppercased name)
""" """
for name, value in module.__dict__.iteritems(): for name, value in dict.iteritems():
if name.isupper(): if name.isupper():
setattr(self, name, value) setattr(self, name, value)
return self return self
def update_with_module(self, module):
"""
Update settings with values from the given module.
Uses update_with_dict() behind the scenes.
"""
return self.update_with_dict(module.__dict__)
@classmethod @classmethod
def from_module(cls, module): def from_module(cls, module):
""" """
@ -97,11 +114,10 @@ class SettingsContainer(object):
def update_with_file(self, filepath): def update_with_file(self, filepath):
""" """
Update settings with values from the given module file. Update settings with values from the given module file.
User update_with_module() behind the scene Uses update_with_dict() behind the scenes.
""" """
sys.path.insert(0, os.path.dirname(filepath)) settings = run_path(filepath)
module_name = os.path.splitext(os.path.basename(filepath))[0] return self.update_with_dict(settings)
return self.update_with_module(__import__(module_name))
settings = SettingsContainer() settings = SettingsContainer()