11# flake8: noqa
22
33from configargparse import *
4- from six . moves import StringIO
4+ from io import StringIO
55
66from gixy .core .plugins_manager import PluginsManager
77
88# used while parsing args to keep track of where they came from
9- _COMMAND_LINE_SOURCE_KEY = ' command_line'
10- _ENV_VAR_SOURCE_KEY = ' environment_variables'
11- _CONFIG_FILE_SOURCE_KEY = ' config_file'
12- _DEFAULTS_SOURCE_KEY = ' defaults'
9+ _COMMAND_LINE_SOURCE_KEY = " command_line"
10+ _ENV_VAR_SOURCE_KEY = " environment_variables"
11+ _CONFIG_FILE_SOURCE_KEY = " config_file"
12+ _DEFAULTS_SOURCE_KEY = " defaults"
1313
1414
1515class GixyConfigParser (DefaultConfigFileParser ):
1616 def get_syntax_description (self ):
17- return ''
17+ return ""
1818
1919 def parse (self , stream ):
2020 """Parses the keys + values from a config file."""
2121
2222 items = OrderedDict ()
23- prefix = ''
23+ prefix = ""
2424 for i , line in enumerate (stream ):
2525 line = line .strip ()
26- if not line or line [0 ] in ['#' , ';' ] or line .startswith (' ---' ):
26+ if not line or line [0 ] in ["#" , ";" ] or line .startswith (" ---" ):
2727 continue
28- if line [0 ] == '[' :
29- prefix = ' %s-' % line [1 :- 1 ].replace ('_' , '-' )
28+ if line [0 ] == "[" :
29+ prefix = " %s-" % line [1 :- 1 ].replace ("_" , "-" )
3030 continue
3131
32- white_space = ' \\ s*'
33- key = ' (?P<key>[^:=;#\s]+?)'
34- value = white_space + ' [:=\s]' + white_space + ' (?P<value>.+?)'
35- comment = white_space + ' (?P<comment>\\ s[;#].*)?'
32+ white_space = " \\ s*"
33+ key = " (?P<key>[^:=;#\s]+?)"
34+ value = white_space + " [:=\s]" + white_space + " (?P<value>.+?)"
35+ comment = white_space + " (?P<comment>\\ s[;#].*)?"
3636
37- key_only_match = re .match ('^' + key + comment + '$' , line )
37+ key_only_match = re .match ("^" + key + comment + "$" , line )
3838 if key_only_match :
39- key = key_only_match .group (' key' )
40- items [key ] = ' true'
39+ key = key_only_match .group (" key" )
40+ items [key ] = " true"
4141 continue
4242
43- key_value_match = re .match ('^' + key + value + comment + '$' , line )
43+ key_value_match = re .match ("^" + key + value + comment + "$" , line )
4444 if key_value_match :
45- key = key_value_match .group (' key' )
46- value = key_value_match .group (' value' )
45+ key = key_value_match .group (" key" )
46+ value = key_value_match .group (" value" )
4747
48- if value .startswith ('[' ) and value .endswith (']' ):
48+ if value .startswith ("[" ) and value .endswith ("]" ):
4949 # handle special case of lists
50- value = [elem .strip () for elem in value [1 :- 1 ].split (',' )]
50+ value = [elem .strip () for elem in value [1 :- 1 ].split ("," )]
5151
5252 items [prefix + key ] = value
5353 continue
5454
55- raise ConfigFileParserException ('Unexpected line %s in %s: %s' % (i ,
56- getattr (stream , 'name' , 'stream' ), line ))
55+ raise ConfigFileParserException (
56+ "Unexpected line %s in %s: %s"
57+ % (i , getattr (stream , "name" , "stream" ), line )
58+ )
5759 return items
5860
5961 def serialize (self , items ):
@@ -63,24 +65,28 @@ def serialize(self, items):
6365 r = StringIO ()
6466 for key , value in items .items ():
6567 if type (value ) == OrderedDict :
66- r .write (' \n [%s]\n ' % key )
68+ r .write (" \n [%s]\n " % key )
6769 r .write (self .serialize (value ))
6870 else :
6971 value , help = value
7072 if help :
71- r .write (' ; %s\n ' % help )
72- r .write (' %s = %s\n ' % (key , value ))
73+ r .write (" ; %s\n " % help )
74+ r .write (" %s = %s\n " % (key , value ))
7375 return r .getvalue ()
7476
7577
7678class GixyHelpFormatter (HelpFormatter ):
7779 def format_help (self ):
7880 manager = PluginsManager ()
7981 help_message = super (GixyHelpFormatter , self ).format_help ()
80- if 'plugins options:' in help_message :
81- # Print available blugins _only_ if we prints options for it
82- plugins = '\n ' .join ('\t ' + plugin .__name__ for plugin in manager .plugins_classes )
83- help_message = '{orig}\n \n available plugins:\n {plugins}\n ' .format (orig = help_message , plugins = plugins )
82+ if "plugins options:" in help_message :
83+ # Print available plugins _only_ if we print options for it
84+ plugins = "\n " .join (
85+ "\t " + plugin .__name__ for plugin in manager .plugins_classes
86+ )
87+ help_message = "{orig}\n \n available plugins:\n {plugins}\n " .format (
88+ orig = help_message , plugins = plugins
89+ )
8490 return help_message
8591
8692
@@ -92,15 +98,14 @@ def get_possible_config_keys(self, action):
9298 """
9399 keys = []
94100 for arg in action .option_strings :
95- if arg in [' --config' , ' --write-config' , ' --version' ]:
101+ if arg in [" --config" , " --write-config" , " --version" ]:
96102 continue
97103 if any ([arg .startswith (2 * c ) for c in self .prefix_chars ]):
98104 keys += [arg [2 :], arg ] # eg. for '--bla' return ['bla', '--bla']
99105
100106 return keys
101107
102- def get_items_for_config_file_output (self , source_to_settings ,
103- parsed_namespace ):
108+ def get_items_for_config_file_output (self , source_to_settings , parsed_namespace ):
104109 """Converts the given settings back to a dictionary that can be passed
105110 to ConfigFormatParser.serialize(..).
106111
@@ -114,29 +119,36 @@ def get_items_for_config_file_output(self, source_to_settings,
114119 config_file_items = OrderedDict ()
115120 for source , settings in source_to_settings .items ():
116121 if source == _COMMAND_LINE_SOURCE_KEY :
117- _ , existing_command_line_args = settings ['' ]
122+ _ , existing_command_line_args = settings ["" ]
118123 for action in self ._actions :
119124 config_file_keys = self .get_possible_config_keys (action )
120- if config_file_keys and not action .is_positional_arg and \
121- already_on_command_line (existing_command_line_args ,
122- action .option_strings ):
125+ if (
126+ config_file_keys
127+ and not action .is_positional_arg
128+ and already_on_command_line (
129+ existing_command_line_args , action .option_strings
130+ )
131+ ):
123132 value = getattr (parsed_namespace , action .dest , None )
124133 if value is not None :
125134 if type (value ) is bool :
126135 value = str (value ).lower ()
127- if ':' in action .dest :
128- section , key = action .dest .split (':' , 2 )
129- key = key .replace ('_' , '-' )
136+ if ":" in action .dest :
137+ section , key = action .dest .split (":" , 2 )
138+ key = key .replace ("_" , "-" )
130139 if section not in config_file_items :
131140 config_file_items [section ] = OrderedDict ()
132141 config_file_items [section ][key ] = (value , action .help )
133142 else :
134- config_file_items [config_file_keys [0 ]] = (value , action .help )
143+ config_file_items [config_file_keys [0 ]] = (
144+ value ,
145+ action .help ,
146+ )
135147 elif source .startswith (_CONFIG_FILE_SOURCE_KEY ):
136148 for key , (action , value ) in settings .items ():
137- if ':' in action .dest :
138- section , key = action .dest .split (':' , 2 )
139- key = key .replace ('_' , '-' )
149+ if ":" in action .dest :
150+ section , key = action .dest .split (":" , 2 )
151+ key = key .replace ("_" , "-" )
140152 if section not in config_file_items :
141153 config_file_items [section ] = OrderedDict ()
142154 config_file_items [section ][key ] = (value , action .help )
@@ -147,13 +159,13 @@ def get_items_for_config_file_output(self, source_to_settings,
147159
148160def create_parser ():
149161 return ArgsParser (
150- description = ' Gixy - a Nginx configuration [sec]analyzer\n \n ' ,
162+ description = " Gixy - a Nginx configuration [sec]analyzer\n \n " ,
151163 formatter_class = GixyHelpFormatter ,
152164 config_file_parser_class = GixyConfigParser ,
153- auto_env_var_prefix = ' GIXY_' ,
165+ auto_env_var_prefix = " GIXY_" ,
154166 add_env_var_help = False ,
155- default_config_files = [' /etc/gixy/gixy.cfg' , ' ~/.config/gixy/gixy.conf' ],
156- args_for_setting_config_path = ['-c' , ' --config' ],
157- args_for_writing_out_config_file = [' --write-config' ],
158- add_config_file_help = False
167+ default_config_files = [" /etc/gixy/gixy.cfg" , " ~/.config/gixy/gixy.conf" ],
168+ args_for_setting_config_path = ["-c" , " --config" ],
169+ args_for_writing_out_config_file = [" --write-config" ],
170+ add_config_file_help = False ,
159171 )
0 commit comments