33import logging
44import inspect
55import sys
6- import os
7-
86from utility_functions import recursive_dictionary_update , path
97
108
@@ -13,47 +11,33 @@ class Singleton(type):
1311
1412 def __call__ (cls , * args , ** kwargs ):
1513 if cls not in cls ._instances :
16- cls ._instances [cls ] = super (Singleton , cls ).__call__ (
17- * args , ** kwargs
18- )
14+ cls ._instances [cls ] = super (Singleton , cls ).__call__ (* args ,
15+ ** kwargs )
1916 return cls ._instances [cls ]
2017
2118
2219class ConfigurationManager (object ):
2320 __metaclass__ = Singleton
24- logger = logging .getLogger ('starrypy.config.ConfigurationManager' )
25- log_format = logging .Formatter (
26- '%(asctime)s - %(levelname)s - %(name)s # %(message)s'
27- )
28- logfile_handle = logging .FileHandler ('config.log' )
21+ logger = logging .getLogger ("starrypy.config.ConfigurationManager" )
22+ log_format = logging .Formatter ('%(asctime)s - %(levelname)s - %(name)s # %(message)s' )
23+ logfile_handle = logging .FileHandler ("config.log" )
2924 logfile_handle .setLevel (9 )
3025 logger .addHandler (logfile_handle )
3126 logfile_handle .setFormatter (log_format )
3227
3328 def __init__ (self ):
34- default_config_path = path .preauthChild (
35- os .path .join ('config' , 'config.json.default' )
36- )
37- self .config_path = path .preauthChild (
38- os .path .join ('config' , 'config.json' )
39- )
29+ default_config_path = path .preauthChild ("config/config.json.default" )
30+ self .config_path = path .preauthChild ("config/config.json" )
4031 if default_config_path .exists ():
4132 try :
4233 with default_config_path .open () as default_config :
4334 default = json .load (default_config )
4435 except ValueError as e :
45- print 'Error: %s' % e
46- self .logger .critical (
47- 'The configuration defaults file (config.json.default) '
48- 'contains invalid JSON. Please run it against a JSON '
49- 'linter, such as http://jsonlint.com. Shutting down.'
50- )
36+ print "Error: %s" % e
37+ self .logger .critical ("The configuration defaults file (config.json.default) contains invalid JSON. Please run it against a JSON linter, such as http://jsonlint.com. Shutting down." )
5138 sys .exit ()
5239 else :
53- self .logger .critical (
54- 'The configuration defaults file (config.json.default)'
55- ' doesn\' t exist! Shutting down.'
56- )
40+ self .logger .critical ("The configuration defaults file (config.json.default) doesn't exist! Shutting down." )
5741 sys .exit ()
5842
5943 if self .config_path .exists ():
@@ -62,95 +46,63 @@ def __init__(self):
6246 config = json .load (c )
6347 self .config = recursive_dictionary_update (default , config )
6448 except ValueError as e :
65- print 'Error: %s' % e
66- self .logger .critical (
67- 'The configuration file (config.json) contains invalid '
68- 'JSON. Please run it against a JSON linter, such as '
69- 'http://jsonlint.com. Shutting down.'
70- )
49+ print "Error: %s" % e
50+ self .logger .critical ("The configuration file (config.json) contains invalid JSON. Please run it against a JSON linter, such as http://jsonlint.com. Shutting down." )
7151 sys .exit ()
7252 else :
73- self .logger .warning (
74- 'The configuration file (config.json)'
75- ' doesn\' t exist! Creating one from defaults.'
76- )
53+ self .logger .warning ("The configuration file (config.json) doesn't exist! Creating one from defaults." )
7754 try :
78- with self .config_path .open ('w' ) as f :
79- json .dump (
80- default ,
81- f ,
82- indent = 4 ,
83- separators = (',' , ': ' ),
84- sort_keys = True ,
85- ensure_ascii = False
86- )
55+ with self .config_path .open ("w" ) as f :
56+ json .dump (default , f , indent = 4 , separators = (',' , ': ' ), sort_keys = True , ensure_ascii = False )
8757 except IOError :
88- self .logger .critical (
89- 'Couldn\' t write a default configuration file. '
90- 'Please check that StarryPy has write access in the '
91- 'config/ directory.'
92- )
93- self .logger .critical ('Exiting...' )
58+ self .logger .critical ("Couldn't write a default configuration file. Please check that StarryPy has write access in the config/ directory." )
59+ self .logger .critical ("Exiting..." )
9460 sys .exit ()
95- self .logger .warning (
96- 'StarryPy will now exit. Please examine config.json '
97- 'and adjust the variables appropriately.'
98- )
61+ self .logger .warning ("StarryPy will now exit. Please examine config.json and adjust the variables appropriately." )
9962 sys .exit ()
10063
101- self .logger .debug (' Created configuration manager.' )
64+ self .logger .debug (" Created configuration manager." )
10265 self .save ()
10366
10467 def save (self ):
10568 try :
106- with io .open (self .config_path .path , 'w' ) as config :
107- self .logger .debug ('Writing configuration file.' )
108- config .write (
109- json .dumps (
110- self .config ,
111- indent = 4 ,
112- separators = (',' , ': ' ),
113- sort_keys = True ,
114- ensure_ascii = False
115- )
116- )
69+ with io .open (self .config_path .path , "w" , encoding = "utf-8" ) as config :
70+ self .logger .debug ("Writing configuration file." )
71+ config .write (json .dumps (self .config , indent = 4 , separators = (',' , ': ' ), sort_keys = True , ensure_ascii = False ))
11772 except Exception as e :
118- self .logger .critical (
119- 'Tried to save the configuration file, failed.\n %s' , str (e )
120- )
73+ self .logger .critical ("Tried to save the configuration file, failed.\n %s" , str (e ))
12174 raise
12275
12376 def __getattr__ (self , item ):
124- if item in [' config' , ' config_path' ]:
77+ if item in [" config" , " config_path" ]:
12578 return super (ConfigurationManager , self ).__getattribute__ (item )
12679
127- elif item == 'plugin_config' :
128- caller = inspect .stack ()[1 ][0 ].f_locals ['self' ].__class__ .name
129- if caller in self .config ['plugin_config' ]:
130- return self .config ['plugin_config' ][caller ]
80+
81+ elif item == "plugin_config" :
82+ caller = inspect .stack ()[1 ][0 ].f_locals ["self" ].__class__ .name
83+ if caller in self .config ["plugin_config" ]:
84+ return self .config ["plugin_config" ][caller ]
13185 else :
13286 return {}
13387
13488 else :
13589 if item in self .config :
13690 return self .config [item ]
13791 else :
138- self .logger .error (
139- 'Couldn\' t find configuration option %s in '
140- 'configuration file.' , item
141- )
92+ self .logger .error ("Couldn't find configuration option %s in configuration file." , item )
14293 raise AttributeError
14394
95+
14496 def __setattr__ (self , key , value ):
145- if key == ' config' :
97+ if key == " config" :
14698 super (ConfigurationManager , self ).__setattr__ (key , value )
14799 self .save ()
148- elif key == ' config_path' :
100+ elif key == " config_path" :
149101 super (ConfigurationManager , self ).__setattr__ (key , value )
150- elif key == ' plugin_config' :
151- caller = inspect .stack ()[1 ][0 ].f_locals [' self' ].__class__ .name
152- self .config [' plugin_config' ][caller ] = value
153- self .save ()
102+ elif key == " plugin_config" :
103+ caller = inspect .stack ()[1 ][0 ].f_locals [" self" ].__class__ .name
104+ self .config [" plugin_config" ][caller ] = value
105+ self .save
154106 else :
155107 self .config [key ] = value
156108 self .save ()
0 commit comments