14
14
except (AttributeError ):
15
15
pass # privilege does't work on several plateform
16
16
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
+
17
26
18
27
def drop_privileges (user = None , group = None , wait = 5 ):
19
28
"""
@@ -72,17 +81,25 @@ def __new__(cls, *args, **kwargs):
72
81
return cls ._instance
73
82
74
83
75
- def update_with_module (self , module ):
84
+ def update_with_dict (self , dict ):
76
85
"""
77
- Update settings with values from the given module .
86
+ Update settings with values from the given mapping object .
78
87
(Taking only variable with uppercased name)
79
88
"""
80
- for name , value in module . __dict__ .iteritems ():
89
+ for name , value in dict .iteritems ():
81
90
if name .isupper ():
82
91
setattr (self , name , value )
83
92
return self
84
93
85
94
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
+
86
103
@classmethod
87
104
def from_module (cls , module ):
88
105
"""
@@ -97,11 +114,10 @@ def from_module(cls, module):
97
114
def update_with_file (self , filepath ):
98
115
"""
99
116
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.
101
118
"""
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 )
105
121
106
122
107
123
settings = SettingsContainer ()
0 commit comments