|
| 1 | +""" Parse arguments from command line and configuration files. """ |
| 2 | +importfnmatch |
| 3 | +importlogging |
| 4 | +fromargparseimportArgumentParser,NamespaceasOptions |
| 5 | +fromosimportgetcwd,path |
| 6 | +fromreimportcompileasre |
| 7 | + |
| 8 | +from .importversion,utils |
| 9 | +from .coreimportDEFAULT_LINTERS,LOGGER,STREAM |
| 10 | +from .iniramaimportNamespace |
| 11 | + |
| 12 | + |
| 13 | +DEFAULT_COMPLEXITY=10 |
| 14 | +CURDIR=getcwd() |
| 15 | +DEFAULT_INI_PATH=path.join(CURDIR,'pylama.ini') |
| 16 | + |
| 17 | + |
| 18 | +defparse_options( |
| 19 | +args=None,async=False,select='',ignore='',linters=DEFAULT_LINTERS, |
| 20 | +complexity=DEFAULT_COMPLEXITY,options=DEFAULT_INI_PATH): |
| 21 | +""" Parse options from command line and configuration files. |
| 22 | +
|
| 23 | + :return argparse.Namespace: |
| 24 | +
|
| 25 | + """ |
| 26 | +parser=get_parser() |
| 27 | +actions=dict((a.dest,a)forainparser._actions) |
| 28 | +options=Options( |
| 29 | +async=_Default(async),format=_Default('pep8'), |
| 30 | +select=_Default(select),ignore=_Default(ignore), |
| 31 | +report=_Default(None),verbose=_Default(False), |
| 32 | +linters=_Default(linters),complexity=_Default(complexity), |
| 33 | +options=_Default(options)) |
| 34 | + |
| 35 | +ifnot (argsisNone): |
| 36 | +options=parser.parse_args(args) |
| 37 | + |
| 38 | +config=get_config(str(options.options)) |
| 39 | + |
| 40 | +fork,vinconfig.default.items(): |
| 41 | +value=getattr(options,k,_Default(None)) |
| 42 | +ifnotisinstance(value,_Default): |
| 43 | +continue |
| 44 | + |
| 45 | +action=actions.get(k) |
| 46 | +LOGGER.info('Find option %s (%s)',k,v) |
| 47 | +name,value=action.dest,action.type(v)\ |
| 48 | +ifcallable(action.type)elsev |
| 49 | +ifaction.const: |
| 50 | +value=bool(int(value)) |
| 51 | +setattr(options,name,value) |
| 52 | + |
| 53 | +opts=dict(options.__dict__.items()) |
| 54 | +forname,valueinopts.items(): |
| 55 | +ifisinstance(value,_Default): |
| 56 | +action=actions.get(name) |
| 57 | +ifactionandcallable(action.type): |
| 58 | +value.value=action.type(value.value) |
| 59 | + |
| 60 | +setattr(options,name,value.value) |
| 61 | + |
| 62 | +options.file_params=dict() |
| 63 | +fork,sinconfig.sections.items(): |
| 64 | +ifk!=config.default_section: |
| 65 | +mask=re(fnmatch.translate(k)) |
| 66 | +options.file_params[mask]=dict(s) |
| 67 | +options.file_params[mask]['lint']=int( |
| 68 | +options.file_params[mask].get('lint',1) |
| 69 | + ) |
| 70 | + |
| 71 | +returnoptions |
| 72 | + |
| 73 | + |
| 74 | +defsetup_logger(options): |
| 75 | +""" Setup logger with options. """ |
| 76 | + |
| 77 | +LOGGER.setLevel(logging.INFOifoptions.verboseelselogging.WARN) |
| 78 | +ifoptions.report: |
| 79 | +LOGGER.removeHandler(STREAM) |
| 80 | +LOGGER.addHandler(logging.FileHandler(options.report,mode='w')) |
| 81 | +LOGGER.info('Try to read configuration from: '+options.options) |
| 82 | + |
| 83 | + |
| 84 | +defget_parser(): |
| 85 | +""" Make command parser for pylama. |
| 86 | +
|
| 87 | + :return ArgumentParser: |
| 88 | +
|
| 89 | + """ |
| 90 | +split_csp_str=lambdas:list( |
| 91 | +set(iforiins.strip().split(',')ifi)) |
| 92 | + |
| 93 | +parser=ArgumentParser(description="Code audit tool for python.") |
| 94 | +parser.add_argument( |
| 95 | +"path",nargs='?',default=_Default(CURDIR), |
| 96 | +help="Path on file or directory.") |
| 97 | + |
| 98 | +parser.add_argument( |
| 99 | +"--verbose","-v",action='store_true',help="Verbose mode.") |
| 100 | + |
| 101 | +parser.add_argument('--version',action='version', |
| 102 | +version='%(prog)s '+version) |
| 103 | + |
| 104 | +parser.add_argument( |
| 105 | +"--format","-f",default=_Default('pep8'),choices=['pep8','pylint'], |
| 106 | +help="Error format.") |
| 107 | + |
| 108 | +parser.add_argument( |
| 109 | +"--select","-s",default=_Default(''),type=split_csp_str, |
| 110 | +help="Select errors and warnings. (comma-separated)") |
| 111 | + |
| 112 | +parser.add_argument( |
| 113 | +"--linters","-l",default=_Default(','.join(DEFAULT_LINTERS)), |
| 114 | +type=split_csp_str, |
| 115 | +help=( |
| 116 | +"Select linters. (comma-separated). Choices are %s." |
| 117 | +%','.join(sforsinutils.__all__) |
| 118 | + )) |
| 119 | + |
| 120 | +parser.add_argument( |
| 121 | +"--ignore","-i",default=_Default(''),type=split_csp_str, |
| 122 | +help="Ignore errors and warnings. (comma-separated)") |
| 123 | + |
| 124 | +parser.add_argument( |
| 125 | +"--skip",default=_Default(''), |
| 126 | +type=lambdas: [re(fnmatch.translate(p))forpins.split(',')ifp], |
| 127 | +help="Skip files by masks (comma-separated, Ex. */messages.py)") |
| 128 | + |
| 129 | +parser.add_argument( |
| 130 | +"--complexity","-c",default=_Default(DEFAULT_COMPLEXITY),type=int, |
| 131 | +help="Set mccabe complexity.") |
| 132 | + |
| 133 | +parser.add_argument("--report","-r",help="Filename for report.") |
| 134 | +parser.add_argument( |
| 135 | +"--hook",action="store_true",help="Install Git (Mercurial) hook.") |
| 136 | + |
| 137 | +parser.add_argument( |
| 138 | +"--async",action="store_true", |
| 139 | +help="Enable async mode. Usefull for checking a lot of files. " |
| 140 | +"Dont supported with pylint.") |
| 141 | + |
| 142 | +parser.add_argument( |
| 143 | +"--options","-o",default=_Default(DEFAULT_INI_PATH), |
| 144 | +help="Select configuration file. By default is '<CURDIR>/pylama.ini'") |
| 145 | + |
| 146 | +returnparser |
| 147 | + |
| 148 | + |
| 149 | +defget_config(ini_path=DEFAULT_INI_PATH): |
| 150 | +""" Load configuration from INI. |
| 151 | +
|
| 152 | + :return Namespace: |
| 153 | +
|
| 154 | + """ |
| 155 | +config=Namespace() |
| 156 | +config.default_section='main' |
| 157 | +config.read(ini_path) |
| 158 | + |
| 159 | +returnconfig |
| 160 | + |
| 161 | + |
| 162 | +class_Default(object): |
| 163 | + |
| 164 | +def__init__(self,value): |
| 165 | +self.value=value |
| 166 | + |
| 167 | +def__getattr__(self,name): |
| 168 | +returngetattr(self.value,name) |
| 169 | + |
| 170 | +def__str__(self): |
| 171 | +returnstr(self.value) |
| 172 | + |
| 173 | + |
| 174 | +# lint_ignore=R0914,W0212,E1103,C901 |