def load_plugins():
    config = ConfigParser.ConfigParser()
    config.read("config.ini")
    words_plugin = config.get("Plugins", "words")
    frequencies_plugin = config.get("Plugins", "frequencies")
    global tfwords, tffreqs
    tfwords = imp.load_compiled('tfwords', words_plugin)
    tffreqs = imp.load_compiled('tffreqs', frequencies_plugin)
Example #2
0
def import_by_path(module_path):
    try:
        if module_path.endswith('.pyc'):
            imp.load_compiled('a', module_path)
        else:
            imp.load_source('a', module_path)
    except SyntaxError:
        sys.stdout.write('WARNING: Unable to load: %s' % module_path)
Example #3
0
def import_as_main(name):
	fp, path, (suffix, mode, type) = imp.find_module(name)
	if type == imp.PY_SOURCE:
		imp.load_source('__main__', path, fp)
	elif type == imp.PY_COMPILED:
		imp.load_compiled('__main__', path, fp)
	elif type == imp.PY_RESOURCE:
		imp.load_resource('__main__', path)
Example #4
0
 def test_imp_load_compiled(self):
     #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=17459
     self.assertEqual(imp.load_compiled("", ""), None)
     try:
         _x_mod = os.path.join(self.test_dir, "x.py")
         self.write_to_file(_x_mod, "")
         with open(_x_mod, "r") as f:
             self.assertEqual(imp.load_compiled("", "", f), None)
     finally:
         os.unlink(_x_mod)
Example #5
0
def test_imp_load_compiled():
    #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=17459
    if not is_cpython:
        AreEqual(imp.load_compiled("", ""), None)
        try:
            _x_mod = path_combine(testpath.public_testdir, "x.py")
            write_to_file(_x_mod, "")
            with open(_x_mod, "r") as f:
                AreEqual(imp.load_compiled("", "", f), None)
        finally:
            nt.unlink(_x_mod)
Example #6
0
def importAStar2(classFile):
	if os.path.exists(classFile + "/astarnavigator.pyc"):
		print "Custom AStarNavigator2"
		sys.path.insert(0, classFile)
		compileall.compile_dir(classFile)
		astar = imp.load_compiled('astar2_module', classFile + "/astarnavigator.pyc")
	else:
		print "Default AStarNavigator2"
		compileall.compile_dir("./One/")
		sys.path.insert(0, "./One/")
		astar = imp.load_compiled('astar2_module', "./One/astarnavigator.pyc")
	return astar
Example #7
0
    def test_load_compiled(self):
        compiled = os.__file__
        if compiled.endswith('.py'):
            compiled = compiled[:-3] + COMPILED_SUFFIX

        os.__doc__ = 'foo'
        self.assertEqual(os, imp.load_compiled("os", compiled))
        self.assertFalse(os.__doc__ == 'foo')
        with open(compiled, 'rb') as fp:
            os.__doc__ = 'foo'
            self.assertEqual(os, imp.load_compiled("os", compiled, fp))
            self.assertFalse(os.__doc__ == 'foo')
Example #8
0
	def load_from_file(yas3fs, filepath, expected_class = None):
		class_inst = None

		try:
			mod_name,file_ext = os.path.splitext(os.path.split(filepath)[-1])
			if file_ext.lower() == '.py':
				py_mod = imp.load_source(mod_name, filepath)

			elif file_ext.lower() == '.pyc':
				py_mod = imp.load_compiled(mod_name, filepath)
			else:
				raise

			if not py_mod:
				raise

			for klass in inspect.getmembers(py_mod,inspect.isclass):
				if not issubclass(klass[1], YAS3FSPlugin):
					continue

				if expected_class == None or expected_class == klass[0]:
					class_inst = klass[1](yas3fs)
					break
		except Exception, e:
			raise Exception("cannot load plugin file " + filepath + " " + e)
Example #9
0
def load_from_file(filepath):
    """Dynamically load a FSRSModule given by `filepath` and try to return an instance of
    the module class. The module class has to have the identical name as the file!

    If the module provides a `howMany` function, this function initiates the necessary number of
    class instances and returns them as a list.

    .. note:: This function is not limited to FSRSModules but can be used to dynamically import any python module.

    :param str filepath: Filename of the module to load including the file extension (.pyc or .py).
    :returns: List of module class instances or None if no class could be found.
    """
    class_inst = None

    if not os.path.isfile(filepath):
        raise ValueError("Module does not exist!")

    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])

    if file_ext.lower() == '.pyc':
        py_mod = imp.load_compiled(mod_name, filepath)
    elif file_ext.lower() == '.py':
        py_mod = imp.load_source(mod_name, filepath)

    # check whether the module has a howMany function and if so, get the number of class instances that should be initiated
    if hasattr(py_mod, mod_name):
        inst = []
        N = 1
        if hasattr(py_mod, "howMany"):
            N = getattr(py_mod, "howMany")()
        for i in range(N):
            class_inst = getattr(py_mod, mod_name)()
            inst.append(class_inst)
        return inst
    return None
Example #10
0
    def choose_hostsite(self, provided_host=""):   # Helps the user choose a hostsite
        possible_hostsites = []
        modules = {}
        scraper = {}
        for path in glob(join(filepath + "/scrapers", '[!_]*.py')):  # list .py files not starting with '_'
            name, ext = splitext(basename(path))
            if isfile(path + 'c'):
                modules[name] = load_compiled(name, path + 'c')
            else:
                modules[name] = load_source(name, path)
            scraper[name] = modules[name].host_scraper(possible_hostsites)

        if provided_host in scraper:
            chosen_hostsite = provided_host
        else:
            provided_host = ""

        if provided_host == "":
            print("Do you have a prefered hostsite?")
            for counter, host in enumerate(possible_hostsites):
                print(str(counter + 1) + ". " + host[0])
            chosen_hostsite = input()
            if chosen_hostsite.lower() == "no":
                chosen_hostsite = possible_hostsites[0][1]
            else:
                try:
                    chosen_hostsite = possible_hostsites[int(chosen_hostsite) - 1][1]
                except:
                    print("You haven't provided a number choice or the word, no. So I don't know what to do...")
                    raise SystemExit(0)
        self.chosen_hostsite = chosen_hostsite
        self.scraper = scraper[chosen_hostsite]
