Skip to content

Commit

Permalink
Updates README based on usage section
Browse files Browse the repository at this point in the history
  • Loading branch information
dbosk committed Jun 30, 2023
1 parent c7ec84f commit 5bcf7e8
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ 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. Say we have the program
`nytid` that wants to use this config module and subcommand.
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
Expand Down Expand Up @@ -59,3 +63,54 @@ 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.

0 comments on commit 5bcf7e8

Please sign in to comment.