@@ -27,64 +27,62 @@ def project_config(self, document_path):
27
27
"""Return project-level (i.e. workspace directory) configuration."""
28
28
raise NotImplementedError ()
29
29
30
- @staticmethod
31
- def read_config_from_files (files ):
30
+ @classmethod
31
+ def read_config_from_files (cls , files ):
32
32
config = configparser .RawConfigParser ()
33
33
for filename in files :
34
34
if os .path .exists (filename ) and not os .path .isdir (filename ):
35
35
config .read (filename )
36
36
37
37
return config
38
38
39
- @staticmethod
40
- def parse_config (config , key , options ):
39
+ @classmethod
40
+ def parse_config (cls , config , key , options ):
41
41
"""Parse the config with the given options."""
42
42
conf = {}
43
43
for source , destination , opt_type in options :
44
- opt_value = _get_opt (config , key , source , opt_type )
44
+ opt_value = cls . _get_opt (config , key , source , opt_type )
45
45
if opt_value is not None :
46
- _set_opt (conf , destination , opt_value )
46
+ cls . _set_opt (conf , destination , opt_value )
47
47
return conf
48
48
49
+ @classmethod
50
+ def _get_opt (cls , config , key , option , opt_type ):
51
+ """Get an option from a configparser with the given type."""
52
+ for opt_key in [option , option .replace ('-' , '_' )]:
53
+ if not config .has_option (key , opt_key ):
54
+ continue
49
55
50
- def _get_opt (config , key , option , opt_type ):
51
- """Get an option from a configparser with the given type."""
52
- for opt_key in [option , option .replace ('-' , '_' )]:
53
- if not config .has_option (key , opt_key ):
54
- continue
56
+ if opt_type == bool :
57
+ return config .getboolean (key , opt_key )
55
58
56
- if opt_type == bool :
57
- return config .getboolean (key , opt_key )
59
+ if opt_type == int :
60
+ return config .getint (key , opt_key )
58
61
59
- if opt_type == int :
60
- return config .getint (key , opt_key )
62
+ if opt_type == str :
63
+ return config .get (key , opt_key )
61
64
62
- if opt_type == str :
63
- return config .get (key , opt_key )
65
+ if opt_type == list :
66
+ return cls . _parse_list_opt ( config .get (key , opt_key ) )
64
67
65
- if opt_type == list :
66
- return _parse_list_opt (config .get (key , opt_key ))
68
+ raise ValueError ("Unknown option type: %s" % opt_type )
67
69
68
- raise ValueError ("Unknown option type: %s" % opt_type )
70
+ @classmethod
71
+ def _parse_list_opt (cls , string ):
72
+ return [s .strip () for s in string .split ("," ) if s .strip ()]
69
73
74
+ @classmethod
75
+ def _set_opt (cls , config_dict , path , value ):
76
+ """Set the value in the dictionary at the given path if the value is not None."""
77
+ if value is None :
78
+ return
70
79
71
- def _parse_list_opt (string ):
72
- if string .startswith ("\n " ):
73
- return [s .strip ().rstrip ("," ) for s in string .split ("\n " ) if s .strip ()]
74
- return [s .strip () for s in string .split ("," ) if s .strip ()]
80
+ if '.' not in path :
81
+ config_dict [path ] = value
82
+ return
75
83
84
+ key , rest = path .split ("." , 1 )
85
+ if key not in config_dict :
86
+ config_dict [key ] = {}
76
87
77
- def _set_opt (config_dict , path , value ):
78
- """Set the value in the dictionary at the given path if the value is not None."""
79
- if value is None :
80
- return
81
-
82
- if '.' not in path :
83
- config_dict [path ] = value
84
- return
85
-
86
- key , rest = path .split ("." , 1 )
87
- if key not in config_dict :
88
- config_dict [key ] = {}
89
-
90
- _set_opt (config_dict [key ], rest , value )
88
+ cls ._set_opt (config_dict [key ], rest , value )
0 commit comments