ConfigUpdater 3.2b1__tar.gz → 4.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. configupdater-4.0.1/.gitignore +59 -0
  2. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/AUTHORS.rst +6 -1
  3. configupdater-4.0.1/CHANGELOG.rst +194 -0
  4. configupdater-4.0.1/CONTRIBUTING.rst +88 -0
  5. configupdater-4.0.1/PKG-INFO +349 -0
  6. configupdater-4.0.1/README.md +320 -0
  7. configupdater-4.0.1/docs/api.rst +8 -0
  8. configupdater-4.0.1/docs/conf.py +65 -0
  9. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/gfx/README.md +5 -5
  10. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/index.rst +15 -13
  11. configupdater-4.0.1/docs/usage.rst +388 -0
  12. configupdater-4.0.1/pyproject.toml +216 -0
  13. configupdater-4.0.1/src/configupdater/__init__.py +18 -0
  14. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/src/configupdater/block.py +59 -50
  15. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/src/configupdater/builder.py +33 -14
  16. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/src/configupdater/configupdater.py +67 -53
  17. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/src/configupdater/container.py +18 -21
  18. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/src/configupdater/document.py +105 -82
  19. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/src/configupdater/option.py +67 -71
  20. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/src/configupdater/parser.py +117 -107
  21. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/src/configupdater/section.py +127 -85
  22. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/tests/conftest.py +4 -7
  23. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/tests/test_configupdater.py +301 -35
  24. configupdater-4.0.1/tests/test_option.py +171 -0
  25. configupdater-4.0.1/tests/test_optionxform.py +270 -0
  26. configupdater-4.0.1/tests/test_parser.py +54 -0
  27. configupdater-4.0.1/tests/test_readme.py +26 -0
  28. configupdater-4.0.1/tests/test_section.py +241 -0
  29. configupdater-4.0.1/tools/release_notes.py +61 -0
  30. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/tools/stdlib_diff.py +14 -9
  31. ConfigUpdater-3.2b1/.cirrus.yml +0 -185
  32. ConfigUpdater-3.2b1/.coveragerc +0 -32
  33. ConfigUpdater-3.2b1/.github/FUNDING.yml +0 -12
  34. ConfigUpdater-3.2b1/.github/ISSUE_TEMPLATE/bug_report.md +0 -27
  35. ConfigUpdater-3.2b1/.github/ISSUE_TEMPLATE/feature_request.md +0 -11
  36. ConfigUpdater-3.2b1/.github/workflows/publish-package.yml +0 -32
  37. ConfigUpdater-3.2b1/.gitignore +0 -53
  38. ConfigUpdater-3.2b1/.isort.cfg +0 -3
  39. ConfigUpdater-3.2b1/.pre-commit-config.yaml +0 -51
  40. ConfigUpdater-3.2b1/.readthedocs.yml +0 -22
  41. ConfigUpdater-3.2b1/CHANGELOG.rst +0 -117
  42. ConfigUpdater-3.2b1/CONTRIBUTING.rst +0 -90
  43. ConfigUpdater-3.2b1/PKG-INFO +0 -281
  44. ConfigUpdater-3.2b1/README.rst +0 -247
  45. ConfigUpdater-3.2b1/docs/Makefile +0 -29
  46. ConfigUpdater-3.2b1/docs/conf.py +0 -295
  47. ConfigUpdater-3.2b1/docs/requirements.txt +0 -3
  48. ConfigUpdater-3.2b1/docs/usage.rst +0 -179
  49. ConfigUpdater-3.2b1/hatch.toml +0 -0
  50. ConfigUpdater-3.2b1/pyproject.toml +0 -8
  51. ConfigUpdater-3.2b1/setup.cfg +0 -110
  52. ConfigUpdater-3.2b1/setup.py +0 -21
  53. ConfigUpdater-3.2b1/src/ConfigUpdater.egg-info/PKG-INFO +0 -281
  54. ConfigUpdater-3.2b1/src/ConfigUpdater.egg-info/SOURCES.txt +0 -64
  55. ConfigUpdater-3.2b1/src/ConfigUpdater.egg-info/dependency_links.txt +0 -1
  56. ConfigUpdater-3.2b1/src/ConfigUpdater.egg-info/not-zip-safe +0 -1
  57. ConfigUpdater-3.2b1/src/ConfigUpdater.egg-info/requires.txt +0 -12
  58. ConfigUpdater-3.2b1/src/ConfigUpdater.egg-info/top_level.txt +0 -1
  59. ConfigUpdater-3.2b1/src/configupdater/__init__.py +0 -20
  60. ConfigUpdater-3.2b1/tests/test_option.py +0 -53
  61. ConfigUpdater-3.2b1/tests/test_optionxform.py +0 -100
  62. ConfigUpdater-3.2b1/tests/test_parser.py +0 -16
  63. ConfigUpdater-3.2b1/tests/test_section.py +0 -91
  64. ConfigUpdater-3.2b1/tox.ini +0 -101
  65. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/LICENSE.txt +0 -0
  66. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/_static/.gitignore +0 -0
  67. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/authors.rst +0 -0
  68. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/changelog.rst +0 -0
  69. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/contributing.rst +0 -0
  70. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/gfx/banner-1280x645.png +0 -0
  71. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/gfx/banner-640x323.png +0 -0
  72. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/gfx/configupdater.svg +0 -0
  73. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/gfx/configupdater_plain.svg +0 -0
  74. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/gfx/favicon.ico +0 -0
  75. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/gfx/icon-92x128.png +0 -0
  76. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/gfx/icon.svg +0 -0
  77. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/gfx/sidebar-254x128.png +0 -0
  78. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/docs/license.rst +0 -0
  79. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/src/configupdater/py.typed +0 -0
  80. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/tests/test_setup.cfg +0 -0
  81. {ConfigUpdater-3.2b1 → configupdater-4.0.1}/tools/__init__.py +0 -0
