Add dorequire()

This commit is contained in:
Fierelier 2022-12-19 09:52:33 +01:00
parent e2f8ea7b67
commit 98bfb665c3
2 changed files with 9 additions and 6 deletions

View File

@ -35,14 +35,16 @@ def docode(st,name = "Unknown"):
def dofile(path):
return docode(open(path,encoding="utf-8").read(),path)
def dorequire(name,*args,**kwargs):
for path in paths:
path = path.replace("?",name)
if os.path.isfile(path):
return dofile(path,*args,**kwargs)
raise Exception("Library " +name+ " not found.")
def require(name,*args,**kwargs):
if not name in loaded:
for path in paths:
path = path.replace("?",name)
if os.path.isfile(path):
loaded[name] = dofile(path,*args,**kwargs)
return loaded[name]
raise Exception("Library " +name+ " not found.")
loaded[name] = dorequire(name,*args,**kwargs)
return loaded[name]
# PROGRAM

View File

@ -19,6 +19,7 @@ Provides the following mfp.* (global to all scripts):
* setProgramName(name): Set the name of your main program, only works on first call. You should run it before running any other mfp modules.
* programName: The name of your program. DO NOT modify this variable directly, use mfp.setProgramName().
* require(name): Runs a module from mfp.paths, saves its global, and returns the script's global as a Bunch. If a module of the same name has already been required, it returns the already saved global.
* dorequire(name): Does the same as above, but does not save the globals, and can re-require.
* paths: The directories mfp should search for modules in with mfp.require(). ? in all paths is replaced with the name given to mfp.require().
* dofile(path): Runs a script from a raw path. It does not use mfp.paths. Returns the script's global as a Bunch.
* docode(text): Runs string as Python code. Returns the script's global as a Bunch.