Compare commits

...

4 Commits

Author SHA1 Message Date
Fierelier b3853e5157 Expand readme 2023-10-14 13:57:42 +02:00
Fierelier 143efc7db3 Don't define distro as global twice 2023-10-14 13:57:22 +02:00
Fierelier 701335f404 Raise exception correctly 2023-10-14 13:57:01 +02:00
Fierelier a9543243b4 Handle Bunch/Munch import failure 2023-10-14 13:56:36 +02:00
2 changed files with 15 additions and 10 deletions

View File

@ -1,3 +1,4 @@
import sys
import os
# BUNCH
@ -6,11 +7,15 @@ try:
Bunch = munch.Munch
bunchify = munch.munchify
unbunchify = munch.unmunchify
except Exception:
import bunch
Bunch = munch.Bunch
bunchify = munch.bunchify
unbunchify = munch.unbunchify
except ModuleNotFoundError:
try:
import bunch
Bunch = munch.Bunch
bunchify = munch.bunchify
unbunchify = munch.unbunchify
except ModuleNotFoundError:
print("Error: Could not find munch/bunch module. Install munch on Python 3, bunch on Python 2.",file=sys.stderr)
sys.exit(1)
# GLOBALS
g = Bunch()
@ -46,7 +51,7 @@ def dorequire(name,*args,**kwargs):
def require(name,env=False,*args,**kwargs):
if env == False: env = loaded
if type(env) != Bunch: raise("require: env is not of type " +distro+ ".Bunch")
if type(env) != Bunch: raise Exception("require: env is not of type " +distro+ ".Bunch")
if not name in env:
env[name] = dorequire(name,*args,**kwargs)
return env[name]
@ -65,7 +70,7 @@ inited = False
def init(mod,modl,sp,d):
global inited
if inited: return
global distro,me,p,pUp,programName,programNameSet,s,sd,distro
global distro,me,p,pUp,programName,programNameSet,s,sd
import sys
inited = True
distro = d

View File

@ -19,12 +19,12 @@ Provides the following mfp.* (global to all scripts):
* pUp(path): Goes up (..) in the path.
* 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.
* require(name,env): Runs a module from mfp.paths, saves its global, and returns the script's global as a Bunch. The module's global is saved in env. If env is not defined, it saves the global in the main env. 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().
* 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.
* g: A Bunch where you can store variables accessible by all mfp modules and scripts.
* Bunch(): Creates a Bunch. Read about it here: https://github.com/Infinidat/munch
* bunchify(list): Converts a list into a Bunch.
* unbunchify(Bunch): Converts a Bunch into a regular list.
* unbunchify(Bunch): Converts a Bunch into a regular list.