@@ -0,0 +1,59 @@
1
+ # Byte-compiled files and caches
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.so
5
+ .cache/
6
+ .mypy_cache/
7
+ .pytest_cache/
8
+ .ruff_cache/
9
+
10
+ # Virtual environments
11
+ .venv*/
12
+ .conda*/
13
+ .tox/
14
+ .python-version
15
+
16
+ # Build artifacts
17
+ build/
18
+ dist/
19
+ *.egg-info/
20
+ *.egg
21
+ .eggs/
22
+
23
+ # Coverage and test reports
24
+ .coverage
25
+ .coverage.*
26
+ htmlcov/
27
+ coverage.xml
28
+ coverage.lcov
29
+ junit*.xml
30
+
31
+ # Docs
32
+ docs/_build/
33
+ docs/api/
34
+ docs/_rst/
35
+ docs/html/
36
+ site/
37
+
38
+ # Editors and OS files
39
+ .idea/
40
+ .vscode/
41
+ .project
42
+ .pydevproject
43
+ .settings/
44
+ .ropeproject
45
+ tags
46
+ *~
47
+ .*.swp
48
+ .DS_Store
49
+ */.ipynb_checkpoints/
50
+
51
+ # Claude Code (local only)
52
+ .claude/worktrees/
53
+ .claude/settings.local.json
54
+
55
+ # Temporary files
56
+ *.orig
57
+ *.bak
58
+ *.log
59
+ *.pot
@@ -3,5 +3,10 @@ Contributors
3
3
  ============
4
4
 
5
5
  * Florian Wilhelm <florian.wilhelm@gmail.com>
6
- * Christian Theune <ct@flyingcircus.io>
7
6
  * Anderson Bravalheri <andersonbravalheri@gmail.com>
