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.

{
  "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.

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.

  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.

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 in the file ~/.config/app.config. 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 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

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.

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.6.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

typerconf-2.6-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typerconf-2.6.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.10.12 Linux/6.2.0-32-generic

File hashes

Hashes for typerconf-2.6.tar.gz
Algorithm Hash digest
SHA256 789bdb5527f867f22275f8b24da20e26d76e6bcc5941ec4f88adbb23ea8d5eac
MD5 1f609b5d1a2af1bd07b49e2b4d316664
BLAKE2b-256 2773282e46ad126124acc46039fd94f566a72f601cd0896c713dd7de0a9f5516

See more details on using hashes here.

File details

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

File metadata

  • Download URL: typerconf-2.6-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.10.12 Linux/6.2.0-32-generic

File hashes

Hashes for typerconf-2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 54761701cb21609bd0a4bf7a8503b1ca5fac034763f43fd2cff3b4ef702f9961
MD5 26442657bee315226cffa551c11f8e93
BLAKE2b-256 2e30a26749050e9ab0c020f1f6243ce74bdc24014174cb9caafe3b9098d51960

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