def testListOfImportsIsSameAsPythonFiles(self):
		"""
		Tests if the list of keys in the dictionary returned from
			DynamicLoader.getModules is equal to list of .py files in the
			requested module directory
		"""
		resultDictionary = Modules.DynamicLoader.getModules("Readers")
		readerPackageDir = os.path.join(bioimagepath, scripting.get_module_dir(), "Readers")
		pythonFilesInPackageDir = glob.glob(os.path.join(readerPackageDir, "*.py"))
		pythonBaseFilesWithoutExtension = [os.path.basename(file).replace(".py", "") \
											for file in pythonFilesInPackageDir]
		self.assertEquals(sorted(resultDictionary.keys()), sorted(pythonBaseFilesWithoutExtension))
Example #2
0
    def _createGlobPathAndSysPath(self, moduleSubDir, globExtension):
        """
		Creates a path to be used in a glob expression and a path to add to sys.path
		so that modules that the glob expression will fetch can be imported. Returns these in a tuple:
		(globPath, pathForSysPath)
		Ex. ("moduleSubDir = Readers, globExtension = *.py") -> (Modules/Readers/*.py, Modules/Readers)
		"""
        # Create list of Module-files to load based on extension
        modulePath = scripting.get_module_dir()
        Logging.info("Module dir=%s" % modulePath, kw="modules")
        pathForSysPath = os.path.join(modulePath, moduleSubDir)
        globPath = pathForSysPath
        if globExtension:
            globPath = os.path.join(globPath, globExtension)
        Logging.info("Path to modes: %s" % pathForSysPath, kw="modules")
        return globPath, pathForSysPath
	def _createGlobPathAndSysPath(self, moduleSubDir, globExtension):
		"""
		Creates a path to be used in a glob expression and a path to add to sys.path
		so that modules that the glob expression will fetch can be imported. Returns these in a tuple:
		(globPath, pathForSysPath)
		Ex. ("moduleSubDir = Readers, globExtension = *.py") -> (Modules/Readers/*.py, Modules/Readers)
		"""
		# Create list of Module-files to load based on extension
		modulePath = scripting.get_module_dir()
		Logging.info("Module dir=%s" % modulePath, kw = "modules")
		pathForSysPath = os.path.join(modulePath, moduleSubDir)
		globPath = pathForSysPath
		if globExtension:
			globPath = os.path.join(globPath, globExtension)
		Logging.info("Path to modes: %s" % pathForSysPath, kw = "modules")
		return globPath, pathForSysPath
	def doNotTestLoadingOfModulesInSubdirectory(self):
		"""
		Checks that you can import modules from a directory that is a subdirectory of
		a directory in the Modules directory. For this test I try to import Task/Adjust and check that
		the list of modules is the same as the list of python files in that directory.
		"""
#		class MockScriptingClass:
#			def get_module_dir(self):
#				return unittestPath
		adjustPath = "Task/Adjust"
		loadedModulesDictionary = Modules.DynamicLoader.getModules(adjustPath)
		os.chdir(os.path.join(bioimagepath, "source"))
		absAdjustPackagePath = os.path.join(bioimagepath, "source", scripting.get_module_dir(), adjustPath)
		pythonFilesInPackageDir = glob.glob(os.path.join(absAdjustPackagePath, "*.py"))
		pythonBaseFilesWithoutExtension = [os.path.basename(file).replace(".py", "") \
											for file in pythonFilesInPackageDir]
		self.assertEquals(sorted(loadedModulesDictionary.keys()), sorted(pythonFilesInPackageDir))