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
|
"""An example application using Confuse for configuration."""
import argparse
import confuse
template = {
"library": confuse.Filename(),
"import_write": confuse.OneOf([bool, "ask", "skip"]),
"ignore": confuse.StrSeq(),
"plugins": list,
"paths": {
"directory": confuse.Filename(),
"default": confuse.Filename(relative_to="directory"),
},
"servers": confuse.Sequence(
{
"hostname": str,
"options": confuse.StrSeq(),
}
),
}
config = confuse.LazyConfig("ConfuseExample", __name__)
def main() -> None:
parser = argparse.ArgumentParser(description="example Confuse program")
parser.add_argument(
"--library",
"-l",
dest="library",
metavar="LIBPATH",
help="library database file",
)
parser.add_argument(
"--directory",
"-d",
dest="paths.directory",
metavar="DIRECTORY",
help="destination music directory",
)
parser.add_argument(
"--verbose",
"-v",
dest="verbose",
action="store_true",
help="print debugging messages",
)
args = parser.parse_args()
config.set_args(args, dots=True)
print("configuration directory is", config.config_dir())
# Use a boolean flag and the transient overlay.
if config["verbose"]:
print("verbose mode")
config["log"]["level"] = 2
else:
config["log"]["level"] = 0
print("logging level is", config["log"]["level"].get(int))
valid = config.get(template)
# Some validated/converted values.
print("library is", valid.library)
print("directory is", valid.paths.directory)
print("paths.default is", valid.paths.default)
print("servers are", [s.hostname for s in valid.servers])
|