Skip to main content

Easy dict-based script configuration with CLI support

Project description

GitlabCIPipeline GitlabCICoverage Appveyor Pypi Downloads

The main webpage for this project is: https://gitlab.kitware.com/utils/scriptconfig

The goal of scriptconfig is to make it easy to be able to define a CLI by simply defining a dictionary. Thie enables you to write simple configs and update from CLI, kwargs, and/or json.

The scriptconfig provides a simple way to make configurable scripts using a combination of config files, command line arguments, and simple Python keyword arguments. A script config object is defined by creating a subclass of Config with a default dict class attribute. An instance of a custom Config object will behave similar a dictionary, but with a few conveniences.

To get started lets consider some example usage:

>>> import scriptconfig as scfg
>>> # In its simplest incarnation, the config class specifies default values.
>>> # For each configuration parameter.
>>> class ExampleConfig(scfg.Config):
>>>     default = {
>>>         'num': 1,
>>>         'mode': 'bar',
>>>         'ignore': ['baz', 'biz'],
>>>     }
>>> # Creating an instance, starts using the defaults
>>> config = ExampleConfig()
>>> # Typically you will want to update default from a dict or file.  By
>>> # specifying cmdline=True you denote that it is ok for the contents of
>>> # `sys.argv` to override config values. Here we pass a dict to `load`.
>>> kwargs = {'num': 2}
>>> config.load(kwargs, cmdline=False)
>>> assert config['num'] == 2
>>> # The `load` method can also be passed a json/yaml file/path.
>>> config_fpath = '/tmp/foo'
>>> open(config_fpath, 'w').write('{"num": 3}')
>>> config.load(config_fpath, cmdline=False)
>>> assert config['num'] == 3
>>> # It is possbile to load only from CLI by setting cmdline=True
>>> # or by setting it to a custom sys.argv
>>> config.load(cmdline=['--num=4'])
>>> assert config['num'] == 4
>>> # Note that using `config.load(cmdline=True)` will just use the
>>> # contents of sys.argv

Notice in the above example the keys in your default dictionary are command line arguments and values are their defaults. You can augment default values by wrapping them in scriptconfig.Value objects to encapsulate information like help documentation or type information.

>>> import scriptconfig as scfg
>>> class ExampleConfig(scfg.Config):
>>>     default = {
>>>         'num': scfg.Value(1, help='a number'),
>>>         'mode': scfg.Value('bar', help='mode1 help'),
>>>         'mode2': scfg.Value('bar', type=str, help='mode2 help'),
>>>         'ignore': scfg.Value(['baz', 'biz'], help='list of ignore vals'),
>>>     }
>>> config = ExampleConfig()
>>> # smartcast can handle lists as long as there are no spaces
>>> config.load(cmdline=['--ignore=spam,eggs'])
>>> assert config['ignore'] == ['spam', 'eggs']
>>> # Note that the Value type can influence how data is parsed
>>> config.load(cmdline=['--mode=spam,eggs', '--mode2=spam,eggs'])

Features

  • Serializes to json

  • Dict-like interface. By default a Config object operates independent of config files or the command line.

  • Can create command line interfaces

    • Can directly create an independent argparse object

    • Can use special command line loading using self.load(cmdline=True). This extends the basic argparse interface with:

      • Can specify options as either --option value or --option=value

      • Default config options allow for “smartcasting” values like lists and paths

      • Automatically add --config, --dumps, and --dump CLI options when reading cmdline via load.

FAQ

Question: How do I override the default values for a scriptconfig object using json file?

Answer: This depends if you want to pass the path to that json file via the command line or if you have that file in memory already. There are ways to do either. In the first case you can pass --config=<path-to-your-file> (assuming you have set the cmdline=True keyword arg when creating your config object e.g.: config = MyConfig(cmdline=True). In the second case when you create an instance of the scriptconfig object pass the default=<your dict> when creating the object: e.g. config = MyConfig(default=json.load(open(fpath, 'r'))). But the special --config --dump and --dumps CLI arg is baked into script config to make this easier.

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

scriptconfig-0.5.6.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

scriptconfig-0.5.6-py2.py3-none-any.whl (15.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file scriptconfig-0.5.6.tar.gz.

File metadata

  • Download URL: scriptconfig-0.5.6.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

File hashes

Hashes for scriptconfig-0.5.6.tar.gz
Algorithm Hash digest
SHA256 30b60ba1e82a61d082d66121ab9ad7c99af3531dcb7f35218ecaa66ed31775f1
MD5 e4c8787354df997ee0c4a3bdb503160d
BLAKE2b-256 80e651d61f5ee1f7fde8000aea350608326471a7420f0fa8744dbe1e5d7189d7

See more details on using hashes here.

File details

Details for the file scriptconfig-0.5.6-py2.py3-none-any.whl.

File metadata

  • Download URL: scriptconfig-0.5.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

File hashes

Hashes for scriptconfig-0.5.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ed7b3817b3eb0a946ca2c168d2b64db863267220d82e9f3698daa74337ed29f0
MD5 99862dac10a14b34df526996c82e5537
BLAKE2b-256 c49dd0aaa15203d1e80dd18a7a35ec1de518e1bb82c48c42affde7b41f2ae977

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