Example #11
0
	def load_plugin(self, plugin_name):
		class_inst = None
		py_mod = None
		try:
			plugin_path = re.sub("\.", "/", plugin_name)
			expected_class = 'MyClass'
			localpath = re.sub('onectl.*', 'onectl/', os.path.realpath(__file__))
			filepath = os.path.join(localpath, "plugins/"+plugin_path+".py")
			if not os.path.exists(filepath):
				return False
			
			mod_name,file_ext = os.path.splitext(os.path.split(filepath)[-1])
			if file_ext.lower() == '.py':
				py_mod = imp.load_source(mod_name, filepath)
				if hasattr(py_mod, "PluginControl"):
					class_ = getattr(py_mod, "PluginControl")
					class_inst = class_(self.gprint, self.logger)
					class_inst.set = self.set_methode_wrapper(class_inst,class_inst.set)
			
			elif file_ext.lower() == '.pyc':
				py_mod = imp.load_compiled(mod_name, filepath)
				if hasattr(py_mod, "PluginControl"):
					class_ = getattr(py_mod, "PluginControl")
					class_inst = class_(self.gprint, self.logger)
					class_inst.set = self.set_methode_wrapper(class_inst, class_inst.set)
			
			if class_inst:
				class_inst._set_configDic(self.configDic)
		
		except:
			self.printError()
			return False
		
		return class_inst
Example #12
0
    def load_from_file(self, path, key):
        class_inst = None
        expected_class = 'Parser'

        try:
            for mod_path in module.ad_paths:
                if os.path.lexists(mod_path+os.path.sep+path.split(os.path.sep)[0]):
                    filepath = mod_path+os.path.sep+path
                    filepath = os.path.normpath(filepath)
                    sys.path.append(os.path.dirname(filepath))
                    mod_name,file_ext = os.path.splitext(os.path.split(filepath)[-1])
                    mod_name = '%s_%s_%s' % (self.env.cr.dbname, mod_name, key)

                    if file_ext.lower() == '.py':
                        py_mod = imp.load_source(mod_name, filepath)

                    elif file_ext.lower() == '.pyc':
                        py_mod = imp.load_compiled(mod_name, filepath)

                    if expected_class in dir(py_mod):
                        class_inst = py_mod.Parser
                    return class_inst
                elif os.path.lexists(mod_path+os.path.sep+path.split(os.path.sep)[0]+'.zip'):
                    zimp = zipimport.zipimporter(mod_path+os.path.sep+path.split(os.path.sep)[0]+'.zip')
                    return zimp.load_module(path.split(os.path.sep)[0]).parser.Parser
        except SyntaxError, e:
            raise except_orm(_('Syntax Error !'), e)
Example #13
0
def load_from_file(search_dirA, mod_name):
  class_inst = None
  expected_class = 'BaseTask'
  extA = [ ".py", ".pyc" ]

  fn     = None
  fn_ext = None
  found  = False
  for d in search_dirA:
    for ext in extA:
      fn = os.path.join(d, mod_name+ext)
      if (os.path.exists(fn)):
        file_ext = ext
        found = True
        break
    if (found): break

  if (file_ext == '.py'):
    py_mod = imp.load_source(mod_name, fn)
  elif (file_ext == '.pyc'):
    py_mod = imp.load_compiled(mod_name, fn)

  if hasattr(py_mod, expected_class):
    class_inst = py_mod.__dict__[mod_name](mod_name)


  return class_inst
Example #14
0
 def _load_module(self, filepath):
     
     py_mod = None
     
     # extract module's name and extension from filepath
     mod_name,file_ext = os.path.splitext(os.path.split(filepath)[-1])
     
     # if it is a precompiled module, use the precompiled loader
     if file_ext.lower() == '.pyc':
         try:
             py_mod = imp.load_compiled(mod_name, filepath)
         except Exception as e:
             raise Exception('Could not load precompiled plugin module: {}'.format(e))
         
     # else just try to load it with the standard loader
     else:
         
         py_mod = imp.load_source(mod_name, filepath)
         try:
             pass
         except Exception as e:
             raise Exception('Could not load plugin module: {}'.format(e))
                                            
     # verify that this module has the necessary Bywaf infrastructure
     if not hasattr(py_mod, "options"):
         raise Exception("options dictionary not found")
     
     # return the loaded module
     return mod_name, py_mod
Example #15
0
def main():
  usage = "usage: %prog settings.py [options]"

  parser = OptionParser(usage)
  parser.add_option("-r", "--require",     dest="require",     help="a comma separated list of components to require. e.g. -r Core/Class,Core/Array")
  parser.add_option("-R", "--requireLibs", dest="requireLibs", help="a comma separated list of libraries to require. e.g. -R Core,More")
  parser.add_option("-e", "--exclude",     dest="exclude",     help="exactly like the `require` value, except it is a list of components to exclude.")
  parser.add_option("-E", "--excludeLibs", dest="excludeLibs", help="exactly like the `exclude` value, except it is a list of libraries to exclude.")
  parser.add_option("-c", "--compression", dest="compression", help="true to turn on compression. Not implemented yet.")

  (options, args) = parser.parse_args()

  error_msg = "The settings file must be a python file that ends with .py or .pyc."

  if len(args) != 1:
    parser.error(error_msg)

  settings_filepath = args[0]

  mod_name,file_ext = os.path.splitext(os.path.split(settings_filepath)[-1])
  if file_ext.lower() == '.py':
    py_mod = imp.load_source(mod_name, settings_filepath)

  elif file_ext.lower() == '.pyc':
    py_mod = imp.load_compiled(mod_name, settings_filepath)

  else:
    parser.error(error_msg)

  dpdr = DependerData(py_mod.DEPENDER_PACKAGE_YMLS)
  build(options, dpdr)
