qcs-python/user/modules/app.py
2023-05-02 04:16:22 +02:00

66 lines
1.5 KiB
Python

import sys
import os
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 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()
modules = {}
import toml
for file in configFindFile("modules.toml"):
tbl = toml.loads(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
import importlib
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.")