def test_importing(self): projdirname = 'myproj.projdir' dirstruct = { os.path.splitext(projdirname)[0]: { 'top.py': """ from openmdao.main.api import Component class MyClass(Component): pass """, 'pkgdir': { '__init__.py': '', 'pkgfile.py': 'from openmdao.main.api import Component, Assembly', 'pkgdir2': { '__init__.py': '', 'pkgfile2.py': """ from openmdao.main.api import Component class PkgClass2(Component): def __init__(self, somearg=8, anotherarg=False): super(PkgClass2, self).__init__() """, } }, 'plaindir': { 'plainfile.py': """ from pkgdir.pkgdir2.pkgfile2 import PkgClass2 p = PkgClass2() """, }, }, } build_directory(dirstruct, topdir=os.getcwd()) try: sys.path_hooks = [ProjFinder] + sys.path_hooks sys.path = [os.path.join(os.getcwd(), projdirname)] + sys.path __import__('top') __import__('pkgdir.pkgfile') __import__('pkgdir.pkgdir2.pkgfile2') mod = sys.modules['pkgdir.pkgdir2.pkgfile2'] expected_classname = 'pkgdir.pkgdir2.pkgfile2.PkgClass2' matches = _match_insts([expected_classname]) self.assertEqual(matches, set()) sig = get_signature('pkgdir.pkgdir2.pkgfile2.PkgClass2') self.assertEqual(sig['args'], [['somearg', '8'], ['anotherarg', 'False']]) inst = getattr(mod, 'PkgClass2')() # create an inst of PkgClass2 matches = _match_insts([expected_classname]) self.assertEqual(matches, set([expected_classname])) finally: sys.path = sys.path[1:]
def test_importing(self): projdirname = 'myproj.projdir' dirstruct = { os.path.splitext(projdirname)[0]: { 'top.py': """ from openmdao.main.api import Component class MyClass(Component): pass """, 'pkgdir': { '__init__.py': '', 'pkgfile.py': 'from openmdao.main.api import Component, Assembly', 'pkgdir2': { '__init__.py': '', 'pkgfile2.py': """ from openmdao.main.api import Component class PkgClass2(Component): def __init__(self, somearg=8, anotherarg=False): super(PkgClass2, self).__init__() """, } }, 'plaindir': { 'plainfile.py': """ from pkgdir.pkgdir2.pkgfile2 import PkgClass2 p = PkgClass2() """, }, }, } build_directory(dirstruct, topdir=os.getcwd()) try: sys.path_hooks = [ProjFinder]+sys.path_hooks sys.path = [os.path.join(os.getcwd(), projdirname)]+sys.path __import__('top') __import__('pkgdir.pkgfile') __import__('pkgdir.pkgdir2.pkgfile2') mod = sys.modules['pkgdir.pkgdir2.pkgfile2'] expected_classname = 'pkgdir.pkgdir2.pkgfile2.PkgClass2' matches = _match_insts([expected_classname]) self.assertEqual(matches, set()) sig = get_signature('pkgdir.pkgdir2.pkgfile2.PkgClass2') self.assertEqual(sig['args'], [['somearg', '8'], ['anotherarg', 'False']]) inst = getattr(mod, 'PkgClass2')() # create an inst of PkgClass2 matches = _match_insts([expected_classname]) self.assertEqual(matches, set([expected_classname])) finally: sys.path = sys.path[1:]
def file_forces_reload(self, filename): """Returns True if the given file (assumed to be a file in the project) has classes that have been instantiated in the current process or if the file is a macro file. Note that this doesn't keep track of removes/deletions, so if an instance was created earlier and then deleted, it will still be reported. """ pdf = self.projdirfactory if pdf: if self.is_macro(filename): return True filename = filename.lstrip('/') filename = os.path.join(self.proj.path, filename) info = pdf._files.get(filename) if info and _match_insts(info.classes.keys()): return True return False