libconf


Namelibconf JSON
Version 2.0.1 PyPI version JSON
download
home_pagehttps://github.com/Grk0/python-libconf
SummaryA pure-Python libconfig reader/writer with permissive license
upload_time2019-11-21 05:17:16
maintainer
docs_urlNone
authorChristian Aichinger
requires_python
licenseMIT
keywords libconfig configuration parser library
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            =======
libconf
=======

libconf is a pure-Python reader/writer for configuration files in `libconfig
format`_, which is often used in C/C++ projects. It's interface is similar to
the `json`_ module: the four main methods are ``load()``, ``loads()``,
``dump()``, and ``dumps()``.

Example usage::

    import io, libconf
    >>> with io.open('example.cfg') as f:
    ...     config = libconf.load(f)
    >>> config
    {'capabilities': {'can-do-arrays': [3, 'yes', True],
                      'can-do-lists': (True,
                                       14880,
                                       ('sublist',),
                                       {'subgroup': 'ok'})},
     'version': 7,
     'window': {'position': {'h': 600, 'w': 800, 'x': 375, 'y': 210},
                'title': 'libconfig example'}}

    >>> config['window']['title']
    'libconfig example'
    >>> config.window.title
    'libconfig example'

    >>> print(libconf.dumps({'size': [10, 15], 'flag': True}))
    flag = True;
    size =
    [
        10,
        15
    ];

The data can be accessed either via indexing (``['title']``) or via attribute
access ``.title``.

Character encoding and escape sequences
---------------------------------------

The recommended way to use libconf is with Unicode objects (``unicode`` on
Python2, ``str`` on Python3). Input strings or streams for ``load()`` and
``loads()`` should be Unicode, as should be all strings contained in data
structures passed to ``dump()`` and ``dumps()``.

In ``load()`` and ``loads()``, escape sequences (such as ``\n``, ``\r``,
``\t``, or ``\xNN``) are decoded. Hex escapes (``\xNN``) are mapped to Unicode
characters U+0000 through U+00FF. All other characters are passed though as-is.

In ``dump()`` and ``dumps()``, unprintable characters below U+0080 are escaped
as ``\n``, ``\r``, ``\t``, ``\f``, or ``\xNN`` sequences. Characters U+0080
and above are passed through as-is.


Writing libconfig files
-----------------------

Reading libconfig files is easy. Writing is made harder by two factors:

* libconfig's distinction between `int and int64`_: ``2`` vs. ``2L``
* libconfig's distinction between `lists`_ and `arrays`_, and
  the limitations on arrays

The first point concerns writing Python ``int`` values. Libconf dumps values
that fit within the C/C++ 32bit ``int`` range without an "L" suffix. For larger
values, an "L" suffix is automatically added. To force the addition of an "L"
suffix even for numbers within the 32 bit integer range, wrap the integer in a
``LibconfInt64`` class.

Examples::

    dumps({'value': 2})                # Returns "value = 2;"
    dumps({'value': 2**32})            # Returns "value = 4294967296L;"
    dumps({'value': LibconfInt64(2)})  # Explicit int64, returns "value = 2L;"

The second complication arises from distinction between `lists`_ and `arrays`_
in the libconfig language. Lists are enclosed by ``()`` parenthesis, and can
contain arbitrary values within them. Arrays are enclosed by ``[]`` brackets,
and have significant limitations: all values must be scalar (int, float, bool,
string) and must be of the same type.

Libconf uses the following convention:

* it maps libconfig ``()``-lists to Python tuples, which also use the ``()``
  syntax.
* it maps libconfig ``[]``-arrays to Python lists, which also use the ``[]``
  syntax.

This provides nice symmetry between the two languages, but has the drawback
that dumping Python lists inherits the limitations of libconfig's arrays.
To explicitly control whether lists or arrays are dumped, wrap the Python
list/tuple in a ``LibconfList`` or ``LibconfArray``.

Examples::

    # Libconfig lists (=Python tuples) can contain arbitrary complex types:
    dumps({'libconf_list': (1, True, {})})

    # Libconfig arrays (=Python lists) must contain scalars of the same type:
    dumps({'libconf_array': [1, 2, 3]})

    # Equivalent, but more explit by using LibconfList/LibconfArray:
    dumps({'libconf_list': LibconfList([1, True, {}])})
    dumps({'libconf_array': LibconfArray([1, 2, 3])})


