qcs-python/user/modules/app.py

83 lines
1.9 KiB
Python
Raw Normal View History

import sys
import os
import importlib
from collections import OrderedDict
def initConfigDirs():
for i in configDirs:
fi = os.path.join(i,"modules")
try:
del sys.path[sys.path.index(fi)]
except Exception:
pass
for i in reversed(configDirs):
sys.path.insert(0,os.path.join(i,"modules"))
def configFindFile(name):
for i in configDirs:
fi = os.path.join(i,name)
if os.path.isfile(fi): yield fi
def parseVariables(st):
rtn = OrderedDict()
st = st.replace("\r","").split("\n")
for line in st:
line = line.split("#",1)[0]
line = line.strip(" \t")
line = line.split("=",1)
if len(line) < 2: continue
line[0] = line[0].strip(" \t")
line[1] = line[1].strip(" \t")
rtn[line[0]] = line[1]
return rtn
def loadmod(mod):
global distro
return importlib.import_module(distro + "." + mod)
def init(scp,dist):
global mod,distro,p,s,sd,configDirs
mod = {}
distro = dist
p = os.path.join
s = scp
sd = os.path.dirname(s)
configDirs = [
p(os.environ.get("HOME",os.environ.get("USERPROFILE",None)),".config",distro),
p(sd,"user")
]
initConfigDirs()
2023-05-02 15:12:25 +00:00
modules = OrderedDict()
for file in configFindFile("modules.txt"):
tbl = parseVariables(open(file).read())
for i in tbl:
if not i in modules:
modules[i] = tbl[i]
length = len(sys.argv)
index = 1
while index < length:
arg = sys.argv[index]
if arg.startswith("--app:module"):
arg = arg.split("--app:module:",1)[1].split("=",1)
modules[arg[0]] = arg[1].replace("$DISTRO",distro)
del sys.argv[index]
length -= 1
continue
index += 1
glb = globals()
for module in modules:
modules[module] = modules[module].replace("$DISTRO",distro)
print("* Importing '" +modules[module]+ "' -> '" +module+ "' ...")
mod[module] = importlib.import_module(modules[module])
for module in modules:
if hasattr(mod[module],"appInit"):
print("* Initializing '" +module+ "' ...")
mod[module].appInit()
print("* Running app.")