7
+ * Christian Theune <ct@flyingcircus.io>
8
+ * codeofwxz (GitHub)
9
+ * mika <211269698+mikamikasuki@users.noreply.github.com>
10
+ * Sinkleberg
11
+ * blouirts123 (GitHub)
12
+ * bysiber (GitHub)
@@ -0,0 +1,194 @@
1
+ =========
2
+ Changelog
3
+ =========
4
+
5
+ Version 4.0.1
6
+ =============
7
+
8
+ - Insert a newline before a newly added option when the file does not end
9
+ with one, so appending an option to the last section no longer glues the
10
+ option onto the last line and silently corrupts the previous value
11
+ (related to the new-section fix in PR #142 for issue #126), PR #178
12
+ - Fix spelling, grammar and factual errors in the documentation and docstrings,
13
+ e.g. the inverted description of ``empty_lines_in_values`` and the name of the
14
+ unsupported ``defaults`` argument of ConfigParser, PR #175
15
+
16
+ Version 4.0
17
+ ===========
18
+
19
+ - **Breaking:** Drop support for Python < 3.11; the minimum supported version is now
20
+ Python 3.11, PR #150
21
+ - Add support for Python 3.15, PR #160
22
+ - Simplify type hints using Python 3.11 features (``typing.Self``, ``X | Y`` unions)
23
+ and remove compatibility code for older Python versions, PR #150
24
+ - Add ``add_before_skip_comments`` to ``Section`` for inserting blocks before the
25
+ comments that directly precede a section, issue #117
26
+ - Fix ``add_section`` gluing the new section header to the last line when the file
27
+ does not end with a newline, issue #126
28
+ - Fix ``None`` being written as delimiter when assigning a value to an option that was
29
+ parsed without a value (``allow_no_value``), issue #141
30
+ - Port the option regexes of Python 3.15's ``configparser`` (CPython gh-146333). The
31
+ old ones backtracked quadratically, so a long line with many spaces could stall
32
+ the parser for minutes, PR #161
33
+ - Fix navigating, detaching or inserting at the wrong position when a container holds
34
+ several equal comment or blank-line blocks, issue #140
35
+ - ``del section[key]`` (and thus ``section.pop(key)``) now applies ``optionxform``
36
+ like ``section[key]`` does, PR #148
37
+ - Document that a custom ``optionxform`` must be idempotent, PR #152
38
+ - Build with hatchling and hatch-vcs, with all metadata in ``pyproject.toml``. The
39
+ wheel is now tagged ``py3-none-any`` (instead of ``py2.py3-none-any``), and the
40
+ license is declared as a PEP 639 expression. The ``testing`` extra is removed;
41
+ development dependencies are dependency groups, installed e.g. with ``uv sync``, PR #156
42
+ - Complete the type annotations of the public API, now checked with mypy in strict
43
+ mode, and fix the ``NoneValueDisallowed`` warning under ``python -OO``, PR #158
44
+ - Fix ``NotAttachedError``, ``AlreadyAttachedError`` and ``AssignMultilineValueError``
45
+ failing with an ``AttributeError`` under ``python -OO``, PR #163
46
+ - Correct the ``NoneValueDisallowed`` message. It claimed that an option without a
47
+ value is written with an empty value, but the option has been omitted since version 3.0.1,
48
+ PR #163
49
+ - ``add_before.option`` and ``add_after.option`` pass ``delimiter`` and
50
+ ``space_around_delimiters`` on to the new option instead of silently ignoring all
51
+ keyword arguments. Other keyword arguments now raise a ``TypeError``, PR #163
52
+ - **Breaking:** An explicit ``space_around_delimiters`` of an ``Option`` (also given
53
+ to ``add_before.option``, ``add_after.option`` or ``Section.create_option``) now
54
+ takes precedence over the syntax options of the ``ConfigUpdater``, so ``False``
55
+ writes ``key=value`` even with the default ``space_around_delimiters=True``.
56
+ ``Option`` defaults to ``space_around_delimiters=None``, i.e. it follows the
57
+ ``ConfigUpdater`` it belongs to: ``Option("key", "value")`` now writes
58
+ ``key=value`` into a ``ConfigUpdater(space_around_delimiters=False)`` instead of
59
+ ``key = value``. Options created with ``section[key] = value`` or ``set`` keep
60
+ following the ``ConfigUpdater`` they are copied to, PR #166
61
+ - **Breaking:** ``key in section``, ``section.has_option(key)`` and
62
+ ``section.get(key)`` now apply ``optionxform`` to ``key``, like ``section[key]``
63
+ and ConfigParser do. With the default ``str.lower``, ``"KEY" in section`` is now
64
+ ``True`` and ``section.get("KEY")`` returns the option instead of ``None``, PR #153
65
+ - **Breaking:** ``Section.set`` and ``ConfigUpdater.set`` keep the given spelling
66
+ when they create a new option, like ``section[key] = value`` does. For example,
67
+ ``updater.set("metadata", "Name", "x")`` now writes ``Name = x`` instead of
68
+ ``name = x``, PR #153
69
+ - ``ConfigUpdater.set``, ``get``, ``has_option`` and ``remove_option`` apply
70
+ ``optionxform`` only once instead of transforming already transformed keys again,
71
+ PR #153
72
+ - Fix wrong code examples in the usage guide and the README, which are now tested,
73
+ PR #164
74
+ - Fix ``update_file`` writing the file with the platform's default encoding instead
75
+ of the encoding it was read with. ``read`` and ``read_file`` remember the encoding
76
+ (``read_file`` takes it from ``f.encoding``), PR #165
77
+ - ``ConfigUpdater.read`` and ``Parser.read`` call ``io.text_encoding`` like
78
+ ConfigParser, so with ``-X warn_default_encoding`` the ``EncodingWarning`` points
79
+ to the caller (PEP 597), PR #165
80
+
81
+ Version 3.2
82
+ ===========
83
+
84
+ - Option ``prepend_newline`` in ``set_values`` to optionally avoid new lines, issue #104
85
+ - Fix square brackets in section names not being parsed, issue #122
86
+
87
+ Version 3.1.1
88
+ =============
89
+
90
+ - Preserve indentation of section when there are comments, issue #92
91
+
92
+ Version 3.1
93
+ ===========
94
+
95
+ - Prevent modifying multi-line values directly with ``value``, issue #87
96
+ - Added ``append`` method to ``Option`` for editing multi-line values
97
+ - Added ``as_list`` method to ``Option`` to handle multi-line values more easily
98
+
99
+ Version 3.0.1
100
+ =============
101
+
102
+ - Fix error when parsing unindented comments in multi-line values, issue #73
103
+ - Fix invalid string produced when ``allow_no_value = False``, issue #68
104
+
105
+ Version 3.0
106
+ ===========
107
+
108
+ - Added type hinting, issue #16
109
+ - Fix parsing error of indented comment lines, issue #25
110
+ - Allow handling of raw section comment, issue #25
111
+ - More unit testing of optionxform, issue #55
112
+ - Allow sections/options to be copied from one document to the other, issue #47
113
+ - New logo, issue #29
114
+ - Whole API was rechecked by @abravalheri and changed for consistency, issue #19
115
+
116
+
117
+ Version 2.0
118
+ ===========
119
+
120
+ - Changes in parser, i.e. comments in multi-line option values are kept
121
+ - Issue #14 is fixed
122
+ - Parameter ``empty_lines_in_values`` is now activated by default and can be changed
123
+ - Renamed ``sections_blocks`` to ``section_blocks`` for consistency
124
+ - Renamed ``last_item`` to ``last_block`` for consistency
125
+ - Added ``first_block``
126
+ - Reworked some internal parts of the inheritance hierarchy
127
+ - Added ``remove`` to remove the current block
128
+ - Added ``next_block`` and ``previous_block`` for easier navigation in section
129
+
130
+ Version 1.1.3
131
+ =============
132
+
133
+ - Added ``fallback`` argument to ``ConfigUpdater.get``, mirroring ConfigParser
134
+
135
+ Version 1.1.2
136
+ =============
137
+
138
+ - Fix wrongly modifying input in ``Option.set_value``, issue #11
139
+
140
+ Version 1.1.1
141
+ =============
142
+
143
+ - Fix iterating over the ``items()`` view of a section, issue #8
144
+
145
+ Version 1.1
146
+ ===========
147
+
148
+ - Validate format on write by default (can be deactivated)
149
+ - Fixed issue #7 with mixed-case options
150
+ - Fixed issue #7 with add_before/add_after problem
151
+ - Fixed issue #7 with wrong duplicate mixed-case entries
152
+ - Fixed issue #7 with duplicate options after add_after/before
153
+
154
+ Version 1.0.1
155
+ =============
156
+
157
+ - More sane error message if ``read_file`` is misused
158
+ - Also run unit tests on Windows
159
+
160
+ Version 1.0
161
+ ===========
162
+
163
+ - Fix: Use ``\n`` instead of ``os.linesep`` where appropriate
164
+
165
+ Version 0.3.2
166
+ =============
167
+
168
+ - Added GitHub and documentation links to the project's metadata
169
+
170
+ Version 0.3.1
171
+ =============
172
+
173
+ - Require Python >= 3.4 with ``python_requires``
174
+
175
+ Version 0.3
176
+ ===========
177
+
178
+ - Added an ``insert_at`` method at section level
179
+ - Some internal code simplifications
180
+
181
+ Version 0.2
182
+ ===========
183
+
184
+ - Added a ``to_dict()`` function
185
+
186
+ Version 0.1.1
187
+ =============
188
+
189
+ - Allow for flexible comment character in ``comment(...)``
190
+
191
+ Version 0.1
192
+ ===========
193
+
194
+ - First release
@@ -0,0 +1,88 @@
1
+ ============
2
+ Contributing
3
+ ============
4
+
5
+ ConfigUpdater is an open-source project and needs your help to improve.
6
+ If you experience bugs or other issues, please file an
7
+ issue report on our `issue tracker`_. If you also want to contribute code
8
+ or improve the documentation, it's best to create a Pull Request (PR) on
9
+ GitHub. Here is a short introduction to how it works.
10
+
11
+
12
+ Code Contributions
13
+ ==================
14
+
15
+ Submit an issue
16
+ ---------------
17
+
18
+ Before you work on any non-trivial code contribution, it's best to first create
19
+ an issue report to start a discussion on the subject. This often provides
20
+ additional considerations and avoids unnecessary work.
21
+
22
+ Create an environment
23
+ ---------------------
24
+
25
+ ConfigUpdater requires Python 3.11 or newer. The development environment is
26
+ managed with uv_, so install it first. uv also downloads a suitable Python
27
+ version if you don't have one.
28
+
29
+ Clone the repository
30
+ --------------------
31
+
32
+ #. `Create a GitHub account`_ if you do not already have one.
33
+ #. Fork the `project repository`_: click on the *Fork* button near the top of the
34
+ page. This creates a copy of the code under your account on the GitHub server.
35
+ #. Clone this copy to your local disk::
36
+
37
+ git clone git@github.com:YourLogin/configupdater.git
38
+
39
+ #. Create a virtual environment in ``.venv`` with ``configupdater`` and all
40
+ development tools (pytest, mypy, tox, pre-commit)::
41
+
42
+ uv sync
43
+
44
+ #. Install the ``pre-commit`` hooks::
45
+
46
+ uv run pre-commit install
47
+
48
+ The hooks automatically check the code you are writing on every commit.
49
+
50
+ #. Create a branch to hold your changes::
51
+
52
+ git checkout -b my-feature
53
+
54
+ and start making changes. Never work on the main branch!
55
+
56
+ #. Start your work on this branch. When you're done editing, do::
57
+
58
+ git add modified_files
59
+ git commit
60
+
61
+ to record your changes in Git, then push them to GitHub with::
62
+
63
+ git push -u origin my-feature
64
+
65
+ #. Please check that your changes don't break any unit tests with::
66
+
67
+ uv run tox
68
+
69
+ or, for a quick run without coverage, ``uv run pytest``.
70
+
71
+ Don't forget to add unit tests for a new feature, and a regression test for
72
+ a bugfix.
73
+
74
+ #. The ``pre-commit`` hooks check your code style with `ruff`_ on every commit.
75
+ You can also run them on all files with ``uv run tox -e lint``, and check the
76
+ type hints with ``uv run tox -e typecheck``.
77
+ #. Add yourself to the list of contributors in ``AUTHORS.rst``.
78
+ #. Go to the web page of your ConfigUpdater fork, and click
79
+ "Create pull request" to send your changes to the maintainers for review.
80
+ Find more detailed information on `creating a PR`_.
81
+
82
+
83
+ .. _project repository: https://github.com/pyscaffold/configupdater/
84
+ .. _uv: https://docs.astral.sh/uv/getting-started/installation/
85
+ .. _issue tracker: https://github.com/pyscaffold/configupdater/issues
86
+ .. _Create a GitHub account: https://github.com/join
87
+ .. _creating a PR: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork
88
+ .. _ruff: https://docs.astral.sh/ruff/
@@ -0,0 +1,349 @@
1
+ Metadata-Version: 2.5
2
+ Name: ConfigUpdater
3
+ Version: 4.0.1
4
+ Summary: Parser like ConfigParser but for updating configuration files
5
+ Project-URL: Homepage, https://github.com/pyscaffold/configupdater
6
+ Project-URL: Documentation, https://configupdater.readthedocs.io/
7
+ Project-URL: Source, https://github.com/pyscaffold/configupdater/
8
+ Project-URL: Issues, https://github.com/pyscaffold/configupdater/issues
9
+ Project-URL: Changelog, https://configupdater.readthedocs.io/en/latest/changelog.html
10
+ Project-URL: Download, https://pypi.org/project/configupdater/#files
11
+ Project-URL: Conda-Forge, https://anaconda.org/conda-forge/configupdater
12
+ Author-email: Florian Wilhelm <florian.wilhelm@gmail.com>, Anderson Bravalheri <andersonbravalheri@gmail.com>
13
+ License-Expression: MIT
14
+ License-File: LICENSE.txt
15
+ Classifier: Development Status :: 5 - Production/Stable
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3 :: Only
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Programming Language :: Python :: 3.14
24
+ Classifier: Programming Language :: Python :: 3.15
25
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
26
+ Classifier: Typing :: Typed
27
+ Requires-Python: >=3.11
28
+ Description-Content-Type: text/markdown
29
+
30
+ <div align="center">
31
+ <img src="https://raw.githubusercontent.com/pyscaffold/configupdater/main/docs/gfx/banner-640x323.png" alt="ConfigUpdater logo" width="500" role="img">
32
+ </div>
33
+ <br/>
34
+
35
+ | | |
36
+ |---------|------------------------------------|
37
+ | CI/CD | [![Tests][Tests-image]][Tests-link] [![Coverage][Coverage-image]][Coverage-link] [![Publish Package][Publish-image]][Publish-link] [![GitHub Sponsors][sponsor-image]][sponsor-link] |
38
+ | Package | [![PyPI - Version][PyPI_ver-image]][PyPI_ver-link] [![Conda - Version][Conda-image]][Conda-link] [![PyPI - Downloads][PyPI_down-image]][PyPI_down-link] [![PyPI - Python Version][PyPI_py-image]][PyPI_py-link] |
39
+ | Details | [![uv][uv-image]][uv-link] [![Linting - Ruff][ruff-image]][ruff-link] [![test - pytest][pytest-image]][pytest-link] [![Pre-Commit][precommit-image]][precommit-link] [![Types - Mypy][mypy-image]][mypy-link] [![License - MIT][MIT-image]][MIT-link] [![Docs - RTD][rtd-image]][rtd-link] |
40
+
41
+ The sole purpose of [ConfigUpdater] is to easily
42
+ update an INI config file with no changes to the original file except
43
+ the intended ones. This means comments, the ordering of sections and
44
+ key/value pairs as well as their case are kept as in the original
45
+ file. Thus ConfigUpdater provides complementary functionality to
46
+ Python's [ConfigParser], which is primarily meant for reading config
47
+ files and writing *new* ones.
48
+
49
+ # Features
50
+
51
+ The key differences to [ConfigParser] are:
52
+
53
+ - minimally invasive changes in the updated configuration file,
54
+ - proper handling of comments,
55
+ - only a single config file can be updated at a time,
56
+ - the original case of sections and keys is kept,
57
+ - control over the position of a new section/key.
58
+
59
+ The following features are **deliberately not** implemented:
60
+
61
+ - interpolation of values,
62
+ - propagation of parameters from the default section,
63
+ - conversion of values,
64
+ - passing key/value pairs with the `defaults` argument,
65
+ - non-strict mode allowing duplicate sections and keys.
66
+
67
+ # Usage
68
+
69
+ First install the package with either:
70
+
71
+ ```console
72
+ pip install configupdater
73
+ ```
74
+
75
+ or add it to a project managed with [uv]:
76
+
77
+ ```console
78
+ uv add configupdater
79
+ ```
80
+
81
+ or:
82
+
83
+ ```console
84
+ conda install -c conda-forge configupdater
85
+ ```
86
+
87
+ Now we can simply do:
88
+
89
+ ```python
90
+ from configupdater import ConfigUpdater
91
+
92
+ updater = ConfigUpdater()
93
+ updater.read("setup.cfg")
94
+ ```
95
+
96
+ which would read the file `setup.cfg` that is found in many projects.
97
+
98
+ To change the value of an existing key we can simply do:
99
+
100
+ ```python
101
+ updater["metadata"]["author"].value = "Alan Turing"
102
+ ```
103
+
104
+ At any point we can print the current state of the configuration file
105
+ with:
106
+
107
+ ```python
108
+ print(updater)
109
+ ```
110
+
111
+ To update the read-in file, with the encoding it was read with, just call
112
+ `updater.update_file()` or `updater.write(open('filename','w'))` to write
113
+ the changed configuration file to another destination. Before actually
114
+ writing, ConfigUpdater will automatically check that the updated
115
+ configuration file is still valid by parsing it with the help of ConfigParser.
116
+
117
+ Many of ConfigParser's methods still exist, and it's best to look them
118
+ up in the [module reference]. Let's look at some examples.
119
+
120
+ ## Adding and removing options
121
+
122
+ Let's say we have the following configuration in a string:
123
+
124
+ ```python
125
+ cfg = """
126
+ [metadata]
127
+ author = Ada Lovelace
128
+ summary = The Analytical Engine
129
+ """
130
+ ```
131
+
132
+ We can add a *license* option, i.e. a key/value pair, in the same way
133
+ we would do with ConfigParser:
134
+
135
+ ```python
136
+ updater = ConfigUpdater()
137
+ updater.read_string(cfg)
138
+ updater["metadata"]["license"] = "MIT"
139
+ ```
140
+
141
+ A simple `print(updater)` will show you that the new option was
142
+ appended to the end:
143
+
144
+ ```ini
145
+ [metadata]
146
+ author = Ada Lovelace
147
+ summary = The Analytical Engine
148
+ license = MIT
149
+ ```
150
+
151
+ Since the license is really important to us, let's say we want to add it
152
+ before the `summary` and even add a short comment before it:
153
+
154
+ ```python
155
+ updater = ConfigUpdater()
156
+ updater.read_string(cfg)
157
+ (
158
+ updater["metadata"]["summary"]
159
+ .add_before.comment("Ada would have loved MIT")
160
+ .option("license", "MIT")
161
+ )
162
+ ```
163
+
164
+ which would result in:
165
+
166
+ ```ini
167
+ [metadata]
168
+ author = Ada Lovelace
169
+ # Ada would have loved MIT
170
+ license = MIT
171
+ summary = The Analytical Engine
172
+ ```
173
+
174
+ Using `add_after` would give the same result and looks like:
175
+
176
+ ```python
177
+ updater = ConfigUpdater()
178
+ updater.read_string(cfg)
179
+ (
180
+ updater["metadata"]["author"]
181
+ .add_after.comment("Ada would have loved MIT")
182
+ .option("license", "MIT")
183
+ )
184
+ ```
185
+
186
+ Let's say we want to rename `summary` to the more common
187
+ `description`:
188
+
189
+ ```python
190
+ updater = ConfigUpdater()
191
+ updater.read_string(cfg)
192
+ updater["metadata"]["summary"].key = "description"
193
+ ```
194
+
195
+ If we wanted no summary at all, we could just do
196
+ `del updater["metadata"]["summary"]`.
197
+
198
+ ## Adding and removing sections
199
+
200
+ Adding and removing sections works just like adding and removing options,
201
+ but on a higher level. Sticking to our *Ada Lovelace* example, let's
202
+ say we want to add a section `options` just before `metadata` with a
203
+ comment and two blank lines to separate it from `metadata`:
204
+
205
+ ```python
206
+ updater = ConfigUpdater()
207
+ updater.read_string(cfg)
208
+ (
209
+ updater["metadata"]
210
+ .add_before.comment("Some specific project options")
211
+ .section("options")
212
+ .space(2)
213
+ )
214
+ ```
215
+
216
+ As expected, this results in:
217
+
218
+ ```ini
219
+ # Some specific project options
220
+ [options]
221
+
222
+
223
+ [metadata]
224
+ author = Ada Lovelace
225
+ summary = The Analytical Engine
226
+ ```
227
+
228
+ We could now fill the new section with options like we learnt before. If
229
+ we wanted to rename an existing section, we could do this with the help
230
+ of the `name` attribute:
231
+
232
+ ```python
233
+ updater["metadata"].name = "MetaData"
234
+ ```
235
+
236
+ Sometimes it might be useful to inject a new section not in a
237
+ programmatic way but more declaratively. Let's assume we have thus
238
+ defined our new section in a multi-line string:
239
+
240
+ ```python
241
+ sphinx_sect_str = """
242
+ [build_sphinx]
243
+ source_dir = docs
244
+ build_dir = docs/_build
245
+ """
246
+ ```
247
+
248
+ With the help of two ConfigUpdater objects we can easily inject this
249
+ section into our example:
250
+
251
+ ```python
252
+ sphinx = ConfigUpdater()
253
+ sphinx.read_string(sphinx_sect_str)
254
+ sphinx_sect = sphinx["build_sphinx"]
255
+
256
+ updater = ConfigUpdater()
257
+ updater.read_string(cfg)
258
+
259
+ (updater["metadata"].add_after.space().section(sphinx_sect.detach()))
260
+ ```
261
+
262
+ The `detach` method removes the `build_sphinx` section from the
263
+ first object, so that it can be added to the second one. This results in:
264
+
265
+ ```ini
266
+ [metadata]
267
+ author = Ada Lovelace
268
+ summary = The Analytical Engine
269
+
270
+ [build_sphinx]
271
+ source_dir = docs
272
+ build_dir = docs/_build
273
+ ```
274
+
275
+ Alternatively, if you want to preserve `build_sphinx` in both
276
+ `ConfigUpdater` objects (i.e., prevent it from being removed from the
277
+ first while still adding a copy to the second), you can also rely on
278
+ the stdlib's `copy.deepcopy` function instead of `detach`:
279
+
280
+ ```python
281
+ from copy import deepcopy
282
+
283
+ sphinx = ConfigUpdater()
284
+ sphinx.read_string(sphinx_sect_str)
285
+ sphinx_sect = sphinx["build_sphinx"]
286
+
287
+ updater = ConfigUpdater()
288
+ updater.read_string(cfg)
289
+
290
+ (updater["metadata"].add_after.space().section(deepcopy(sphinx_sect)))
291
+ ```
292
+
293
+ This technique can be used for all objects inside ConfigUpdater:
294
+ sections, options, comments and blank spaces.
295
+
296
+ Shallow copies are discouraged in the context of ConfigUpdater because
297
+ each configuration block keeps a reference to its container to allow
298
+ easy document editing. When making edits (such as adding or changing
299
+ options and comments) based on a shallow copy, the results can be
300
+ unreliable and unexpected.
301
+
302
+ For more examples of how the API of ConfigUpdater works, it's best to
303
+ take a look at the [unit tests] and read the [module reference].
304
+
305
+ # Notes
306
+
307
+ ConfigUpdater started out as a helper for [PyScaffold], which still uses
308
+ it to update the `setup.cfg` of existing projects. Since then it has
309
+ become a useful tool in its own right for everyone who needs to change
310
+ configuration files programmatically without losing the comments and
311
+ formatting that humans put into them.
312
+
313
+ [ConfigUpdater]: https://configupdater.readthedocs.io/
314
+ [ConfigParser]: https://docs.python.org/3/library/configparser.html
315
+ [PyScaffold]: https://pyscaffold.org/
316
+ [uv]: https://docs.astral.sh/uv/
317
+ [module reference]: https://configupdater.readthedocs.io/en/latest/api.html#configupdater.configupdater.ConfigUpdater
318
+ [unit tests]: https://github.com/pyscaffold/configupdater/blob/main/tests/test_configupdater.py
319
+
320
+ [Tests-image]: https://github.com/pyscaffold/configupdater/actions/workflows/ci.yml/badge.svg
321
+ [Tests-link]: https://github.com/pyscaffold/configupdater/actions/workflows/ci.yml
322
+ [Coverage-image]: https://img.shields.io/coveralls/github/pyscaffold/configupdater/main.svg
323
+ [Coverage-link]: https://coveralls.io/r/pyscaffold/configupdater
324
+ [Publish-image]: https://github.com/pyscaffold/configupdater/actions/workflows/publish-package.yml/badge.svg
325
+ [Publish-link]: https://github.com/pyscaffold/configupdater/actions/workflows/publish-package.yml
326
+ [PyPI_ver-image]:https://img.shields.io/pypi/v/configupdater.svg?logo=pypi&label=PyPI&logoColor=gold
327
+ [PyPI_ver-link]: https://pypi.org/project/configupdater/
328
+ [Conda-image]: https://img.shields.io/conda/vn/conda-forge/configupdater.svg?logo=Anaconda&label=Conda-Forge&logoColor=44A833
329
+ [Conda-link]: https://anaconda.org/conda-forge/configupdater/
330
+ [PyPI_down-image]: https://img.shields.io/pypi/dm/configupdater.svg?color=blue&label=Downloads&logo=pypi&logoColor=gold
331
+ [PyPI_down-link]: https://clickpy.clickhouse.com/dashboard/configupdater
332
+ [PyPI_py-image]: https://img.shields.io/pypi/pyversions/configupdater.svg?logo=python&label=Python&logoColor=gold
333
+ [PyPI_py-link]: https://pypi.org/project/configupdater/
334
+ [uv-image]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json
335
+ [uv-link]: https://github.com/astral-sh/uv
336
+ [ruff-image]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
337
+ [ruff-link]: https://github.com/astral-sh/ruff
338
+ [mypy-image]: https://img.shields.io/badge/Types-mypy-blue.svg
339
+ [mypy-link]: https://mypy-lang.org/
340
+ [pytest-image]: https://img.shields.io/static/v1?label=‎&message=Pytest&logo=Pytest&color=0A9EDC&logoColor=white
341
+ [pytest-link]: https://docs.pytest.org/
342
+ [rtd-image]: https://img.shields.io/readthedocs/configupdater/latest.svg?logo=readthedocs&logoColor=white&label=Docs
343
+ [rtd-link]: https://configupdater.readthedocs.io/
344
+ [precommit-image]: https://img.shields.io/static/v1?label=‎&message=pre-commit&logo=pre-commit&color=76877c
345
+ [precommit-link]: https://pre-commit.com/
346
+ [MIT-image]: https://img.shields.io/badge/License-MIT-9400d3.svg
347
+ [MIT-link]: LICENSE.txt
348
+ [sponsor-image]: https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=ff69b4
349
+ [sponsor-link]: https://github.com/sponsors/FlorianWilhelm