|
| 1 | +""" Parse arguments from command line and configuration files. """ |
| 2 | +import fnmatch |
| 3 | +import logging |
| 4 | +from argparse import ArgumentParser, Namespace as Options |
| 5 | +from os import getcwd, path |
| 6 | +from re import compile as re |
| 7 | + |
| 8 | +from . import version, utils |
| 9 | +from .core import DEFAULT_LINTERS, LOGGER, STREAM |
| 10 | +from .inirama import Namespace |
| 11 | + |
| 12 | + |
| 13 | +DEFAULT_COMPLEXITY = 10 |
| 14 | +CURDIR = getcwd() |
| 15 | +DEFAULT_INI_PATH = path.join(CURDIR, 'pylama.ini') |
| 16 | + |
| 17 | + |
| 18 | +def parse_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) for a in parser._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 | + if not (args is None): |
| 36 | + options = parser.parse_args(args) |
| 37 | + |
| 38 | + config = get_config(str(options.options)) |
| 39 | + |
| 40 | + for k, v in config.default.items(): |
| 41 | + value = getattr(options, k, _Default(None)) |
| 42 | + if not isinstance(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 | + if callable(action.type) else v |
| 49 | + if action.const: |
| 50 | + value = bool(int(value)) |
| 51 | + setattr(options, name, value) |
| 52 | + |
| 53 | + opts = dict(options.__dict__.items()) |
| 54 | + for name, value in opts.items(): |
| 55 | + if isinstance(value, _Default): |
| 56 | + action = actions.get(name) |
| 57 | + if action and callable(action.type): |
| 58 | + value.value = action.type(value.value) |
| 59 | + |
| 60 | + setattr(options, name, value.value) |
| 61 | + |
| 62 | + options.file_params = dict() |
| 63 | + for k, s in config.sections.items(): |
| 64 | + if k != 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 | + return options |
| 72 | + |
| 73 | + |
| 74 | +def setup_logger(options): |
| 75 | + """ Setup logger with options. """ |
| 76 | + |
| 77 | + LOGGER.setLevel(logging.INFO if options.verbose else logging.WARN) |
| 78 | + if options.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 | +def get_parser(): |
| 85 | + """ Make command parser for pylama. |
| 86 | +
|
| 87 | + :return ArgumentParser: |
| 88 | +
|
| 89 | + """ |
| 90 | + split_csp_str = lambda s: list( |
| 91 | + set(i for i in s.strip().split(',') if i)) |
| 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(s for s in utils.__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=lambda s: [re(fnmatch.translate(p)) for p in s.split(',') if p], |
| 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 | + return parser |
| 147 | + |
| 148 | + |
| 149 | +def get_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 | + return config |
| 160 | + |
| 161 | + |
| 162 | +class _Default(object): |
| 163 | + |
| 164 | + def __init__(self, value): |
| 165 | + self.value = value |
| 166 | + |
| 167 | + def __getattr__(self, name): |
| 168 | + return getattr(self.value, name) |
| 169 | + |
| 170 | + def __str__(self): |
| 171 | + return str(self.value) |
| 172 | + |
| 173 | + |
| 174 | +# lint_ignore=R0914,W0212,E1103,C901 |
0 commit comments