Get a python module by its given fully-qualified name. @param modname: The fully-qualified Python module name to load. @type modname: C{str} @return: an object representing the module identified by C{modname} @rtype: L{PythonModule} @raise KeyErr
(self, modname)
| 687 | yield PathEntry(fp, self) |
| 688 | |
| 689 | def __getitem__(self, modname): |
| 690 | """ |
| 691 | Get a python module by its given fully-qualified name. |
| 692 | |
| 693 | @param modname: The fully-qualified Python module name to load. |
| 694 | |
| 695 | @type modname: C{str} |
| 696 | |
| 697 | @return: an object representing the module identified by C{modname} |
| 698 | |
| 699 | @rtype: L{PythonModule} |
| 700 | |
| 701 | @raise KeyError: if the module name is not a valid module name, or no |
| 702 | such module can be identified as loadable. |
| 703 | """ |
| 704 | # See if the module is already somewhere in Python-land. |
| 705 | moduleObject = self.moduleDict.get(modname) |
| 706 | if moduleObject is not None: |
| 707 | # we need 2 paths; one of the path entry and one for the module. |
| 708 | pe = PathEntry( |
| 709 | self._smartPath(self._findEntryPathString(moduleObject)), self |
| 710 | ) |
| 711 | mp = self._smartPath(moduleObject.__file__) |
| 712 | return PythonModule(modname, mp, pe) |
| 713 | |
| 714 | # Recurse if we're trying to get a submodule. |
| 715 | if "." in modname: |
| 716 | pkg = self |
| 717 | for name in modname.split("."): |
| 718 | pkg = pkg[name] |
| 719 | return pkg |
| 720 | |
| 721 | # Finally do the slowest possible thing and iterate |
| 722 | for module in self.iterModules(): |
| 723 | if module.name == modname: |
| 724 | return module |
| 725 | raise KeyError(modname) |
| 726 | |
| 727 | def __contains__(self, module): |
| 728 | """ |
no test coverage detected