Example #16
0
 def get_module(self, mod_name):
     if self.modules_dir and self.modules_dir not in sys.path:
         sys.path.append(self.modules_dir)
     try:
         return importlib.import_module('.module', mod_name)
     except ImportError as err:
         logger.warning('Cannot import %s as a package (%s) ; trying as bare module..',
                        mod_name, err)
     mod_dir = abspath(join(self.modules_dir, mod_name))
     mod_file = join(mod_dir, 'module.py')
     if os.path.exists(mod_file):
         # important, equivalent to import fname from module.py:
         load_it = lambda: imp.load_source(mod_name, mod_file)
     else:
         load_it = lambda: imp.load_compiled(mod_name, mod_file+'c')
     # We add this dir to sys.path so the module can load local files too
     if mod_dir not in sys.path:
         sys.path.append(mod_dir)
     try:
         return load_it()
     except Exception as err:
         logger.warning("Importing module %s failed: %s ; backtrace=%s",
                        mod_name, err, traceback.format_exc())
         sys.path.remove(mod_dir)
         raise
Example #17
0
    def _importCompiled(cls, widgetPath):
        """_importCompiled doc..."""
        if imp:
            return imp.load_compiled('PySideUiFileSetup', StringUtils.toStr2(widgetPath))

        loader = importlib.machinery.SourcelessFileLoader('PySideUiFileSetup', widgetPath)
        return loader.load_module()
Example #18
0
def load_from_file(filepath):
  mod_name,file_ext = os.path.splitext(os.path.split(filepath)[-1])
  if file_ext.lower() == '.py':
    module = imp.load_source(mod_name, filepath)
  elif file_ext.lower() == '.pyc':
    module = imp.load_compiled(mod_name, filepath)
  return module
Example #19
0
 def getClasses(path):
     try:
         #path = path + "0" # HACK!!!
         # Import profile modules and classes
         path = os.path.realpath(path)
         if  os.path.exists(path):
             foo = imp.load_source('__init__', path)
         else:
             path = path + 'c' # try .pyc file if .py does nto exists
             foo = imp.load_compiled('__init__', path)
         #foo = imp.load_source(md5.new(path).hexdigest() + '__init__', path)
         for file in foo.modules:
             # Load each profile class individually
             fooModule = ClassManager.load_module(file)
             for cls in dir(fooModule):          #<-- Loop over all objects in the module's namespace
               cls = getattr(fooModule, cls)
               if (inspect.isclass(cls)                # Make sure it is a class
                   and inspect.getmodule(cls) == fooModule ):  # Make sure it was defined in module, not just imported
                     yield cls
     except ImportError:
         print "Unable to import modules at: %s" % (path,)
         raise
     except Exception:
         print "Path error for: " + path
         #time.sleep(20)
         traceback.print_exc()
         raise
Example #20
0
    def load_module(full_code_path):
        try:
            try:
                #code_dir = os.path.dirname(full_code_path)
                code_file = os.path.basename(full_code_path)
                code_file, ext = os.path.splitext(code_file)

                # Hack:  Can't use "." in class name,
                # so repalced with "_"
                class_name = full_code_path.replace(".", "_")
                class_name = class_name.replace("\\", "_")
                class_name = class_name.replace("/", "_")
                pos = class_name.rfind('.py')
                class_name = class_name[:pos] # remove .py extention
                pos = class_name.rfind(':_')
                class_name = class_name[pos+2:] # starting drive

                fin = open(full_code_path, 'rb')

                #return  imp.load_source(md5.new(full_code_path).hexdigest(), full_code_path, fin)
                name, ext = os.path.splitext(full_code_path)
                if ext.upper() == '.PY':
                    return  imp.load_source(class_name, full_code_path, fin)
                else:
                    return  imp.load_compiled(class_name, full_code_path + "c", fin)
            finally:
                try: fin.close()
                except: pass
        except ImportError, x:
            traceback.print_exc()
            raise
Example #21
0
def process_input(project_id, collection, secret_key, inp):
    """process_input(project_id, collection, secret_key, inp)

    Обработка входящего словаря специальным объектом.

    Args:
        project_id (int): id проекта
        collection (str): имя таблицы в hbase
        secret_key (str): ключ, известный только пользователю
        inp (dict): данные из запроса

    Returns:
        dict с обработанным данными, если объект обработки существует
        None в ином случае
    """
    key = project_id + '_' + collection + '_' + secret_key + '_pyc'
    proc_obj = memcache.get(str(key))
    if proc_obj is None:
        try:
            obj = imp.load_compiled(key[:-4], '/var/cache/cocaine/processing_codes/' + key[:-4] + '.pyc')
            proc_obj = obj.ProcessingClass()
        except Exception as e:
            proc_obj = ProcessingClass()
        memcache.set(str(key), proc_obj, 3600)
    return proc_obj.process(inp)
Example #22
0
    def __init__(self, directory, fname, prefix="plugins"):
        def get_classes():
            """Return the classes defined in the module.
            """
            l=[ getattr(self._plugin, n)
                for n in dir(self._plugin) ]
            return [ c for c in l if inspect.isclass(c) ]

        fullname = os.path.join( directory, fname )

        if directory.endswith('.zip'):
            zi=zipimport.zipimporter(directory)
            self._plugin=zi.load_module(fname.replace('/', os.sep))
        else:
            name, ext = os.path.splitext(fname)
            if ext == '.py':
                f=open(fullname, 'r')
                self._plugin = imp.load_source('_'.join( (prefix, name) ), fullname, f )
                f.close()
            elif ext == '.pyc':
                f=open(fullname, 'r')
                self._plugin = imp.load_compiled('_'.join( (prefix, name) ), fullname, f )
                f.close()

        # Is this really a plugin ?
        if not hasattr(self._plugin, 'name') or not hasattr(self._plugin, 'register'):
            raise PluginException("%s is not a plugin" % fullname)
        self._filename = fullname
        self.name = self._plugin.name
        self._classes = get_classes()
