METADATA 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. Metadata-Version: 2.1
  2. Name: ConfigUpdater
  3. Version: 3.2
  4. Summary: Parser like ConfigParser but for updating configuration files
  5. Home-page: https://github.com/pyscaffold/configupdater
  6. Author: Florian Wilhelm
  7. Author-email: florian.wilhelm@gmail.com
  8. License: MIT
  9. Project-URL: Documentation, https://configupdater.readthedocs.io/
  10. Project-URL: Source, https://github.com/pyscaffold/configupdater/
  11. Project-URL: Tracker, https://github.com/pyscaffold/configupdater/issues
  12. Project-URL: Changelog, https://configupdater.readthedocs.io/en/latest/changelog.html
  13. Project-URL: Download, https://pypi.org/project/configupdater/#files
  14. Project-URL: Conda-Forge, https://anaconda.org/conda-forge/configupdater
  15. Platform: any
  16. Classifier: Development Status :: 5 - Production/Stable
  17. Classifier: Programming Language :: Python :: 3
  18. Classifier: Programming Language :: Python :: 3 :: Only
  19. Classifier: Programming Language :: Python :: 3.6
  20. Classifier: Programming Language :: Python :: 3.7
  21. Classifier: Programming Language :: Python :: 3.8
  22. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  23. Classifier: Operating System :: OS Independent
  24. Classifier: License :: OSI Approved :: MIT License
  25. Classifier: Intended Audience :: Developers
  26. Classifier: Operating System :: POSIX :: Linux
  27. Classifier: Operating System :: Unix
  28. Classifier: Operating System :: MacOS
  29. Classifier: Operating System :: Microsoft :: Windows
  30. Requires-Python: >=3.6
  31. Description-Content-Type: text/x-rst; charset=UTF-8
  32. License-File: LICENSE.txt
  33. Requires-Dist: importlib-metadata ; python_version < "3.8"
  34. Provides-Extra: testing
  35. Requires-Dist: sphinx ; extra == 'testing'
  36. Requires-Dist: flake8 ; extra == 'testing'
  37. Requires-Dist: pytest ; extra == 'testing'
  38. Requires-Dist: pytest-cov ; extra == 'testing'
  39. Requires-Dist: pytest-xdist ; extra == 'testing'
  40. Requires-Dist: pytest-randomly ; extra == 'testing'
  41. .. image:: https://api.cirrus-ci.com/github/pyscaffold/configupdater.svg?branch=main
  42. :alt: Built Status
  43. :target: https://cirrus-ci.com/github/pyscaffold/configupdater
  44. .. image:: https://readthedocs.org/projects/pyscaffold/badge/?version=latest
  45. :alt: ReadTheDocs
  46. :target: https://configupdater.readthedocs.io/
  47. .. image:: https://img.shields.io/coveralls/github/pyscaffold/configupdater/main.svg
  48. :alt: Coveralls
  49. :target: https://coveralls.io/r/pyscaffold/configupdater
  50. .. image:: https://img.shields.io/pypi/v/configupdater.svg
  51. :alt: PyPI-Server
  52. :target: https://pypi.org/project/configupdater/
  53. .. image:: https://img.shields.io/conda/vn/conda-forge/configupdater.svg
  54. :alt: Conda-Forge
  55. :target: https://anaconda.org/conda-forge/configupdater
  56. .. image:: https://static.pepy.tech/badge/configupdater/month
  57. :alt: Monthly Downloads
  58. :target: https://pepy.tech/project/configupdater
  59. .. image:: https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=ff69b4
  60. :alt: Sponsor me
  61. :target: https://github.com/sponsors/FlorianWilhelm
  62. |
  63. .. image:: https://configupdater.readthedocs.io/en/latest/_images/banner-640x323.png
  64. :height: 323px
  65. :width: 640px
  66. :scale: 60 %
  67. :alt: Config Updater
  68. :align: center
  69. |
  70. The sole purpose of `ConfigUpdater`_ is to easily update an INI config file
  71. with no changes to the original file except the intended ones. This means
  72. comments, the ordering of sections and key/value-pairs as wells as their
  73. cases are kept as in the original file. Thus ConfigUpdater provides
  74. complementary functionality to Python's `ConfigParser`_ which is primarily
  75. meant for reading config files and writing *new* ones.
  76. Features
  77. ========
  78. The key differences to `ConfigParser`_ are:
  79. * minimal invasive changes in the update configuration file,
  80. * proper handling of comments,
  81. * only a single config file can be updated at a time,
  82. * the original case of sections and keys are kept,
  83. * control over the position of a new section/key
  84. Following features are **deliberately not** implemented:
  85. * interpolation of values,
  86. * propagation of parameters from the default section,
  87. * conversions of values,
  88. * passing key/value-pairs with ``default`` argument,
  89. * non-strict mode allowing duplicate sections and keys.
  90. Usage
  91. =====
  92. First install the package with either::
  93. pip install configupdater
  94. or::
  95. conda install -c conda-forge configupdater
  96. Now we can simply do::
  97. from configupdater import ConfigUpdater
  98. updater = ConfigUpdater()
  99. updater.read("setup.cfg")
  100. which would read the file ``setup.cfg`` that is found in many projects.
  101. To change the value of an existing key we can simply do::
  102. updater["metadata"]["author"].value = "Alan Turing"
  103. At any point we can print the current state of the configuration file with::
  104. print(updater)
  105. To update the read-in file just call ``updater.update_file()`` or ``updater.write(open('filename','w'))``
  106. to write the changed configuration file to another destination. Before actually writing,
  107. ConfigUpdater will automatically check that the updated configuration file is still valid by
  108. parsing it with the help of ConfigParser.
  109. Many of ConfigParser's methods still exists and it's best to look them up in the `module reference`_.
  110. Let's look at some examples.
  111. Adding and removing options
  112. ---------------------------
  113. Let's say we have the following configuration in a string::
  114. cfg = """
  115. [metadata]
  116. author = Ada Lovelace
  117. summary = The Analytical Engine
  118. """
  119. We can add an *license* option, i.e. a key/value pair, in the same way we would do with ConfigParser::
  120. updater = ConfigUpdater()
  121. updater.read_string(cfg)
  122. updater["metadata"]["license"] = "MIT"
  123. A simple ``print(updater)`` will give show you that the new option was appended to the end::
  124. [metadata]
  125. author = Ada Lovelace
  126. summary = The Analytical Engine
  127. license = MIT
  128. Since the license is really important to us let's say we want to add it before the ``summary``
  129. and even add a short comment before it::
  130. updater = ConfigUpdater()
  131. updater.read_string(cfg)
  132. (updater["metadata"]["summary"].add_before
  133. .comment("Ada would have loved MIT")
  134. .option("license", "MIT"))
  135. which would result in::
  136. [metadata]
  137. author = Ada Lovelace
  138. # Ada would have loved MIT
  139. license = MIT
  140. summary = Analytical Engine calculating the Bernoulli numbers
  141. Using ``add_after`` would give the same result and looks like::
  142. updater = ConfigUpdater()
  143. updater.read_string(cfg)
  144. (updater["metadata"]["author"].add_after
  145. .comment("Ada would have loved MIT")
  146. .option("license", "MIT"))
  147. Let's say we want to rename `summary` to the more common `description`::
  148. updater = ConfigUpdater()
  149. updater.read_string(cfg)
  150. updater["metadata"]["summary"].key = "description"
  151. If we wanted no summary at all, we could just do ``del updater["metadata"]["summary"]``.
  152. Adding and removing sections
  153. ----------------------------
  154. Adding and remove sections just works like adding and removing options but on a higher level.
  155. Sticking to our *Ada Lovelace* example, let's say we want to add a section ``options`` just
  156. before ``metadata`` with a comment and two new lines to separate it from ``metadata``::
  157. updater = ConfigUpdater()
  158. updater.read_string(cfg)
  159. (updater["metadata"].add_before
  160. .section("options")
  161. .comment("Some specific project options")
  162. .space(2))
  163. As expected, this results in::
  164. [options]
  165. # Some specific project options
  166. [metadata]
  167. author = Ada Lovelace
  168. summary = The Analytical Engine
  169. We could now fill the new section with options like we learnt before. If we wanted to rename
  170. an existing section we could do this with the help of the ``name`` attribute::
  171. updater["metadata"].name = "MetaData"
  172. Sometimes it might be useful to inject a new section not in a programmatic way but more declarative.
  173. Let's assume we have thus defined our new section in a multi-line string::
  174. sphinx_sect_str = """
  175. [build_sphinx]
  176. source_dir = docs
  177. build_dir = docs/_build
  178. """
  179. With the help of two ConfigUpdater objects we can easily inject this section into our example::
  180. sphinx = ConfigUpdater()
  181. sphinx.read_string(sphinx_sect_str)
  182. sphinx_sect = sphinx["build_sphinx"]
  183. updater = ConfigUpdater()
  184. updater.read_string(cfg)
  185. (updater["metadata"].add_after
  186. .space()
  187. .section(sphinx_sect.detach()))
  188. The ``detach`` method will remove the ``build_sphinx`` section from the first object
  189. and add it to the second object. This results in::
  190. [metadata]
  191. author = Ada Lovelace
  192. summary = The Analytical Engine
  193. [build_sphinx]
  194. source_dir = docs
  195. build_dir = docs/_build
  196. Alternatively, if you want to preserve ``build_sphinx`` in both
  197. ``ConfigUpdater`` objects (i.e., prevent it from being removed from the first
  198. while still adding a copy to the second), you call also rely on stdlib's
  199. ``copy.deepcopy`` function instead of ``detach``::
  200. from copy import deepcopy
  201. (updater["metadata"].add_after
  202. .space()
  203. .section(deepcopy(sphinx_sect)))
  204. This technique can be used for all objects inside ConfigUpdater: sections,
  205. options, comments and blank spaces.
  206. Shallow copies are discouraged in the context of ConfigUpdater because each
  207. configuration block keeps a reference to its container to allow easy document
  208. editing. When doing editions (such as adding or changing options and comments)
  209. based on a shallow copy, the results can be unreliable and unexpected.
  210. For more examples on how the API of ConfigUpdater works it's best to take a look into the
  211. `unit tests`_ and read the references.
  212. Notes
  213. =====
  214. ConfigUpdater is mainly developed for `PyScaffold`_.
  215. .. _ConfigParser: https://docs.python.org/3/library/configparser.html
  216. .. _ConfigUpdater: https://configupdater.readthedocs.io/
  217. .. _PyScaffold: https://pyscaffold.org/
  218. .. _module reference: https://configupdater.readthedocs.io/en/latest/api.html#configupdater.configupdater.ConfigUpdater
  219. .. _unit tests: https://github.com/pyscaffold/configupdater/blob/main/tests/test_configupdater.py