From 8236d117da1d7c790a1ae60484e8d1b028815c4a Mon Sep 17 00:00:00 2001 From: Stefan Appelhoff Date: Sun, 23 Oct 2022 09:47:06 +0200 Subject: [PATCH 1/4] doc: clarify config reading --- README.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.rst b/README.rst index fb5702e262..a7cbed848a 100644 --- a/README.rst +++ b/README.rst @@ -117,6 +117,17 @@ These are both equivalent to running:: codespell --quiet-level 3 --count --skip "*.po,*.ts,./src/3rdParty,./src/Test" +If several config files are present, they are read in the following order: + +#. ``pyproject.toml`` +#. ``setup.cfg`` +#. ``.codespellrc`` +#. any additional file supplied via ``--config`` + +If a codespell configuration is supplied in several of these files, +the configuration from the most recently read file overwrites previously +specified configurations. + Any options specified in the command line will *override* options from the config files. From ec38cc5561f91a542c30865badecfde3a44f15c0 Mon Sep 17 00:00:00 2001 From: Stefan Appelhoff Date: Sun, 23 Oct 2022 10:01:06 +0200 Subject: [PATCH 2/4] enh: print considered config files --- codespell_lib/_codespell.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index df55894980..002d0d016c 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -423,8 +423,21 @@ def parse_options(args): with open(toml_file, 'rb') as f: data = tomli.load(f).get('tool', {}) config.read_dict(data) - config.read(cfg_files) + # Report which config files are going to be used + used_cfg_files = [] + for cfg_file in cfg_files: + _cfg = configparser.ConfigParser() + _cfg.read(cfg_file) + if _cfg.has_section('codespell'): + used_cfg_files.append(cfg_file) + if len(used_cfg_files) > 0: + print('Used config files:\n') + for ifile, cfg_file in enumerate(used_cfg_files): + print(' %i : %s' % (ifile, cfg_file)) + + # Use config files + config.read(cfg_files) if config.has_section('codespell'): # Build a "fake" argv list using option name and value. cfg_args = [] From 2381c2b100a0d3dfde0455334228d5256f488244 Mon Sep 17 00:00:00 2001 From: Stefan Appelhoff Date: Mon, 24 Oct 2022 13:53:04 +0200 Subject: [PATCH 3/4] doc: clarify tomli --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index a7cbed848a..8ad961d800 100644 --- a/README.rst +++ b/README.rst @@ -119,7 +119,7 @@ These are both equivalent to running:: If several config files are present, they are read in the following order: -#. ``pyproject.toml`` +#. ``pyproject.toml`` (only if the ``tomli`` library is available) #. ``setup.cfg`` #. ``.codespellrc`` #. any additional file supplied via ``--config`` From 9c34d92cead0e76c47ec53a0214b4e11686c1f02 Mon Sep 17 00:00:00 2001 From: Stefan Appelhoff Date: Tue, 25 Oct 2022 10:40:10 +0200 Subject: [PATCH 4/4] only collect in parse, report in main --- codespell_lib/_codespell.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 002d0d016c..e461bb2d3a 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -424,17 +424,13 @@ def parse_options(args): data = tomli.load(f).get('tool', {}) config.read_dict(data) - # Report which config files are going to be used + # Collect which config files are going to be used used_cfg_files = [] for cfg_file in cfg_files: _cfg = configparser.ConfigParser() _cfg.read(cfg_file) if _cfg.has_section('codespell'): used_cfg_files.append(cfg_file) - if len(used_cfg_files) > 0: - print('Used config files:\n') - for ifile, cfg_file in enumerate(used_cfg_files): - print(' %i : %s' % (ifile, cfg_file)) # Use config files config.read(cfg_files) @@ -458,7 +454,7 @@ def parse_options(args): if not options.files: options.files.append('.') - return options, parser + return options, parser, used_cfg_files def parse_ignore_words_option(ignore_words_option): @@ -789,7 +785,13 @@ def _script_main(): def main(*args): """Contains flow control""" - options, parser = parse_options(args) + options, parser, used_cfg_files = parse_options(args) + + # Report used config files + if len(used_cfg_files) > 0: + print('Used config files:') + for ifile, cfg_file in enumerate(used_cfg_files, start=1): + print(' %i: %s' % (ifile, cfg_file)) if options.regex and options.write_changes: print("ERROR: --write-changes cannot be used together with "