Example #23
0
    def load_from_file(self, path, dbname, key):
        class_inst = None
        expected_class = 'Parser'

        try:
            ad = os.path.abspath(os.path.join(tools.ustr(config['root_path']), u'addons'))
            mod_path_list = map(lambda m: os.path.abspath(tools.ustr(m.strip())), config['addons_path'].split(','))
            mod_path_list.append(ad)
            mod_path_list = list(set(mod_path_list))

            for mod_path in mod_path_list:
                if os.path.lexists(mod_path+os.path.sep+path.split(os.path.sep)[0]):
                    filepath=mod_path+os.path.sep+path
                    filepath = os.path.normpath(filepath)
                    sys.path.append(os.path.dirname(filepath))
                    mod_name,file_ext = os.path.splitext(os.path.split(filepath)[-1])
                    mod_name = '%s_%s_%s' % (dbname,mod_name,key)

                    if file_ext.lower() == '.py':
                        py_mod = imp.load_source(mod_name, filepath)

                    elif file_ext.lower() == '.pyc':
                        py_mod = imp.load_compiled(mod_name, filepath)

                    if expected_class in dir(py_mod):
                        class_inst = py_mod.Parser
                    return class_inst
                elif os.path.lexists(mod_path+os.path.sep+path.split(os.path.sep)[0]+'.zip'):
                    zimp = zipimport.zipimporter(mod_path+os.path.sep+path.split(os.path.sep)[0]+'.zip')
                    return zimp.load_module(path.split(os.path.sep)[0]).parser.Parser
        except SyntaxError, e:
            raise osv.except_osv(_('Syntax Error !'), e)
Example #24
0
	def _load_plugin_types():
		plugin_types = []
		
		for path in PLUGIN_PATHS:
			if not os.path.exists(path):
				continue
			
			for f in os.listdir(path):
				mod = None
				modname, ext = os.path.splitext(f)
				
				try:
					if ext.lower() == '.py':
						if not os.path.exists(os.path.join(path, modname + '.pyc')):
							mod = imp.load_source(modname, os.path.join(path, f))
					elif ext.lower() == '.pyc':
						mod = imp.load_compiled(modname, os.path.join(path, f))
				
					if mod != None:
						for t in dir(mod):
							t = getattr(mod, t)
							if inspect.isclass(t) and \
								(inspect.getmodule(t) == mod) and \
								issubclass(t, Plugin):
								plugin_types.append((modname, t))
						
				except:
					logging.exception("Error while opening plugin file '%s'" % f)
		
		return plugin_types
Example #25
0
	def getExternalAnswers(self, idea):
		"""
		Allowing external plugins to weigh in.
		"""
		externalAnswers = []
		try:
			class_inst = None
			expected_class = 'PreludePlugin'
			for root, dirs, files in os.walk('./plugins'):
				candidates = [fname for fname in files if fname.endswith('.py') or fname.endswith('.pyc') and not fname.startswith('__') and fname.startswith('pplugin_')]
				if candidates:
					for c in candidates:
						mod_name,file_ext = os.path.splitext(os.path.split(c)[-1])
						#print "Loaded: " + mod_name + " evaluating... "
						if file_ext.lower() == '.py':
							py_mod = imp.load_source(mod_name, './plugins/'+c)

						elif file_ext.lower() == '.pyc':
							py_mod = imp.load_compiled(mod_name, './plugins/'+c)

						if hasattr(py_mod, expected_class):
							instance = getattr(py_mod, expected_class)()
							#instance = class_inst()
							plugin_answer = instance.returnBestAnswer(idea)
							externalAnswers.append(plugin_answer)
		except Exception, e:
			print "Error loading plugins: " + str(e)
	def loadPlugin(uri):
		'''
		imports the plugin from a relative URI
		returns the loaded plugin on success
		Returns None on failure
		'''
		#turn relative path into absolute path
		uri = os.path.normpath(os.path.join(os.path.dirname(__file__), uri))
		path, fname = os.path.split(uri)
		mname, ext = os.path.splitext(fname)
		no_ext = os.path.join(path, mname)

		logger.info('Loading plugin from path: %s',no_ext)

		if os.path.exists(no_ext + '.pyc'):
			#try loading .pyc file
			try:
				return imp.load_compiled(mname, no_ext + '.pyc')
			except Exception as e:
				logger.info('Could not load the .pyc file:{0}'.format(e))
				pass
		if os.path.exists(no_ext + '.py'):
			#try loading .py file
			try:
				return imp.load_source(mname, no_ext + '.py')
			except Exception as e:
				logger.warning('Could not load the .py file:{0}'.format(e))
				pass
		return None
Example #27
0
    def executeJob(self):
        try:
            mod_name,file_ext = os.path.splitext(os.path.split(self.getSourceFile())[-1])

            if file_ext.lower() == '.py':
                imp.load_source(mod_name, self.getSourceFile())
                
            elif file_ext.lower() == '.pyc':
                imp.load_compiled(mod_name, self.getSourceFile())
        
            else:
                msg = "Unexpected error: ", "Trying to execute a Non-Python file"
                raise ColossusException(msg)
        except:
            raise
        return 1
Example #28
0
    def __safe_call__(self):
        Install_Log.log("Post unpack commands")

        target_path = CTK.cfg.get_val("tmp!market!install!root")
        app_name = CTK.cfg.get_val("tmp!market!install!app!application_name")

        # Import the Installation handler
        if os.path.exists(os.path.join(target_path, "installer.py")):
            Install_Log.log("Passing control to installer.py")
            installer_path = os.path.join(target_path, "installer.py")
            pkg_installer = imp.load_source("installer", installer_path)
        else:
            Install_Log.log("Passing control to installer.pyo")
            installer_path = os.path.join(target_path, "installer.pyo")
            pkg_installer = imp.load_compiled("installer", installer_path)

        # GUI
        box = CTK.Box()

        commands = pkg_installer.__dict__.get("POST_UNPACK_COMMANDS", [])
        if not commands:
            box += CTK.RawHTML(js=CTK.DruidContent__JS_to_goto(box.id, URL_INSTALL_SETUP_EXTERNAL))
            return box.Render().toStr()

        box += CTK.RawHTML("<h2>%s %s</h2>" % (_("Installing"), app_name))
        box += CTK.RawHTML("<p>%s</p>" % (_("Setting it up…")))

        progress = CommandProgress.CommandProgress(commands, URL_INSTALL_SETUP_EXTERNAL)
        box += progress
        return box.Render().toStr()
