7
7
8
8
import termtosvg .asciicast as asciicast
9
9
10
+
10
11
logger = logging .getLogger (__name__ )
11
12
logger .addHandler (logging .NullHandler ())
12
13
13
- _PKG_CONFIGURATION_PATH = os .path .join ('data' , 'termtosvg.ini' )
14
- DEFAULT_CONFIG = pkg_resources .resource_string (__name__ , _PKG_CONFIGURATION_PATH ).decode ('utf-8' )
14
+ PKG_CONF_PATH = os .path .join ('data' , 'termtosvg.ini' )
15
+ DEFAULT_CONFIG = pkg_resources .resource_string (__name__ , PKG_CONF_PATH ).decode ('utf-8' )
16
+
17
+
18
+ class CaseInsensitiveDict (dict ):
19
+ @classmethod
20
+ def _lower_key (cls , key ):
21
+ return key .lower () if isinstance (key , str ) else key
22
+
23
+ def __init__ (self , * args , ** kwargs ):
24
+ super (CaseInsensitiveDict , self ).__init__ (* args , ** kwargs )
25
+ for key in list (self .keys ()):
26
+ value = super (CaseInsensitiveDict , self ).pop (key )
27
+ self .__setitem__ (key , value )
28
+
29
+ def __getitem__ (self , key ):
30
+ lower_case_key = self .__class__ ._lower_key (key )
31
+ return super (CaseInsensitiveDict , self ).__getitem__ (lower_case_key )
32
+
33
+ def __setitem__ (self , key , value ):
34
+ lower_case_key = self .__class__ ._lower_key (key )
35
+ super (CaseInsensitiveDict , self ).__setitem__ (lower_case_key , value )
36
+
37
+ def __delitem__ (self , key ):
38
+ lower_case_key = self .__class__ ._lower_key (key )
39
+ return super (CaseInsensitiveDict , self ).__delitem__ (lower_case_key )
15
40
41
+ def __contains__ (self , key ):
42
+ lower_case_key = self .__class__ ._lower_key (key )
43
+ return super (CaseInsensitiveDict , self ).__contains__ (lower_case_key )
16
44
17
- if 'XDG_CONFIG_HOME' in os .environ :
18
- USER_CONFIG_DIR = os .environ ['XDG_CONFIG_HOME' ]
19
- elif 'HOME' in os .environ :
20
- USER_CONFIG_DIR = os .path .join (os .environ ['HOME' ], '.config' )
21
- else :
22
- logger .info ('Environment variable XDG_CONFIG_HOME and HOME are not set: user configuration '
23
- 'cannot be used' )
24
- USER_CONFIG_DIR = None
45
+ def pop (self , key , * args , ** kwargs ):
46
+ lower_case_key = self .__class__ ._lower_key (key )
47
+ return super (CaseInsensitiveDict , self ).pop (lower_case_key , * args , ** kwargs )
48
+
49
+ def get (self , key , * args , ** kwargs ):
50
+ lower_case_key = self .__class__ ._lower_key (key )
51
+ return super (CaseInsensitiveDict , self ).get (lower_case_key , * args , ** kwargs )
52
+
53
+ def setdefault (self , key , * args , ** kwargs ):
54
+ lower_case_key = self .__class__ ._lower_key (key )
55
+ return super (CaseInsensitiveDict , self ).setdefault (lower_case_key , * args , ** kwargs )
56
+
57
+ def update (self , E = None , ** F ):
58
+ super (CaseInsensitiveDict , self ).update (self .__class__ (E ))
59
+ super (CaseInsensitiveDict , self ).update (self .__class__ (** F ))
25
60
26
61
27
62
def conf_to_dict (configuration ):
28
- # type: (str) -> Dict [str, Union[Dict[str, str], asciicast.AsciiCastTheme]]
63
+ # type: (str) -> CaseInsensitiveDict [str, Union[Dict[str, str], asciicast.AsciiCastTheme]]
29
64
"""Read a configuration string in INI format and return a dictionary
30
65
31
66
Raise a subclass of configparser.Error if parsing the configuration string fails
32
67
Raise ValueError if the color theme is invalid
33
68
"""
34
- user_config = configparser .ConfigParser (comment_prefixes = (';' ,))
35
- user_config .read_string (configuration )
36
- config_dict = {
37
- 'GLOBAL' : {
38
- 'font' : user_config .get ('GLOBAL' , 'font' ),
39
- 'theme' : user_config .get ('GLOBAL' , 'theme' ),
69
+ parser = configparser .ConfigParser (dict_type = CaseInsensitiveDict ,
70
+ comment_prefixes = (';' ,))
71
+ parser .read_string (configuration )
72
+ config_dict = CaseInsensitiveDict ({
73
+ 'global' : {
74
+ 'font' : parser .get ('global' , 'font' ),
75
+ 'theme' : parser .get ('global' , 'theme' ),
40
76
}
41
- }
77
+ })
42
78
43
- themes = user_config .sections ()
44
- themes .remove ('GLOBAL' )
79
+ themes = [theme .lower () for theme in parser .sections () if theme .lower () != 'global' ]
45
80
for theme_name in themes :
46
- fg = user_config .get (theme_name , 'foreground' , fallback = '' )
47
- bg = user_config .get (theme_name , 'background' , fallback = '' )
48
- palette = ':' .join (user_config .get (theme_name , 'color{}' .format (i ), fallback = '' )
81
+ fg = parser .get (theme_name , 'foreground' , fallback = '' )
82
+ bg = parser .get (theme_name , 'background' , fallback = '' )
83
+ palette = ':' .join (parser .get (theme_name , 'color{}' .format (i ), fallback = '' )
49
84
for i in range (16 ))
50
85
51
86
# This line raises ValueError if the color theme is invalid
@@ -69,32 +104,38 @@ def get_configuration(user_config, default_config):
69
104
70
105
# Override default values with user configuration
71
106
for section in user_config_dict :
72
- if section == 'GLOBAL ' :
107
+ if section . lower () == 'global ' :
73
108
for _property in 'theme' , 'font' :
74
- if _property in user_config_dict ['GLOBAL' ]:
75
- config_dict ['GLOBAL' ][_property ] = user_config_dict ['GLOBAL' ][_property ]
109
+ config_dict ['GLOBAL' ][_property ] = user_config_dict ['global' ][_property ]
76
110
else :
77
111
config_dict [section ] = user_config_dict [section ]
78
112
79
113
return config_dict
80
114
81
115
82
- def init_read_conf (user_config_dir = USER_CONFIG_DIR , default_config = DEFAULT_CONFIG ):
116
+ def init_read_conf ():
117
+ if 'XDG_CONFIG_HOME' in os .environ :
118
+ user_config_dir = os .environ ['XDG_CONFIG_HOME' ]
119
+ elif 'HOME' in os .environ :
120
+ user_config_dir = os .path .join (os .environ ['HOME' ], '.config' )
121
+ else :
122
+ logger .info ('Environment variable XDG_CONFIG_HOME and HOME are not set: user '
123
+ 'configuration cannot be used' )
124
+ user_config_dir = None
125
+
126
+ if user_config_dir is None :
127
+ return get_configuration (DEFAULT_CONFIG , DEFAULT_CONFIG )
128
+
83
129
config_dir = os .path .join (user_config_dir , 'termtosvg' )
84
130
config_path = os .path .join (config_dir , 'termtosvg.ini' )
85
131
try :
86
132
with open (config_path , 'r' ) as config_file :
87
133
user_config = config_file .read ()
88
134
except FileNotFoundError :
89
135
user_config = DEFAULT_CONFIG
90
- try :
91
- with open (config_path , 'w' ) as config_file :
92
- config_file .write (DEFAULT_CONFIG )
93
- logger .info ('Created default configuration file: {}' .format (config_path ))
94
- except FileNotFoundError :
95
- os .makedirs (config_dir )
96
- with open (config_path , 'w' ) as config_file :
97
- config_file .write (DEFAULT_CONFIG )
98
- logger .info ('Created default configuration file: {}' .format (config_path ))
99
-
100
- return get_configuration (user_config , default_config )
136
+ os .makedirs (config_dir , exist_ok = True )
137
+ with open (config_path , 'w' ) as config_file :
138
+ config_file .write (DEFAULT_CONFIG )
139
+ logger .info ('Created default configuration file: {}' .format (config_path ))
140
+
141
+ return get_configuration (user_config , DEFAULT_CONFIG )
0 commit comments