pygui: revamped config to leverage classes mapped to yaml, removes need for using keys all over and type hinting on glasses, future changes should support defaults better
This commit is contained in:
parent
d9f48d14a7
commit
86ae87eafe
16 changed files with 251 additions and 208 deletions
|
@ -1,6 +1,7 @@
|
|||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
|
||||
import yaml
|
||||
|
||||
|
@ -37,11 +38,6 @@ TERMINALS = {
|
|||
"gnome-terminal": "gnome-terminal --window --",
|
||||
}
|
||||
EDITORS = ["$EDITOR", "vim", "emacs", "gedit", "nano", "vi"]
|
||||
DEFAULT_IP4S = ["10.0.0.0", "192.168.0.0", "172.16.0.0"]
|
||||
DEFAULT_IP4 = DEFAULT_IP4S[0]
|
||||
DEFAULT_IP6S = ["2001::", "2002::", "a::"]
|
||||
DEFAULT_IP6 = DEFAULT_IP6S[0]
|
||||
DEFAULT_MAC = "00:00:00:aa:00:00"
|
||||
|
||||
|
||||
class IndentDumper(yaml.Dumper):
|
||||
|
@ -49,13 +45,151 @@ class IndentDumper(yaml.Dumper):
|
|||
return super().increase_indent(flow, False)
|
||||
|
||||
|
||||
def copy_files(current_path, new_path):
|
||||
class CustomNode(yaml.YAMLObject):
|
||||
yaml_tag = "!CustomNode"
|
||||
yaml_loader = yaml.SafeLoader
|
||||
|
||||
def __init__(self, name: str, image: str, services: List[str]) -> None:
|
||||
self.name = name
|
||||
self.image = image
|
||||
self.services = services
|
||||
|
||||
|
||||
class CoreServer(yaml.YAMLObject):
|
||||
yaml_tag = "!CoreServer"
|
||||
yaml_loader = yaml.SafeLoader
|
||||
|
||||
def __init__(self, name: str, address: str) -> None:
|
||||
self.name = name
|
||||
self.address = address
|
||||
|
||||
|
||||
class Observer(yaml.YAMLObject):
|
||||
yaml_tag = "!Observer"
|
||||
yaml_loader = yaml.SafeLoader
|
||||
|
||||
def __init__(self, name: str, cmd: str) -> None:
|
||||
self.name = name
|
||||
self.cmd = cmd
|
||||
|
||||
|
||||
class PreferencesConfig(yaml.YAMLObject):
|
||||
yaml_tag = "!PreferencesConfig"
|
||||
yaml_loader = yaml.SafeLoader
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
editor: str = EDITORS[1],
|
||||
terminal: str = None,
|
||||
theme: str = themes.THEME_DARK,
|
||||
gui3d: str = "/usr/local/bin/std3d.sh",
|
||||
width: int = 1000,
|
||||
height: int = 750,
|
||||
) -> None:
|
||||
self.theme = theme
|
||||
self.editor = editor
|
||||
self.terminal = terminal
|
||||
self.gui3d = gui3d
|
||||
self.width = width
|
||||
self.height = height
|
||||
|
||||
|
||||
class LocationConfig(yaml.YAMLObject):
|
||||
yaml_tag = "!LocationConfig"
|
||||
yaml_loader = yaml.SafeLoader
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
x: float = 0.0,
|
||||
y: float = 0.0,
|
||||
z: float = 0.0,
|
||||
lat: float = 47.5791667,
|
||||
lon: float = -122.132322,
|
||||
alt: float = 2.0,
|
||||
scale: float = 150.0,
|
||||
) -> None:
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.z = z
|
||||
self.lat = lat
|
||||
self.lon = lon
|
||||
self.alt = alt
|
||||
self.scale = scale
|
||||
|
||||
|
||||
class IpConfigs(yaml.YAMLObject):
|
||||
yaml_tag = "!IpConfigs"
|
||||
yaml_loader = yaml.SafeLoader
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
ip4: str = None,
|
||||
ip6: str = None,
|
||||
ip4s: List[str] = None,
|
||||
ip6s: List[str] = None,
|
||||
) -> None:
|
||||
if ip4s is None:
|
||||
ip4s = ["10.0.0.0", "192.168.0.0", "172.16.0.0"]
|
||||
self.ip4s = ip4s
|
||||
if ip6s is None:
|
||||
ip6s = ["2001::", "2002::", "a::"]
|
||||
self.ip6s = ip6s
|
||||
if ip4 is None:
|
||||
ip4 = self.ip4s[0]
|
||||
self.ip4 = ip4
|
||||
if ip6 is None:
|
||||
ip6 = self.ip6s[0]
|
||||
self.ip6 = ip6
|
||||
|
||||
|
||||
class GuiConfig(yaml.YAMLObject):
|
||||
yaml_tag = "!GuiConfig"
|
||||
yaml_loader = yaml.SafeLoader
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
preferences: PreferencesConfig = None,
|
||||
location: LocationConfig = None,
|
||||
servers: List[CoreServer] = None,
|
||||
nodes: List[CustomNode] = None,
|
||||
recentfiles: List[str] = None,
|
||||
observers: List[Observer] = None,
|
||||
scale: float = 1.0,
|
||||
ips: IpConfigs = None,
|
||||
mac: str = "00:00:00:aa:00:00",
|
||||
) -> None:
|
||||
if preferences is None:
|
||||
preferences = PreferencesConfig()
|
||||
self.preferences = preferences
|
||||
if location is None:
|
||||
location = LocationConfig()
|
||||
self.location = location
|
||||
if servers is None:
|
||||
servers = []
|
||||
self.servers = servers
|
||||
if nodes is None:
|
||||
nodes = []
|
||||
self.nodes = nodes
|
||||
if recentfiles is None:
|
||||
recentfiles = []
|
||||
self.recentfiles = recentfiles
|
||||
if observers is None:
|
||||
observers = []
|
||||
self.observers = observers
|
||||
self.scale = scale
|
||||
if ips is None:
|
||||
ips = IpConfigs()
|
||||
self.ips = ips
|
||||
self.mac = mac
|
||||
|
||||
|
||||
def copy_files(current_path, new_path) -> None:
|
||||
for current_file in current_path.glob("*"):
|
||||
new_file = new_path.joinpath(current_file.name)
|
||||
shutil.copy(current_file, new_file)
|
||||
|
||||
|
||||
def find_terminal():
|
||||
def find_terminal() -> Optional[str]:
|
||||
for term in sorted(TERMINALS):
|
||||
cmd = TERMINALS[term]
|
||||
if shutil.which(term):
|
||||
|
@ -63,7 +197,7 @@ def find_terminal():
|
|||
return None
|
||||
|
||||
|
||||
def check_directory():
|
||||
def check_directory() -> None:
|
||||
if HOME_PATH.exists():
|
||||
return
|
||||
HOME_PATH.mkdir()
|
||||
|
@ -85,45 +219,16 @@ def check_directory():
|
|||
editor = EDITORS[0]
|
||||
else:
|
||||
editor = EDITORS[1]
|
||||
config = {
|
||||
"preferences": {
|
||||
"theme": themes.THEME_DARK,
|
||||
"editor": editor,
|
||||
"terminal": terminal,
|
||||
"gui3d": "/usr/local/bin/std3d.sh",
|
||||
"width": 1000,
|
||||
"height": 750,
|
||||
},
|
||||
"location": {
|
||||
"x": 0.0,
|
||||
"y": 0.0,
|
||||
"z": 0.0,
|
||||
"lat": 47.5791667,
|
||||
"lon": -122.132322,
|
||||
"alt": 2.0,
|
||||
"scale": 150.0,
|
||||
},
|
||||
"servers": [],
|
||||
"nodes": [],
|
||||
"recentfiles": [],
|
||||
"observers": [],
|
||||
"scale": 1.0,
|
||||
"ips": {
|
||||
"ip4": DEFAULT_IP4,
|
||||
"ip6": DEFAULT_IP6,
|
||||
"ip4s": DEFAULT_IP4S,
|
||||
"ip6s": DEFAULT_IP6S,
|
||||
},
|
||||
"mac": DEFAULT_MAC,
|
||||
}
|
||||
preferences = PreferencesConfig(editor, terminal)
|
||||
config = GuiConfig(preferences=preferences)
|
||||
save(config)
|
||||
|
||||
|
||||
def read():
|
||||
def read() -> GuiConfig:
|
||||
with CONFIG_PATH.open("r") as f:
|
||||
return yaml.load(f, Loader=yaml.SafeLoader)
|
||||
|
||||
|
||||
def save(config):
|
||||
def save(config: GuiConfig) -> None:
|
||||
with CONFIG_PATH.open("w") as f:
|
||||
yaml.dump(config, f, Dumper=IndentDumper, default_flow_style=False)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue