33import logging
44import inspect
55import sys
6+ import os
7+
68from utility_functions import recursive_dictionary_update , path
79
810
@@ -11,33 +13,47 @@ class Singleton(type):
1113
1214 def __call__ (cls , * args , ** kwargs ):
1315 if cls not in cls ._instances :
14- cls ._instances [cls ] = super (Singleton , cls ).__call__ (* args ,
15- ** kwargs )
16+ cls ._instances [cls ] = super (Singleton , cls ).__call__ (
17+ * args , ** kwargs
18+ )
1619 return cls ._instances [cls ]
1720
1821
1922class ConfigurationManager (object ):
2023 __metaclass__ = Singleton
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" )
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' )
2429 logfile_handle .setLevel (9 )
2530 logger .addHandler (logfile_handle )
2631 logfile_handle .setFormatter (log_format )
2732
2833 def __init__ (self ):
29- default_config_path = path .preauthChild ("config/config.json.default" )
30- self .config_path = path .preauthChild ("config/config.json" )
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+ )
3140 if default_config_path .exists ():
3241 try :
3342 with default_config_path .open () as default_config :
3443 default = json .load (default_config )
3544 except ValueError as e :
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." )
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+ )
3851 sys .exit ()
3952 else :
40- self .logger .critical ("The configuration defaults file (config.json.default) doesn't exist! Shutting down." )
53+ self .logger .critical (
54+ 'The configuration defaults file (config.json.default)'
55+ ' doesn\' t exist! Shutting down.'
56+ )
4157 sys .exit ()
4258
4359 if self .config_path .exists ():
@@ -46,63 +62,95 @@ def __init__(self):
4662 config = json .load (c )
4763 self .config = recursive_dictionary_update (default , config )
4864 except ValueError as e :
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." )
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+ )
5171 sys .exit ()
5272 else :
53- self .logger .warning ("The configuration file (config.json) doesn't exist! Creating one from defaults." )
73+ self .logger .warning (
74+ 'The configuration file (config.json)'
75+ ' doesn\' t exist! Creating one from defaults.'
76+ )
5477 try :
55- with self .config_path .open ("w" ) as f :
56- json .dump (default , f , indent = 4 , separators = (',' , ': ' ), sort_keys = True , ensure_ascii = False )
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+ )
5787 except IOError :
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..." )
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...' )
6094 sys .exit ()
61- self .logger .warning ("StarryPy will now exit. Please examine config.json and adjust the variables appropriately." )
95+ self .logger .warning (
96+ 'StarryPy will now exit. Please examine config.json '
97+ 'and adjust the variables appropriately.'
98+ )
6299 sys .exit ()
63100
64- self .logger .debug (" Created configuration manager." )
101+ self .logger .debug (' Created configuration manager.' )
65102 self .save ()
66103
67104 def save (self ):
68105 try :
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 ))
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+ )
72117 except Exception as e :
73- self .logger .critical ("Tried to save the configuration file, failed.\n %s" , str (e ))
118+ self .logger .critical (
119+ 'Tried to save the configuration file, failed.\n %s' , str (e )
120+ )
74121 raise
75122
76123 def __getattr__ (self , item ):
77- if item in [" config" , " config_path" ]:
124+ if item in [' config' , ' config_path' ]:
78125 return super (ConfigurationManager , self ).__getattribute__ (item )
79126
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 ]
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 ]
85131 else :
86132 return {}
87133
88134 else :
89135 if item in self .config :
90136 return self .config [item ]
91137 else :
92- self .logger .error ("Couldn't find configuration option %s in configuration file." , item )
138+ self .logger .error (
139+ 'Couldn\' t find configuration option %s in '
140+ 'configuration file.' , item
141+ )
93142 raise AttributeError
94143
95-
96144 def __setattr__ (self , key , value ):
97- if key == " config" :
145+ if key == ' config' :
98146 super (ConfigurationManager , self ).__setattr__ (key , value )
99147 self .save ()
100- elif key == " config_path" :
148+ elif key == ' config_path' :
101149 super (ConfigurationManager , self ).__setattr__ (key , value )
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
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 ()
106154 else :
107155 self .config [key ] = value
108156 self .save ()
0 commit comments