This commit is contained in:
2024-07-13 22:51:32 +03:00
parent 6a4a3228fe
commit f0209fda73
6 changed files with 137 additions and 0 deletions

22
py/cfg.py Normal file
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
py/cfg_aux.py Normal file
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
py/fs.py Normal file
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
py/fs_aux.py Normal file
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

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"