Skip to content

Configuration

Sean Collings edited this page Jul 17, 2021 · 4 revisions

Configuring an Arc app

File Configuration

To configure arc, create a .arc file with the following content

mode=development # I'm a comment
namespace_sep=::

The file loading system expects simple key=value pairs, and will allow Python-style single line comments denoted with #.

Arc will default to looking for a file called .arc in the current directory. But you can specify a different name in a number of ways:

from arc import CLI, config, run
cli = CLI(arcfile="myacrfile") # most common one
config.from_file("myarcfile") # useful when not using the CLI object
run(namespace, arcfile="myarcfile")

Environment Configuration

Configuration values can also be loaded in from the environment. Each attribute is expected to be all uppercase and prefixed with ARC_

from arc import config
config.from_env()

Creating a Config

The ConfigBase that the arc configuration object uses provides the utility functions for loading in values from a file. It can be used to create a basic configuration app for your tool.

Arc's Config definition

@dataclass
class Config(ConfigBase):
    namespace_sep: str = ":"
    arg_assignment: str = "="
    flag_denoter: str = "--"
    mode: str = "production"
    loglevel: int = 30

Loading Files

When you define a Config class, it comes with the from_file method for file loading. This will behave like it does on the config object, but you can modulate it's behavior a bit by defining the parse method

class MyConfig(ConfigBase):
    favorite_color: str = "red"
    favorite_number: int

    def parse(self, lines: list[str]) -> dict[str, Any]:
        # Recieves the lines of the loaded
        # file (with comments removed).
        # Expects you to return a dictionary of values
        # that correspond to the values in your config.
        # For example, for this config:
        {
           "favorite_color": "green",
           "favorite_number": 2
        }
        # would be a valid return statement.

Clone this wiki locally