click_ is a framework to simplify writing composable commands for
command-line tools. This package extends the click_ functionality
by adding support for commands that use configuration files.
.. _click: https://click.pocoo.org/
EXAMPLE:
A configuration file, like:
.. code-block:: INI
# -- FILE: foo.ini
[foo]
flag = yes
name = Alice and Bob
numbers = 1 4 9 16 25
filenames = foo/xxx.txt
bar/baz/zzz.txt
[person.alice]
name = Alice
birthyear = 1995
[person.bob]
name = Bob
birthyear = 2001
can be processed with:
.. code-block:: python
# EXAMPLE:
# -- FILE: example_command_with_configfile.py (ALL PARTS: simplified)
from click_configfile import ConfigFileReader, Param, SectionSchema
from click_configfile import matches_section
import click
class ConfigSectionSchema(object):
"""Describes all config sections of this configuration file."""
@matches_section("foo")
class Foo(SectionSchema):
name = Param(type=str)
flag = Param(type=bool, default=True)
numbers = Param(type=int, multiple=True)
filenames = Param(type=click.Path(), multiple=True)
@matches_section("person.*") # Matches multiple sections
class Person(SectionSchema):
name = Param(type=str)
birthyear = Param(type=click.IntRange(1990, 2100))
class ConfigFileProcessor(ConfigFileReader):
config_files = ["foo.ini", "foo.cfg"]
config_section_schemas = [
ConfigSectionSchema.Foo, # PRIMARY SCHEMA
ConfigSectionSchema.Person,
]
# -- SIMPLIFIED STORAGE-SCHEMA:
# section:person.* -> storage:person.*
# section:person.alice -> storage:person.alice
# section:person.bob -> storage:person.bob
# -- ALTERNATIVES: Override ConfigFileReader methods:
# * process_config_section(config_section, storage)
# * get_storage_name_for(section_name)
# * get_storage_for(section_name, storage)
# -- COMMAND:
CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor.read_config())
@click.command(context_settings=CONTEXT_SETTINGS)
@click.option("-n", "--number", "numbers", type=int, multiple=True)
@click.pass_context
def command_with_config(ctx, numbers):
# -- ACCESS ADDITIONAL DATA FROM CONFIG FILES: Using ctx.default_map
for person_data_key in ctx.default_map.keys():
if not person_data_key.startswith("person."):
continue
person_data = ctx.default_map[person_data_key]
process_person_data(person_data) # as dict.
Raw data
{
"_id": null,
"home_page": "https://github.com/click-contrib/click-configfile",
"name": "click-configfile",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "click,configfile,configparser",
"author": "Jens Engel",
"author_email": "jenisys@noreply.github.com",
"download_url": "https://files.pythonhosted.org/packages/5a/2b/722c718db0f44e6927767aa422c73a77eaf333138e0a4201ca9b1f72aa2d/click-configfile-0.2.3.tar.gz",
"platform": "any",
"description": "click_ is a framework to simplify writing composable commands for\ncommand-line tools. This package extends the click_ functionality\nby adding support for commands that use configuration files.\n\n.. _click: https://click.pocoo.org/\n\nEXAMPLE:\n\nA configuration file, like:\n\n.. code-block:: INI\n\n # -- FILE: foo.ini\n [foo]\n flag = yes\n name = Alice and Bob\n numbers = 1 4 9 16 25\n filenames = foo/xxx.txt\n bar/baz/zzz.txt\n\n [person.alice]\n name = Alice\n birthyear = 1995\n\n [person.bob]\n name = Bob\n birthyear = 2001\n\ncan be processed with:\n\n.. code-block:: python\n\n # EXAMPLE:\n # -- FILE: example_command_with_configfile.py (ALL PARTS: simplified)\n from click_configfile import ConfigFileReader, Param, SectionSchema\n from click_configfile import matches_section\n import click\n\n class ConfigSectionSchema(object):\n \"\"\"Describes all config sections of this configuration file.\"\"\"\n\n @matches_section(\"foo\")\n class Foo(SectionSchema):\n name = Param(type=str)\n flag = Param(type=bool, default=True)\n numbers = Param(type=int, multiple=True)\n filenames = Param(type=click.Path(), multiple=True)\n\n @matches_section(\"person.*\") # Matches multiple sections\n class Person(SectionSchema):\n name = Param(type=str)\n birthyear = Param(type=click.IntRange(1990, 2100))\n\n\n class ConfigFileProcessor(ConfigFileReader):\n config_files = [\"foo.ini\", \"foo.cfg\"]\n config_section_schemas = [\n ConfigSectionSchema.Foo, # PRIMARY SCHEMA\n ConfigSectionSchema.Person,\n ]\n\n # -- SIMPLIFIED STORAGE-SCHEMA:\n # section:person.* -> storage:person.*\n # section:person.alice -> storage:person.alice\n # section:person.bob -> storage:person.bob\n\n # -- ALTERNATIVES: Override ConfigFileReader methods:\n # * process_config_section(config_section, storage)\n # * get_storage_name_for(section_name)\n # * get_storage_for(section_name, storage)\n\n\n # -- COMMAND:\n CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor.read_config())\n\n @click.command(context_settings=CONTEXT_SETTINGS)\n @click.option(\"-n\", \"--number\", \"numbers\", type=int, multiple=True)\n @click.pass_context\n def command_with_config(ctx, numbers):\n # -- ACCESS ADDITIONAL DATA FROM CONFIG FILES: Using ctx.default_map\n for person_data_key in ctx.default_map.keys():\n if not person_data_key.startswith(\"person.\"):\n continue\n person_data = ctx.default_map[person_data_key]\n process_person_data(person_data) # as dict.\n\n\n",
"bugtrack_url": null,
"license": "BSD",
"summary": "This package supports click commands that use configuration files.",
"version": "0.2.3",
"project_urls": {
"Download": "https://pypi.python.org/pypi/click-configfile/0.2.3",
"Homepage": "https://github.com/click-contrib/click-configfile"
},
"split_keywords": [
"click",
"configfile",
"configparser"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c4c4a2f9be9e758ec758542fc0348109c886bb840f88d25f05cb76ed01c07c84",
"md5": "ba76d0ef57cab2c15e45607983ca6b87",
"sha256": "af2ae7123af57d850cd18edd915893e655b6b1bc30d1302fd040b1059bec073d"
},
"downloads": -1,
"filename": "click_configfile-0.2.3-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "ba76d0ef57cab2c15e45607983ca6b87",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 9953,
"upload_time": "2017-09-24T13:03:20",
"upload_time_iso_8601": "2017-09-24T13:03:20.493157Z",
"url": "https://files.pythonhosted.org/packages/c4/c4/a2f9be9e758ec758542fc0348109c886bb840f88d25f05cb76ed01c07c84/click_configfile-0.2.3-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5a2b722c718db0f44e6927767aa422c73a77eaf333138e0a4201ca9b1f72aa2d",
"md5": "f1871c54e0d3116ae55313120dde6597",
"sha256": "95beec13bee950e98f43c81dcdabef4f644091559ea66298f9dadf59351d90d1"
},
"downloads": -1,
"filename": "click-configfile-0.2.3.tar.gz",
"has_sig": false,
"md5_digest": "f1871c54e0d3116ae55313120dde6597",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 62544,
"upload_time": "2017-09-24T13:03:34",
"upload_time_iso_8601": "2017-09-24T13:03:34.731803Z",
"url": "https://files.pythonhosted.org/packages/5a/2b/722c718db0f44e6927767aa422c73a77eaf333138e0a4201ca9b1f72aa2d/click-configfile-0.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2017-09-24 13:03:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "click-contrib",
"github_project": "click-configfile",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"tox": true,
"lcname": "click-configfile"
}