Example #1
0
	def GetModule(filename):
		""" Give module object from python file path. Warning, the name of python_file must be the same of the classe name.
		"""

		dir_name = os.path.dirname(filename)

		### if python_file is ...../toto.amd/Atomic_Model.py, then the parent dir is zipfile.
		if zipfile.is_zipfile(dir_name):
			zf = ZipManager.Zip(dir_name)
			return zf.GetModule()
		elif zipfile.is_zipfile(filename):
			zf = ZipManager.Zip(filename)
			return zf.GetModule()
		### if python file is on the web !
		elif filename.startswith(('http','https')):
			net = Net(filename)
			return net.GetModule()
		### pure python file
		else:

			### add path to sys.path
			if dir_name not in sys.path:
				sys.path.append(dir_name)

			module_name = os.path.basename(filename).split('.py')[0]

			# find and load module
			try:
				f, fn, description = imp.find_module(module_name, [dir_name])
				module = imp.load_module(module_name, f, fn, description)
				f.close()
				return module

			except Exception, info:
				return sys.exc_info()
Example #2
0
    def GetModule(filename):
        """ Give module object from python file path. Warning, the name of python_file must be the same of the classe name.
		"""

        dir_name = os.path.dirname(filename)

        ### if python_file is ...../toto.amd/Atomic_Model.py, then the parent dir is zipfile.
        if zipfile.is_zipfile(dir_name):
            zf = ZipManager.Zip(dir_name)
            return zf.GetModule()
        elif zipfile.is_zipfile(filename):
            zf = ZipManager.Zip(filename)
            return zf.GetModule()
        ### if python file is on the web !
        elif filename.startswith(('http', 'https')):
            net = Net(filename)
            return net.GetModule()
        ### pure python file
        else:

            ### add path to sys.path recursively
            current_dirname = dir_name
            while (current_dirname != os.path.dirname(current_dirname)):
                if current_dirname not in sys.path:
                    sys.path.append(current_dirname)
                current_dirname = os.path.dirname(current_dirname)

            module_name = os.path.basename(filename).split('.py')[0]

            # find and load module
            try:
                #name, ext = os.path.splitext(module_name)
                #pkg = '.'.join(modulename.split('.')[0:-1])
                #module = importlib.import_module(name, package=pkg)

                f, fn, description = imp.find_module(module_name, [dir_name])
                module = imp.load_module(module_name, f, fn, description)
                f.close()
                return module

            except Exception as info:
                sys.stderr.write(
                    _("Module %s not imported from %s!\n" %
                      (module_name, dir_name)))
                return sys.exc_info()