typerconf


Nametyperconf JSON
Version 2.6 PyPI version JSON
download
home_pagehttps://github.com/dbosk/typerconf
SummaryLibrary to read and write configs using API and CLI with Typer
upload_time2023-09-13 19:45:36
maintainer
docs_urlNone
authorDaniel Bosk
requires_python>=3.7,<4.0
licenseMIT
keywords typer conf config git-like config lib write conf
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            The configuration is a JSON structure. We'll use the following for the
coming examples.

```JSON
{
  "courses": {
    "datintro22": {
      "timesheet": {
        "url": "https://sheets.google..."
      },
      "schedule": {
        "url": "https://timeedit.net/..."
      }
    }
  }
}
```

The format is actually irrelevant to anyone outside of this library,
since it will never be accessed directly anyway. But it will be used to
illustrate the examples.

We can access values by dot-separated addresses. For instance, we can
use `courses.datintro22.schedule.url` to access the TimeEdit URL of the
datintro22 course.

Let's have a look at some usage examples.

### A command-line application

Say we have the program `nytid` that wants to use this config module and
subcommand.

```python
import typer
import typerconf as config

cli = typer.Typer()
# add some other subcommands
config.add_config_cmd(cli)
```

We want the CLI command to have the following form when used with
`nytid`.

```bash
  nytid config courses.datintro22.schedule.url --set https://timeedit.net/...
```

will set the configuration value at the path, whereas

```bash
  nytid config courses.datintro22.schedule.url
```

will return it.

Internally, `nytid`'s different parts can access the config through the
following API.

```python
import typerconf as config

url = config.get("courses.datintro22.schedule.url")
```

### Without the CLI

We can also use it without the CLI and application features. Then it's
the `typerconf.Config` class that is of interest.

Let's assume that we have the structure from above in the file 
`~/.config/app.config`. Consider the following code.

```python
defaults = {
  "courses": {
    "datintro22": {
      "root": "/afs/kth.se/..."
    }
  }
}

conf = Config(json_data=defaults, conf_file="~/.config/app.config")

print(f"datintro22 root directory = {conf.get('courses.datintro22.root')}")
print(f"datintro22 schedule = {conf.get('courses.datintro22.schedule')}")
```

When we construct `conf` above, we merge the default config
with the values set in the config file that was loaded.

We note that the construction of `conf` above, can be replaced
by the equivalent

```python
conf = Config(defaults)
conf.read_config("~/.config/app.config", writeback=True)
```

The meaning of `writeback` is that whenever we change the
config, it will automatically be written back to the file from which it
was read. Writeback is enabled by default when supplying the file to the
constructor. It's disabled by default for the method
`conf.read_config`, but we can turn it on by passing the
`writeback=True`.

We can change the config by using the `conf.set` method.

```python
conf.set("courses.datintro22.root", "/home/dbosk/...")
```

