2020-09-19 19:27:21 +00:00
|
|
|
from .util import dbg, err
|
|
|
|
from os import path
|
|
|
|
import json
|
2020-09-20 20:54:10 +00:00
|
|
|
import copy
|
|
|
|
from .config import Config
|
2020-09-19 19:27:21 +00:00
|
|
|
|
2020-09-24 10:39:42 +00:00
|
|
|
JSON_PROFILE_NAME = "__internal_json_profile__"
|
|
|
|
JSON_LAYOUT_NAME = "__internal_json_layout__"
|
|
|
|
|
2023-04-11 19:51:57 +00:00
|
|
|
|
2020-09-20 20:54:10 +00:00
|
|
|
class ConfigJson(object):
|
|
|
|
profile_to_use = 'default'
|
2020-09-24 12:19:58 +00:00
|
|
|
|
|
|
|
def get_profile_to_use(self):
|
|
|
|
return self.profile_to_use
|
2020-09-20 20:54:10 +00:00
|
|
|
|
2020-09-19 19:27:21 +00:00
|
|
|
def build_single_tab_layout(self, layoutjson, vertical):
|
|
|
|
dbg ('Budiling a single tab layout from json: %s ' % layoutjson)
|
|
|
|
|
|
|
|
result = {
|
|
|
|
'root': {
|
|
|
|
'type': 'Window'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 18:15:14 +00:00
|
|
|
self.build_container_layout(layoutjson, result, 'root', 0, vertical)
|
2020-09-19 19:27:21 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def build_multi_tab_layout(self, layoutjson, vertical):
|
|
|
|
dbg ('Budiling multi tabs layout from json: %s ' % layoutjson)
|
|
|
|
|
|
|
|
tabs = {
|
|
|
|
'type': 'Notebook',
|
|
|
|
'parent': 'root',
|
|
|
|
'labels': []
|
|
|
|
}
|
|
|
|
|
|
|
|
result = {
|
|
|
|
'root': {
|
|
|
|
'type': 'Window'
|
|
|
|
},
|
|
|
|
'tabs': tabs
|
|
|
|
}
|
|
|
|
|
|
|
|
counter = 0
|
|
|
|
|
|
|
|
for tab in layoutjson:
|
|
|
|
tabs['labels'].append(tab)
|
2020-09-20 18:15:14 +00:00
|
|
|
self.build_container_layout(layoutjson[tab], result, 'tabs', counter, vertical)
|
2020-09-19 19:27:21 +00:00
|
|
|
counter += 1
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def build_terminal_layout(self, layoutjson, children, parent, order):
|
|
|
|
dbg ('Building a terminal from json: %s' % layoutjson)
|
2023-04-02 23:17:02 +00:00
|
|
|
|
|
|
|
def from_json(layout_name, json_name=None):
|
|
|
|
if json_name is None:
|
|
|
|
json_name = layout_name
|
|
|
|
|
|
|
|
if json_name in layoutjson:
|
2023-04-09 16:01:41 +00:00
|
|
|
children[parent + "." + str(order)][layout_name] = layoutjson[json_name]
|
2023-04-02 23:17:02 +00:00
|
|
|
|
2020-09-19 19:27:21 +00:00
|
|
|
children[parent + "." + str(order)] = {
|
|
|
|
'type': 'Terminal',
|
|
|
|
'order': order,
|
|
|
|
'parent': parent,
|
2023-04-02 23:17:02 +00:00
|
|
|
'profile': self.profile_to_use
|
2020-09-19 19:27:21 +00:00
|
|
|
}
|
2023-04-02 23:17:02 +00:00
|
|
|
|
|
|
|
from_json('command')
|
|
|
|
from_json('title')
|
|
|
|
|
2020-09-19 19:27:21 +00:00
|
|
|
def build_container_layout(self, layoutjson, children, parent, order, vertical):
|
2020-09-20 18:15:14 +00:00
|
|
|
if len(layoutjson) == 1:
|
|
|
|
layoutjson = layoutjson[0]
|
|
|
|
|
|
|
|
if 'children' in layoutjson:
|
|
|
|
self.build_container_layout(layoutjson['children'], children, parent, order, False if vertical else True)
|
|
|
|
else:
|
|
|
|
self.build_terminal_layout(layoutjson, children, parent, order)
|
|
|
|
return
|
|
|
|
|
2023-04-11 19:51:57 +00:00
|
|
|
dbg ('Building %s layout from json: %s' % ("vertical" if vertical else "horizontal", layoutjson))
|
2020-09-19 19:27:21 +00:00
|
|
|
|
|
|
|
counter = 0
|
|
|
|
actualparent = parent
|
|
|
|
|
|
|
|
for pane in layoutjson:
|
|
|
|
if counter < (len(layoutjson) - 1):
|
|
|
|
containername = parent + "." + str(order) + "." + str(counter)
|
|
|
|
ratio = (100 / (len(layoutjson) - counter)) / 100
|
2020-09-24 10:12:23 +00:00
|
|
|
if 'ratio' in pane:
|
|
|
|
ratio = pane['ratio']
|
2020-09-19 19:27:21 +00:00
|
|
|
children[containername] = {
|
|
|
|
'type': 'VPaned' if vertical else 'HPaned',
|
|
|
|
'order': order + counter,
|
2020-09-24 10:12:23 +00:00
|
|
|
'ratio': ratio,
|
2020-09-19 19:27:21 +00:00
|
|
|
'parent': actualparent
|
|
|
|
}
|
|
|
|
actualparent = containername
|
|
|
|
|
|
|
|
if 'children' in pane:
|
2020-09-20 18:15:14 +00:00
|
|
|
self.build_container_layout(pane['children'], children, containername, counter, False if vertical else True)
|
2020-09-19 19:27:21 +00:00
|
|
|
else:
|
|
|
|
self.build_terminal_layout(pane, children, containername, counter)
|
|
|
|
|
|
|
|
counter += 1
|
|
|
|
|
2020-09-20 20:54:10 +00:00
|
|
|
def get_layout(self, layoutjson):
|
2020-09-19 19:27:21 +00:00
|
|
|
try:
|
|
|
|
vertical = True
|
|
|
|
if "vertical" in layoutjson:
|
|
|
|
vertical = layoutjson["vertical"]
|
|
|
|
del layoutjson["vertical"]
|
2023-04-11 19:51:57 +00:00
|
|
|
|
2020-09-19 19:27:21 +00:00
|
|
|
if len(layoutjson) == 1:
|
|
|
|
firstitem = next(iter(layoutjson.values()))
|
|
|
|
result = self.build_single_tab_layout(firstitem, vertical)
|
|
|
|
else:
|
|
|
|
result = self.build_multi_tab_layout(layoutjson, vertical)
|
|
|
|
|
|
|
|
dbg('Json layout is: %s' % result)
|
|
|
|
return result
|
|
|
|
except Exception as ex:
|
2020-09-20 20:54:10 +00:00
|
|
|
err('Error building a layout from file %s' % ex)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_profile(self, profilejson, baseprofile):
|
|
|
|
try:
|
|
|
|
result = copy.deepcopy(baseprofile)
|
|
|
|
|
|
|
|
result.update(profilejson)
|
|
|
|
|
|
|
|
dbg('Json profile is: %s' % result)
|
|
|
|
return result
|
|
|
|
except Exception as ex:
|
|
|
|
err('Error building a profile from json file %s' % ex)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def read_config(self, jsonfile):
|
|
|
|
if not path.exists(jsonfile):
|
|
|
|
dbg("Json config file is missing %s" % jsonfile)
|
|
|
|
return None
|
|
|
|
|
|
|
|
dbg('Loading config json from a file: %s' % jsonfile)
|
|
|
|
|
|
|
|
layoutjson = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(jsonfile) as json_file:
|
|
|
|
layoutjson = json.load(json_file)
|
|
|
|
except Exception as ex:
|
|
|
|
err('Error loading config json file %s (%s)' % (jsonfile, ex))
|
|
|
|
return None
|
|
|
|
|
|
|
|
return layoutjson
|
|
|
|
|
|
|
|
def extend_config(self, jsonfile):
|
|
|
|
configjson = self.read_config(jsonfile)
|
|
|
|
|
|
|
|
if not configjson:
|
2020-09-19 19:27:21 +00:00
|
|
|
return None
|
2020-09-20 20:54:10 +00:00
|
|
|
|
|
|
|
config = Config()
|
|
|
|
|
|
|
|
if 'profile' in configjson:
|
|
|
|
profile = self.get_profile(configjson['profile'], config.base.profiles['default'])
|
|
|
|
if profile:
|
2020-09-24 10:39:42 +00:00
|
|
|
config.base.profiles[JSON_PROFILE_NAME] = profile
|
|
|
|
self.profile_to_use = JSON_PROFILE_NAME
|
2020-09-20 20:54:10 +00:00
|
|
|
|
|
|
|
if 'layout' in configjson:
|
|
|
|
layout = self.get_layout(configjson['layout'])
|
|
|
|
if layout:
|
2020-09-24 10:39:42 +00:00
|
|
|
config.base.layouts[JSON_LAYOUT_NAME] = layout
|
|
|
|
return JSON_LAYOUT_NAME
|
2020-09-20 20:54:10 +00:00
|
|
|
|
|
|
|
return None
|