Skip to main content

manage user configuration for python packages

Project description

manage user configuration for python projects

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 new (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"
    general = Section(
        name=StringOption(
            doc="your name",
            default="unknown person"),
        age=IntegerOption(
            doc="your age",
            required=True))
    address = Section(
        street=StringOption(
            doc="street including house number",
            required=True),
        city=StringOption(required=True),
        required=False,
        doc="shipping address")

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 26, in <module>
    CONFIG = MyConfig()
  File "/git/user_config/user_config/user_config/__init__.py", line 541, in __init__
    self._elements[element].validate_data(self._data)
  File "/git/user_config/user_config/user_config/__init__.py", line 322, in validate_data
    self._elements[element].validate_data(self._data)
  File "/git/user_config/user_config/user_config/__init__.py", line 216, 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.

[address]
## shipping address
## OPTIONAL_SECTION

## REQUIRED
# city =
city =

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


[general]
## your age
## REQUIRED
# age =
age =

## your name
# name = unknown person

Planned features

  • support for multi file configuration

  • 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.0a4.tar.gz (406.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

user_config-1.0a4-py2.py3-none-any.whl (11.3 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: user_config-1.0a4.tar.gz
  • Upload date:
  • Size: 406.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for user_config-1.0a4.tar.gz
Algorithm Hash digest
SHA256 b4511526c7e251e6ddc1feaa337365b899d69e8dafc428298e5202a0f6dcdab9
MD5 2c783fd541f990060ce1dab0a1a2a6c2
BLAKE2b-256 ea66562cdc9bf13a8bf8fe9e1b68531195e07bcf4578ea23a03718de56dad794

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for user_config-1.0a4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6a7ac1d109ae2c10a3a7c873088f70d2885aa2b8336e4f50f0a030a8d5562e2c
MD5 d634fd130394c73a050286101970eb89
BLAKE2b-256 22bed5ea41a36415df64d86eb24160c97619275b6531c85465355ddbc5d8d6dc

See more details on using hashes here.

Supported by

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