@@ -27,64 +27,62 @@ def project_config(self, document_path):
2727 """Return project-level (i.e. workspace directory) configuration."""
2828 raise NotImplementedError ()
2929
30- @staticmethod
31- def read_config_from_files (files ):
30+ @classmethod
31+ def read_config_from_files (cls , files ):
3232 config = configparser .RawConfigParser ()
3333 for filename in files :
3434 if os .path .exists (filename ) and not os .path .isdir (filename ):
3535 config .read (filename )
3636
3737 return config
3838
39- @staticmethod
40- def parse_config (config , key , options ):
39+ @classmethod
40+ def parse_config (cls , config , key , options ):
4141 """Parse the config with the given options."""
4242 conf = {}
4343 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 )
4545 if opt_value is not None :
46- _set_opt (conf , destination , opt_value )
46+ cls . _set_opt (conf , destination , opt_value )
4747 return conf
4848
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
4955
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 )
5558
56- if opt_type == bool :
57- return config .getboolean (key , opt_key )
59+ if opt_type == int :
60+ return config .getint (key , opt_key )
5861
59- if opt_type == int :
60- return config .getint (key , opt_key )
62+ if opt_type == str :
63+ return config .get (key , opt_key )
6164
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 ) )
6467
65- if opt_type == list :
66- return _parse_list_opt (config .get (key , opt_key ))
68+ raise ValueError ("Unknown option type: %s" % opt_type )
6769
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 ()]
6973
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
7079
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
7583
84+ key , rest = path .split ("." , 1 )
85+ if key not in config_dict :
86+ config_dict [key ] = {}
7687
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