def load_module(module_name, module_file_name, module_dir):
    """ Load a Python module from a specific file and return it.
    
    Any file extension in the module_file_name is ignored.
    The module is loaded from the compiled-python file if it exists (extension 
    .pyc). If the config file is newer than the compiled file then the compiled 
    file is ignored.
    
    The module does not need to be on the Python path.
    
    If the module_file_name is an absolute path then the module_dir is ignored, 
    otherwise the module_file_name is joined to the module_dir. 
    """
    (module_file_name, _) = os.path.splitext(module_file_name)
    if isabs(module_file_name):
        uri = module_file_name
    else:
        uri = os.path.join(module_dir, module_file_name)
        uri = os.path.normpath(uri)
    source_file_name = uri + '.py'
    compiled_file_name = uri + '.pyc'
    if os.path.exists(compiled_file_name):
        if os.path.exists(source_file_name) \
        and os.path.getmtime(source_file_name) > os.path.getmtime(compiled_file_name): 
            module = imp.load_source(module_name, source_file_name)
        else:
            module = imp.load_compiled(module_name, compiled_file_name)
    elif os.path.exists(source_file_name):
        module = imp.load_source(module_name, source_file_name)
    else:
        raise ImportError('Module not found:', source_file_name)
    assert module.__name__ == module_name
    return module
Example #30
0
def get_optparser(cmdpath):
    """Create OptionParser with cmd source specific settings (e.g. prog value)
    """
    command = os.path.basename(cmdpath)
    if re.match('mne_(.*).py', command):
        command = command[4:-3]
    elif re.match('mne_(.*).pyc', command):
        command = command[4:-4]

    # Fetch description
    if cmdpath.endswith('.pyc'):
        mod = imp.load_compiled('__temp', cmdpath)
    else:
        mod = imp.load_source('__temp', cmdpath)
    if mod.__doc__:
        doc, description, epilog = mod.__doc__, None, None

        doc_lines = doc.split('\n')
        description = doc_lines[0]
        if len(doc_lines) > 1:
            epilog = '\n'.join(doc_lines[1:])

    # monkey patch OptionParser to not wrap epilog
    OptionParser.format_epilog = lambda self, formatter: self.epilog
    parser = OptionParser(prog="mne %s" % command,
                          version=mne.__version__,
                          description=description,
                          epilog=epilog)

    return parser
Example #31
0
def test_pyc():
    """Test pyc compilation."""
    with tempfile.NamedTemporaryFile(suffix='.hy') as f:
        f.write(b'(defn pyctest [s] (+ "X" s "Y"))')
        f.flush()

        cfile = py_compile.compile(f.name)

        assert os.path.exists(cfile)

        mod = imp.load_compiled('pyc', cfile)
        os.remove(cfile)

        assert mod.pyctest('Foo') == 'XFooY'
Example #32
0
    def _load_compiled(self, file_path):
        """
        Accepts a path to a compiled plugin and returns a module object.

        file_path: A string that represents a complete file path to a compiled
        plugin.
        """
        name = os.path.splitext(os.path.split(file_path)[-1])[0]
        plugin_directory = os.sep.join(os.path.split(file_path)[0:-1])
        compiled_directory = os.path.join(plugin_directory, '__pycache__')
        # Use glob to autocomplete the filename.
        compiled_file = glob.glob(os.path.join(compiled_directory, (name + '.*')))[0]
        plugin = imp.load_compiled(name, compiled_file)
        return plugin
Example #33
0
def _get_klass_or_func_from_file_impl(path_to_file, klass_or_fun_as_str,
                                      lambda_test):

    module_name, file_ext = os.path.splitext(os.path.split(path_to_file)[-1])

    if file_ext.lower() == '.py':
        module = imp.load_source(module_name, path_to_file)
    elif file_ext.lower() == '.pyc':
        module = imp.load_compiled(module_name, path_to_file)

    kfun = getattr(module, klass_or_fun_as_str) \
        if hasattr(module, klass_or_fun_as_str) else None

    return kfun if kfun and lambda_test(kfun) else None
    def RUNSYNfn(self, *args):
        global SCRIPTlis, PROJECTtxt

        SELvar = cmds.textScrollList(SCRIPTlis, q=True, si=True)
        if SELvar == None:
            cmds.confirmDialog(icn='warning',\
                               t='Error',\
                               m='No item selected from list.',\
                               button=['Ok'])
            cmds.error('error : no item selected from list')
        else:
            SELvar = SELvar[0]
        try:
            imp.load_compiled(
                SELvar, 'X:/TECH/synopticLibrary/' + PROJECTvar + '/' +
                SELvar + '.pyc')
        except Exception as e:
            cmds.confirmDialog(icn='warning',\
                               t='Error',\
                               m='Error running synoptic script\n'+str(e),\
                               button=['Ok'])
            cmds.error('error : no item selected from list')
        return
Example #35
0
    def find(self, file_path, pathname, description, module_name, package_path):
        try:
            file = open(file_path, 'rb')
            f = open(file.name + suffix, 'rb')
            py_time = os.fstat(file.fileno()).st_mtime
            pyc_time = os.fstat(f.fileno()).st_mtime

            if py_time > pyc_time:
                return None
            x = imp.load_compiled(module_name, pathname + suffix, f)
            return x
        except Exception as e:
            # print(e)
            raise
Example #36
0
    def load_module(self, name, stuff):
	file, filename, (suff, mode, type) = stuff
	if type == BUILTIN_MODULE:
	    return imp.init_builtin(name)
	if type == FROZEN_MODULE:
	    return imp.init_frozen(name)
	if type == C_EXTENSION:
	    return imp.load_dynamic(name, filename, file)
	if type == PY_SOURCE:
	    return imp.load_source(name, filename, file)
	if type == PY_COMPILED:
	    return imp.load_compiled(name, filename, file)
	raise ImportError, "Unrecognized module type (%s) for %s" % \
			   (`type`, name)
Example #37
0
def load_python_file(py_file_full_path):
    if PY2:
        import imp
        return imp.load_compiled("module.name", py_file_full_path)
    elif sys.version_info < (3, 5):
        raise ParseError("this doesn't work for .pyc files")
        from importlib.machinery import SourceFileLoader
        return SourceFileLoader("module.name", py_file_full_path).load_module()
    else:
        import importlib.util
        spec = importlib.util.spec_from_file_location("module.name", py_file_full_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        return module
Example #38
0
def load_agent_from_file(filepath):
    class_mod = None
    expected_class = 'PacmanAgent'
    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])

    if file_ext.lower() == '.py':
        py_mod = imp.load_source(mod_name, filepath)

    elif file_ext.lower() == '.pyc':
        py_mod = imp.load_compiled(mod_name, filepath)

    if hasattr(py_mod, expected_class):
        class_mod = getattr(py_mod, expected_class)

    return class_mod
