parent
commit
f0209fda73
6 changed files with 137 additions and 0 deletions
  1. +14
    -0
      main.py
  2. +22
    -0
      py/cfg.py
  3. +47
    -0
      py/cfg_aux.py
  4. +40
    -0
      py/fs.py
  5. +10
    -0
      py/fs_aux.py
  6. +4
    -0
      py/ht_Context.py

+ 14
- 0
main.py View File

@@ -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)


+ 22
- 0
py/cfg.py View File

@@ -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

+ 47
- 0
py/cfg_aux.py View File

@@ -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

+ 40
- 0
py/fs.py View File

@@ -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

+ 10
- 0
py/fs_aux.py View File

@@ -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

+ 4
- 0
py/ht_Context.py View File

@@ -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"


Loading…
Cancel
Save