From f0209fda73df33a4e2ebf13c299626733a2532e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 13 Jul 2024 22:51:32 +0300 Subject: [PATCH] d --- main.py | 14 ++++++++++++++ py/cfg.py | 22 ++++++++++++++++++++++ py/cfg_aux.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ py/fs.py | 40 ++++++++++++++++++++++++++++++++++++++++ py/fs_aux.py | 10 ++++++++++ py/ht_Context.py | 4 ++++ 6 files changed, 137 insertions(+) create mode 100644 py/cfg.py create mode 100644 py/cfg_aux.py create mode 100644 py/fs.py create mode 100644 py/fs_aux.py diff --git a/main.py b/main.py index 3f6831a..7b24c56 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,12 @@ import os 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])) 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") import arcade +from cfg import * from cld import * from ctx import * +from fs import * from desktop_Platform import * from desktop_Window import * from ht_Context import * ctrl = ctx_Controller(ht_createContext()) ctrl.registerFunctions([ + cfg_parseConfigTree, + fs_locateConfigDir, + fs_readConfig, ]) def printDbg(c): @@ -34,6 +45,9 @@ def process(c): ### desktop_deselectMismatchedTiles(p) ctrl.registerCallback(process) +ctrl.set("cfgPath", CFG) +ctrl.set("scriptDir", SCRIPT_DIR) + ctrl.set("windowWidth", 900) ctrl.set("windowHeight", 600) ctrl.set("windowAntialiasing", False) diff --git a/py/cfg.py b/py/cfg.py new file mode 100644 index 0000000..3aed14a --- /dev/null +++ b/py/cfg.py @@ -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 diff --git a/py/cfg_aux.py b/py/cfg_aux.py new file mode 100644 index 0000000..3e7c68b --- /dev/null +++ b/py/cfg_aux.py @@ -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 diff --git a/py/fs.py b/py/fs.py new file mode 100644 index 0000000..82ccfea --- /dev/null +++ b/py/fs.py @@ -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 diff --git a/py/fs_aux.py b/py/fs_aux.py new file mode 100644 index 0000000..5e6c9a9 --- /dev/null +++ b/py/fs_aux.py @@ -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 diff --git a/py/ht_Context.py b/py/ht_Context.py index f523e41..5eebe45 100644 --- a/py/ht_Context.py +++ b/py/ht_Context.py @@ -1,5 +1,9 @@ class ht_Context: def __init__(self): + self.cfgContents = [] + self.cfgDir = None + self.cfgPath = None + self.cfgTree = {} self.didLaunch = False self.recentField = "none" self.windowBackgroundColor = "#000000"