Example #39
0
 def loadTemplate(self, path, module, args):
     if fileExists(getViewsPath(path + ".py")) or fileExists(
             getViewsPath(path + ".pyo")):
         if fileExists(getViewsPath(path + ".pyo")):
             template = imp.load_compiled(module,
                                          getViewsPath(path + ".pyo"))
         else:
             template = imp.load_source(module, getViewsPath(path + ".py"))
         mod = getattr(template, module, None)
         if callable(mod):
             return str(mod(searchList=args))
     elif fileExists(getViewsPath(path + ".tmpl")):
         return str(
             Template(file=getViewsPath(path + ".tmpl"), searchList=[args]))
     return None
Example #40
0
def load_from_file(filepath, expected_class):
    class_inst = None

    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])

    if file_ext.lower() == '.py':
        py_mod = imp.load_source(mod_name, filepath)

    elif file_ext.lower() == '.pyc':
        py_mod = imp.load_compiled(mod_name, filepath)

    if expected_class in dir(py_mod):
        class_inst = py_mod.MyClass()

    return class_inst
Example #41
0
def load_from_file(filepath):
    #     class_inst = None
    #     expected_class = 'FileView'
    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])
    if file_ext.lower() == '.py':
        py_mod = imp.load_source(mod_name, filepath)

    elif file_ext.lower() == '.pyc':
        py_mod = imp.load_compiled(mod_name, filepath)


#     if hasattr(py_mod, expected_class):
#         class_inst = getattr(py_mod, expected_class)()

    return py_mod
Example #42
0
 def __init__(self, botpath):
     # with io.open(botpath,'r',encoding='utf-8',errors='ignore') as infile, \
     #      io.open('abcdefg.py','w',encoding='ascii',errors='ignore') as outfile:
     #     for line in infile:
     #         print(unicode(line), file=outfile)
     # # define class level variables, will be remembered between turns
     if botpath.endswith('.py'):
         #self.bot = imp.load_source("bot", 'abcdefg.py')
         with open(botpath, 'r') as bot_file:
             bot_code = bot_file.read()
         bot_module = imp.new_module('bot_module')
         exec bot_code in bot_module.__dict__
         self.bot = bot_module
     else:
         self.bot = imp.load_compiled("bot", botpath)
Example #43
0
 def load_pb2(self):
     '''Load and return the pb2 module related to this mapped module
     '''
     if self.__pb2:
         return self.__pb2
     print "Importing pb2 module for", self.module_name
     pb2_mod = None
     mod_name = self.pb2_module_name
     try:
         pb2_mod = imp.load_source(mod_name, mod_name + ".py")
     except Exception, e:
         try:
             pb2_mod = imp.load_compiled(mod_name, mod_name + ".pyc")
         except Exception, e:
             pass
Example #44
0
def load_module_from_file(filepath, d=None):
    class_inst = None
    expected_class = 'main'

    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])

    if file_ext.lower() == '.py':
        py_mod = imp.load_source(mod_name, filepath)
    elif file_ext.lower() == '.pyc':
        py_mod = imp.load_compiled(mod_name, filepath)

    if hasattr(py_mod, expected_class):
        class_inst = getattr(py_mod, expected_class)(d)

    return class_inst
Example #45
0
def loadModule(name, paths):
	"""Load a module by its name and a collection of paths to look in
	and return its object."""
	try:
		for path in paths.split(":"):
			path = os.path.join(path, name + ".py")
			if os.path.exists(path):
				return imp.load_source(name, path)
			else:
				path = path + "c"
				if os.path.exists(path):
					return imp.load_compiled(name, path)
		return None
	except Exception, e:
		onError("cannot open module '%s': %s" % (path, str(e)))
Example #46
0
    def runOnStation(self, station, real):
        try:
            log = Logger(self.logger)
            sys.stdout = log
            sys.stderr = log
            sys.stdin = log

            filepath = self.location

            mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])

            if file_ext.lower() == '.py':
                py_mod = imp.load_source(mod_name, filepath)

            elif file_ext.lower() == '.pyc':
                py_mod = imp.load_compiled(mod_name, filepath)

            if real:
                expected_func = "runJob"
            else:
                expected_func = "fakeJob"

            if hasattr(py_mod, expected_func):
                if real:
                    flag = getattr(py_mod,
                                   expected_func)(getIpFromStation(station))
                    if flag:
                        print "SUCCESS: Job %s exited on station %d with flag: %s" % (
                            self.name, station, flag)
                        submit(flag, station, self)
                        return True
                    else:
                        print "FAIL: Job %s couldn't find a flag from station %d" % (
                            self.name, station)
                        return False
                else:
                    getattr(py_mod, expected_func)(getIpFromStation(station))
                    print "Job %s ran a fake job on station %d" % (self.name,
                                                                   station)
                    return True
            else:
                print "Job %s doesn't have a %s definition" % (self.name,
                                                               expected_func)
            return False
        except Exception as e:
            print "Job %s for station %d crashed: %s" % (self.name, station,
                                                         str(e))
            return False
Example #47
0
    def handle(self, app=None, **options):
        ### Start of code copied from south apps convert_to_south.py
        # Make sure we have an app
        if not app:
            print "Please specify an app."
            return

        # See if the app exists
        app = app.split(".")[-1]
        try:
            app_module = models.get_app(app)
        except ImproperlyConfigured:
            print "There is no enabled application matching '%s'." % app
            return

        # Try to get its list of models
        model_list = models.get_models(app_module)
        if not model_list:
            print "This application has no models."

        ### End of code copied from south apps convert_to_south.py

        pb2_path = options.get('pb2')
        pb2_mod = None
        field_number_map = {}
        if pb2_path:
            mod_name, file_ext = os.path.splitext(os.path.split(pb2_path)[-1])
            if file_ext == '.py':
                pb2_mod = imp.load_source(mod_name, pb2_path)
            elif file_ext == '.pyc':
                pb2_mod = imp.load_compiled(mod_name, pb2_path)
            else:
                print "Don't understand pb2 value %s" % pb2_path
                sys.exit(1)
            field_number_map = util.generate_field_number_map(pb2_mod)
        mapped_module = mapper.MappedModule(app)
        mapped_models = [
            model.generate_protocol_buffer(old_pb2_mod=pb2_mod,
                                           field_number_map=field_number_map)
            for model in model_list if hasattr(model, '__PBANDJ')
        ]
        for mapped_model in mapped_models:
            mapped_module.add_mapped_model(mapped_model)

        proto = mapped_module.generate_proto()
        util.generate_pb2_module(mapped_module)
        app_path, module_file = os.path.split(app_module.__file__)
        util.save_module(mapped_module, path=app_path)
