예제 #1
0
def load_wrapitk_module(path, name):
    """ Load a WrapITK module, and add its symbols to the itk module """

    old_path = sys.path

    sys.path = sys.path + [path]

    config_module_name = "{0}Config".format(name)
    config_module = __import__(config_module_name)

    path = os.path.dirname(os.path.abspath(config_module.__file__))
    if path not in sys.path:
        sys.path = sys.path + [path]

    data = config_module.__dict__
    itkBase.module_data[name] = data

    namespace = {}
    itkBase.LoadModule(name, namespace)

    for k, v in namespace.items():
        if not hasattr(itk, k):
            setattr(itk, k, v)

    sys.path = old_path
예제 #2
0
 def __getattribute__(self, attr):
     value = types.ModuleType.__getattribute__(self, attr)
     if value is not_loaded:
         module = self.__belong_lazy_attributes[attr]
         namespace = {}
         itkBase.LoadModule(module, namespace)
         for k, v in namespace.items():
             setattr(self, k, v)
         value = namespace[attr]
     return value
예제 #3
0
 def __setstate__(self, state):
     self.__dict__.update(state)
     for module_name, lazy_attributes in state["lazy_modules"]:
         self.__dict__.update(
             {module_name: LazyITKModule(module_name, lazy_attributes)})
     for module in state["loaded_lazy_modules"]:
         namespace = {}
         itkBase.LoadModule(module, namespace)
         for k, v in namespace.items():
             setattr(self, k, v)
예제 #4
0
    def _LoadModules(self):
        """Loads all the module that may have not been loaded by the lazy loading system.

        If multiple modules use the same object, the lazy loading system is only going to
        load the module in which the object belongs. The other modules will be loaded only when necessary.
        """
        name=self.__name__.split('::')[-1] # Remove 'itk::' or 'itk::Function::'
        modules = itkBase.lazy_attributes[name]
        for module in modules:
            # find the module's name in sys.modules, or create a new module so named
            this_module = sys.modules.setdefault(module, types.ModuleType(module))
            namespace = {}
            if not hasattr(this_module, '__templates_loaded'):
                itkBase.LoadModule(module, namespace)
예제 #5
0
 def __getattribute__(self, attr):
     value = types.ModuleType.__getattribute__(self, attr)
     if value is not_loaded:
         module = self.__lazy_attributes[attr]
         namespace = {}
         itkBase.LoadModule(module, namespace)
         # Load into 'namespace' first, then self.__dict__ (via setattr) to
         # prevent the warnings about overwriting the 'NotLoaded' values already
         # in in self.__dict__ we would get if we just passed self.__dict__ to
         # itkBase.LoadModule.
         for k, v in namespace.items():
             setattr(self, k, v)
         value = namespace[attr]
     return value
예제 #6
0
        templateNames = [t[0] for t in data['templates']]
        attributes = dict([(n, module) for n in templateNames])
        lazyAttributes.update(attributes)
    if isinstance(thisModule, itkLazy.LazyITKModule):
        # Handle reload case where we've already done this once.
        # If we made a new module every time, multiple reload()s would fail
        # because the identity of sys.modules['itk'] would always be changing.
        thisModule.__init__(__name__, lazyAttributes)
        del lazyAttributes
    else:
        thisModule = itkLazy.LazyITKModule(__name__, lazyAttributes)
else:
    # We're not lazy-loading. Just load the modules in the order specified in
    # the known_modules list for consistency.
    for module in itkBase.known_modules:
        itkBase.LoadModule(module, thisModule.__dict__)

# Regardless of how it was loaded, fill up the itk module with the ITK types
# and extras.
for k, v in itkTypes.__dict__.items():
    if k != 'itkCType' and not k.startswith('_'):
        setattr(thisModule, k, v)
for k, v in itkExtras.__dict__.items():
    if not k.startswith('_'):
        setattr(thisModule, k, v)

if itkConfig.LazyLoading:
    # this has to be the last step, else python gets confused about itkTypes
    # and itkExtras above. I'm not sure why...
    sys.modules[__name__] = thisModule
else: