def run_tests_from_names(names, buffer_test_output, xml_output): loader = unittest.TestLoader() for name in names: package_path = name.split('.') for i in range(0, len(package_path)): package = '.'.join( itertools.chain(['irods', 'test'], package_path[0:i])) module = ''.join(['.', package_path[i]]) try: if 'importlib' in globals(): importlib.import_module(module, package=package) else: __import__(''.join([package, module])) except ImportError: break suites = [ loader.loadTestsFromName(name, module=irods.test) for name in names ] super_suite = unittest.TestSuite(suites) if xml_output: import xmlrunner runner = xmlrunner.XMLTestRunner(output='test-reports', verbosity=2) else: runner = unittest.TextTestRunner(verbosity=2, failfast=True, buffer=buffer_test_output, resultclass=RegisteredTestResult) results = runner.run(super_suite) return results
def _unpickle_node(node_name, modulename, is_bound, vardata=None): """returns an MDFNode object from the pickled results of _pickle_node""" # make sure all the required module is imported _log.debug("Unpickling node %s from module %s" % (node_name, modulename)) if modulename: if modulename not in sys.modules: # TODO: we have to reference __builtin__ for now due to the way the import hook works # in the cluster. Get rid of it once a more stable import hook is implemented. __builtin__.__import__(modulename) # get the node and return it (there's no state to recover) try: return _get_node(node_name, is_bound) except MissingNodeError: if not vardata: raise # if the above block didn't return or raise then it must be a varnode # that hasn't been created (possibly one that was originally dynamically # created) so re-create it now. _log.debug("Creating new varnode node %s" % node_name) return MDFVarNode(name=node_name, fqname=node_name, default=vardata["default"], category=vardata["categories"], modulename=modulename)
def initializePlugins(self): startup_pattern = re.sub(r"[\/\\][^\/\\]*$","/startup/*.py*",__file__) # startup_pattern = os.environ['PYMOL_PATH']+"/modules/pmg_tk/startup/*.py*" raw_list = glob(startup_pattern) unique = {} for a in raw_list: unique[re.sub(r".*[\/\\]|\.py.*$","",a)] = 1 for name in unique.keys(): try: if name != "__init__": module_context = string.join(string.split(__name__,'.')[0:-1]) mod_name = module_context+".startup."+name __builtin__.__import__(mod_name) mod = sys.modules[mod_name] if hasattr(mod,'__init_plugin__'): mod.__init_plugin__(self) elif hasattr(mod,'__init__'): mod.__init__(self) except: suppress = 0 # suppress error reporting when using old versions of Python if float(sys.version[0:3])<2.3: if( name in ['apbs_tools' ]): suppress = 1 if not suppress: print "Exception in plugin '%s' -- Traceback follows..."%name traceback.print_exc() print "Error: unable to initialize plugin '%s'."%name
def validateInputs( self, inputs ) : from __builtin__ import __import__ try : __import__( 'snippets.%s'%( inputs[2] ) ) except ImportError : return False return True
def __init__(self, name): """ Creates an instance. Stores the name of the module (for lookup) and a reference to the module itself (for reloading) """ self.name = name[3:] # strip off the 'RC_' prefix __builtin__.__import__(name) # import the module self.mod = sys.modules[name] # keep reference to this module
def test_module_utils_basic_imports(self): realimport = __builtin__.__import__ def _mock_import(name, *args, **kwargs): if name == 'json': raise ImportError() realimport(name, *args, **kwargs) with patch.object(__builtin__, '__import__', _mock_import, create=True) as m: m('ansible.module_utils.basic') __builtin__.__import__('ansible.module_utils.basic')
def import_(module=None,version=None,magic=None): if module: pass elif version: module = by_version[version] elif magic: module = by_magic[magic] else: raise 'at least one argument is required' from __builtin__ import __import__ if module == 'marshal': # use current version's 'marshal' module return __import__('marshal', globals(), locals()) else: return __import__('decompyle.%s' % module, globals(), locals(), 'decompyle')
def simple_import(name): mod = __builtin__.__import__(name) # in case of dotted name, return last module, not first as __import__ does components = name.split('.') for c in components[1:]: mod = getattr(mod, c) return mod
def library_list(): print "--------------------------------------------------" print "Here are the available library's:" print "--------------------------------------------------" table = PrettyTable(["Index", "Package"]) count = 0 for mod in path_mod: table.add_row(["%d" % count, "%s" % mod]) count += 1 print table print "--------------------------------------------------" ans = raw_input("what library would you like to look at?: ") print "--------------------------------------------------" print "You have chosen to look at %s" % ans print "--------------------------------------------------" sleep(1) try: global module module = __builtin__.__import__(ans) except ImportError: print "no such file(try changing lower/uppercase)" sleep(3) library_list() contents = dir(module) for c in contents: print c
def cython_import(filename, verbose=False, compile_message=False, use_cache=False, create_local_c_file=True): """ Compile a file containing Cython code, then import and return the module. Raises an ``ImportError`` if anything goes wrong. INPUT: - ``filename`` - a string; name of a file that contains Cython code OUTPUT: - the module that contains the compiled Cython code. """ name, build_dir = cython(filename, verbose=verbose, compile_message=compile_message, use_cache=use_cache, create_local_c_file=create_local_c_file) sys.path.append(build_dir) return __builtin__.__import__(name)
def test__apply_patches(self): redis = self.mox.CreateMockAnything() redis.smembers('key').AndReturn(['a']) self.mox.StubOutWithMock(patch.glob, 'glob') patch.glob.glob(mox.IgnoreArg()).AndReturn(['c', 'a', 'b']) self.mox.StubOutWithMock(__builtin__, '__import__') b = self.mox.CreateMockAnything() __builtin__.__import__('mod.b', fromlist=['mod.b']).AndReturn(b) b.main() redis.sadd('key', 'b') c = self.mox.CreateMockAnything() __builtin__.__import__('mod.c', fromlist=['mod.c']).AndReturn(c) c.main() redis.sadd('key', 'c') self.mox.ReplayAll() patch._apply_patches(redis, 'key', 'dir', 'mod')
def cython_import(filename, verbose=False, compile_message=False, use_cache=False, create_local_c_file=True): """ INPUT: - ``filename`` - name of a file that contains cython code OUTPUT: - ``module`` - the module that contains the compiled cython code. Raises an ``ImportError`` exception if anything goes wrong. """ name, build_dir = sage.misc.cython.cython(filename, verbose=verbose, compile_message=compile_message, use_cache=use_cache, create_local_c_file=create_local_c_file) sys.path.append(build_dir) return __builtin__.__import__(name)
def importSnippet( self ) : '''Imports a valid snippet from a tar.gz file of the same name as the snippet.''' snippetFile = str( QtGui.QFileDialog.getOpenFileName( parent = self, caption = "Select the snippet to import ... ", filter = '*.tar.gz' ) ) if snippetFile == '' : QtGui.QMessageBox.warning( self, "Error !", 'Please select a \'tar\' file to import a snippet from ... ' ) return os.system( "tar -xvf \"%s\" -C /tmp > /dev/null"%snippetFile ) snippetFile = snippetFile.split('/')[-1].split('.')[0] from __builtin__ import __import__ as im import snippetValidator as val sys.path.append( '/tmp' ) try : s = getattr( __import__(snippetFile), snippetFile ) except BaseException : QtGui.QMessageBox.warning( self, "Error !", 'The file you selected does not contain a snippet at all. Please recheck.' ) return if( val.snippetValidator(s).validate() == 'OK' ) : os.system ( "cat /tmp/%s.py > /home/vikrams/Shellom/snippets/%s.py"%( snippetFile, snippetFile ) ) else : QtGui.QMessageBox.warning( self, "Error !", 'The file you selected contains an invalid snippet. Please recheck.' ) return
def loadPluginInstances(self, plugins, manager): """ plugins := {name: spec, name: spec, ...} spec := {class: class, args: args} """ self.plugins = [] #for (instance_name, instance_spec) in plugins.items(): plugin_keys = plugins.keys() plugin_keys.sort() for instance_name in plugin_keys: instance_spec = plugins[instance_name] instance_class = instance_spec["class"] instance_args = instance_spec["args"] if not self.plugin_defs.has_key(instance_class): rospy.logerr('cannot find %s in plugins for %s' % (instance_class, self.package_name)) else: try: module_path = self.plugin_defs[instance_class] module_name, class_from_class_type = module_path.rsplit('.', 1) module = __builtin__.__import__(module_name, fromlist=[class_from_class_type], level=0) class_ref = getattr(module, class_from_class_type, None) if class_ref is None: rospy.logfatal('cannot find %s' % (class_from_class_type)) else: self.plugins.append(class_ref(instance_name, instance_args)) self.plugins[-1].registerManager(manager) except: rospy.logerr('failed to load %s' % (instance_class)) traceback.print_exc(file=sys.stdout) return self.plugins
def cython_import(filename, verbose=False, compile_message=False, use_cache=False, create_local_c_file=True, **kwds): """ Compile a file containing Cython code, then import and return the module. Raises an ``ImportError`` if anything goes wrong. INPUT: - ``filename`` - a string; name of a file that contains Cython code See the function :func:`sage.misc.cython.cython` for documentation for the other inputs. OUTPUT: - the module that contains the compiled Cython code. """ name, build_dir = cython(filename, verbose=verbose, compile_message=compile_message, use_cache=use_cache, create_local_c_file=create_local_c_file, **kwds) sys.path.append(build_dir) return __builtin__.__import__(name)
def param_objhook(obj): ''' A custom JSON "decoder" which can recognize certain types of serialized python objects (django models, function calls, object constructors) and re-create the objects Parameters ---------- obj : dict The deserialized JSON data in python dictionary form (after calling json.loads) Returns ------- object If possible, a python object based on the JSON data is created. If not, the original dictionary is simply returned. ''' if '__django_model__' in obj: model = getattr(models, obj['__django_model__']) return model(pk = obj['pk']) elif '__builtin__' in obj: func = getattr(__builtin__, obj['__builtin__']) return func(*obj['args']) elif '__class__' in obj: # look up the module mod = __builtin__.__import__(obj['__module__'], fromlist=[obj['__class__']]) # get the class with the 'getattr' and then run the class constructor on the class data return getattr(mod, obj['__class__'])(obj['__dict__']) else: # the type of object is unknown, just return the original dictionary return obj
def load(self, plugin_id, plugin_context): # get class reference from plugin descriptor attributes = self._plugin_descriptors[plugin_id].attributes() sys.path.append(os.path.join(attributes['plugin_path'], attributes['library_path'])) try: module = __builtin__.__import__( attributes['module_name'], fromlist=[attributes['class_from_class_type']], level=0) except NotImplementedError as e: qCritical('RosPluginProvider.load(%s): raised an exception:\n%s' % (plugin_id, e)) return None except Exception as e: qCritical('RosPluginProvider.load(%s) exception raised in ' '__builtin__.__import__(%s, [%s]):\n%s' % ( plugin_id, attributes['module_name'], attributes['class_from_class_type'], traceback.format_exc())) raise e class_ref = getattr(module, attributes['class_from_class_type'], None) if class_ref is None: qCritical('RosPluginProvider.load(%s): could not find class "%s" in module "%s"' % (plugin_id, attributes['class_from_class_type'], module)) return None # create plugin provider instance without context try: code = class_ref.__init__.func_code except AttributeError: code = class_ref.__init__.__code__ if code.co_argcount == 1 and plugin_context is None: return class_ref() # create plugin instance return class_ref(plugin_context)
def __import__(name, globals={}, locals={}, fromlist=[], level=-1): """Compatibility definition for Python 2.4. Silently ignore the `level` argument missing in Python < 2.5. """ # we need the level arg because the default changed in Python 3.3 return __builtin__.__import__(name, globals, locals, fromlist)
def __import__(module): # pylint: disable=W0622 utils.debug("module to import: %s" % module, 2) if not os.path.isfile(module): try: return __builtin__.__import__(module) except ImportError as e: utils.warning("import failed for module %s: %s" % (module, e.message)) return None else: sys.path.append(os.path.dirname(module)) modulename = os.path.basename(module).replace(".py", "") suff_index = None for suff in imp.get_suffixes(): if suff[0] == ".py": suff_index = suff break if suff_index is None: utils.die("no .py suffix found") with open(module) as fileHdl: try: return imp.load_module(modulename.replace(".", "_"), fileHdl, module, suff_index) except ImportError as e: utils.warning("import failed for file %s: %s" % (module, e.message)) return None
def _named_import(name): parts = name.split('.') assert(len(parts) >= 2) module = __builtin__.__import__(name) for m in parts[1:]: module = module.__dict__[m] module_name = parts[-1] _register_binding_module(module_name, module)
def _named_import(name): parts = name.split('.') assert (len(parts) >= 2) module = builtins.__import__(name) for m in parts[1:]: module = module.__dict__[m] module_name = parts[-1] _register_binding_module(module_name, module)
def r_import(name, globals={}, locals={}, fromlist=[]): '''Restricted __import__ only allows importing of specific modules''' ok_modules = ("mud", "obj", "char", "room", "exit", "account", "mudsock", "event", "action", "random", "traceback", "utils", "__restricted_builtin__") if name not in ok_modules: raise ImportError, "Untrusted module, %s" % name return __builtin__.__import__(name, globals, locals, fromlist)
def _named_import(name): import __builtin__ parts = name.split('.') assert(len(parts) >= 2) module = __builtin__.__import__(name) for m in parts[1:]: module = module.__dict__[m] sys.modules[parts[-1]] = module QT_BINDING_MODULES.append(parts[-1])
def _named_import(name): import __builtin__ parts = name.split('.') assert (len(parts) >= 2) module = __builtin__.__import__(name) for m in parts[1:]: module = module.__dict__[m] sys.modules[parts[-1]] = module QT_BINDING_MODULES.append(parts[-1])
def r_import(name, globals = {}, locals = {}, fromlist = [], level = -1): '''Restricted __import__ only allows importing of specific modules''' ok_modules = ("mud", "obj", "char", "room", "exit", "account", "mudsock", "event", "action", "random", "traceback", "utils", "__restricted_builtin__") if name not in ok_modules: raise ImportError, "Untrusted module, %s" % name return __builtin__.__import__(name, globals, locals, fromlist)
def __init__(self,kernel): magics_path = os.path.dirname(__file__)+"/magics/*.py" for file in glob(magics_path): if file != magics_path.replace("*.py","__init__.py"): module_path="JupyROOT.kernel.magics."+file.split("/")[-1].replace(".py","") try: module= __builtin__.__import__(module_path, globals(), locals(), ['register_magics'], -1) module.register_magics(kernel) except ImportError: raise Exception("Error importing Magic: %s"%module_path)
def addSnippet( self, selection ) : '''Displays the snippet selected''' #snippet = self.dictOfSnippets[ str( selection.text() ) ] snippet = self.dictOfSnippets[ selection ] if snippet == snippets.mapSnippet.mapSnippet : temp = QtGui.QInputDialog.getItem( self, 'Select the snippet to map into ...', 'Snippet', self.dictOfSnippets.keys(), editable = False ) if temp[1] == False : return snippetToMap = self.dictOfSnippets[ str( temp[0] ) ] self.maps.append( snippetToMap ) x = [ snippet, len( snippetToMap.details ) *[''] ] else : x = [ snippet, len( snippet.details )*[''] ] self.maps.append( None ) self.snippetChanged = True #-------------------------------------------------------------------------------- for i in snippet.packages : if( os.system( "dpkg -l | awk '{ print $2 }' | tail -n +6 | grep %s > /dev/null"%i ) != 0 ) : try : from __builtin__ import __import__ __import__( i ) except ImportError : QtGui.QMessageBox.warning( self, "Error !", '%s missing. Install the package before using this snippet.'%i ) return -1 #-------------------------------------------------------------------------------- self.workflow.append( x ) #print 'appended' snip = QtGui.QTableWidgetItem( snippet.name ) self.snippetsTable.insertRow( self.snippetsTable.rowCount() ) self.snippetsTable.setItem( self.snippetsTable.rowCount() - 1, 0, snip ) self.showInputs( self.snippetsTable.rowCount() - 1 ) self.snippetChanged = False
def listImportables(modName, returnModDict=False, modulesOnly=False): """! @brief list all available importables of a modules @param modName name of module to inspect @param returnModDict boolean to return a dict with module-names (k) and modules (v) @param modulesOnly only list other modules @return default return a list() of module names, if returnModDict true returns a dict { module-name (str) : module } """ emptyReturn = {} if returnModDict else [] # check if we are a sub moduel baseMod = modName if modName.find(".") != -1: baseMod, modName = modName.rsplit(".", 1) modName = [modName] else: modName = [] # try to import base module try: mod = __builtin__.__import__(baseMod, globals(), locals(), modName, -1) except: return __errorMsg("No module found named: '%s'\n" % baseMod, emptyReturn) # if is submodule get correct module if modName: mod = getattr(mod, modName[0], None) if not mod: __errorMsg("No submodule found named: '%s'\n" % modName[0], emptyReturn) # check if module is init file modIsInit = hasattr(mod, '__file__') and '__init__' in mod.__file__ modDir = os.path.dirname(mod.__file__) if modIsInit else None # check modules __all__ list foundModules = [] foundModules.extend(getattr(mod, '__all__', [])) # look for importable modules (simple dir on module) if modIsInit or (not hasattr(mod, '__file__')) or (not modulesOnly): if modulesOnly: foundModules.extend([ret for ret in dir(mod) if inspect.ismodule(getattr(mod, ret))]) else: foundModules.extend([ret for ret in dir(mod) if not(ret[-2:] == '__' and ret[:2] == '__')]) # append from file system if modDir: foundModules.extend(listModules(modDir)) # remove all __init__ foundModules = set(foundModules) if '__init__' in foundModules: foundModules.remove('__init__') if returnModDict: return (mod, list(foundModules) ) return list(foundModules)
def run(self, dreq): plugins = [p.split(".")[0] for p in self.loadPlugins()] for plugin in plugins: try: cplugin = __import__(self.pack + "."+plugin, fromlist = [plugin, ]) vulc = cplugin.getPluginClass() self.dlog.debug("[Audit Plugin]Loading Plugin --> %s"%str(vulc)) c = vulc(dreq) c.audit() except Exception,e: elog.exception(e)
def import_(module=None, version=None, magic=None): if module: pass elif version: module = by_version[version] elif magic: module = by_magic[magic] else: raise 'at least one argument is required' from __builtin__ import __import__ if module == 'marshal': return __import__('marshal', globals(), locals()) else: raise 'marshal_20 not found'
def _get_class_from_name(cls_name): ''' Get the class for the given name ''' g = globals() if g.has_key(cls_name): return g[cls_name] else: mod_name = camel_to_py_case(cls_name) m = __import__("model.classes.%s" % mod_name) cls = getattr(m.classes, mod_name) cls = getattr(cls, cls_name) g[cls_name] = cls return cls
def check_import(lib): try: global module module = __builtin__.__import__(lib) or importlib.import_module(lib) # try importing module except ImportError: print "Library does not exist: check your spelling or make sure library is installed" # module not in library or mispelled/ out of range sleep(4) library_list() print_output()
def __import__(name, glob=globals(), loc=locals(), path=""): """Runtime reloading importer. Import a module multiple times, when code has changed on disk between each import, updating the code getting executed.""" if debug: print "###IMPORTER###" leaf = name if path: name = path + "." + name if name in sys.modules: if hasattr(sys.modules[name],'__file__'): if debug: print "###LOADING:" + sys.modules[name].__file__ module_path = sys.modules[name].__file__ if module_path[-13:] == '/__init__.pyc': module_path = module_path[0:-13] if module_path[-4:] == '.pyc': module_path = module_path[0:-1] if os.path.exists(module_path): if (os.path.isdir(module_path) and os.stat(module_path).st_mtime > os.stat(module_path + "/__init__.pyc").st_mtime) or not os.path.isdir(module_path) and os.stat(module_path).st_mtime > os.stat(module_path + 'c').st_mtime: # The module has been modified, reimport it del(sys.modules[name]) if debug: print "###REIMPORTED###" return __builtin__.__import__(name, fromlist=path) else: # The module no longer exists, remove it del(sys.modules[name]) if debug: print "###REMOVED###" return None # Do not reimport, simply return the unmodified module if debug: print "###RETURNED###" return sys.modules[name] else: # Initial import if debug: print "###INITIALLY IMPORTED###" return __builtin__.__import__(name, fromlist=path)
def displaySnippet( self ) : '''Displays the snippet selected''' if self.firstTime == False : x = self.submitInputs() if x == -2 : return -1 else : self.firstTime = False #-------------------------------------------------------------------------------- selection = str( self.snippetsList.currentItem().text() ) snippet = getattr( getattr( snippets, selection ), selection ) #-------------------------------------------------------------------------------- for i in snippet.packages : if( os.system( "dpkg -l | awk '{ print $2 }' | tail -n +6 | grep %s > /dev/null"%i ) != 0 ) : try : from __builtin__ import __import__ __import__( i ) except ImportError : QtGui.QMessageBox.warning( self, "Error !", '%s missing. Install the package before using this snippet.'%i ) curSnip = '' return -1 #-------------------------------------------------------------------------------- node = QtGui.QTreeWidgetItem( self.inputsList ) node.setFlags( QtCore.Qt.ItemIsEnabled ) node.setText( 0, snippet.sname ) for i in snippet.details : item = QtGui.QTreeWidgetItem( node ) item.setFlags( QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEditable|QtCore.Qt.ItemIsDragEnabled|QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled ) item.setText( 1, i ) self.curSnip = snippet.sname self.inputsList.expandItem( node )
def check_import(clib): try: global module module = __builtin__.__import__(clib) or importlib.import_module(clib) # try importing module except ImportError: print( "Library does not exist: check your spelling or make sure library is installed" ) # module not in library or mispelled/ out of range sleep(4) library_list() print_output()
def run_tests_from_names(names, buffer_test_output, xml_output): loader = unittest.TestLoader() for name in names: package_path = name.split('.') for i in range(0, len(package_path)): package = '.'.join(itertools.chain(['irods', 'test'], package_path[0:i])) module = ''.join(['.', package_path[i]]) try: if 'importlib' in globals(): importlib.import_module(module, package=package) else: __import__(''.join([package, module])) except ImportError: break suites = [loader.loadTestsFromName(name, module=irods.test) for name in names] super_suite = unittest.TestSuite(suites) if xml_output: import xmlrunner runner = xmlrunner.XMLTestRunner(output='test-reports', verbosity=2) else: runner = unittest.TextTestRunner(verbosity=2, failfast=True, buffer=buffer_test_output, resultclass=RegisteredTestResult) results = runner.run(super_suite) return results
def init_invts(lm,ruledb,rulefile,infile,driverfile,outfile,flags,lm_object): """Processes the ruleset. Return values: 0 : Ok 1 : General Error 2 : Could not initialize Language Module """ global log log.Warning("lm_object: %s"%lm_object) try: langmodule=__builtin__.__import__(lm.split(":")[0],None,None,['LanguageModule']) except Exception, inst: log.ERROR("Cannot load Language Module: %s"%lm.split(":")[0]) log.ERROR(str(inst)) return 2
def check_import(lib): try: global module module = __builtin__.__import__(lib) or importlib.import_module(lib) # try importing module except ImportError: print "Library does not exit: check your spelling or make sure library is installed" # module not in library or mispelled/ out of range sleep(4) library_list() contents = dir(module) # handle for the module contents for c in contents: print c # iterate over contents of module showing whats inside
def run(self, dreq): plugins = [p.split(".")[0] for p in self.loadPlugins()] for plugin in plugins: try: cplugin = __import__(self.pack + "." + plugin, fromlist=[ plugin, ]) vulc = cplugin.getPluginClass() self.dlog.debug("[Audit Plugin]Loading Plugin --> %s" % str(vulc)) c = vulc(dreq) c.audit() except Exception, e: elog.exception(e)
def dynamic_filter_loader(): """Fancy way of dynamically importing existing filters. Returns ------- dict: Available filters dictionary. Keys are parameters that can be supplied to the filters. """ # We assume the filters are in the same directory as THIS file. filter_dir = os.path.dirname(__file__) filter_dir = os.path.abspath(filter_dir) # This is populated when the module is first imported. avail_filters = {} # Add this directory to the syspath. sys.path.insert(0, filter_dir) # Find all "py" files. for filter_mod in glob.glob(os.path.join(filter_dir, "*.py")): # Derive name of the module where filter is. filter_mod_file = os.path.basename(filter_mod) # Ignore this file, obviously. if filter_mod_file.startswith("__init__"): continue # Import the module with a filter. mod = __import__(filter_mod_file.replace(".pyc", "").replace(".py", "")) # Find all the classes contained in this module. classes = inspect.getmembers(mod, inspect.isclass) for cls_name, cls in classes: # For each class, if it is a sublass of PHEFilterBase, add it. if cls_name != "PHEFilterBase" and issubclass(cls, PHEFilterBase): # The parameters are inherited and defined within each filter. avail_filters[cls.parameter] = cls sys.path.remove(filter_dir) return avail_filters
def tryDoc(object_name, doesclass=None): try: import __builtin__ #Magic. module = __builtin__.__import__(module_name) #You suppose `module.__getattribute__(object_name)' works?! Ain't it. :D if doesclass: classobject = module.__getattribute__(doesclass) object = getattr(classobject, object_name) else: object = module.__getattribute__(object_name) #object.__doc__ docstring = getattr(object, '__doc__') #Exists? if docstring: print docstring except: pass
def library_list(): global lib print "--------------------------------------------------" print "Here are the available library's:" print "--------------------------------------------------" table = PrettyTable(["Index", "Package"]) # initiate prettytable count = 0 for mod in path_mod: # adds numbers and modules to prettytable table.add_row(["%d" % count, "%s" % mod]) count += 1 print table print "--------------------------------------------------" ans = raw_input("what library would you like to look at?: ") print "--------------------------------------------------" try: lib = mod_dict[ans] # check if input is in module dictionary except KeyError: print "Please choose a valid number associating to a library" sleep(3) # ^ error checking for invalid input library_list() # print repr(lib) - shows raw string literal lib = lib.replace("\n", "") # ^get rid of all newlines so only actual module name is checked print "You have chosen to look at %s" % lib print "--------------------------------------------------" sleep(1) try: global module module = __builtin__.__import__(lib) or importlib.import_module(lib) # try importing module except ImportError: print "Please choose a valid number associating to a library" # module not in library or mispelled/ out of range sleep(3) library_list() contents = dir(module) # handle for the module contents for c in contents: print c # iterate over contents of module showing whats inside
def loadPluginInstances(self, plugins): self.plugins = [] for plugin in plugins: if not self.plugin_defs.has_key(plugin): rospy.logerr('cannot find %s in plugins for %s' % (plugin, self.package_name)) else: try: module_path = self.plugin_defs[plugin] module_name, class_from_class_type = module_path.rsplit( '.', 1) module = __builtin__.__import__( module_name, fromlist=[class_from_class_type], level=0) class_ref = getattr(module, class_from_class_type, None) if class_ref is None: rospy.logfatal('cannot find %s' % (class_from_class_type)) else: self.plugins.append(class_ref()) except: rospy.logerr('failed to load %s' % (plugin)) traceback.print_exc(file=sys.stdout) return self.plugins
def loadPluginInstances(self, plugins, manager): """ plugins := {name: spec, name: spec, ...} spec := {class: class, args: args} """ self.plugins = [] #for (instance_name, instance_spec) in plugins.items(): plugin_keys = plugins.keys() plugin_keys.sort() for instance_name in plugin_keys: instance_spec = plugins[instance_name] instance_class = instance_spec["class"] instance_args = instance_spec["args"] if not self.plugin_defs.has_key(instance_class): rospy.logerr('cannot find %s in plugins for %s' % (instance_class, self.package_name)) else: try: module_path = self.plugin_defs[instance_class] module_name, class_from_class_type = module_path.rsplit( '.', 1) module = __builtin__.__import__( module_name, fromlist=[class_from_class_type], level=0) class_ref = getattr(module, class_from_class_type, None) if class_ref is None: rospy.logfatal('cannot find %s' % (class_from_class_type)) else: self.plugins.append( class_ref(instance_name, instance_args)) self.plugins[-1].registerManager(manager) except: rospy.logerr('failed to load %s' % (instance_class)) traceback.print_exc(file=sys.stdout) return self.plugins
def getInstance(self, dashboard_name, dashboard_node=None): """! Load Python package. @param descriptor: package descriptor xml node. @type descriptor: Element. @param uuid: dashboard id. @type uuid: string. @return dashboard_instance: dashboard instance. @type dashboard_instance: Dashboard. """ dashboard_pkg_name = self.getPkgByName(dashboard_name) dashboard_dir = get_pkg_dir(dashboard_pkg_name) dashboard_descriptor_file = os.path.join(dashboard_dir, "dashboard_descriptor.xml") if not os.path.isfile(dashboard_descriptor_file): self._context.getLogger().err( 'Cannot found dashboard_descriptor.xml into plugin %s' % dashboard_name) return None dashboard_descriptor_root = ElementTree.parse( dashboard_descriptor_file).getroot() dashboard_import = dashboard_descriptor_root.find('import') dashboard_module_path = dashboard_import.attrib['module'] dashboard_class_name = dashboard_import.attrib['class'] sys.path.append( os.path.join(dashboard_dir, self.DASHBOARD_SOURCES_LOCATION)) dashboard_class_ref = None #Import package module #Raise exception try: module = __builtin__.__import__(dashboard_module_path, fromlist=[dashboard_class_name], level=0) except Exception as ex: self._context.getLogger().err("Cannot import plugin '%s' !\n%s" % (dashboard_name, str(ex))) return None #Get referance to plugin class dashboard_class_ref = getattr(module, dashboard_class_name) if dashboard_class_ref is None: self._context.getLogger().err("Cannot found plugin class '%s' !" % dashboard_class_name) return None dashboard_instance = dashboard_class_ref(self._context) dashboard_params = DashboardProvider.getParameters( dashboard_descriptor_root, dashboard_node) dashboard_instance.setup(dashboard_descriptor_root, dashboard_params) return dashboard_instance
def safe_import(name, globals=None, locals=None, fromlist=None, level=-1): mod = __builtin__.__import__(name, globals, locals, fromlist, level) if mod: mod.open = dummyOpen mod.urllib2 = dummyUrllib2 return mod
def eval(self, x, globals=None, locals=None): """ EXAMPLES:: sage: from sage.misc.inline_fortran import InlineFortran, _example sage: fortran = InlineFortran(globals()) sage: fortran(_example) sage: import numpy sage: n = numpy.array(range(10),dtype=float) sage: fib(n,int(10)) sage: n array([ 0., 1., 1., 2., 3., 5., 8., 13., 21., 34.]) TESTS:: sage: os.chdir(SAGE_ROOT) sage: fortran.eval("SYNTAX ERROR !@#$") Traceback (most recent call last): ... RuntimeError: failed to compile Fortran code:... sage: os.getcwd() == SAGE_ROOT True """ if len(x.splitlines()) == 1 and os.path.exists(x): filename = x x = open(x).read() if filename.lower().endswith('.f90'): x = '!f90\n' + x from numpy import f2py # Create everything in a temporary directory mytmpdir = tmp_dir() try: old_cwd = os.getcwd() os.chdir(mytmpdir) old_import_path = os.sys.path os.sys.path.append(mytmpdir) name = "fortran_module" # Python module name # if the first line has !f90 as a comment, gfortran will # treat it as Fortran 90 code if x.startswith('!f90'): fortran_file = name + '.f90' else: fortran_file = name + '.f' s_lib_path = "" s_lib = "" for s in self.library_paths: s_lib_path = s_lib_path + "-L%s " for s in self.libraries: s_lib = s_lib + "-l%s " % s log = name + ".log" extra_args = '--quiet --f77exec=sage-inline-fortran --f90exec=sage-inline-fortran %s %s >"%s" 2>&1' % ( s_lib_path, s_lib, log) f2py.compile(x, name, extra_args=extra_args, source_fn=fortran_file) log_string = open(log).read() # f2py.compile() doesn't raise any exception if it fails. # So we manually check whether the compiled file exists. # NOTE: the .so extension is used expect on Cygwin, # that is even on OS X where .dylib might be expected. soname = name uname = os.uname()[0].lower() if uname[:6] == "cygwin": soname += '.dll' else: soname += '.so' if not os.path.isfile(soname): raise RuntimeError("failed to compile Fortran code:\n" + log_string) if self.verbose: print(log_string) m = __builtin__.__import__(name) finally: os.sys.path = old_import_path os.chdir(old_cwd) try: import shutil shutil.rmtree(mytmpdir) except OSError: # This can fail for example over NFS pass for k, x in m.__dict__.iteritems(): if k[0] != '_': self.globals[k] = x
def eval(self,x,globals=None, locals=None): """ EXAMPLES:: sage: from sage.misc.inline_fortran import InlineFortran, _example sage: test_fortran = InlineFortran(globals()) # optional -- fortran sage: test_fortran(_example) # optional -- fortran sage: import numpy sage: n = numpy.array(range(10),dtype=float) sage: fib(n,int(10)) # optional -- fortran sage: n # optional -- fortran array([ 0., 1., 1., 2., 3., 5., 8., 13., 21., 34.]) """ if len(x.splitlines()) == 1 and os.path.exists(x): filename = x x = open(x).read() if filename.lower().endswith('.f90'): x = '!f90\n' + x global count # On linux g77_shared should be a script that runs sage_fortran -shared # On OS X it should be a script that runs gfortran -bundle -undefined dynamic_lookup path = os.environ['SAGE_LOCAL']+'/bin/sage-g77_shared' from numpy import f2py old_import_path=os.sys.path cwd=os.getcwd() os.sys.path.append(cwd) #name = tmp_dir() + '/fortran_module_%d'%count name = 'fortran_module_%d'%count if os.path.exists(name): os.unlink(name) s_lib_path="" s_lib="" for s in self.library_paths: s_lib_path=s_lib_path+"-L"+s+" " for s in self.libraries: s_lib=s_lib +"-l"+s + " " # if the first line has !f90 as a comment gfortran will treat it as # fortran 90 code if x.startswith('!f90'): fname = os.path.join(tmp_filename() +'.f90') else: fname = os.path.join(tmp_filename() +'.f') log = tmp_filename() extra_args = '--quiet --f77exec=%s --f90exec=%s %s %s 1>&2 >"%s"'%( path, path, s_lib_path, s_lib, log) f2py.compile(x, name, extra_args = extra_args, source_fn=fname) log_string = open(log).read() os.unlink(log) os.unlink(fname) if self.verbose: print log_string count += 1 try: m=__builtin__.__import__(name) except ImportError: if not self.verbose: print log_string return finally: os.sys.path=old_import_path os.unlink(name + '.so') for k, x in m.__dict__.iteritems(): if k[0] != '_': self.globals[k] = x
def handleRequest(self): """Handle a single request from the remote process. Blocks until a request is available.""" result = None while True: try: ## args, kwds are double-pickled to ensure this recv() call never fails cmd, reqId, nByteMsgs, optStr = self.conn.recv() break except EOFError: self.debugMsg( ' handleRequest: got EOFError from recv; raise ClosedError.' ) ## remote process has shut down; end event loop raise ClosedError() except IOError as err: if err.errno == 4: ## interrupted system call; try again self.debugMsg( ' handleRequest: got IOError 4 from recv; try again.') continue else: self.debugMsg( ' handleRequest: got IOError %d from recv (%s); raise ClosedError.' % (err.errno, err.strerror)) raise ClosedError() self.debugMsg(" handleRequest: received %s %s" % (str(cmd), str(reqId))) ## read byte messages following the main request byteData = [] if nByteMsgs > 0: self.debugMsg(" handleRequest: reading %d byte messages" % nByteMsgs) for i in range(nByteMsgs): while True: try: byteData.append(self.conn.recv_bytes()) break except EOFError: self.debugMsg( " handleRequest: got EOF while reading byte messages; raise ClosedError." ) raise ClosedError() except IOError as err: if err.errno == 4: self.debugMsg( " handleRequest: got IOError 4 while reading byte messages; try again." ) continue else: self.debugMsg( " handleRequest: got IOError while reading byte messages; raise ClosedError." ) raise ClosedError() try: if cmd == 'result' or cmd == 'error': resultId = reqId reqId = None ## prevents attempt to return information from this request ## (this is already a return from a previous request) opts = pickle.loads(optStr) self.debugMsg(" handleRequest: id=%s opts=%s" % (str(reqId), str(opts))) #print os.getpid(), "received request:", cmd, reqId, opts returnType = opts.get('returnType', 'auto') if cmd == 'result': with self.resultLock: self.results[resultId] = ('result', opts['result']) elif cmd == 'error': with self.resultLock: self.results[resultId] = ('error', (opts['exception'], opts['excString'])) elif cmd == 'getObjAttr': result = getattr(opts['obj'], opts['attr']) elif cmd == 'callObj': obj = opts['obj'] fnargs = opts['args'] fnkwds = opts['kwds'] ## If arrays were sent as byte messages, they must be re-inserted into the ## arguments if len(byteData) > 0: for i, arg in enumerate(fnargs): if isinstance(arg, tuple) and len( arg) > 0 and arg[0] == '__byte_message__': ind = arg[1] dtype, shape = arg[2] fnargs[i] = np.fromstring( byteData[ind], dtype=dtype).reshape(shape) for k, arg in fnkwds.items(): if isinstance(arg, tuple) and len( arg) > 0 and arg[0] == '__byte_message__': ind = arg[1] dtype, shape = arg[2] fnkwds[k] = np.fromstring( byteData[ind], dtype=dtype).reshape(shape) if len( fnkwds ) == 0: ## need to do this because some functions do not allow keyword arguments. try: result = obj(*fnargs) except: print("Failed to call object %s: %d, %s" % (obj, len(fnargs), fnargs[1:])) raise else: result = obj(*fnargs, **fnkwds) elif cmd == 'getObjValue': result = opts[ 'obj'] ## has already been unpickled into its local value returnType = 'value' elif cmd == 'transfer': result = opts['obj'] returnType = 'proxy' elif cmd == 'transferArray': ## read array data from next message: result = np.fromstring( byteData[0], dtype=opts['dtype']).reshape(opts['shape']) returnType = 'proxy' elif cmd == 'import': name = opts['module'] fromlist = opts.get('fromlist', []) mod = builtins.__import__(name, fromlist=fromlist) if len(fromlist) == 0: parts = name.lstrip('.').split('.') result = mod for part in parts[1:]: result = getattr(result, part) else: result = map(mod.__getattr__, fromlist) elif cmd == 'del': LocalObjectProxy.releaseProxyId(opts['proxyId']) #del self.proxiedObjects[opts['objId']] elif cmd == 'close': if reqId is not None: result = True returnType = 'value' exc = None except: exc = sys.exc_info() if reqId is not None: if exc is None: self.debugMsg( " handleRequest: sending return value for %d: %s" % (reqId, str(result))) #print "returnValue:", returnValue, result if returnType == 'auto': with self.optsLock: noProxyTypes = self.proxyOptions['noProxyTypes'] result = self.autoProxy(result, noProxyTypes) elif returnType == 'proxy': result = LocalObjectProxy(result) try: self.replyResult(reqId, result) except: sys.excepthook(*sys.exc_info()) self.replyError(reqId, *sys.exc_info()) else: self.debugMsg(" handleRequest: returning exception for %d" % reqId) self.replyError(reqId, *exc) elif exc is not None: sys.excepthook(*exc) if cmd == 'close': if opts.get('noCleanup', False) is True: os._exit( 0 ) ## exit immediately, do not pass GO, do not collect $200. ## (more importantly, do not call any code that would ## normally be invoked at exit) else: raise ClosedError()
#!/usr/bin/env python # -*- coding: utf-8 -*- # 2012.07.31 import sys import __builtin__ names = sys.builtin_module_names for name in names: module = __builtin__.__import__(name) print name print '-' * name.__len__() print print module.__doc__ print print '#' * 83 del module