pybatmesh/naxalnet/config.py

108 lines
2.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
config.py
---------
This file contains functions to parse configuration files
and arguments. Most of these functions are meant to be used
by parse_args() internally, so only parse_args() should
be imported outside this file.
"""
from pathlib import Path
from configparser import ConfigParser
from argparse import ArgumentParser
from naxalnet.default import CONFIG, CONFIG_FILES, CONFIG_DIRS
def get_config_files():
"""returns list of configuration files as Path objects to parse"""
config_files = []
for directory in CONFIG_DIRS:
path = Path(directory)
if path.exists():
for i in CONFIG_FILES:
glob = path.glob(i)
config_files.extend(glob)
return config_files
def parse_config():
"""
Parse all configuration files, with the values in
default.py as fallback
"""
parser = ConfigParser()
# encoded defaults
parser.read_dict(CONFIG)
# read config files
files = get_config_files()
for i in files:
parser.read_file(i.open())
return parser
def parse_args():
"""
Parse all arguments and return ArgumentParser.parse_args(),
with values in config files as fallback. Ideally, only this
function should be used by naxalnet to get arguments and
configuration.
"""
config = parse_config()
parser = ArgumentParser(
description="setup batman-adv networks with systemd and iwd"
)
parser.add_argument(
"--ap-ssid",
"-n",
type=str,
help="SSID of the WiFi AP",
default=config["ap"]["ssid"],
)
parser.add_argument(
"--ap-passwd",
"-p",
"--ap-password",
type=str,
help="password of the WiFi AP",
default=config["ap"]["passwd"],
)
parser.add_argument(
"--adhoc-name",
"-a",
type=str,
default=config["adhoc"]["name"],
help="name of adhoc network",
)
# TODO: print info about wifi network from config and args and exit
parser.add_argument(
"--print-wifi",
action="store_true",
default=False,
help="prints the ssid and password of the WiFi network and exit",
)
parser.add_argument(
"--networkd-config-dir",
type=str,
default=config["networkd"]["confdir"],
help="the directory where systemd-networkd configuration files are stored",
)
parser.add_argument(
"--networkd-runtime-dir",
type=str,
default=config["networkd"]["runtimedir"],
help="the directory where configuration files of systemd-networkd should be copied",
)
# TODO: implement --verbose
# parser.add_argument(
# "-v",
# "--verbose",
# help="increase output verbosity; can be used multiple times",
# action="count",
# default=0,
# )
return parser.parse_args()