@@ -1,6 +1,12 @@ | |||||
import os | import os | ||||
import sys | import sys | ||||
if len(sys.argv) < 2: | |||||
print("Usage: python3 /path/to/main.py /path/to/ht.config") | |||||
sys.exit(1) | |||||
CFG = os.path.realpath(sys.argv[1]) | |||||
SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) | SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) | ||||
sys.path.append(f"{SCRIPT_DIR}/../cross-language-dialect/ctx") | sys.path.append(f"{SCRIPT_DIR}/../cross-language-dialect/ctx") | ||||
@@ -8,14 +14,19 @@ sys.path.append(f"{SCRIPT_DIR}/../cross-language-dialect/lib") | |||||
sys.path.append(f"{SCRIPT_DIR}/py") | sys.path.append(f"{SCRIPT_DIR}/py") | ||||
import arcade | import arcade | ||||
from cfg import * | |||||
from cld import * | from cld import * | ||||
from ctx import * | from ctx import * | ||||
from fs import * | |||||
from desktop_Platform import * | from desktop_Platform import * | ||||
from desktop_Window import * | from desktop_Window import * | ||||
from ht_Context import * | from ht_Context import * | ||||
ctrl = ctx_Controller(ht_createContext()) | ctrl = ctx_Controller(ht_createContext()) | ||||
ctrl.registerFunctions([ | ctrl.registerFunctions([ | ||||
cfg_parseConfigTree, | |||||
fs_locateConfigDir, | |||||
fs_readConfig, | |||||
]) | ]) | ||||
def printDbg(c): | def printDbg(c): | ||||
@@ -34,6 +45,9 @@ def process(c): | |||||
### desktop_deselectMismatchedTiles(p) | ### desktop_deselectMismatchedTiles(p) | ||||
ctrl.registerCallback(process) | ctrl.registerCallback(process) | ||||
ctrl.set("cfgPath", CFG) | |||||
ctrl.set("scriptDir", SCRIPT_DIR) | |||||
ctrl.set("windowWidth", 900) | ctrl.set("windowWidth", 900) | ||||
ctrl.set("windowHeight", 600) | ctrl.set("windowHeight", 600) | ||||
ctrl.set("windowAntialiasing", False) | ctrl.set("windowAntialiasing", False) | ||||
@@ -0,0 +1,22 @@ | |||||
import os | |||||
from cfg_aux import * | |||||
from cld import * | |||||
from ht_Context import * | |||||
# Construct section -> key -> value tree from config contents | |||||
# | |||||
# Conditions: | |||||
# 1. Config contents have just become available | |||||
@cld_by_value | |||||
def cfg_parseConfigTree( | |||||
c: ht_Context | |||||
) -> ht_Context: | |||||
if ( | |||||
c.recentField == "cfgContents" | |||||
): | |||||
c.cfgTree = cfg_aux_tree(c.cfgContents) | |||||
c.recentField = "cfgTree" | |||||
return c | |||||
c.recentField = "none" | |||||
return c |
@@ -0,0 +1,47 @@ | |||||
from cld import * | |||||
# Convert config contents to tree: sections -> keys -> values | |||||
def cfg_aux_tree( | |||||
cfgContents: [str] | |||||
) -> dict[str, dict[str, str]]: | |||||
tree: dict[str, dict[str, str]] = {} | |||||
currentSection = None | |||||
n = cld_len(cfgContents) | |||||
for i in range(0, n): | |||||
line = cfgContents[i] | |||||
# Section. | |||||
if ( | |||||
cld_startswith(line, "[") | |||||
): | |||||
currentSection = cfg_aux_treeCreateSection(tree, line) | |||||
# Key = Value. | |||||
else: | |||||
cfg_aux_treeSetKeyValue(tree, currentSection, line) | |||||
return tree | |||||
# Create new section in the tree | |||||
def cfg_aux_treeCreateSection( | |||||
tree: dict[str, dict[str, str]], | |||||
line: str | |||||
) -> str: | |||||
# Strip characters `[` and `]` at both ends. | |||||
sectionName = line[1:-1] | |||||
tree[sectionName] = {} | |||||
return sectionName | |||||
# Register key-value pair in the tree | |||||
def cfg_aux_treeSetKeyValue( | |||||
tree: dict[str, dict[str, str]], | |||||
sectionName: str, | |||||
line: str | |||||
): | |||||
parts = line.split(" = ") | |||||
# Ignore invalid key-value pairs. | |||||
if ( | |||||
cld_len(parts) != 2 | |||||
): | |||||
return | |||||
key = parts[0] | |||||
value = parts[1] | |||||
tree[sectionName][key] = value |
@@ -0,0 +1,40 @@ | |||||
import os | |||||
from cld import * | |||||
from fs_aux import * | |||||
from ht_Context import * | |||||
# Extract directory from config path | |||||
# | |||||
# Conditions: | |||||
# 1. Config path has just been set | |||||
@cld_by_value | |||||
def fs_locateConfigDir( | |||||
c: ht_Context | |||||
) -> ht_Context: | |||||
if ( | |||||
c.recentField == "cfgPath" | |||||
): | |||||
c.cfgDir = os.path.dirname(c.cfgPath) | |||||
c.recentField = "cfgDir" | |||||
return c | |||||
c.recentField = "none" | |||||
return c | |||||
# Read config file contents | |||||
# | |||||
# Conditions: | |||||
# 1. Config path has just been set | |||||
@cld_by_value | |||||
def fs_readConfig( | |||||
c: ht_Context | |||||
) -> ht_Context: | |||||
if ( | |||||
c.recentField == "cfgPath" | |||||
): | |||||
c.cfgContents = fs_aux_readFile(c.cfgPath) | |||||
c.recentField = "cfgContents" | |||||
return c | |||||
c.recentField = "none" | |||||
return c |
@@ -0,0 +1,10 @@ | |||||
# Read file into an array of strings | |||||
def fs_aux_readFile( | |||||
fileName: str | |||||
) -> [str]: | |||||
lines = [] | |||||
with open(fileName) as file: | |||||
for line in file: | |||||
lines.append(line.rstrip()) | |||||
return lines |
@@ -1,5 +1,9 @@ | |||||
class ht_Context: | class ht_Context: | ||||
def __init__(self): | def __init__(self): | ||||
self.cfgContents = [] | |||||
self.cfgDir = None | |||||
self.cfgPath = None | |||||
self.cfgTree = {} | |||||
self.didLaunch = False | self.didLaunch = False | ||||
self.recentField = "none" | self.recentField = "none" | ||||
self.windowBackgroundColor = "#000000" | self.windowBackgroundColor = "#000000" | ||||