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.itk_load_swig_module(module, namespace) for k, v in namespace.items(): setattr(self, k, v)
def __getattribute__(self, attr): value = types.ModuleType.__getattribute__(self, attr) if value is not_loaded: module = self.__belong_lazy_attributes[attr] namespace = {} itkBase.itk_load_swig_module(module, namespace) self.loaded_lazy_modules.add(module) for k, v in namespace.items(): setattr(self, k, v) value = namespace[attr] return value
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.itk_base_global_lazy_attributes[name] for module in modules: # find the module's name in sys.modules, or create a new module so named swig_module_name = "itk." + module + "Python" this_module = sys.modules.setdefault( swig_module_name, itkBase.create_itk_module(module)) namespace = {} if not hasattr(this_module, "__templates_loaded"): itkBase.itk_load_swig_module(module, namespace)
def _initialize_module(): """ A function to explicitly avoid polluting the global namespace """ def _get_lazy_attributes(local_lazy_attributes, l_module, l_data): template_names = [t[0] for t in l_data["templates"]] is_in_library = [t[3] for t in l_data["templates"] if len(t) > 3] local_attributes = dict([(n, l_module) for n in template_names]) attributes_in_module = dict( [(n, belongs) for n, belongs in zip(template_names, is_in_library)] ) items = local_attributes.items() for kk, vv in items: if is_in_library and attributes_in_module[kk] is True: local_lazy_attributes.setdefault(kk, []).insert(0, vv) else: local_lazy_attributes.setdefault(kk, []).append(vv) if "snake_case_functions" in l_data: for function in l_data["snake_case_functions"]: local_lazy_attributes.setdefault(function, []).append(l_module) return l_module, l_data import itkBase import itkConfig import itkLazy import itkTypes import itkExtras import os import sys this_module = sys.modules[__name__] if itkConfig.LazyLoading: # If we are loading lazily (on-demand), make a dict mapping the available # classes/functions/etc. (read from the configuration modules) to the # modules they are declared in. Then pass that dict to a LazyITKModule # instance and (later) do some surgery on sys.modules so that the 'itk' # module becomes that new instance instead of what is executed from this # file. lazy_attributes = {} for module, data in itkBase.itk_base_global_module_data.items(): _get_lazy_attributes(lazy_attributes, module, data) if isinstance(this_module, 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. this_module.__init__(__name__, lazy_attributes) del lazy_attributes else: this_module = itkLazy.LazyITKModule(__name__, lazy_attributes) 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.itk_base_global_known_modules: itkBase.itk_load_swig_module(module, this_module.__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(this_module, k, v) for k, v in itkExtras.__dict__.items(): if not k.startswith("_"): setattr(this_module, k, v) # Populate itk.ITKModuleName for module, data in itkBase.itk_base_global_module_data.items(): attributes = {} module, data = _get_lazy_attributes(attributes, module, data) itk_module = itkLazy.LazyITKModule(module, attributes) setattr(this_module, module, itk_module) # Set the __path__ attribute, which is required for this module to be used as a # package setattr(this_module, "__path__", __path__) setattr(this_module, "__spec__", __spec__) # pytype: disable=name-error 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__] = this_module else: # do some cleanup del module, this_module, itk_module del itkBase, itkConfig, itkLazy, itkTypes, itkExtras, os, sys
def _initialize_module(): """ A function to explicitly avoid polluting the global namespace """ def _get_lazy_attributes(local_lazy_attributes, l_module, l_data): template_feature_tuples = [ (t[0], t[3]) if len(t) > 3 else (t[0], False) for t in l_data["templates"] ] for (template_name, is_in_library) in template_feature_tuples: if is_in_library: # insert in front front if in library local_lazy_attributes.setdefault(template_name, []).insert(0, l_module) else: # append to end local_lazy_attributes.setdefault(template_name, []).append(l_module) if "snake_case_functions" in l_data: for function in l_data["snake_case_functions"]: # snake case always appended to end local_lazy_attributes.setdefault(function, []).append(l_module) import itkBase import itkConfig import itkLazy import sys if itkConfig.LazyLoading: # If we are loading lazily (on-demand), make a dict mapping the available # classes/functions/etc. (read from the configuration modules) to the # modules they are declared in. Then pass that dict to a LazyITKModule # instance and (later) do some surgery on sys.modules so that the 'itk' # module becomes that new instance instead of what is executed from this # file. lazy_attributes = {} for module, data in itkBase.itk_base_global_module_data.items(): _get_lazy_attributes(lazy_attributes, module, data) if isinstance(sys.modules[__name__], 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. sys.modules[__name__].__init__(__name__, lazy_attributes) del lazy_attributes else: # Create a new LazyITKModule lzy_module = itkLazy.LazyITKModule(__name__, lazy_attributes) # Set the __path__ attribute, which is required for this_module # to be used as a package setattr(lzy_module, "__path__", __path__) setattr(lzy_module, "__spec__", __spec__) # pytype: disable=name-error # Now override the default sys.modules[__name__] (__name__ == 'itk' ) sys.modules[__name__] = lzy_module 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.itk_base_global_module_data.keys(): itkBase.itk_load_swig_module(module, sys.modules[__name__].__dict__) # Populate itk.ITKModuleName for module, data in itkBase.itk_base_global_module_data.items(): attributes = {} _get_lazy_attributes(attributes, module, data) itk_module = itkLazy.LazyITKModule(module, attributes) setattr(sys.modules[__name__], module, itk_module) # Regardless of how it was loaded, fill up the itk module with the ITK types # and extras. import itkTypes for k, v in itkTypes.__dict__.items(): if k != "itkCType" and not k.startswith("_"): setattr(sys.modules[__name__], k, v) del itkTypes import itkInitHelpers for k, v in itkInitHelpers.__dict__.items(): if not k.startswith("_"): setattr(sys.modules[__name__], k, v) del itkInitHelpers import itkExtras for k, v in itkExtras.__dict__.items(): if not k.startswith("_"): setattr(sys.modules[__name__], k, v) del itkExtras # -- # Needed to propagate symbol to itk.image from itkTemplate.image import itkTemplate setattr(sys.modules[__name__], "image", itkTemplate.image) setattr(sys.modules[__name__], "output", itkTemplate.output) del itkTemplate