11""" Parse arguments from command line and configuration files. """
22import fnmatch
33import sys
4- from os import getcwd , path
4+ import os
55from re import compile as re
66
77import logging
2121#: A default checkers
2222DEFAULT_LINTERS = 'pep8' , 'pyflakes' , 'mccabe'
2323
24- CURDIR = getcwd ()
25- DEFAULT_INI_PATH = path .join (CURDIR , 'pylama.ini' )
24+ CURDIR = os .getcwd ()
25+ CONFIG_FILES = [
26+ os .path .join (CURDIR , basename ) for basename in
27+ ('pylama.ini' , 'setup.cfg' , 'tox.ini' , 'pytest.ini' )
28+ ]
2629
2730
2831class _Default (object ):
@@ -67,7 +70,7 @@ def parse_linters(linters):
6770PARSER = ArgumentParser (description = "Code audit tool for python." )
6871PARSER .add_argument (
6972 "path" , nargs = '?' , default = _Default (CURDIR ),
70- help = "Path on file or directory." )
73+ help = "Path on file or directory for code check ." )
7174
7275PARSER .add_argument (
7376 "--verbose" , "-v" , action = 'store_true' , help = "Verbose mode." )
@@ -77,11 +80,11 @@ def parse_linters(linters):
7780
7881PARSER .add_argument (
7982 "--format" , "-f" , default = _Default ('pep8' ), choices = ['pep8' , 'pylint' ],
80- help = "Error format." )
83+ help = "Choose errors format (pep8, pylint) ." )
8184
8285PARSER .add_argument (
8386 "--select" , "-s" , default = _Default ('' ), type = split_csp_str ,
84- help = "Select errors and warnings. (comma-separated)" )
87+ help = "Select errors and warnings. (comma-separated list )" )
8588
8689
8790PARSER .add_argument (
@@ -100,7 +103,7 @@ def parse_linters(linters):
100103 type = lambda s : [re (fnmatch .translate (p )) for p in s .split (',' ) if p ],
101104 help = "Skip files by masks (comma-separated, Ex. */messages.py)" )
102105
103- PARSER .add_argument ("--report" , "-r" , help = "Filename for report. " )
106+ PARSER .add_argument ("--report" , "-r" , help = "Send report to file [REPORT] " )
104107PARSER .add_argument (
105108 "--hook" , action = "store_true" , help = "Install Git (Mercurial) hook." )
106109
@@ -110,7 +113,7 @@ def parse_linters(linters):
110113 "Dont supported with pylint." )
111114
112115PARSER .add_argument (
113- "--options" , "-o" , default = _Default ( DEFAULT_INI_PATH ) ,
116+ "--options" , "-o" , default = "" ,
114117 help = "Select configuration file. By default is '<CURDIR>/pylama.ini'" )
115118
116119PARSER .add_argument (
@@ -151,17 +154,22 @@ def parse_options(args=None, config=True, **overrides): # noqa
151154 setattr (options , k , _Default (v ))
152155
153156 # Parse file related options
154- for k , s in cfg .sections .items ():
155- if k == cfg .default_section :
157+ for name , opts in cfg .sections .items ():
158+
159+ if not name .startswith ('pylama' ):
160+ continue
161+
162+ if name == cfg .default_section :
156163 continue
157- if k in LINTERS :
158- options .linter_params [k ] = dict (s )
164+
165+ name = name [7 :]
166+
167+ if name in LINTERS :
168+ options .linter_params [name ] = dict (opts )
159169 continue
160- mask = re (fnmatch .translate (k ))
161- options .file_params [mask ] = dict (s )
162- options .file_params [mask ]['lint' ] = int (
163- options .file_params [mask ].get ('lint' , 1 )
164- )
170+
171+ mask = re (fnmatch .translate (name ))
172+ options .file_params [mask ] = dict (opts )
165173
166174 # Postprocess options
167175 opts = dict (options .__dict__ .items ())
@@ -187,15 +195,21 @@ def process_value(name, value):
187195 return value
188196
189197
190- def get_config (ini_path = DEFAULT_INI_PATH ):
198+ def get_config (ini_path = None ):
191199 """ Load configuration from INI.
192200
193201 :return Namespace:
194202
195203 """
196204 config = Namespace ()
197- config .default_section = 'main'
198- config .read (ini_path )
205+ config .default_section = 'pylama'
206+
207+ if not ini_path :
208+ for path in CONFIG_FILES :
209+ if os .path .isfile (path ) and os .access (path , os .R_OK ):
210+ config .read (path )
211+ else :
212+ config .read (ini_path )
199213
200214 return config
201215
@@ -207,3 +221,5 @@ def setup_logger(options):
207221 LOGGER .removeHandler (STREAM )
208222 LOGGER .addHandler (logging .FileHandler (options .report , mode = 'w' ))
209223 LOGGER .info ('Try to read configuration from: ' + options .options )
224+
225+ # pylama:ignore=W0212
0 commit comments