Skip to main content

manage user configuration for python packages

Project description

user_config

Manage user configuration for python projects.

For easy and well-documented user-defined configuration.

Badges

Fallback order

  1. command line arguments

  2. user config files in ~/.config/<app>/config.<extension>

  3. global config files in /etc/xdg/<app>/config.<extension>

  4. default values

For directories on operating systems than linux, see: https://github.com/ActiveState/appdirs

Config format

Supported out of the box: ini

Other config formats can be supported via plug-ins.

Requirements

  • Linux, or Os X, or Windows (but not Windows Vista)

  • python 2.7 or newer (python 3.6 supported)

  • relatively new versions of setuptools and pip (version requirement to follow)

Examples

Simple configuration example

"""Usage example for user_config."""
from user_config import Config, Section, StringOption, IntegerOption

class MyConfig(Config):

    """This will be displayed in the configuration documentation."""

    application = "my_application"
    author = "me"

    class GeneralSection(Section):
        """General information."""
        name = StringOption(
            doc="your name",
            default="unknown person")
        age = IntegerOption(
            doc="your age",
            required=True)
    general = GeneralSection()
    class AddressSection(Section):
        """shipping address"""
        street = StringOption(
            doc="street including house number",
            required=True)
        city = StringOption(required=True)
    address = AddressSection(required=False)

if __name__ == "__main__":
    CONFIG = MyConfig()
    print("hello there, {}!".format(CONFIG.general.name))

Command line help text:

$ python examples/simple_example.py -h
usage: my_application [-h] [--generate-config] [--city CITY] [--street STREET]
                      [--age AGE] [--name NAME]

This will be displayed in the configuration documentation. Command line
arguments overwrite configuration found in:
/root/.config/my_application/config.cfg /etc/xdg/my_application/config.cfg

optional arguments:
  -h, --help         show this help message and exit
  --generate-config  print a complete configuration file with current settings
  --city CITY
  --street STREET    street including house number
  --age AGE          your age
  --name NAME        your name

Command line use with default value:

$ python examples/simple_example.py --age 211
hello there, unknown person!

Command line use without required value:

$ python examples/simple_example.py
Traceback (most recent call last):
  File "examples/simple_example.py", line 29, in <module>
    CONFIG = MyConfig()
  File "/git/user_config/user_config/user_config/__init__.py", line 622, in __init__
    self._elements[element].validate_data(self._data)
  File "/git/user_config/user_config/user_config/__init__.py", line 464, in validate_data
    self._elements[element].validate_data(self._data)
  File "/git/user_config/user_config/user_config/__init__.py", line 380, in validate_data
    self.element_name))
user_config.MissingData: no value was provided for required option age

Command line use:

$ python examples/simple_example.py --age 211 --name mystery_user
hello there, mystery_user!

Generate configuration file:

$ python examples/simple_example.py --generate-config
## This will be displayed in the configuration documentation.

[general]
## General information.

## your name
# name = unknown person
name = tamara

## your age
## REQUIRED
# age =
age =


[address]
## shipping address
## OPTIONAL_SECTION

## street including house number
## REQUIRED
# street =
street =

## REQUIRED
# city =
city =

Documentation

$ pip install -e ".[doc]"
$ python setup.py build_sphinx

Testing

  • pytest

  • pytest-cov

  • coverage

  • codacy-coverage

$ python -m pytest --cov=user_config --cov-report xml

Planned features

  • multi matching sections / wildcard sections

  • yaml config format

  • json config format

  • hook for overwriting config from database or other storage function

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

user_config-1.0a10.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

user_config-1.0a10-py2.py3-none-any.whl (13.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file user_config-1.0a10.tar.gz.

File metadata

File hashes

Hashes for user_config-1.0a10.tar.gz
Algorithm Hash digest
SHA256 fad55ef57d0970bdcb9601bd0b20736b6a78973acd55b9d4abda03631f6d2407
MD5 b46078929152464e567c46c9f6df2d33
BLAKE2b-256 7f6ef078de405c7a0a3e19f831e893f991fb4165a1700726af33cab9729c3998

See more details on using hashes here.

File details

Details for the file user_config-1.0a10-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for user_config-1.0a10-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 31e1219e0db112627bcf561676f05578ce168d96429f61891850261bef851234
MD5 afedc9eb47b4e1e41b97e2dad734c60a
BLAKE2b-256 2b881eb3baaf0ad10a1ce99f9be094228d3c097b289a13d22dc0e432e4d23768

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page