Skip to main content

Library to read and write configs using API and CLI with Typer

Project description

The configuration is a JSON structure. We'll use the following for the coming examples. []{#ConfigStructure label="ConfigStructure"}

{
  "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. We want the CLI command to have the following form when used with nytid.

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

will set the configuration value at the path, whereas

  nytid config courses.datintro22.schedule.url

will return it.

We can do this with both typer (and click) and argparse. With typer:

import typer
import typerconf as config

cli = typer.Typer()
config.add_config_cmd(cli)

With argparse:

import argparse
import typerconf as config
import sys

cli = argparse.ArgumentParser()
subparsers = cli.add_subparsers(dest="subcommand")
config.add_config_cmd(subparsers)

args = cli.parse_args()
if args.subcommand == "config":
  args.func(args)

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

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 ([ConfigStructure]{reference-type="ref" reference="ConfigStructure"}) in the file ~/.config/app.config{.bash}. Consider the following code.

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{.python} above, we merge the default config with the values set in the config file that was loaded.

We note that the construction of conf{.python} above, can be replaced by the equivalent

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

The meaning of writeback{.python} 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{.python}, but we can turn it on by passing the writeback=True{.python}.

We can change the config by using the conf.set{.python} method.

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.

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

typerconf-2.12.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

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

typerconf-2.12-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file typerconf-2.12.tar.gz.

File metadata

  • Download URL: typerconf-2.12.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.4 CPython/3.13.7 Linux/6.17.0-22-generic

File hashes

Hashes for typerconf-2.12.tar.gz
Algorithm Hash digest
SHA256 0d660e86b526c385cb90aa577d916ffaec27f3935aef9483da94566807a5b70b
MD5 b6a5cf82d6e8d9ec2b4af6c05cab014b
BLAKE2b-256 c79e51ef6e05f9d09694e8945030af796072956e8de8091d8b83614e7ea4e012

See more details on using hashes here.

File details

Details for the file typerconf-2.12-py3-none-any.whl.

File metadata

  • Download URL: typerconf-2.12-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.4 CPython/3.13.7 Linux/6.17.0-22-generic

File hashes

Hashes for typerconf-2.12-py3-none-any.whl
Algorithm Hash digest
SHA256 d4cef85c09db6ee2853b84be907cde51114bd70dc9217a1e3e5c8e756dd0f4a7
MD5 e3cd9461d2535454b081f4d1a9474384
BLAKE2b-256 d23300e9a79a2db7866e19743a4ec8b7452eefb25821652756f805348155ad76

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