config.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. """Configuration file parsing and utilities."""
  2. import copy
  3. import itertools
  4. import operator
  5. import os
  6. import sys
  7. from collections import namedtuple
  8. from collections.abc import Set
  9. from configparser import NoOptionError, NoSectionError, RawConfigParser
  10. from functools import reduce
  11. from re import compile as re
  12. from ._version import __version__
  13. from .utils import log
  14. from .violations import ErrorRegistry, conventions
  15. if sys.version_info >= (3, 11):
  16. import tomllib
  17. else:
  18. try:
  19. import tomli as tomllib
  20. except ImportError: # pragma: no cover
  21. tomllib = None # type: ignore
  22. def check_initialized(method):
  23. """Check that the configuration object was initialized."""
  24. def _decorator(self, *args, **kwargs):
  25. if self._arguments is None or self._options is None:
  26. raise RuntimeError('using an uninitialized configuration')
  27. return method(self, *args, **kwargs)
  28. return _decorator
  29. class TomlParser:
  30. """ConfigParser that partially mimics RawConfigParser but for toml files.
  31. See RawConfigParser for more info. Also, please note that not all
  32. RawConfigParser functionality is implemented, but only the subset that is
  33. currently used by pydocstyle.
  34. """
  35. def __init__(self):
  36. """Create a toml parser."""
  37. self._config = {}
  38. def read(self, filenames, encoding=None):
  39. """Read and parse a filename or an iterable of filenames.
  40. Files that cannot be opened are silently ignored; this is
  41. designed so that you can specify an iterable of potential
  42. configuration file locations (e.g. current directory, user's
  43. home directory, systemwide directory), and all existing
  44. configuration files in the iterable will be read. A single
  45. filename may also be given.
  46. Return list of successfully read files.
  47. """
  48. if isinstance(filenames, (str, bytes, os.PathLike)):
  49. filenames = [filenames]
  50. read_ok = []
  51. for filename in filenames:
  52. try:
  53. with open(filename, "rb") as fp:
  54. if not tomllib:
  55. log.warning(
  56. "The %s configuration file was ignored, "
  57. "because the `tomli` package is not installed.",
  58. filename,
  59. )
  60. continue
  61. self._config.update(tomllib.load(fp))
  62. except OSError:
  63. continue
  64. if isinstance(filename, os.PathLike):
  65. filename = os.fspath(filename)
  66. read_ok.append(filename)
  67. return read_ok
  68. def _get_section(self, section, allow_none=False):
  69. try:
  70. current = reduce(
  71. operator.getitem,
  72. section.split('.'),
  73. self._config['tool'],
  74. )
  75. except KeyError:
  76. current = None
  77. if isinstance(current, dict):
  78. return current
  79. elif allow_none:
  80. return None
  81. else:
  82. raise NoSectionError(section)
  83. def has_section(self, section):
  84. """Indicate whether the named section is present in the configuration."""
  85. return self._get_section(section, allow_none=True) is not None
  86. def options(self, section):
  87. """Return a list of option names for the given section name."""
  88. current = self._get_section(section)
  89. return list(current.keys())
  90. def get(self, section, option, *, _conv=None):
  91. """Get an option value for a given section."""
  92. d = self._get_section(section)
  93. option = option.lower()
  94. try:
  95. value = d[option]
  96. except KeyError:
  97. raise NoOptionError(option, section)
  98. if isinstance(value, dict):
  99. raise TypeError(
  100. f"Expected {section}.{option} to be an option, not a section."
  101. )
  102. # toml should convert types automatically
  103. # don't manually convert, just check, that the type is correct
  104. if _conv is not None and not isinstance(value, _conv):
  105. raise TypeError(
  106. f"The type of {section}.{option} should be {_conv}"
  107. )
  108. return value
  109. def getboolean(self, section, option):
  110. """Get a boolean option value for a given section."""
  111. return self.get(section, option, _conv=bool)
  112. def getint(self, section, option):
  113. """Get an integer option value for a given section."""
  114. return self.get(section, option, _conv=int)
  115. class ConfigurationParser:
  116. """Responsible for parsing configuration from files and CLI.
  117. There are 2 types of configurations: Run configurations and Check
  118. configurations.
  119. Run Configurations:
  120. ------------------
  121. Responsible for deciding things that are related to the user interface and
  122. configuration discovery, e.g. verbosity, debug options, etc.
  123. All run configurations default to `False` or `None` and are decided only
  124. by CLI.
  125. Check Configurations:
  126. --------------------
  127. Configurations that are related to which files and errors will be checked.
  128. These are configurable in 2 ways: using the CLI, and using configuration
  129. files.
  130. Configuration files are nested within the file system, meaning that the
  131. closer a configuration file is to a checked file, the more relevant it will
  132. be. For instance, imagine this directory structure:
  133. A
  134. +-- tox.ini: sets `select=D100`
  135. +-- B
  136. +-- foo.py
  137. +-- tox.ini: sets `add-ignore=D100`
  138. Then `foo.py` will not be checked for `D100`.
  139. The configuration build algorithm is described in `self._get_config`.
  140. Note: If any of `BASE_ERROR_SELECTION_OPTIONS` was selected in the CLI, all
  141. configuration files will be ignored and each file will be checked for
  142. the error codes supplied in the CLI.
  143. """
  144. CONFIG_FILE_OPTIONS = (
  145. 'convention',
  146. 'select',
  147. 'ignore',
  148. 'add-select',
  149. 'add-ignore',
  150. 'match',
  151. 'match-dir',
  152. 'ignore-decorators',
  153. 'ignore-self-only-init',
  154. )
  155. BASE_ERROR_SELECTION_OPTIONS = ('ignore', 'select', 'convention')
  156. DEFAULT_MATCH_RE = r'(?!test_).*\.py'
  157. DEFAULT_MATCH_DIR_RE = r'[^\.].*'
  158. DEFAULT_IGNORE_DECORATORS_RE = ''
  159. DEFAULT_PROPERTY_DECORATORS = (
  160. "property,cached_property,functools.cached_property"
  161. )
  162. DEFAULT_CONVENTION = conventions.pep257
  163. DEFAULT_IGNORE_SELF_ONLY_INIT = False
  164. PROJECT_CONFIG_FILES = (
  165. 'setup.cfg',
  166. 'tox.ini',
  167. '.pydocstyle',
  168. '.pydocstyle.ini',
  169. '.pydocstylerc',
  170. '.pydocstylerc.ini',
  171. 'pyproject.toml',
  172. # The following is deprecated, but remains for backwards compatibility.
  173. '.pep257',
  174. )
  175. POSSIBLE_SECTION_NAMES = ('pydocstyle', 'pep257')
  176. def __init__(self):
  177. """Create a configuration parser."""
  178. self._cache = {}
  179. self._override_by_cli = None
  180. self._options = self._arguments = self._run_conf = None
  181. self._parser = self._create_option_parser()
  182. # ---------------------------- Public Methods -----------------------------
  183. def get_default_run_configuration(self):
  184. """Return a `RunConfiguration` object set with default values."""
  185. options, _ = self._parse_args([])
  186. return self._create_run_config(options)
  187. def parse(self):
  188. """Parse the configuration.
  189. If one of `BASE_ERROR_SELECTION_OPTIONS` was selected, overrides all
  190. error codes to check and disregards any error code related
  191. configurations from the configuration files.
  192. """
  193. self._options, self._arguments = self._parse_args()
  194. self._arguments = self._arguments or ['.']
  195. if not self._validate_options(self._options):
  196. raise IllegalConfiguration()
  197. self._run_conf = self._create_run_config(self._options)
  198. config = self._create_check_config(self._options, use_defaults=False)
  199. self._override_by_cli = config
  200. @check_initialized
  201. def get_user_run_configuration(self):
  202. """Return the run configuration for the script."""
  203. return self._run_conf
  204. @check_initialized
  205. def get_files_to_check(self):
  206. """Generate files and error codes to check on each one.
  207. Walk dir trees under `self._arguments` and yield file names
  208. that `match` under each directory that `match_dir`.
  209. The method locates the configuration for each file name and yields a
  210. tuple of (filename, [error_codes]).
  211. With every discovery of a new configuration file `IllegalConfiguration`
  212. might be raised.
  213. """
  214. def _get_matches(conf):
  215. """Return the `match` and `match_dir` functions for `config`."""
  216. match_func = re(conf.match + '$').match
  217. match_dir_func = re(conf.match_dir + '$').match
  218. return match_func, match_dir_func
  219. def _get_ignore_decorators(conf):
  220. """Return the `ignore_decorators` as None or regex."""
  221. return (
  222. re(conf.ignore_decorators) if conf.ignore_decorators else None
  223. )
  224. def _get_property_decorators(conf):
  225. """Return the `property_decorators` as None or set."""
  226. return (
  227. set(conf.property_decorators.split(","))
  228. if conf.property_decorators
  229. else None
  230. )
  231. for name in self._arguments:
  232. if os.path.isdir(name):
  233. for root, dirs, filenames in os.walk(name):
  234. config = self._get_config(os.path.abspath(root))
  235. match, match_dir = _get_matches(config)
  236. ignore_decorators = _get_ignore_decorators(config)
  237. property_decorators = _get_property_decorators(config)
  238. # Skip any dirs that do not match match_dir
  239. dirs[:] = [d for d in dirs if match_dir(d)]
  240. for filename in map(os.path.basename, filenames):
  241. if match(filename):
  242. full_path = os.path.join(root, filename)
  243. yield (
  244. full_path,
  245. list(config.checked_codes),
  246. ignore_decorators,
  247. property_decorators,
  248. config.ignore_self_only_init,
  249. )
  250. else:
  251. config = self._get_config(os.path.abspath(name))
  252. match, _ = _get_matches(config)
  253. ignore_decorators = _get_ignore_decorators(config)
  254. property_decorators = _get_property_decorators(config)
  255. if match(os.path.basename(name)):
  256. yield (
  257. name,
  258. list(config.checked_codes),
  259. ignore_decorators,
  260. property_decorators,
  261. config.ignore_self_only_init,
  262. )
  263. # --------------------------- Private Methods -----------------------------
  264. def _get_config_by_discovery(self, node):
  265. """Get a configuration for checking `node` by config discovery.
  266. Config discovery happens when no explicit config file is specified. The
  267. file system is searched for config files starting from the directory
  268. containing the file being checked, and up until the root directory of
  269. the project.
  270. See `_get_config` for further details.
  271. """
  272. path = self._get_node_dir(node)
  273. if path in self._cache:
  274. return self._cache[path]
  275. config_file = self._get_config_file_in_folder(path)
  276. if config_file is None:
  277. parent_dir, tail = os.path.split(path)
  278. if tail:
  279. # No configuration file, simply take the parent's.
  280. config = self._get_config(parent_dir)
  281. else:
  282. # There's no configuration file and no parent directory.
  283. # Use the default configuration or the one given in the CLI.
  284. config = self._create_check_config(self._options)
  285. else:
  286. # There's a config file! Read it and merge if necessary.
  287. options, inherit = self._read_configuration_file(config_file)
  288. parent_dir, tail = os.path.split(path)
  289. if tail and inherit:
  290. # There is a parent dir and we should try to merge.
  291. parent_config = self._get_config(parent_dir)
  292. config = self._merge_configuration(parent_config, options)
  293. else:
  294. # No need to merge or parent dir does not exist.
  295. config = self._create_check_config(options)
  296. return config
  297. def _get_config(self, node):
  298. """Get and cache the run configuration for `node`.
  299. If no configuration exists (not local and not for the parent node),
  300. returns and caches a default configuration.
  301. The algorithm:
  302. -------------
  303. * If the current directory's configuration exists in
  304. `self._cache` - return it.
  305. * If a configuration file does not exist in this directory:
  306. * If the directory is not a root directory:
  307. * Cache its configuration as this directory's and return it.
  308. * Else:
  309. * Cache a default configuration and return it.
  310. * Else:
  311. * Read the configuration file.
  312. * If a parent directory exists AND the configuration file
  313. allows inheritance:
  314. * Read the parent configuration by calling this function with the
  315. parent directory as `node`.
  316. * Merge the parent configuration with the current one and
  317. cache it.
  318. * If the user has specified one of `BASE_ERROR_SELECTION_OPTIONS` in
  319. the CLI - return the CLI configuration with the configuration match
  320. clauses
  321. * Set the `--add-select` and `--add-ignore` CLI configurations.
  322. """
  323. if self._run_conf.config is None:
  324. log.debug('No config file specified, discovering.')
  325. config = self._get_config_by_discovery(node)
  326. else:
  327. log.debug('Using config file %r', self._run_conf.config)
  328. if not os.path.exists(self._run_conf.config):
  329. raise IllegalConfiguration(
  330. 'Configuration file {!r} specified '
  331. 'via --config was not found.'.format(self._run_conf.config)
  332. )
  333. if None in self._cache:
  334. return self._cache[None]
  335. options, _ = self._read_configuration_file(self._run_conf.config)
  336. if options is None:
  337. log.warning(
  338. 'Configuration file does not contain a '
  339. 'pydocstyle section. Using default configuration.'
  340. )
  341. config = self._create_check_config(self._options)
  342. else:
  343. config = self._create_check_config(options)
  344. # Make the CLI always win
  345. final_config = {}
  346. for attr in CheckConfiguration._fields:
  347. cli_val = getattr(self._override_by_cli, attr)
  348. conf_val = getattr(config, attr)
  349. final_config[attr] = cli_val if cli_val is not None else conf_val
  350. config = CheckConfiguration(**final_config)
  351. self._set_add_options(config.checked_codes, self._options)
  352. # Handle caching
  353. if self._run_conf.config is not None:
  354. self._cache[None] = config
  355. else:
  356. self._cache[self._get_node_dir(node)] = config
  357. return config
  358. @staticmethod
  359. def _get_node_dir(node):
  360. """Return the absolute path of the directory of a filesystem node."""
  361. path = os.path.abspath(node)
  362. return path if os.path.isdir(path) else os.path.dirname(path)
  363. def _read_configuration_file(self, path):
  364. """Try to read and parse `path` as a configuration file.
  365. If the configurations were illegal (checked with
  366. `self._validate_options`), raises `IllegalConfiguration`.
  367. Returns (options, should_inherit).
  368. """
  369. if path.endswith('.toml'):
  370. parser = TomlParser()
  371. else:
  372. parser = RawConfigParser(inline_comment_prefixes=('#', ';'))
  373. options = None
  374. should_inherit = True
  375. if parser.read(path) and self._get_section_name(parser):
  376. all_options = self._parser.option_list[:]
  377. for group in self._parser.option_groups:
  378. all_options.extend(group.option_list)
  379. option_list = {o.dest: o.type or o.action for o in all_options}
  380. # First, read the default values
  381. new_options, _ = self._parse_args([])
  382. # Second, parse the configuration
  383. section_name = self._get_section_name(parser)
  384. for opt in parser.options(section_name):
  385. if opt == 'inherit':
  386. should_inherit = parser.getboolean(section_name, opt)
  387. continue
  388. if opt.replace('_', '-') not in self.CONFIG_FILE_OPTIONS:
  389. log.warning(f"Unknown option '{opt}' ignored")
  390. continue
  391. normalized_opt = opt.replace('-', '_')
  392. opt_type = option_list[normalized_opt]
  393. if opt_type in ('int', 'count'):
  394. value = parser.getint(section_name, opt)
  395. elif opt_type == 'string':
  396. value = parser.get(section_name, opt)
  397. else:
  398. assert opt_type in ('store_true', 'store_false')
  399. value = parser.getboolean(section_name, opt)
  400. setattr(new_options, normalized_opt, value)
  401. # Third, fix the set-options
  402. options = self._fix_set_options(new_options)
  403. if options is not None:
  404. if not self._validate_options(options):
  405. raise IllegalConfiguration(f'in file: {path}')
  406. return options, should_inherit
  407. def _merge_configuration(self, parent_config, child_options):
  408. """Merge parent config into the child options.
  409. The migration process requires an `options` object for the child in
  410. order to distinguish between mutually exclusive codes, add-select and
  411. add-ignore error codes.
  412. """
  413. # Copy the parent error codes so we won't override them
  414. error_codes = copy.deepcopy(parent_config.checked_codes)
  415. if self._has_exclusive_option(child_options):
  416. error_codes = self._get_exclusive_error_codes(child_options)
  417. self._set_add_options(error_codes, child_options)
  418. kwargs = dict(checked_codes=error_codes)
  419. for key in (
  420. 'match',
  421. 'match_dir',
  422. 'ignore_decorators',
  423. 'property_decorators',
  424. 'ignore_self_only_init',
  425. ):
  426. child_value = getattr(child_options, key)
  427. kwargs[key] = (
  428. child_value
  429. if child_value is not None
  430. else getattr(parent_config, key)
  431. )
  432. return CheckConfiguration(**kwargs)
  433. def _parse_args(self, args=None, values=None):
  434. """Parse the options using `self._parser` and reformat the options."""
  435. options, arguments = self._parser.parse_args(args, values)
  436. return self._fix_set_options(options), arguments
  437. @staticmethod
  438. def _create_run_config(options):
  439. """Create a `RunConfiguration` object from `options`."""
  440. values = {
  441. opt: getattr(options, opt) for opt in RunConfiguration._fields
  442. }
  443. return RunConfiguration(**values)
  444. @classmethod
  445. def _create_check_config(cls, options, use_defaults=True):
  446. """Create a `CheckConfiguration` object from `options`.
  447. If `use_defaults`, any of the match options that are `None` will
  448. be replaced with their default value and the default convention will be
  449. set for the checked codes.
  450. """
  451. checked_codes = None
  452. if cls._has_exclusive_option(options) or use_defaults:
  453. checked_codes = cls._get_checked_errors(options)
  454. kwargs = dict(checked_codes=checked_codes)
  455. defaults = {
  456. 'match': "MATCH_RE",
  457. 'match_dir': "MATCH_DIR_RE",
  458. 'ignore_decorators': "IGNORE_DECORATORS_RE",
  459. 'property_decorators': "PROPERTY_DECORATORS",
  460. 'ignore_self_only_init': "IGNORE_SELF_ONLY_INIT",
  461. }
  462. for key, default in defaults.items():
  463. kwargs[key] = (
  464. getattr(cls, f"DEFAULT_{default}")
  465. if getattr(options, key) is None and use_defaults
  466. else getattr(options, key)
  467. )
  468. return CheckConfiguration(**kwargs)
  469. @classmethod
  470. def _get_section_name(cls, parser):
  471. """Parse options from relevant section."""
  472. for section_name in cls.POSSIBLE_SECTION_NAMES:
  473. if parser.has_section(section_name):
  474. return section_name
  475. return None
  476. @classmethod
  477. def _get_config_file_in_folder(cls, path):
  478. """Look for a configuration file in `path`.
  479. If exists return its full path, otherwise None.
  480. """
  481. if os.path.isfile(path):
  482. path = os.path.dirname(path)
  483. for fn in cls.PROJECT_CONFIG_FILES:
  484. if fn.endswith('.toml'):
  485. config = TomlParser()
  486. else:
  487. config = RawConfigParser(inline_comment_prefixes=('#', ';'))
  488. full_path = os.path.join(path, fn)
  489. if config.read(full_path) and cls._get_section_name(config):
  490. return full_path
  491. @classmethod
  492. def _get_exclusive_error_codes(cls, options):
  493. """Extract the error codes from the selected exclusive option."""
  494. codes = set(ErrorRegistry.get_error_codes())
  495. checked_codes = None
  496. if options.ignore is not None:
  497. ignored = cls._expand_error_codes(options.ignore)
  498. checked_codes = codes - ignored
  499. elif options.select is not None:
  500. checked_codes = cls._expand_error_codes(options.select)
  501. elif options.convention is not None:
  502. checked_codes = getattr(conventions, options.convention)
  503. # To not override the conventions nor the options - copy them.
  504. return copy.deepcopy(checked_codes)
  505. @classmethod
  506. def _set_add_options(cls, checked_codes, options):
  507. """Set `checked_codes` by the `add_ignore` or `add_select` options."""
  508. checked_codes |= cls._expand_error_codes(options.add_select)
  509. checked_codes -= cls._expand_error_codes(options.add_ignore)
  510. @staticmethod
  511. def _expand_error_codes(code_parts):
  512. """Return an expanded set of error codes to ignore."""
  513. codes = set(ErrorRegistry.get_error_codes())
  514. expanded_codes = set()
  515. try:
  516. for part in code_parts:
  517. # Dealing with split-lined configurations; The part might begin
  518. # with a whitespace due to the newline character.
  519. part = part.strip()
  520. if not part:
  521. continue
  522. codes_to_add = {
  523. code for code in codes if code.startswith(part)
  524. }
  525. if not codes_to_add:
  526. log.warning(
  527. 'Error code passed is not a prefix of any '
  528. 'known errors: %s',
  529. part,
  530. )
  531. expanded_codes.update(codes_to_add)
  532. except TypeError as e:
  533. raise IllegalConfiguration(e) from e
  534. return expanded_codes
  535. @classmethod
  536. def _get_checked_errors(cls, options):
  537. """Extract the codes needed to be checked from `options`."""
  538. checked_codes = cls._get_exclusive_error_codes(options)
  539. if checked_codes is None:
  540. checked_codes = cls.DEFAULT_CONVENTION
  541. cls._set_add_options(checked_codes, options)
  542. return checked_codes
  543. @classmethod
  544. def _validate_options(cls, options):
  545. """Validate the mutually exclusive options.
  546. Return `True` iff only zero or one of `BASE_ERROR_SELECTION_OPTIONS`
  547. was selected.
  548. """
  549. for opt1, opt2 in itertools.permutations(
  550. cls.BASE_ERROR_SELECTION_OPTIONS, 2
  551. ):
  552. if getattr(options, opt1) and getattr(options, opt2):
  553. log.error(
  554. 'Cannot pass both {} and {}. They are '
  555. 'mutually exclusive.'.format(opt1, opt2)
  556. )
  557. return False
  558. if options.convention and options.convention not in conventions:
  559. log.error(
  560. "Illegal convention '{}'. Possible conventions: {}".format(
  561. options.convention, ', '.join(conventions.keys())
  562. )
  563. )
  564. return False
  565. return True
  566. @classmethod
  567. def _has_exclusive_option(cls, options):
  568. """Return `True` iff one or more exclusive options were selected."""
  569. return any(
  570. [
  571. getattr(options, opt) is not None
  572. for opt in cls.BASE_ERROR_SELECTION_OPTIONS
  573. ]
  574. )
  575. @classmethod
  576. def _fix_set_options(cls, options):
  577. """Alter the set options from None/strings to sets in place."""
  578. optional_set_options = ('ignore', 'select')
  579. mandatory_set_options = ('add_ignore', 'add_select')
  580. def _get_set(value_str):
  581. """Split `value_str` by the delimiter `,` and return a set.
  582. Removes empty values ('') and strips whitespace.
  583. Also expands error code prefixes, to avoid doing this for every
  584. file.
  585. """
  586. if isinstance(value_str, str):
  587. value_str = value_str.split(",")
  588. return cls._expand_error_codes(
  589. {x.strip() for x in value_str} - {""}
  590. )
  591. for opt in optional_set_options:
  592. value = getattr(options, opt)
  593. if value is not None:
  594. setattr(options, opt, _get_set(value))
  595. for opt in mandatory_set_options:
  596. value = getattr(options, opt)
  597. if value is None:
  598. value = ''
  599. if not isinstance(value, Set):
  600. value = _get_set(value)
  601. setattr(options, opt, value)
  602. return options
  603. @classmethod
  604. def _create_option_parser(cls):
  605. """Return an option parser to parse the command line arguments."""
  606. from optparse import OptionGroup, OptionParser
  607. parser = OptionParser(
  608. version=__version__,
  609. usage='Usage: pydocstyle [options] [<file|dir>...]',
  610. )
  611. option = parser.add_option
  612. # Run configuration options
  613. option(
  614. '-e',
  615. '--explain',
  616. action='store_true',
  617. default=False,
  618. help='show explanation of each error',
  619. )
  620. option(
  621. '-s',
  622. '--source',
  623. action='store_true',
  624. default=False,
  625. help='show source for each error',
  626. )
  627. option(
  628. '-d',
  629. '--debug',
  630. action='store_true',
  631. default=False,
  632. help='print debug information',
  633. )
  634. option(
  635. '-v',
  636. '--verbose',
  637. action='store_true',
  638. default=False,
  639. help='print status information',
  640. )
  641. option(
  642. '--count',
  643. action='store_true',
  644. default=False,
  645. help='print total number of errors to stdout',
  646. )
  647. option(
  648. '--config',
  649. metavar='<path>',
  650. default=None,
  651. help='use given config file and disable config discovery',
  652. )
  653. parser.add_option_group(
  654. OptionGroup(
  655. parser,
  656. 'Note',
  657. 'When using --match, --match-dir or --ignore-decorators consider '
  658. 'whether you should use a single quote (\') or a double quote ("), '
  659. 'depending on your OS, Shell, etc.',
  660. )
  661. )
  662. check_group = OptionGroup(
  663. parser,
  664. 'Error Check Options',
  665. 'Only one of --select, --ignore or --convention can be '
  666. 'specified. If none is specified, defaults to '
  667. '`--convention=pep257`. These three options select the "basic '
  668. 'list" of error codes to check. If you wish to change that list '
  669. '(for example, if you selected a known convention but wish to '
  670. 'ignore a specific error from it or add a new one) you can '
  671. 'use `--add-[ignore/select]` in order to do so.',
  672. )
  673. add_check = check_group.add_option
  674. # Error check options
  675. add_check(
  676. '--select',
  677. metavar='<codes>',
  678. default=None,
  679. help='choose the basic list of checked errors by '
  680. 'specifying which errors to check for (with a list of '
  681. 'comma-separated error codes or prefixes). '
  682. 'for example: --select=D101,D2',
  683. )
  684. add_check(
  685. '--ignore',
  686. metavar='<codes>',
  687. default=None,
  688. help='choose the basic list of checked errors by '
  689. 'specifying which errors to ignore out of all of the '
  690. 'available error codes (with a list of '
  691. 'comma-separated error codes or prefixes). '
  692. 'for example: --ignore=D101,D2',
  693. )
  694. add_check(
  695. '--convention',
  696. metavar='<name>',
  697. default=None,
  698. help='choose the basic list of checked errors by specifying '
  699. 'an existing convention. Possible conventions: {}.'.format(
  700. ', '.join(conventions)
  701. ),
  702. )
  703. add_check(
  704. '--add-select',
  705. metavar='<codes>',
  706. default=None,
  707. help='add extra error codes to check to the basic list of '
  708. 'errors previously set by --select, --ignore or '
  709. '--convention.',
  710. )
  711. add_check(
  712. '--add-ignore',
  713. metavar='<codes>',
  714. default=None,
  715. help='ignore extra error codes by removing them from the '
  716. 'basic list previously set by --select, --ignore '
  717. 'or --convention.',
  718. )
  719. add_check(
  720. '--ignore-self-only-init',
  721. default=None,
  722. action='store_true',
  723. help='ignore __init__ methods which only have a self param.',
  724. )
  725. parser.add_option_group(check_group)
  726. # Match clauses
  727. option(
  728. '--match',
  729. metavar='<pattern>',
  730. default=None,
  731. help=(
  732. "check only files that exactly match <pattern> regular "
  733. "expression; default is --match='{}' which matches "
  734. "files that don't start with 'test_' but end with "
  735. "'.py'"
  736. ).format(cls.DEFAULT_MATCH_RE),
  737. )
  738. option(
  739. '--match-dir',
  740. metavar='<pattern>',
  741. default=None,
  742. help=(
  743. "search only dirs that exactly match <pattern> regular "
  744. "expression; default is --match-dir='{}', which "
  745. "matches all dirs that don't start with "
  746. "a dot"
  747. ).format(cls.DEFAULT_MATCH_DIR_RE),
  748. )
  749. # Decorators
  750. option(
  751. '--ignore-decorators',
  752. metavar='<decorators>',
  753. default=None,
  754. help=(
  755. "ignore any functions or methods that are decorated "
  756. "by a function with a name fitting the <decorators> "
  757. "regular expression; default is --ignore-decorators='{}'"
  758. " which does not ignore any decorated functions.".format(
  759. cls.DEFAULT_IGNORE_DECORATORS_RE
  760. )
  761. ),
  762. )
  763. option(
  764. '--property-decorators',
  765. metavar='<property-decorators>',
  766. default=None,
  767. help=(
  768. "consider any method decorated with one of these "
  769. "decorators as a property, and consequently allow "
  770. "a docstring which is not in imperative mood; default "
  771. "is --property-decorators='{}'".format(
  772. cls.DEFAULT_PROPERTY_DECORATORS
  773. )
  774. ),
  775. )
  776. return parser
  777. # Check configuration - used by the ConfigurationParser class.
  778. CheckConfiguration = namedtuple(
  779. 'CheckConfiguration',
  780. (
  781. 'checked_codes',
  782. 'match',
  783. 'match_dir',
  784. 'ignore_decorators',
  785. 'property_decorators',
  786. 'ignore_self_only_init',
  787. ),
  788. )
  789. class IllegalConfiguration(Exception):
  790. """An exception for illegal configurations."""
  791. pass
  792. # General configurations for pydocstyle run.
  793. RunConfiguration = namedtuple(
  794. 'RunConfiguration',
  795. ('explain', 'source', 'debug', 'verbose', 'count', 'config'),
  796. )