mirror of
https://git.disroot.org/pranav/pybatmesh.git
synced 2024-12-27 10:30:23 +05:30
added new args and support for conf files
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. 😁
This commit is contained in:
parent
90464f0304
commit
dfd521f9b9
@ -16,4 +16,17 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
__version__ = "0.2.0a3"
|
||||
# GUIDE FOR CHANGING __version__
|
||||
#
|
||||
# All commits in master should have the version as
|
||||
# {last published version tag}.a{1,2,3,...}
|
||||
# example: 0.2.0a3 should mean 3 commits after tag v0.2.0
|
||||
#
|
||||
# All commits in other branches should
|
||||
# have {version in master}.dev{1,2,...}
|
||||
# example: 0.2.0a3.dev1 should mean 1 commit in the new
|
||||
# branch after the commit in master.
|
||||
#
|
||||
# In case you forgot to add a version, put the next number
|
||||
# in the next commit
|
||||
__version__ = "0.2.0a3.dev1"
|
||||
|
@ -15,6 +15,13 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
naxalnet
|
||||
========
|
||||
|
||||
If called as python -m naxalnet, this file makes naxalnet run like
|
||||
it was called from the commandline.
|
||||
"""
|
||||
|
||||
from naxalnet.scripts import here_be_dragons
|
||||
|
||||
|
107
naxalnet/config.py
Normal file
107
naxalnet/config.py
Normal file
@ -0,0 +1,107 @@
|
||||
#!/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()
|
19
naxalnet/default.py
Normal file
19
naxalnet/default.py
Normal file
@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
This file contains default values for configuration.
|
||||
The values are likely to be replaced by other configuration files.
|
||||
"""
|
||||
|
||||
CONFIG = {
|
||||
"networkd": {
|
||||
"confdir": "/usr/share/naxalnet/networkd",
|
||||
"runtimedir": "/run/systemd/network",
|
||||
},
|
||||
"adhoc": {"name": "NxMesh"},
|
||||
"ap": {"ssid": "MeshWiFi", "passwd": "naxalnet256"},
|
||||
}
|
||||
|
||||
# glob
|
||||
CONFIG_FILES = ["naxalnet.conf", "naxalnet.d/*.conf"]
|
||||
CONFIG_DIRS = ["/etc"]
|
@ -23,9 +23,9 @@ with systemd-networkd and iwd
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from shutil import copy
|
||||
from argparse import ArgumentParser
|
||||
from dasbus.error import DBusError
|
||||
from naxalnet.iwd import IWD, Device, Adapter
|
||||
from naxalnet.config import parse_args
|
||||
|
||||
NETWORKD_CONFIGS = "/usr/share/naxalnet/networkd"
|
||||
NETWORKD_VOLATILE_DIR = "/run/systemd/network"
|
||||
@ -35,7 +35,7 @@ AP_SSID = "NaxalNet"
|
||||
AP_PASSWD = "naxalnet256"
|
||||
|
||||
|
||||
def copy_files():
|
||||
def copy_files(args):
|
||||
"""
|
||||
Copy networkd configs to volatile dir.
|
||||
The D-Bus API does not support creating new interfaces
|
||||
@ -44,8 +44,8 @@ def copy_files():
|
||||
"""
|
||||
|
||||
print("Copying network config files")
|
||||
dest = Path(NETWORKD_VOLATILE_DIR)
|
||||
src = Path(NETWORKD_CONFIGS)
|
||||
dest = Path(args.networkd_runtime_dir)
|
||||
src = Path(args.networkd_config_dir)
|
||||
|
||||
# Create the volatile directory if it doesn't exist
|
||||
dest.mkdir(parents=True, exist_ok=True)
|
||||
@ -111,15 +111,13 @@ def here_be_dragons():
|
||||
execute naxalnet from commandline
|
||||
"""
|
||||
args = parse_args()
|
||||
|
||||
try:
|
||||
copy_files()
|
||||
copy_files(args)
|
||||
except PermissionError as error:
|
||||
print(error)
|
||||
sys.exit("Make sure you are root")
|
||||
|
||||
# TODO: implement the daemon here so that it will call setup_devices
|
||||
# every time a device is connected or removed.
|
||||
# Now, the iwd part
|
||||
try:
|
||||
setup_devices(args)
|
||||
except DBusError as error:
|
||||
@ -128,45 +126,3 @@ def here_be_dragons():
|
||||
|
||||
# naxalnet will print Bye if no errors occured
|
||||
print("Bye")
|
||||
|
||||
|
||||
def parse_args():
|
||||
"""parse all arguments and return ArgumentParser.parse_args()"""
|
||||
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=AP_SSID,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--ap-passwd",
|
||||
"-p",
|
||||
"--ap-password",
|
||||
type=str,
|
||||
help="password of the WiFi AP",
|
||||
default=AP_PASSWD,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--adhoc-name", "-a", type=str, default=ADHOC_NAME, help="name of adhoc network"
|
||||
)
|
||||
# TODO: print info about wifi network from config and args
|
||||
parser.add_argument(
|
||||
"--print-wifi",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="prints the ssid and password of the WiFi network and exit",
|
||||
)
|
||||
# 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()
|
||||
|
Loading…
Reference in New Issue
Block a user