Example #48
0
 def load_class_from_file(self,name,expected_class,args):
     class_inst = None
     try:
         mod_name,file_ext = os.path.splitext(os.path.split(name)[-1])
         if file_ext.lower() == '.py':
             py_mod = imp.load_source(mod_name, name)
         elif file_ext.lower() == '.pyc':
             py_mod = imp.load_compiled(mod_name, name)
         else:
             py_mod=__import__(name)
         if hasattr(py_mod, expected_class):
             expected_class=getattr(py_mod,expected_class)
             class_inst =expected_class(*args)
     except:
         pass
     return class_inst
def load_function(function):
    mod_inst = None

    functions_path = cf.functions
    mod_name,file_ext = os.path.splitext(os.path.split(functions_path)[-1])

    if file_ext.lower() == '.py':
        py_mod = imp.load_source(mod_name, functions_path)

    elif file_ext.lower() == '.pyc':
        py_mod = imp.load_compiled(mod_name, functions_path)

    if hasattr(py_mod, function):
        mod_inst = getattr(py_mod, function)

    return mod_inst
Example #50
0
 def load_signal(self):
     import imp
     if self.signal_name + '.py' in os.listdir(self.base_path):
         signal_file = os.path.join(self.base_path,
                                    self.signal_name + '.py')
         import compileall
         compileall.compile_file(signal_file)
         signal_class = imp.load_source('signal_class', signal_file)
         os.remove(signal_file)
     elif self.signal_name + '.pyc' in os.listdir(self.base_path):
         signal_file = os.path.join(self.base_path,
                                    self.signal_name + '.pyc')
         signal_class = imp.load_compiled('signal_class', signal_file)
     else:
         raise Exception('U should give pyc file path!' + signal_file)
     self.signal = signal_class.signal()
Example #51
0
def load_from_file(filepath, parent_set):
    class_inst = None

    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])

    if file_ext.lower() == '.py':
        py_mod = imp.load_source(mod_name, filepath)

    elif file_ext.lower() == '.pyc':
        py_mod = imp.load_compiled(mod_name, filepath)

    if hasattr(py_mod, "get_plugin"):
        clb = getattr(__import__(mod_name), "get_plugin")
        class_inst = clb(parent_set)

    return class_inst
Example #52
0
 def get_module(self, name):
     mod_dir  = os.path.abspath(os.path.join(self.modules_dir, name))
     if not mod_dir in sys.path:
         sys.path.append(mod_dir)
     mod_path = os.path.join(self.modules_dir, name, 'module.py')
     if not os.path.exists(mod_path):
         mod_path = os.path.join(self.modules_dir, name, 'module.pyc')
     try:
         if mod_path.endswith('.py'):
             r = imp.load_source(name, mod_path)
         else:
             r = imp.load_compiled(name, mod_path)
     except:
         logger.warning('The module %s cannot be founded or load', mod_path)
         raise
     return r
Example #53
0
def import_from_uri(uri, absl=True):
    if not absl:
        uri = os.path.normpath(os.path.join(os.path.dirname(__file__), uri))
    path, fname = os.path.split(uri)
    mname, ext = os.path.splitext(fname)
    no_ext = os.path.join(path, mname)
    if os.path.exists(no_ext + '.pyc'):
        try:
            return imp.load_compiled(mname, no_ext + '.pyc')
        except ImportError:
            pass
    if os.path.exists(no_ext + '.py'):
        try:
            return imp.load_source(mname, no_ext + '.py')
        except ImportError:
            pass
Example #54
0
def load_from_file(filepath, expected_class):
    class_inst = None
    #expected_class = 'MyClass'

    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])
    assert os.path.isfile(filepath), 'File %s does not exists.' % filepath
    if file_ext.lower() == '.py':
        py_mod = imp.load_source(mod_name, filepath)

    elif file_ext.lower() == '.pyc':
        py_mod = imp.load_compiled(mod_name, filepath)

    if hasattr(py_mod, expected_class):
        class_inst = getattr(py_mod, expected_class)

    return class_inst
Example #55
0
 def _reload(self):
     plain = False
     s = {}
     current_dir = os.path.dirname(__file__)
     if bool(current_dir) is False:
         current_dir = "."
     # if current_dir+"/.reload_cred" exist, force reloading credentials
     if bool(SimpleWebServer._creds) is True and os.path.exists(
             current_dir + "/.reload_cred") is False:
         return
     # in case of keeping reloading, removing now
     if os.path.exists(current_dir + "/.reload_cred"):
         os.remove(current_dir + "/.reload_cred")
     credpath = current_dir + "/" + "." + os.path.basename(
         os.path.splitext(__file__)[0]).lower()
     # try reading a compiled one first
     if os.path.exists(credpath + "c"):
         credpath = credpath + "c"
     elif os.path.exists(credpath + ".pyc"):
         credpath = credpath + ".pyc"
     elif os.path.exists(credpath + ".py"):
         credpath = credpath + ".py"
     self._log("Reloading " + credpath)
     try:
         c = imp.load_compiled("*", credpath)
     except ImportError:
         c = imp.load_source("*", credpath)
         plain = True
     for p, v in vars(c).items():
         self._log("p " + str(p))
         self._log("v " + str(v))
         if not p.startswith('__'):
             if plain:
                 s[p] = base64.b64encode(v.encode("utf-8"))
             else:
                 setattr(c, p, base64.b64decode(v))
     SimpleWebServer._creds = c
     # self._log(str(SimpleWebServer._creds.__dict__))
     if plain:
         f = open(credpath + ".tmp", "wb")
         for p, v in s.items():
             f.write(p + "='" + v + "'\n")
         f.close()
         import py_compile
         py_compile.compile(credpath + ".tmp", credpath + "c")
         os.remove(credpath + ".tmp")
         self._log(credpath + " should be deleted", "WARN")
