README.rst 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. .. image:: https://img.shields.io/pypi/v/configparser.svg
  2. :target: https://pypi.org/project/configparser
  3. .. image:: https://img.shields.io/pypi/pyversions/configparser.svg
  4. .. image:: https://img.shields.io/travis/jaraco/configparser/master.svg
  5. :target: https://travis-ci.org/jaraco/configparser
  6. .. image:: https://img.shields.io/badge/code%20style-black-000000.svg
  7. :target: https://github.com/ambv/black
  8. :alt: Code style: Black
  9. .. .. image:: https://img.shields.io/appveyor/ci/jaraco/configparser/master.svg
  10. .. :target: https://ci.appveyor.com/project/jaraco/configparser/branch/master
  11. .. image:: https://readthedocs.org/projects/configparser/badge/?version=latest
  12. :target: https://configparser.readthedocs.io/en/latest/?badge=latest
  13. .. image:: https://tidelift.com/badges/package/pypi/configparser
  14. :target: https://tidelift.com/subscription/pkg/pypi-configparser?utm_source=pypi-configparser&utm_medium=readme
  15. The ancient ``ConfigParser`` module available in the standard library 2.x has
  16. seen a major update in Python 3.2. This is a backport of those changes so that
  17. they can be used directly in Python 2.6 - 3.5.
  18. To use the ``configparser`` backport instead of the built-in version on both
  19. Python 2 and Python 3, simply import it explicitly as a backport::
  20. from backports import configparser
  21. If you'd like to use the backport on Python 2 and the built-in version on
  22. Python 3, use that invocation instead::
  23. import configparser
  24. For detailed documentation consult the vanilla version at
  25. http://docs.python.org/3/library/configparser.html.
  26. Why you'll love ``configparser``
  27. --------------------------------
  28. Whereas almost completely compatible with its older brother, ``configparser``
  29. sports a bunch of interesting new features:
  30. * full mapping protocol access (`more info
  31. <http://docs.python.org/3/library/configparser.html#mapping-protocol-access>`_)::
  32. >>> parser = ConfigParser()
  33. >>> parser.read_string("""
  34. [DEFAULT]
  35. location = upper left
  36. visible = yes
  37. editable = no
  38. color = blue
  39. [main]
  40. title = Main Menu
  41. color = green
  42. [options]
  43. title = Options
  44. """)
  45. >>> parser['main']['color']
  46. 'green'
  47. >>> parser['main']['editable']
  48. 'no'
  49. >>> section = parser['options']
  50. >>> section['title']
  51. 'Options'
  52. >>> section['title'] = 'Options (editable: %(editable)s)'
  53. >>> section['title']
  54. 'Options (editable: no)'
  55. * there's now one default ``ConfigParser`` class, which basically is the old
  56. ``SafeConfigParser`` with a bunch of tweaks which make it more predictable for
  57. users. Don't need interpolation? Simply use
  58. ``ConfigParser(interpolation=None)``, no need to use a distinct
  59. ``RawConfigParser`` anymore.
  60. * the parser is highly `customizable upon instantiation
  61. <http://docs.python.org/3/library/configparser.html#customizing-parser-behaviour>`__
  62. supporting things like changing option delimiters, comment characters, the
  63. name of the DEFAULT section, the interpolation syntax, etc.
  64. * you can easily create your own interpolation syntax but there are two powerful
  65. implementations built-in (`more info
  66. <http://docs.python.org/3/library/configparser.html#interpolation-of-values>`__):
  67. * the classic ``%(string-like)s`` syntax (called ``BasicInterpolation``)
  68. * a new ``${buildout:like}`` syntax (called ``ExtendedInterpolation``)
  69. * fallback values may be specified in getters (`more info
  70. <http://docs.python.org/3/library/configparser.html#fallback-values>`__)::
  71. >>> config.get('closet', 'monster',
  72. ... fallback='No such things as monsters')
  73. 'No such things as monsters'
  74. * ``ConfigParser`` objects can now read data directly `from strings
  75. <http://docs.python.org/3/library/configparser.html#configparser.ConfigParser.read_string>`__
  76. and `from dictionaries
  77. <http://docs.python.org/3/library/configparser.html#configparser.ConfigParser.read_dict>`__.
  78. That means importing configuration from JSON or specifying default values for
  79. the whole configuration (multiple sections) is now a single line of code. Same
  80. goes for copying data from another ``ConfigParser`` instance, thanks to its
  81. mapping protocol support.
  82. * many smaller tweaks, updates and fixes
  83. A few words about Unicode
  84. -------------------------
  85. ``configparser`` comes from Python 3 and as such it works well with Unicode.
  86. The library is generally cleaned up in terms of internal data storage and
  87. reading/writing files. There are a couple of incompatibilities with the old
  88. ``ConfigParser`` due to that. However, the work required to migrate is well
  89. worth it as it shows the issues that would likely come up during migration of
  90. your project to Python 3.
  91. The design assumes that Unicode strings are used whenever possible [1]_. That
  92. gives you the certainty that what's stored in a configuration object is text.
  93. Once your configuration is read, the rest of your application doesn't have to
  94. deal with encoding issues. All you have is text [2]_. The only two phases when
  95. you should explicitly state encoding is when you either read from an external
  96. source (e.g. a file) or write back.
  97. Versioning
  98. ----------
  99. This project uses `semver <https://semver.org/spec/v2.0.0.html>`_ to
  100. communicate the impact of various releases while periodically syncing
  101. with the upstream implementation in CPython.
  102. `The changelog <https://github.com/jaraco/configparser/blob/master/CHANGES.rst>`_
  103. serves as a reference indicating which versions incorporate
  104. which upstream functionality.
  105. Prior to the ``4.0.0`` release, `another scheme
  106. <https://github.com/jaraco/configparser/blob/3.8.1/README.rst#versioning>`_
  107. was used to associate the CPython and backports releases.
  108. Maintenance
  109. -----------
  110. This backport was originally authored by Łukasz Langa, the current vanilla
  111. ``configparser`` maintainer for CPython and is currently maintained by
  112. Jason R. Coombs:
  113. * `configparser repository <https://github.com/jaraco/configparser>`_
  114. * `configparser issue tracker <https://github.com/jaraco/configparser/issues>`_
  115. Security Contact
  116. ----------------
  117. To report a security vulnerability, please use the
  118. `Tidelift security contact <https://tidelift.com/security>`_.
  119. Tidelift will coordinate the fix and disclosure.
  120. Conversion Process
  121. ------------------
  122. This section is technical and should bother you only if you are wondering how
  123. this backport is produced. If the implementation details of this backport are
  124. not important for you, feel free to ignore the following content.
  125. ``configparser`` is converted using `python-future
  126. <http://python-future.org>`_. The project takes the following
  127. branching approach:
  128. * the ``3.x`` branch holds unchanged files synchronized from the upstream
  129. CPython repository. The synchronization is currently done by manually copying
  130. the required files and stating from which CPython changeset they come from.
  131. * the ``master`` branch holds a version of the ``3.x`` code with some tweaks
  132. that make it independent from libraries and constructions unavailable on 2.x.
  133. Code on this branch still *must* work on the corresponding Python 3.x but
  134. will also work on Python 2.6 and 2.7 (including PyPy). You can check this
  135. running the supplied unit tests with ``tox``.
  136. The process works like this:
  137. 1. In the ``3.x`` branch, run ``pip-run -- sync-upstream.py``, which
  138. downloads the latest stable release of Python and copies the relevant
  139. files from there into their new locations here and then commits those
  140. changes with a nice reference to the relevant upstream commit hash.
  141. 2. I check for new names in ``__all__`` and update imports in
  142. ``configparser.py`` accordingly. I run the tests on Python 3. Commit.
  143. 3. I merge the new commit to ``master``. I run ``tox``. Commit.
  144. 4. If there are necessary changes, I do them now (on ``master``). Note that
  145. the changes should be written in the syntax subset supported by Python
  146. 2.6.
  147. 5. I run ``tox``. If it works, I update the docs and release the new version.
  148. Otherwise, I go back to point 3. I might use ``pasteurize`` to suggest me
  149. required changes but usually I do them manually to keep resulting code in
  150. a nicer form.
  151. Footnotes
  152. ---------
  153. .. [1] To somewhat ease migration, passing bytestrings is still supported but
  154. they are converted to Unicode for internal storage anyway. This means
  155. that for the vast majority of strings used in configuration files, it
  156. won't matter if you pass them as bytestrings or Unicode. However, if you
  157. pass a bytestring that cannot be converted to Unicode using the naive
  158. ASCII codec, a ``UnicodeDecodeError`` will be raised. This is purposeful
  159. and helps you manage proper encoding for all content you store in
  160. memory, read from various sources and write back.
  161. .. [2] Life gets much easier when you understand that you basically manage
  162. **text** in your application. You don't care about bytes but about
  163. letters. In that regard the concept of content encoding is meaningless.
  164. The only time when you deal with raw bytes is when you write the data to
  165. a file. Then you have to specify how your text should be encoded. On
  166. the other end, to get meaningful text from a file, the application
  167. reading it has to know which encoding was used during its creation. But
  168. once the bytes are read and properly decoded, all you have is text. This
  169. is especially powerful when you start interacting with multiple data
  170. sources. Even if each of them uses a different encoding, inside your
  171. application data is held in abstract text form. You can program your
  172. business logic without worrying about which data came from which source.
  173. You can freely exchange the data you store between sources. Only
  174. reading/writing files requires encoding your text to bytes.