mirror of
https://git.disroot.org/pranav/pybatmesh.git
synced 2024-12-02 19:50:47 +05:30
Pranav Jerry
dfd521f9b9
Tidied up code, added more documentation, and the version now confirms
to some PEP standard. Because of the way configuration and arguments are
implemented, ALL the keys in configuration files should have an argument
that can change its value.
Verbose option was commented out, since it is not implemented. An
argument --print-wifi, which should print out WiFi ssid and password is
not implemented at the moment. Also, the README should be updated to
show the new changes. By the way I just remembered I didn't update the
CHANGELOG; I'll do it in the next commit. 😁
108 lines
2.9 KiB
Python
108 lines
2.9 KiB
Python
#!/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()
|