File: generated_config.rst

package info (click to toggle)
python-confspirator 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 320 kB
  • sloc: python: 2,388; makefile: 20
file content (89 lines) | stat: -rw-r--r-- 2,935 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Example config generation
=========================

CONFspirator supports generated config files from the built
config tree. These will populate all the fields, and if a
default value is supplied will put that in place.

To use this functionality you must supply your root config group,
and call the function with a file location. CONFspirator does not
supply any CLI support for this, so you will want to build a command
into your application or project to import your root config group,
and call the generation function.

Assuming the config group as shown in the getting started page::

    # ./my_app/config/root.py
    from confspirator import groups, fields

    root_group = groups.ConfigGroup(
        "my_app", description="The root config group.")
    root_group.register_child_config(
        fields.StrConfig(
            "top_level_config",
            help_text="Some top level config on the root group.",
            default="some_default",
        )
    )

    # ./my_app/config/sub_section.py
    from confspirator import groups, fields
    from my_app.config import root

    sub_group = groups.ConfigGroup(
        "sub_section", description="A sub group under the root group.")
    sub_group.register_child_config(
        fields.BoolConfig(
            "bool_value",
            help_text="some boolean flag value",
            default=True,
        )
    )

    root.root_group.register_child_config(sub_group)

You would call the generation logic as follows::

    # ./my_app/commands.py

    import confspirator
    from my_app.config import root


    def create_config():
        confspirator.create_example_config(
            root.root_group, "conf.yaml")

This would produce a yaml config example that looks like the following::

    # String - Some top level config on the root group.
    top_level_config: some_default
    # A sub group under the root group.
    sub_section:
      # Boolean - some boolean flag value
      bool_value: true

Alternatively if you wanted ``toml`` instead, you can simply change the
file extension and the exporter will pick that up. Or if you want to use
an extension other than ``yaml`` or ``toml`` you can explicitly set
``output_format`` to either ``yaml`` or ``toml``, and the file extension
will be ignored::

    confspirator.create_example_config(
        root.root_group, "my_app.conf", output_format="toml")

In any case, if by extension or explicit output format ``toml`` is set,
your generated example config will look as follows::

    [my_app]
    # String - Some top level config on the root group.
    top_level_config = "some_default"

    # A sub group under the root group.
    [my_app.sub_section]
      # Boolean - some boolean flag value
      bool_value = true

For complicated nested configs yaml tends to be easier to deal with,
but for people with a preference for ``ini`` style configs toml does
provide a good option that still allows nesting.