def eval_template(template, cmd):
cmdname = cmd.__name__
classname = to_classname(cmd.__name__)
# Use the same technic as namedtuple
class_definition = template.format(
cmdname = cmdname,
docstring = cmd.__doc__ if cmd.__doc__ is not None else '',
classname = classname
)
namespace = dict(__name__='template_%s' % cmdname)
exec(class_definition, namespace)
result = namespace[classname]
result.cmd = cmd
result._source = class_definition
try:
result.__module__ = sys._getframe(1).f_globals.get(
'__name__', '__main__')
except (AttributeError, ValueError):
pass
return result
评论列表
文章目录