Comparison to other Python libconfig libraries
----------------------------------------------

`Pylibconfig2`_ is another pure-Python libconfig reader. It's API
is based on the C++ interface, instead of the Python `json`_ module.
It's licensed under GPLv3, which makes it unsuitable for use in a large number
of projects.

`Python-libconfig`_ is a library that provides Python bindings for the
libconfig++ C++ library. While permissively licensed (BSD), it requires a
compilation step upon installation, which can be a drawback.

I wrote libconf (this library) because both of the existing libraries didn't
fit my requirements. I had a work-related project which is not open source
(ruling out pylibconfig2) and I didn't want the deployment headache of
python-libconfig. Further, I enjoy writing parsers and this seemed like a nice
opportunity :-)

Release notes
-------------

* **2.0.1**, released on 2019-11-21

  - Allow trailing commas in lists and arrays for improved compatibility
    with the libconfig C implementation. Thanks to nibua-r for reporting
    this issue.

* **2.0.0**, released on 2018-11-23

  - Output validation for ``dump()`` and ``dumps()``: raise an exception when
    dumping data that can not be read by the C libconfig implementation.
    *This change may raise exceptions on code that worked with <2.0.0!*
  - Add ``LibconfList``, ``LibconfArray``, ``LibconfInt64`` classes for
    more fine-grained control of the ``dump()``/``dumps()`` output.
  - Fix ``deepcopy()`` of ``AttrDict`` classes (thanks AnandTella).

* **1.0.1**, released on 2017-01-06

  - Drastically improve performance when reading larger files
  - Several smaller improvements and fixes

* **1.0.0**, released on 2016-10-26:

  - Add the ability to write libconf files (``dump()`` and ``dumps()``,
    thanks clarkli86 and eatsan)
  - Several smaller improvements and fixes

* **0.9.2**, released on 2016-09-09:

  - Fix compatibility with Python versions older than 2.7.6 (thanks AnandTella)


