localconfig


Namelocalconfig JSON
Version 1.1.4 PyPI version JSON
download
home_pagehttps://github.com/maxzheng/localconfig
SummaryA simplified interface to ConfigParser using dot notion with data type / comment support.
upload_time2024-04-05 01:59:40
maintainerNone
docs_urlNone
authorMax Zheng
requires_python>=3.6
licenseMIT
keywords configuration config configparser data type support
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            localconfig
===========

A simplified interface to `ConfigParser`_ using dot notion with data type / comment support.

Feature Summary
===============

* Simple access to config using dot notion and iterators
* Full compatibility with `ConfigParser`_ ini formats (as that is used as the backend)
* Data type support by intelligently guessing the data types based on value on read.
* Multiple config source input (read from string, file pointer, file, or list of them)
* Full comment support / retention on save
* Lazy reading of config sources for performance (only read when a config value is accessed)

.. _ConfigParser: https://docs.python.org/3/library/configparser.html

Quick Start Tutorial
====================

To install::

    pip install localconfig

Let's say we have a script named `program` with the following config in `~/.config/program`:

.. code-block:: ini

    [Web Server]
    # Server host
    host = 0.0.0.0

    # Server port
    port = 8080

    # Debug logging
    debug = off

To read the config, simply do:

.. code-block:: python

    from localconfig import config

    start_server(config.web_server.host, config.web_server.port, config.web_server.debug)

    # Or use get method:
    # start_server(config.get('Web Server', 'host'),
    #              config.get('Web Server', 'port'),
    #              config.get('web_server', 'debug'))  # Yes, 'web_server' also works here!
    #
    # Or if the config is in docstring, read from it:
    # config.read(__doc__)
    #
    # Or if the config file is elsewhere:
    # config.read('/etc/path/to/config.ini')  # Non-existing file is ignored
    #
    # Or read from a list of sources
    # config.read(['string config', file_path, file_pointer, io.StringIO('config')])
    #
    # Or create another instance for another config:
    # from localconfig import LocalConfig
    # config2 = LocalConfig('/etc/path/to/another/config.ini')

Configs are read in the order they are called using `config.read()`, but the config file passed to the `LocalConfig()`
constructor (defaults to `~/.config/$script_name`) will be read last before the first access to config values, which
allows us to read configs from various locations, like default configs from a string that is checked in with the code,
while allowing them to be overrriden from the config file that is passed to the constructor.

Now, let's do some inspection:

.. code-block:: python

    # Iterate over sections and their keys/values
    for section in config:
      print(section)                   # Web Server

      for key, value in config.items(section):
        print(key, value, type(value)) # host 0.0.0.0 <type 'str'>
                                       # port 8080 <type 'int'>
                                       # debug False <type 'bool'>

    sections = list(config)            # ['Web Server']

    # Iterate over keys/values
    for key, value in config.web_server:
      print(key, value, type(value))    # Same output as above config.items()

    items = list(config.web_server)    # [('host', '0.0.0.0'), ('port', 8080), ('debug', False)]
    items = dict(config.web_server)    # {'host': '0.0.0.0', 'port': 8080, 'debug': False}

    # Check if a section or key is set - any non-existing section or key defaults to None.
    if config.web_server or config.no_such_section:
      pass

    if config.web_server and (config.web_server.port or config.web_server.no_such_key):
      pass

To add a section and set a value:

.. code-block:: python

    config.add_section('App Server', comment='Settings for application server')
    config.app_server.host = 'localhost'

    # Use `set` if you want to set a comment
    config.set('App Server', 'port', 9090, comment='App server port')

    # Set value for the DEFAULT section (default value for all other sections)
    config.env = 'prod'

To write the config:

.. code-block:: python

    config.save()

    # Or simply get the config as a string:
    # config_str = str(config)
    #
    # Or save to a different location:
    # config.save('/path/to/save/to.ini')

If we open `~/.config/program` now, we would see::

    [DEFAULT]

    env = prod


    [Web Server]

    # Server host
    host = 0.0.0.0

    # Server port
    port = 8080

    # Debug logging
    debug = off


    # Settings for application server
    [App Server]

    host = localhost

    # App server port
    port = 9090

Supported Data Types
====================