That would change the root that we got from the default config. Since we
enabled writeback above, this would automatically update the config file
with the new values.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dbosk/typerconf",
    "name": "typerconf",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7,<4.0",
    "maintainer_email": "",
    "keywords": "typer,conf,config,git-like,config lib,write conf",
    "author": "Daniel Bosk",
    "author_email": "daniel@bosk.se",
    "download_url": "https://files.pythonhosted.org/packages/27/73/282e46ad126124acc46039fd94f566a72f601cd0896c713dd7de0a9f5516/typerconf-2.6.tar.gz",
    "platform": null,
    "description": "The configuration is a JSON structure. We'll use the following for the\ncoming examples.\n\n```JSON\n{\n  \"courses\": {\n    \"datintro22\": {\n      \"timesheet\": {\n        \"url\": \"https://sheets.google...\"\n      },\n      \"schedule\": {\n        \"url\": \"https://timeedit.net/...\"\n      }\n    }\n  }\n}\n```\n\nThe format is actually irrelevant to anyone outside of this library,\nsince it will never be accessed directly anyway. But it will be used to\nillustrate the examples.\n\nWe can access values by dot-separated addresses. For instance, we can\nuse `courses.datintro22.schedule.url` to access the TimeEdit URL of the\ndatintro22 course.\n\nLet's have a look at some usage examples.\n\n### A command-line application\n\nSay we have the program `nytid` that wants to use this config module and\nsubcommand.\n\n```python\nimport typer\nimport typerconf as config\n\ncli = typer.Typer()\n# add some other subcommands\nconfig.add_config_cmd(cli)\n```\n\nWe want the CLI command to have the following form when used with\n`nytid`.\n\n```bash\n  nytid config courses.datintro22.schedule.url --set https://timeedit.net/...\n```\n\nwill set the configuration value at the path, whereas\n\n```bash\n  nytid config courses.datintro22.schedule.url\n```\n\nwill return it.\n\nInternally, `nytid`'s different parts can access the config through the\nfollowing API.\n\n```python\nimport typerconf as config\n\nurl = config.get(\"courses.datintro22.schedule.url\")\n```\n\n### Without the CLI\n\nWe can also use it without the CLI and application features. Then it's\nthe `typerconf.Config` class that is of interest.\n\nLet's assume that we have the structure from above in the file \n`~/.config/app.config`. Consider the following code.\n\n```python\ndefaults = {\n  \"courses\": {\n    \"datintro22\": {\n      \"root\": \"/afs/kth.se/...\"\n    }\n  }\n}\n\nconf = Config(json_data=defaults, conf_file=\"~/.config/app.config\")\n\nprint(f\"datintro22 root directory = {conf.get('courses.datintro22.root')}\")\nprint(f\"datintro22 schedule = {conf.get('courses.datintro22.schedule')}\")\n```\n\nWhen we construct `conf` above, we merge the default config\nwith the values set in the config file that was loaded.\n\nWe note that the construction of `conf` above, can be replaced\nby the equivalent\n\n```python\nconf = Config(defaults)\nconf.read_config(\"~/.config/app.config\", writeback=True)\n```\n\nThe meaning of `writeback` is that whenever we change the\nconfig, it will automatically be written back to the file from which it\nwas read. Writeback is enabled by default when supplying the file to the\nconstructor. It's disabled by default for the method\n`conf.read_config`, but we can turn it on by passing the\n`writeback=True`.\n\nWe can change the config by using the `conf.set` method.\n\n```python\nconf.set(\"courses.datintro22.root\", \"/home/dbosk/...\")\n```\n\nThat would change the root that we got from the default config. Since we\nenabled writeback above, this would automatically update the config file\nwith the new values.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Library to read and write configs using API and CLI with Typer",
    "version": "2.6",
    "project_urls": {
        "Bug Tracker": "https://github.com/dbosk/typerconf/issues",
        "Homepage": "https://github.com/dbosk/typerconf",
        "Releases": "https://github.com/dbosk/typerconf/releases",
        "Repository": "https://github.com/dbosk/typerconf"
    },
    "split_keywords": [
        "typer",
        "conf",
        "config",
        "git-like",
        "config lib",
        "write conf"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2e30a26749050e9ab0c020f1f6243ce74bdc24014174cb9caafe3b9098d51960",
                "md5": "26442657bee315226cffa551c11f8e93",
                "sha256": "54761701cb21609bd0a4bf7a8503b1ca5fac034763f43fd2cff3b4ef702f9961"
            },
            "downloads": -1,
            "filename": "typerconf-2.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "26442657bee315226cffa551c11f8e93",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7,<4.0",
            "size": 17296,
            "upload_time": "2023-09-13T19:45:34",
            "upload_time_iso_8601": "2023-09-13T19:45:34.539755Z",
            "url": "https://files.pythonhosted.org/packages/2e/30/a26749050e9ab0c020f1f6243ce74bdc24014174cb9caafe3b9098d51960/typerconf-2.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2773282e46ad126124acc46039fd94f566a72f601cd0896c713dd7de0a9f5516",
                "md5": "1f609b5d1a2af1bd07b49e2b4d316664",
                "sha256": "789bdb5527f867f22275f8b24da20e26d76e6bcc5941ec4f88adbb23ea8d5eac"
            },
            "downloads": -1,
            "filename": "typerconf-2.6.tar.gz",
            "has_sig": false,
            "md5_digest": "1f609b5d1a2af1bd07b49e2b4d316664",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7,<4.0",
            "size": 15422,
            "upload_time": "2023-09-13T19:45:36",
            "upload_time_iso_8601": "2023-09-13T19:45:36.280737Z",
            "url": "https://files.pythonhosted.org/packages/27/73/282e46ad126124acc46039fd94f566a72f601cd0896c713dd7de0a9f5516/typerconf-2.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-13 19:45:36",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dbosk",
    "github_project": "typerconf",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "typerconf"
}
        
Elapsed time: 0.11175s