2019-11-01 23:31:40 +00:00
|
|
|
import logging
|
2019-11-11 23:35:48 +00:00
|
|
|
import os
|
2019-11-01 23:31:40 +00:00
|
|
|
import shutil
|
|
|
|
from pathlib import Path
|
|
|
|
|
2019-11-05 21:10:42 +00:00
|
|
|
import yaml
|
|
|
|
|
2019-11-01 23:31:40 +00:00
|
|
|
# gui home paths
|
2019-11-14 06:49:32 +00:00
|
|
|
from coretk import themes
|
|
|
|
|
2019-11-01 23:31:40 +00:00
|
|
|
HOME_PATH = Path.home().joinpath(".coretk")
|
|
|
|
BACKGROUNDS_PATH = HOME_PATH.joinpath("backgrounds")
|
|
|
|
CUSTOM_EMANE_PATH = HOME_PATH.joinpath("custom_emane")
|
|
|
|
CUSTOM_SERVICE_PATH = HOME_PATH.joinpath("custom_services")
|
|
|
|
ICONS_PATH = HOME_PATH.joinpath("icons")
|
|
|
|
MOBILITY_PATH = HOME_PATH.joinpath("mobility")
|
|
|
|
XML_PATH = HOME_PATH.joinpath("xml")
|
|
|
|
CONFIG_PATH = HOME_PATH.joinpath("gui.yaml")
|
|
|
|
|
|
|
|
# local paths
|
|
|
|
LOCAL_ICONS_PATH = Path(__file__).parent.joinpath("icons").absolute()
|
|
|
|
LOCAL_BACKGROUND_PATH = Path(__file__).parent.joinpath("backgrounds").absolute()
|
2019-11-26 19:32:48 +00:00
|
|
|
LOCAL_XMLS_PATH = Path(__file__).parent.joinpath("xmls").absolute()
|
2019-11-01 23:31:40 +00:00
|
|
|
|
2019-11-11 23:35:48 +00:00
|
|
|
# configuration data
|
|
|
|
TERMINALS = [
|
|
|
|
"$TERM",
|
|
|
|
"gnome-terminal --window --",
|
|
|
|
"lxterminal -e",
|
|
|
|
"konsole -e",
|
|
|
|
"xterm -e",
|
|
|
|
"aterm -e",
|
|
|
|
"eterm -e",
|
|
|
|
"rxvt -e",
|
|
|
|
"xfce4-terminal -x",
|
|
|
|
]
|
|
|
|
EDITORS = ["$EDITOR", "vim", "emacs", "gedit", "nano", "vi"]
|
|
|
|
|
2019-11-01 23:31:40 +00:00
|
|
|
|
2019-11-05 21:10:42 +00:00
|
|
|
class IndentDumper(yaml.Dumper):
|
|
|
|
def increase_indent(self, flow=False, indentless=False):
|
|
|
|
return super().increase_indent(flow, False)
|
|
|
|
|
|
|
|
|
2019-11-01 23:31:40 +00:00
|
|
|
def check_directory():
|
|
|
|
if HOME_PATH.exists():
|
|
|
|
logging.info("~/.coretk exists")
|
|
|
|
return
|
|
|
|
logging.info("creating ~/.coretk")
|
|
|
|
HOME_PATH.mkdir()
|
|
|
|
BACKGROUNDS_PATH.mkdir()
|
|
|
|
CUSTOM_EMANE_PATH.mkdir()
|
|
|
|
CUSTOM_SERVICE_PATH.mkdir()
|
|
|
|
ICONS_PATH.mkdir()
|
|
|
|
MOBILITY_PATH.mkdir()
|
|
|
|
XML_PATH.mkdir()
|
|
|
|
for image in LOCAL_ICONS_PATH.glob("*"):
|
|
|
|
new_image = ICONS_PATH.joinpath(image.name)
|
|
|
|
shutil.copy(image, new_image)
|
|
|
|
for background in LOCAL_BACKGROUND_PATH.glob("*"):
|
|
|
|
new_background = BACKGROUNDS_PATH.joinpath(background.name)
|
|
|
|
shutil.copy(background, new_background)
|
2019-11-26 19:32:48 +00:00
|
|
|
for xml_file in LOCAL_XMLS_PATH.glob("*"):
|
|
|
|
new_xml = XML_PATH.joinpath(xml_file.name)
|
|
|
|
shutil.copy(xml_file, new_xml)
|
2019-11-11 23:35:48 +00:00
|
|
|
|
|
|
|
if "TERM" in os.environ:
|
|
|
|
terminal = TERMINALS[0]
|
|
|
|
else:
|
|
|
|
terminal = TERMINALS[1]
|
|
|
|
if "EDITOR" in os.environ:
|
|
|
|
editor = EDITORS[0]
|
|
|
|
else:
|
|
|
|
editor = EDITORS[1]
|
2019-11-07 19:33:40 +00:00
|
|
|
config = {
|
2019-11-11 23:35:48 +00:00
|
|
|
"preferences": {
|
2019-11-14 06:49:32 +00:00
|
|
|
"theme": themes.DARK,
|
2019-11-11 23:35:48 +00:00
|
|
|
"editor": editor,
|
|
|
|
"terminal": terminal,
|
|
|
|
"gui3d": "/usr/local/bin/std3d.sh",
|
|
|
|
},
|
2019-11-22 05:56:28 +00:00
|
|
|
"location": {
|
|
|
|
"x": 0.0,
|
|
|
|
"y": 0.0,
|
|
|
|
"z": 0.0,
|
|
|
|
"lat": 47.5791667,
|
|
|
|
"lon": -122.132322,
|
|
|
|
"alt": 2.0,
|
|
|
|
"scale": 150.0,
|
|
|
|
},
|
2019-11-07 19:33:40 +00:00
|
|
|
"servers": [{"name": "example", "address": "127.0.0.1", "port": 50051}],
|
|
|
|
"nodes": [],
|
2019-11-11 23:35:48 +00:00
|
|
|
"observers": [{"name": "hello", "cmd": "echo hello"}],
|
2019-11-07 19:33:40 +00:00
|
|
|
}
|
2019-11-11 23:35:48 +00:00
|
|
|
save(config)
|
2019-11-05 21:10:42 +00:00
|
|
|
|
|
|
|
|
2019-11-11 23:35:48 +00:00
|
|
|
def read():
|
2019-11-05 21:10:42 +00:00
|
|
|
with CONFIG_PATH.open("r") as f:
|
|
|
|
return yaml.load(f, Loader=yaml.SafeLoader)
|
2019-11-06 00:16:46 +00:00
|
|
|
|
|
|
|
|
2019-11-11 23:35:48 +00:00
|
|
|
def save(config):
|
2019-11-06 00:16:46 +00:00
|
|
|
with CONFIG_PATH.open("w") as f:
|
|
|
|
yaml.dump(config, f, Dumper=IndentDumper, default_flow_style=False)
|