.. _libconfig format: http://www.hyperrealm.com/libconfig/libconfig_manual.html#Configuration-Files
.. _json: https://docs.python.org/3/library/json.html
.. _lists: https://hyperrealm.github.io/libconfig/libconfig_manual.html#Lists
.. _arrays: https://hyperrealm.github.io/libconfig/libconfig_manual.html#Arrays
.. _int and int64: https://hyperrealm.github.io/libconfig/libconfig_manual.html#g_t64_002dbit-Integer-Values
.. _Pylibconfig2: https://github.com/heinzK1X/pylibconfig2
.. _Python-libconfig: https://github.com/cnangel/python-libconfig



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Grk0/python-libconf",
    "name": "libconf",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "libconfig configuration parser library",
    "author": "Christian Aichinger",
    "author_email": "Greek0@gmx.net",
    "download_url": "https://files.pythonhosted.org/packages/c8/41/592c24b42ec6e349629f2b6310ff31255b213bc696c0e4ad2ee1d2bbb68e/libconf-2.0.1.tar.gz",
    "platform": "",
    "description": "=======\nlibconf\n=======\n\nlibconf is a pure-Python reader/writer for configuration files in `libconfig\nformat`_, which is often used in C/C++ projects. It's interface is similar to\nthe `json`_ module: the four main methods are ``load()``, ``loads()``,\n``dump()``, and ``dumps()``.\n\nExample usage::\n\n    import io, libconf\n    >>> with io.open('example.cfg') as f:\n    ...     config = libconf.load(f)\n    >>> config\n    {'capabilities': {'can-do-arrays': [3, 'yes', True],\n                      'can-do-lists': (True,\n                                       14880,\n                                       ('sublist',),\n                                       {'subgroup': 'ok'})},\n     'version': 7,\n     'window': {'position': {'h': 600, 'w': 800, 'x': 375, 'y': 210},\n                'title': 'libconfig example'}}\n\n    >>> config['window']['title']\n    'libconfig example'\n    >>> config.window.title\n    'libconfig example'\n\n    >>> print(libconf.dumps({'size': [10, 15], 'flag': True}))\n    flag = True;\n    size =\n    [\n        10,\n        15\n    ];\n\nThe data can be accessed either via indexing (``['title']``) or via attribute\naccess ``.title``.\n\nCharacter encoding and escape sequences\n---------------------------------------\n\nThe recommended way to use libconf is with Unicode objects (``unicode`` on\nPython2, ``str`` on Python3). Input strings or streams for ``load()`` and\n``loads()`` should be Unicode, as should be all strings contained in data\nstructures passed to ``dump()`` and ``dumps()``.\n\nIn ``load()`` and ``loads()``, escape sequences (such as ``\\n``, ``\\r``,\n``\\t``, or ``\\xNN``) are decoded. Hex escapes (``\\xNN``) are mapped to Unicode\ncharacters U+0000 through U+00FF. All other characters are passed though as-is.\n\nIn ``dump()`` and ``dumps()``, unprintable characters below U+0080 are escaped\nas ``\\n``, ``\\r``, ``\\t``, ``\\f``, or ``\\xNN`` sequences. Characters U+0080\nand above are passed through as-is.\n\n\nWriting libconfig files\n-----------------------\n\nReading libconfig files is easy. Writing is made harder by two factors:\n\n* libconfig's distinction between `int and int64`_: ``2`` vs. ``2L``\n* libconfig's distinction between `lists`_ and `arrays`_, and\n  the limitations on arrays\n\nThe first point concerns writing Python ``int`` values. Libconf dumps values\nthat fit within the C/C++ 32bit ``int`` range without an \"L\" suffix. For larger\nvalues, an \"L\" suffix is automatically added. To force the addition of an \"L\"\nsuffix even for numbers within the 32 bit integer range, wrap the integer in a\n``LibconfInt64`` class.\n\nExamples::\n\n    dumps({'value': 2})                # Returns \"value = 2;\"\n    dumps({'value': 2**32})            # Returns \"value = 4294967296L;\"\n    dumps({'value': LibconfInt64(2)})  # Explicit int64, returns \"value = 2L;\"\n\nThe second complication arises from distinction between `lists`_ and `arrays`_\nin the libconfig language. Lists are enclosed by ``()`` parenthesis, and can\ncontain arbitrary values within them. Arrays are enclosed by ``[]`` brackets,\nand have significant limitations: all values must be scalar (int, float, bool,\nstring) and must be of the same type.\n\nLibconf uses the following convention:\n\n* it maps libconfig ``()``-lists to Python tuples, which also use the ``()``\n  syntax.\n* it maps libconfig ``[]``-arrays to Python lists, which also use the ``[]``\n  syntax.\n\nThis provides nice symmetry between the two languages, but has the drawback\nthat dumping Python lists inherits the limitations of libconfig's arrays.\nTo explicitly control whether lists or arrays are dumped, wrap the Python\nlist/tuple in a ``LibconfList`` or ``LibconfArray``.\n\nExamples::\n\n    # Libconfig lists (=Python tuples) can contain arbitrary complex types:\n    dumps({'libconf_list': (1, True, {})})\n\n    # Libconfig arrays (=Python lists) must contain scalars of the same type:\n    dumps({'libconf_array': [1, 2, 3]})\n\n    # Equivalent, but more explit by using LibconfList/LibconfArray:\n    dumps({'libconf_list': LibconfList([1, True, {}])})\n    dumps({'libconf_array': LibconfArray([1, 2, 3])})\n\n\nComparison to other Python libconfig libraries\n----------------------------------------------\n\n`Pylibconfig2`_ is another pure-Python libconfig reader. It's API\nis based on the C++ interface, instead of the Python `json`_ module.\nIt's licensed under GPLv3, which makes it unsuitable for use in a large number\nof projects.\n\n`Python-libconfig`_ is a library that provides Python bindings for the\nlibconfig++ C++ library. While permissively licensed (BSD), it requires a\ncompilation step upon installation, which can be a drawback.\n\nI wrote libconf (this library) because both of the existing libraries didn't\nfit my requirements. I had a work-related project which is not open source\n(ruling out pylibconfig2) and I didn't want the deployment headache of\npython-libconfig. Further, I enjoy writing parsers and this seemed like a nice\nopportunity :-)\n\nRelease notes\n-------------\n\n* **2.0.1**, released on 2019-11-21\n\n  - Allow trailing commas in lists and arrays for improved compatibility\n    with the libconfig C implementation. Thanks to nibua-r for reporting\n    this issue.\n\n* **2.0.0**, released on 2018-11-23\n\n  - Output validation for ``dump()`` and ``dumps()``: raise an exception when\n    dumping data that can not be read by the C libconfig implementation.\n    *This change may raise exceptions on code that worked with <2.0.0!*\n  - Add ``LibconfList``, ``LibconfArray``, ``LibconfInt64`` classes for\n    more fine-grained control of the ``dump()``/``dumps()`` output.\n  - Fix ``deepcopy()`` of ``AttrDict`` classes (thanks AnandTella).\n\n* **1.0.1**, released on 2017-01-06\n\n  - Drastically improve performance when reading larger files\n  - Several smaller improvements and fixes\n\n* **1.0.0**, released on 2016-10-26:\n\n  - Add the ability to write libconf files (``dump()`` and ``dumps()``,\n    thanks clarkli86 and eatsan)\n  - Several smaller improvements and fixes\n\n* **0.9.2**, released on 2016-09-09:\n\n  - Fix compatibility with Python versions older than 2.7.6 (thanks AnandTella)\n\n\n.. _libconfig format: http://www.hyperrealm.com/libconfig/libconfig_manual.html#Configuration-Files\n.. _json: https://docs.python.org/3/library/json.html\n.. _lists: https://hyperrealm.github.io/libconfig/libconfig_manual.html#Lists\n.. _arrays: https://hyperrealm.github.io/libconfig/libconfig_manual.html#Arrays\n.. _int and int64: https://hyperrealm.github.io/libconfig/libconfig_manual.html#g_t64_002dbit-Integer-Values\n.. _Pylibconfig2: https://github.com/heinzK1X/pylibconfig2\n.. _Python-libconfig: https://github.com/cnangel/python-libconfig\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A pure-Python libconfig reader/writer with permissive license",
    "version": "2.0.1",
    "project_urls": {
        "Download": "https://github.com/Grk0/python-libconf/tarball/2.0.1",
        "Homepage": "https://github.com/Grk0/python-libconf"
    },
    "split_keywords": [
        "libconfig",
        "configuration",
        "parser",
        "library"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0bc1f3345223779616ef4df444a5721123cc84bf45b5b56d6f6efba62bf83383",
                "md5": "a0af12be0481fe2191ab29d11f6280ea",
                "sha256": "b9e9bc79d0e22a9c6239a16cd8ecf198114395c4365b9fb62c39cffff0593ffc"
            },
            "downloads": -1,
            "filename": "libconf-2.0.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a0af12be0481fe2191ab29d11f6280ea",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 11339,
            "upload_time": "2019-11-21T05:17:14",
            "upload_time_iso_8601": "2019-11-21T05:17:14.439762Z",
            "url": "https://files.pythonhosted.org/packages/0b/c1/f3345223779616ef4df444a5721123cc84bf45b5b56d6f6efba62bf83383/libconf-2.0.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c841592c24b42ec6e349629f2b6310ff31255b213bc696c0e4ad2ee1d2bbb68e",
                "md5": "e212611cbf6a696e05742a983b3a0c57",
                "sha256": "2f907258953ba60a95a82d5633726b47c81f2d5cf8d8801b092579016d757f4a"
            },
            "downloads": -1,
            "filename": "libconf-2.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "e212611cbf6a696e05742a983b3a0c57",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 17900,
            "upload_time": "2019-11-21T05:17:16",
            "upload_time_iso_8601": "2019-11-21T05:17:16.290767Z",
            "url": "https://files.pythonhosted.org/packages/c8/41/592c24b42ec6e349629f2b6310ff31255b213bc696c0e4ad2ee1d2bbb68e/libconf-2.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2019-11-21 05:17:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Grk0",
    "github_project": "python-libconf",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "libconf"
}
        
Elapsed time: 0.23167s