mirror of
https://github.com/k4yt3x/wg-meshconf.git
synced 2024-11-22 07:33:27 +01:00
fixed windows newline bug; changed prettytable to rich
This commit is contained in:
parent
17f0de5606
commit
5dd760b625
@ -4,22 +4,22 @@
|
|||||||
Name: Database Manager
|
Name: Database Manager
|
||||||
Creator: K4YT3X
|
Creator: K4YT3X
|
||||||
Date Created: July 19, 2020
|
Date Created: July 19, 2020
|
||||||
Last Modified: May 29, 2021
|
Last Modified: June 16, 2021
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# local imports
|
||||||
|
from .wireguard import WireGuard
|
||||||
|
|
||||||
# built-in imports
|
# built-in imports
|
||||||
import contextlib
|
|
||||||
import copy
|
import copy
|
||||||
import csv
|
import csv
|
||||||
import pathlib
|
import pathlib
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# third party imports
|
# third party imports
|
||||||
with contextlib.suppress(ImportError):
|
from rich.console import Console
|
||||||
from prettytable import PrettyTable
|
from rich.table import Table
|
||||||
|
|
||||||
# local imports
|
|
||||||
from .wireguard import WireGuard
|
|
||||||
|
|
||||||
INTERFACE_ATTRIBUTES = [
|
INTERFACE_ATTRIBUTES = [
|
||||||
"Address",
|
"Address",
|
||||||
@ -90,7 +90,7 @@ class DatabaseManager:
|
|||||||
def init(self):
|
def init(self):
|
||||||
"""initialize an empty database file"""
|
"""initialize an empty database file"""
|
||||||
if not self.database_path.exists():
|
if not self.database_path.exists():
|
||||||
with self.database_path.open(mode="w", encoding="utf-8") as database_file:
|
with self.database_path.open(mode="w", encoding="utf-8", newline="") as database_file:
|
||||||
writer = csv.DictWriter(
|
writer = csv.DictWriter(
|
||||||
database_file, KEY_TYPE.keys(), quoting=csv.QUOTE_ALL
|
database_file, KEY_TYPE.keys(), quoting=csv.QUOTE_ALL
|
||||||
)
|
)
|
||||||
@ -150,7 +150,7 @@ class DatabaseManager:
|
|||||||
data (dict): content of database
|
data (dict): content of database
|
||||||
"""
|
"""
|
||||||
|
|
||||||
with self.database_path.open(mode="w", encoding="utf-8") as database_file:
|
with self.database_path.open(mode="w", encoding="utf-8", newline="") as database_file:
|
||||||
writer = csv.DictWriter(
|
writer = csv.DictWriter(
|
||||||
database_file, KEY_TYPE.keys(), quoting=csv.QUOTE_ALL
|
database_file, KEY_TYPE.keys(), quoting=csv.QUOTE_ALL
|
||||||
)
|
)
|
||||||
@ -249,7 +249,7 @@ class DatabaseManager:
|
|||||||
# write changes into database
|
# write changes into database
|
||||||
self.write_database(database)
|
self.write_database(database)
|
||||||
|
|
||||||
def showpeers(self, Name: str, style: str = "table", simplify: bool = False):
|
def showpeers(self, Name: str, verbose: bool = False):
|
||||||
database = self.read_database()
|
database = self.read_database()
|
||||||
|
|
||||||
# if name is specified, show the specified peer
|
# if name is specified, show the specified peer
|
||||||
@ -266,7 +266,7 @@ class DatabaseManager:
|
|||||||
field_names = ["Name"]
|
field_names = ["Name"]
|
||||||
|
|
||||||
# exclude all columns that only have None's in simplified mode
|
# exclude all columns that only have None's in simplified mode
|
||||||
if simplify is True:
|
if verbose is False:
|
||||||
for peer in peers:
|
for peer in peers:
|
||||||
for key in INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES:
|
for key in INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES:
|
||||||
if (
|
if (
|
||||||
@ -279,44 +279,36 @@ class DatabaseManager:
|
|||||||
else:
|
else:
|
||||||
field_names += INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES
|
field_names += INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES
|
||||||
|
|
||||||
# if the style is table
|
# create new rich table
|
||||||
# print with prettytable
|
table = Table(show_lines=True)
|
||||||
if style == "table":
|
|
||||||
try:
|
|
||||||
table = PrettyTable()
|
|
||||||
table.field_names = field_names
|
|
||||||
|
|
||||||
|
# create columns
|
||||||
|
for field in field_names:
|
||||||
|
table.add_column(
|
||||||
|
field,
|
||||||
|
style={
|
||||||
|
"Name": "cyan",
|
||||||
|
"Address": "red",
|
||||||
|
"ListenPort": "yellow",
|
||||||
|
"PrivateKey": "magenta",
|
||||||
|
"Endpoint": "green",
|
||||||
|
}.get(field),
|
||||||
|
)
|
||||||
|
|
||||||
|
# add rows to table
|
||||||
for peer in peers:
|
for peer in peers:
|
||||||
table.add_row(
|
table.add_row(
|
||||||
[peer]
|
peer,
|
||||||
+ [
|
*[
|
||||||
database["peers"][peer].get(k)
|
str(database["peers"][peer].get(k))
|
||||||
if not isinstance(database["peers"][peer].get(k), list)
|
if not isinstance(database["peers"][peer].get(k), list)
|
||||||
else ",".join(database["peers"][peer].get(k))
|
else ",".join(database["peers"][peer].get(k))
|
||||||
for k in [i for i in table.field_names if i != "Name"]
|
for k in [i for i in field_names if i != "Name"]
|
||||||
]
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
print(table)
|
# print the constructed table in console
|
||||||
except NameError:
|
Console().print(table)
|
||||||
print("PrettyTable is not installed", file=sys.stderr)
|
|
||||||
print("Displaying in table mode is not available", file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# if the style is text
|
|
||||||
# print in plaintext format
|
|
||||||
elif style == "text":
|
|
||||||
for peer in peers:
|
|
||||||
print(f"{'peer': <14}{peer}")
|
|
||||||
for key in field_names:
|
|
||||||
print(
|
|
||||||
f"{key: <14}{database['peers'][peer].get(key)}"
|
|
||||||
) if not isinstance(
|
|
||||||
database["peers"][peer].get(key), list
|
|
||||||
) else print(
|
|
||||||
f"{key: <14}{','.join(database['peers'][peer].get(key))}"
|
|
||||||
)
|
|
||||||
print()
|
|
||||||
|
|
||||||
def genconfig(self, Name: str, output: pathlib.Path):
|
def genconfig(self, Name: str, output: pathlib.Path):
|
||||||
database = self.read_database()
|
database = self.read_database()
|
||||||
|
@ -4,21 +4,21 @@
|
|||||||
Name: wg-meshconf
|
Name: wg-meshconf
|
||||||
Creator: K4YT3X
|
Creator: K4YT3X
|
||||||
Date Created: July 19, 2020
|
Date Created: July 19, 2020
|
||||||
Last Modified: May 29, 2021
|
Last Modified: June 16, 2021
|
||||||
|
|
||||||
Licensed under the GNU General Public License Version 3 (GNU GPL v3),
|
Licensed under the GNU General Public License Version 3 (GNU GPL v3),
|
||||||
available at: https://www.gnu.org/licenses/gpl-3.0.txt
|
available at: https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
(C) 2018-2021 K4YT3X
|
(C) 2018-2021 K4YT3X
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# local imports
|
||||||
|
from .database_manager import DatabaseManager
|
||||||
|
|
||||||
# built-in imports
|
# built-in imports
|
||||||
import argparse
|
import argparse
|
||||||
import pathlib
|
import pathlib
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# local imports
|
|
||||||
from .database_manager import DatabaseManager
|
|
||||||
|
|
||||||
|
|
||||||
def parse_arguments():
|
def parse_arguments():
|
||||||
"""parse CLI arguments"""
|
"""parse CLI arguments"""
|
||||||
@ -110,15 +110,9 @@ def parse_arguments():
|
|||||||
nargs="?",
|
nargs="?",
|
||||||
)
|
)
|
||||||
showpeers.add_argument(
|
showpeers.add_argument(
|
||||||
"--style",
|
"-v",
|
||||||
choices=["table", "text"],
|
"--verbose",
|
||||||
help="peers information printing style",
|
help="display all columns despite they hold empty values",
|
||||||
default="table",
|
|
||||||
)
|
|
||||||
showpeers.add_argument(
|
|
||||||
"-s",
|
|
||||||
"--simplify",
|
|
||||||
help="do not print columns that are all None",
|
|
||||||
action="store_true",
|
action="store_true",
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -195,7 +189,7 @@ def main():
|
|||||||
database_manager.delpeer(args.name)
|
database_manager.delpeer(args.name)
|
||||||
|
|
||||||
elif args.command == "showpeers":
|
elif args.command == "showpeers":
|
||||||
database_manager.showpeers(args.name, args.style, args.simplify)
|
database_manager.showpeers(args.name, args.verbose)
|
||||||
|
|
||||||
elif args.command == "genconfig":
|
elif args.command == "genconfig":
|
||||||
database_manager.genconfig(args.name, args.output)
|
database_manager.genconfig(args.name, args.output)
|
||||||
|
Loading…
Reference in New Issue
Block a user