Commit 336e53c6 authored by Nicolas Dumazet's avatar Nicolas Dumazet

generic code to create dynamic Python modules


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@38643 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent a77a2c88
from types import ModuleType
import sys
class DynamicModule(ModuleType):
"""This module may generate new objects at runtime."""
# it's useful to have such a generic utility
# please subclass it if you need ERP5-specific behaviors
def __init__(self, name, factory, doc=None):
super(DynamicModule, self).__init__(name, doc=doc)
self._factory = factory
def __getattr__(self, name):
if name == '__path__':
raise AttributeError('%s does not have __path__' % (self,))
obj = self._factory(name)
if hasattr(obj, '__module__'):
obj.__module__ = self.__name__
setattr(self, name, obj)
return obj
def dynamicmodule(name, factory):
d = DynamicModule(name, factory)
sys.modules[name] = d
return d
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment