Skip to content

Commit 391df05

Browse files
committed
Settings now don't fiddle with sys.path
1 parent 1a04268 commit 391df05

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

zerobin/utils.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
except (AttributeError):
1515
pass # privilege does't work on several plateform
1616

17+
try:
18+
from runpy import run_path
19+
except ImportError:
20+
# python-2.6 or earlier - use simplier less-optimized execfile()
21+
def run_path(file_path):
22+
mod_globals = {'__file__': file_path}
23+
execfile(file_path, mod_globals)
24+
return mod_globals
25+
1726

1827
def drop_privileges(user=None, group=None, wait=5):
1928
"""
@@ -72,17 +81,25 @@ def __new__(cls, *args, **kwargs):
7281
return cls._instance
7382

7483

75-
def update_with_module(self, module):
84+
def update_with_dict(self, dict):
7685
"""
77-
Update settings with values from the given module.
86+
Update settings with values from the given mapping object.
7887
(Taking only variable with uppercased name)
7988
"""
80-
for name, value in module.__dict__.iteritems():
89+
for name, value in dict.iteritems():
8190
if name.isupper():
8291
setattr(self, name, value)
8392
return self
8493

8594

95+
def update_with_module(self, module):
96+
"""
97+
Update settings with values from the given module.
98+
Uses update_with_dict() behind the scenes.
99+
"""
100+
return self.update_with_dict(module.__dict__)
101+
102+
86103
@classmethod
87104
def from_module(cls, module):
88105
"""
@@ -97,11 +114,10 @@ def from_module(cls, module):
97114
def update_with_file(self, filepath):
98115
"""
99116
Update settings with values from the given module file.
100-
User update_with_module() behind the scene
117+
Uses update_with_dict() behind the scenes.
101118
"""
102-
sys.path.insert(0, os.path.dirname(filepath))
103-
module_name = os.path.splitext(os.path.basename(filepath))[0]
104-
return self.update_with_module(__import__(module_name))
119+
settings = run_path(filepath)
120+
return self.update_with_dict(settings)
105121

106122

107123
settings = SettingsContainer()

0 commit comments

Comments
 (0)