Data type is guessed based on the value and converted on read.

The following types are supported:

======= ===========================================
Type    Example Value
======= ===========================================
int     1 0b1101 0o70 0xFF
float   2.0
bool    true false yes no on off (case insensitive)
None    none (case insensitive)
str     Any other value not matched by above
======= ===========================================

Remote Config
=============

Check out: https://pypi.python.org/pypi/remoteconfig

More
====

| Documentation: http://localconfig.readthedocs.org/
|
| PyPI Package: https://pypi.python.org/pypi/localconfig
| GitHub Source: https://github.com/maxzheng/localconfig
| Report Issues/Bugs: https://github.com/maxzheng/localconfig/issues
|
| Connect: https://www.linkedin.com/in/maxzheng
| Contact: maxzheng.os @t gmail.com

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/maxzheng/localconfig",
    "name": "localconfig",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "configuration config ConfigParser data type support",
    "author": "Max Zheng",
    "author_email": "maxzheng.os @t gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/50/1a/01037aab418fd7834b0604599eeed3ff5e0891b3692ae974164275e8d53b/localconfig-1.1.4.tar.gz",
    "platform": null,
    "description": "localconfig\n===========\n\nA simplified interface to `ConfigParser`_ using dot notion with data type / comment support.\n\nFeature Summary\n===============\n\n* Simple access to config using dot notion and iterators\n* Full compatibility with `ConfigParser`_ ini formats (as that is used as the backend)\n* Data type support by intelligently guessing the data types based on value on read.\n* Multiple config source input (read from string, file pointer, file, or list of them)\n* Full comment support / retention on save\n* Lazy reading of config sources for performance (only read when a config value is accessed)\n\n.. _ConfigParser: https://docs.python.org/3/library/configparser.html\n\nQuick Start Tutorial\n====================\n\nTo install::\n\n    pip install localconfig\n\nLet's say we have a script named `program` with the following config in `~/.config/program`:\n\n.. code-block:: ini\n\n    [Web Server]\n    # Server host\n    host = 0.0.0.0\n\n    # Server port\n    port = 8080\n\n    # Debug logging\n    debug = off\n\nTo read the config, simply do:\n\n.. code-block:: python\n\n    from localconfig import config\n\n    start_server(config.web_server.host, config.web_server.port, config.web_server.debug)\n\n    # Or use get method:\n    # start_server(config.get('Web Server', 'host'),\n    #              config.get('Web Server', 'port'),\n    #              config.get('web_server', 'debug'))  # Yes, 'web_server' also works here!\n    #\n    # Or if the config is in docstring, read from it:\n    # config.read(__doc__)\n    #\n    # Or if the config file is elsewhere:\n    # config.read('/etc/path/to/config.ini')  # Non-existing file is ignored\n    #\n    # Or read from a list of sources\n    # config.read(['string config', file_path, file_pointer, io.StringIO('config')])\n    #\n    # Or create another instance for another config:\n    # from localconfig import LocalConfig\n    # config2 = LocalConfig('/etc/path/to/another/config.ini')\n\nConfigs are read in the order they are called using `config.read()`, but the config file passed to the `LocalConfig()`\nconstructor (defaults to `~/.config/$script_name`) will be read last before the first access to config values, which\nallows us to read configs from various locations, like default configs from a string that is checked in with the code,\nwhile allowing them to be overrriden from the config file that is passed to the constructor.\n\nNow, let's do some inspection:\n\n.. code-block:: python\n\n    # Iterate over sections and their keys/values\n    for section in config:\n      print(section)                   # Web Server\n\n      for key, value in config.items(section):\n        print(key, value, type(value)) # host 0.0.0.0 <type 'str'>\n                                       # port 8080 <type 'int'>\n                                       # debug False <type 'bool'>\n\n    sections = list(config)            # ['Web Server']\n\n    # Iterate over keys/values\n    for key, value in config.web_server:\n      print(key, value, type(value))    # Same output as above config.items()\n\n    items = list(config.web_server)    # [('host', '0.0.0.0'), ('port', 8080), ('debug', False)]\n    items = dict(config.web_server)    # {'host': '0.0.0.0', 'port': 8080, 'debug': False}\n\n    # Check if a section or key is set - any non-existing section or key defaults to None.\n    if config.web_server or config.no_such_section:\n      pass\n\n    if config.web_server and (config.web_server.port or config.web_server.no_such_key):\n      pass\n\nTo add a section and set a value:\n\n.. code-block:: python\n\n    config.add_section('App Server', comment='Settings for application server')\n    config.app_server.host = 'localhost'\n\n    # Use `set` if you want to set a comment\n    config.set('App Server', 'port', 9090, comment='App server port')\n\n    # Set value for the DEFAULT section (default value for all other sections)\n    config.env = 'prod'\n\nTo write the config:\n\n.. code-block:: python\n\n    config.save()\n\n    # Or simply get the config as a string:\n    # config_str = str(config)\n    #\n    # Or save to a different location:\n    # config.save('/path/to/save/to.ini')\n\nIf we open `~/.config/program` now, we would see::\n\n    [DEFAULT]\n\n    env = prod\n\n\n    [Web Server]\n\n    # Server host\n    host = 0.0.0.0\n\n    # Server port\n    port = 8080\n\n    # Debug logging\n    debug = off\n\n\n    # Settings for application server\n    [App Server]\n\n    host = localhost\n\n    # App server port\n    port = 9090\n\nSupported Data Types\n====================\n\nData type is guessed based on the value and converted on read.\n\nThe following types are supported:\n\n======= ===========================================\nType    Example Value\n======= ===========================================\nint     1 0b1101 0o70 0xFF\nfloat   2.0\nbool    true false yes no on off (case insensitive)\nNone    none (case insensitive)\nstr     Any other value not matched by above\n======= ===========================================\n\nRemote Config\n=============\n\nCheck out: https://pypi.python.org/pypi/remoteconfig\n\nMore\n====\n\n| Documentation: http://localconfig.readthedocs.org/\n|\n| PyPI Package: https://pypi.python.org/pypi/localconfig\n| GitHub Source: https://github.com/maxzheng/localconfig\n| Report Issues/Bugs: https://github.com/maxzheng/localconfig/issues\n|\n| Connect: https://www.linkedin.com/in/maxzheng\n| Contact: maxzheng.os @t gmail.com\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A simplified interface to ConfigParser using dot notion with data type / comment support.",
    "version": "1.1.4",
    "project_urls": {
        "Homepage": "https://github.com/maxzheng/localconfig"
    },
    "split_keywords": [
        "configuration",
        "config",
        "configparser",
        "data",
        "type",
        "support"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7d15ed0b22bf270fc96ff2b4ab67ea3ea532f680182f6177094912a7f73550fc",
                "md5": "c725d3cdfe1a2ebea954818097e5e399",
                "sha256": "51ba3469c0b488f7fb3100a4092382d6cf7c4191ca7fc75abba7cb40239e133d"
            },
            "downloads": -1,
            "filename": "localconfig-1.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c725d3cdfe1a2ebea954818097e5e399",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 8589,
            "upload_time": "2024-04-05T01:59:38",
            "upload_time_iso_8601": "2024-04-05T01:59:38.593828Z",
            "url": "https://files.pythonhosted.org/packages/7d/15/ed0b22bf270fc96ff2b4ab67ea3ea532f680182f6177094912a7f73550fc/localconfig-1.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "501a01037aab418fd7834b0604599eeed3ff5e0891b3692ae974164275e8d53b",
                "md5": "6846ca557c928b6d0e2c4fe9ad383e30",
                "sha256": "b71464eca5ef77c722f56ad548df5efd079e3d96aa777f511d1a18114d7b7221"
            },
            "downloads": -1,
            "filename": "localconfig-1.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "6846ca557c928b6d0e2c4fe9ad383e30",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 19742,
            "upload_time": "2024-04-05T01:59:40",
            "upload_time_iso_8601": "2024-04-05T01:59:40.867630Z",
            "url": "https://files.pythonhosted.org/packages/50/1a/01037aab418fd7834b0604599eeed3ff5e0891b3692ae974164275e8d53b/localconfig-1.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-05 01:59:40",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "maxzheng",
    "github_project": "localconfig",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": false,
    "tox": true,
    "lcname": "localconfig"
}
        
Elapsed time: 0.34908s