11
11
12
12
13
13
class Config (object ):
14
- def __init__ (self , app , name , base_dir = '~/.config/' ,
14
+ def __init__ (self , app = None , name = None , base_dir = '~/.config/' ,
15
15
template_file = None , template = None , envvars = {},
16
- defaults = {}, cli_args = {}, path_override = None ):
16
+ defaults = {}, cli_args = {}, path_override = None ,
17
+ file_override = None ):
17
18
"""
18
19
Automatically load an app config with environment variable and
19
20
default overrides.
@@ -30,46 +31,54 @@ def __init__(self, app, name, base_dir='~/.config/',
30
31
:param dict defaults: Default values for config keys
31
32
:param dict cli_args: Config key overrides likely from the app CLI.
32
33
These override any config file or env var values.
34
+ :param str path_override: Specify direct path instead of default base dir
35
+ :param str file_override: Direct path to config file to read
33
36
34
37
:raises exceptions.IOError: when permissions denied
35
38
"""
36
39
37
- if path_override :
38
- self .base_dir = os .path .expanduser (path_override )
39
- else :
40
- self .base_dir = os .path .expanduser (os .path .join (base_dir , app ))
41
-
42
- if not os .path .isdir (self .base_dir ):
43
- os .makedirs (self .base_dir )
44
-
45
40
self .filename = None
46
41
self .full_path = None
47
42
48
- for ext in VALID_EXT :
49
- for _ext in [ext , ext .upper ()]:
50
- filename = '{}.{}' .format (name , _ext )
51
- cfg = os .path .join (self .base_dir , filename )
52
- if os .path .isfile (cfg ):
53
- self .full_path = cfg
54
- self .filename = filename
55
- break
56
-
57
- if self .filename is None :
58
- self .filename = name + '.' + VALID_EXT [0 ]
59
- self .full_path = os .path .join (self .base_dir , self .filename )
60
- if template_file :
61
- shutil .copyfile (template_file , self .full_path )
43
+ if file_override is not None :
44
+ self .full_path = file_override
45
+ if not os .path .isfile (self .full_path ):
46
+ raise Exception ('Unable to find specified file {}' .format (self .full_path ))
47
+ else :
48
+ if path_override :
49
+ self .base_dir = os .path .expanduser (path_override )
62
50
else :
63
- with open (self .full_path , 'w' ) as f :
64
- if template :
65
- if isinstance (template , str ):
66
- f .write (template )
67
- elif isinstance (template , dict ):
68
- yaml .dump (template , f , default_flow_style = False )
69
- else :
70
- yaml .dump ({}, f , default_flow_style = False )
51
+ self .base_dir = os .path .expanduser (os .path .join (base_dir , app ))
52
+
53
+ if not os .path .isdir (self .base_dir ):
54
+ os .makedirs (self .base_dir )
55
+
56
+ for ext in VALID_EXT :
57
+ for _ext in [ext , ext .upper ()]:
58
+ filename = '{}.{}' .format (name , _ext )
59
+ cfg = os .path .join (self .base_dir , filename )
60
+ if os .path .isfile (cfg ):
61
+ self .full_path = cfg
62
+ self .filename = filename
63
+ break
64
+
65
+ if self .filename is None :
66
+ self .filename = name + '.' + VALID_EXT [0 ]
67
+ self .full_path = os .path .join (self .base_dir , self .filename )
68
+ if template_file :
69
+ shutil .copyfile (template_file , self .full_path )
70
+ else :
71
+ with open (self .full_path , 'w' ) as f :
72
+ if template :
73
+ if isinstance (template , str ):
74
+ f .write (template )
75
+ elif isinstance (template , dict ):
76
+ yaml .dump (template , f , default_flow_style = False )
77
+ else :
78
+ yaml .dump ({}, f , default_flow_style = False )
71
79
72
80
self ._data = defaults
81
+ print (self .full_path )
73
82
with open (self .full_path , 'r' ) as f :
74
83
data = yaml .full_load (f )
75
84
self ._data .update (data )
0 commit comments