1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
"""
Macro
Macro support and dispatcher
"""
import os
from inspect import getmembers, getmodule, isfunction
from creoleparser import parse_args
class Macro:
def __init__(self, name, arg_string, body, isblock):
super().__init__()
self.name = name
self.arg_string = arg_string
self.body = body
self.isblock = isblock
def dispatcher(name, arg_string, body, isblock, environ):
if name in environ['macros']:
macro = Macro(name, arg_string, body, isblock)
args, kwargs = parse_args(arg_string)
try:
return environ['macros'][name](macro, environ, *args, **kwargs)
except Exception as e:
return f'ERROR: Error while executing macro {name!r} ({e})'
else:
return 'Macro not found!'
def loadMacros():
path = os.path.abspath(os.path.dirname(__file__))
def p(x):
return os.path.splitext(x)[1] == '.py'
modules = [x for x in os.listdir(path) if p(x) and x != '__init__.py']
macros = {}
for module in modules:
name, _ = os.path.splitext(module)
moduleName = f'{__package__}.{name}'
m = __import__(moduleName, globals(), locals(), __package__)
def p(x):
return isfunction(x) and getmodule(x) is m
for name, function in getmembers(m, p):
name = name.replace('_', '-')
try:
macros[name] = function
except Exception:
continue
return macros
|