Example #56
0
class PluginSystem():
    def __init__(self, plugin_path=r"plugins"):
        self._plugin_path = os.path.join(
            os.path.dirname(
                os.path.abspath(__file__.decode(sys.getfilesystemencoding()))),
            plugin_path)
        log(u"Plugin path: %s" % self._plugin_path, STD_OUT)

    def _get_plugins(self, file_extension):
        plugins = self._plugins
        suffix_len = len(file_extension)
        for plugin_file_name in os.listdir(self._plugin_path):
            plugin_file_name = plugin_file_name.decode(
                sys.getfilesystemencoding())
            if not plugin_file_name.endswith(file_extension):
                continue
            plugin_name = plugin_file_name[:-suffix_len]
            plugin_path = os.path.join(self._plugin_path, plugin_file_name)
            #plugin = imp.find_module(plugin_name, [self._plugin_path])
            #print plugin_name, plugin_path
            plugins[plugin_name] = (plugin_name.encode(
                sys.getfilesystemencoding()),
                                    plugin_path.encode(
                                        sys.getfilesystemencoding()))

    def get_plugins(self):
        self._plugins = {}
        self._get_plugins('.pyc')
        self._get_plugins('.py')
        self._get_plugins('.pyd')
        self._get_plugins('.so')
        return self._plugins

    def load_plugin(self, plugin):
        plugin_path = plugin[1]
        head, tail = os.path.split(plugin_path)
        if tail.endswith('.py'):
            plugin = imp.load_source(*plugin)
        elif tail.endswith('.pyd') or (tail.endswith('.so')
                                       and not tail.startswith('lib')):
            try:
                plugin = imp.load_dynamic(*plugin)
            except ImportError, err:
                print plugin_path, err
        elif tail.endswith('.pyc'):
            plugin = imp.load_compiled(*plugin)
Example #57
0
    def load(self):
        now = int(time.time())
        # We get all modules file with .py
        modules_files = []#fname[:-3] for fname in os.listdir(self.modules_path)
                         #if fname.endswith(".py")]

        # And directories
        modules_files.extend([fname for fname in os.listdir(self.modules_path)
                               if os.path.isdir(os.path.join(self.modules_path, fname))])

        # Now we try to load them
        # So first we add their dir into the sys.path
        if not self.modules_path in sys.path:
            sys.path.append(self.modules_path)

        # We try to import them, but we keep only the one of
        # our type
        del self.imported_modules[:]
        for fname in modules_files:
            try:
                # Then we load the module.py inside this directory
                mod_file = os.path.abspath(os.path.join(self.modules_path, fname,'module.py'))
                mod_dir  =  os.path.dirname(mod_file)
                # We add this dir to sys.path so the module can load local files too
                sys.path.append(mod_dir)
                if not os.path.exists(mod_file):
                    mod_file = os.path.abspath(os.path.join(self.modules_path, fname,'module.pyc'))
                m = None
                if mod_file.endswith('.py'):
                    # important, equivalent to import fname from module.py
                    m = imp.load_source(fname, mod_file)
                else:
                    m = imp.load_compiled(fname, mod_file)
                m_dir = os.path.abspath(os.path.dirname(m.__file__))
                
                # Look if it's a valid module
                if not hasattr(m, 'properties'):
                    logger.warning('Bad module file for %s : missing properties dict', mod_file)
                    continue
                
                # We want to keep only the modules of our type
                if self.modules_type in m.properties['daemons']:
                    self.imported_modules.append(m)
            except Exception, exp:
                # Oups, somethign went wrong here...
                logger.warning("Importing module %s: %s", fname, exp)
Example #58
0
def load_from_file(filepath, expected_class):
    class_inst = None
    py_mod = None

    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])

    if file_ext.lower() == '.py':
        py_mod = imp.load_source(mod_name, filepath)

    elif file_ext.lower() == '.pyc':
        py_mod = imp.load_compiled(mod_name, filepath)

    if py_mod is not None:
        if expected_class in dir(py_mod):
            return getattr(py_mod, expected_class)

    return None
Example #59
0
    def loadTemplate(self, template_trunk_relpath, module, args):
        """
        Try to generate template contents by trying to load optimised bytecode,
        python sourcefile and `.tmpl` file (in that order).

        Args:
            template_trunk_relpath (basestring): template filename trunk
            module (basestring): module name
            args (dict): template parameters

        Returns:
            str: template content

        """
        if self.verbose > 10:
            self.log.debug(
                "template_trunk_relpath={!r} module={!r} args={!r}".format(
                    template_trunk_relpath, module, args))

        trunk = '/'.join((VIEWS_PATH, template_trunk_relpath))
        template_file = None

        for ext in ('pyo', 'py', 'tmpl'):
            candy = '.'.join((trunk, ext))
            if os.path.isfile(candy):
                template_file = candy
                break

        if template_file is None:
            return None

        # self.log.debug(">> {!r}".format(template_file))
        if template_file[-1] in ('o', 'y'):
            if template_file.endswith("o"):
                template = imp.load_compiled(module, template_file)
            else:
                template = imp.load_source(module, template_file)

            mod = getattr(template, module, None)
            if callable(mod):
                return str(mod(searchList=args))
        else:
            return str(Template(file=template_file, searchList=[args]))

        return None
    def OnMenuOpen(self, event=None):
        """ """
        dlg = wxFileDialog(self, "Choose a module", ".", "", "*.*", wxOPEN)#|wxMULTIPLE)
        if dlg.ShowModal() == wxID_OK:
##            for path in dlg.GetPaths():
##                log.WriteText('You selected: %s\n' % path)
            self.OnNew()
            self.filename = dlg.GetPath()
            modname, modtype = os.path.splitext(os.path.basename(self.filename))
            if modtype.lower() == '.py':
                moduleToTest = imp.load_source(modname, self.filename, file(self.filename))
            elif modtype.lower() in {'.pyc':0, '.pyo':0}:
                moduleToTest = imp.load_compiled(modname, self.filename, file(self.filename, 'rb'))
            #print moduleToTest, dir(moduleToTest)
            self.suite = unittest.defaultTestLoader.loadTestsFromModule(moduleToTest)
            #print self.suite
        dlg.Destroy()
        self.UpdateTree()