def load_backends(): backends = [] configured_backends = getattr(settings, "NOTIFICATION_BACKENDS", default_backends) for medium_id, bits in enumerate(configured_backends): if len(bits) == 2: label, backend_path = bits spam_sensitivity = None elif len(bits) == 3: label, backend_path, spam_sensitivity = bits else: raise exceptions.ImproperlyConfigured( "NOTIFICATION_BACKENDS does not contain enough data." ) dot = backend_path.rindex(".") backend_mod, backend_class = backend_path[:dot], backend_path[dot + 1:] try: # import the module and get the module from sys.modules __import__(backend_mod) mod = sys.modules[backend_mod] except ImportError as e: raise exceptions.ImproperlyConfigured( "Error importing notification backend {}: \"{}\"".format(backend_mod, e) ) # add the backend label and an instantiated backend class to the # backends list. backend_instance = getattr(mod, backend_class)(medium_id, spam_sensitivity) backends.append(((medium_id, label), backend_instance)) return dict(backends)
def _import_transporter(self, transporter): """Imports transporter module and class, returns class. Input value can be: * a full/absolute module path, like "MyTransporterPackage.SomeTransporterClass" """ transporter_class = None module = None alternatives = [] default_prefix = 'cloud_sync_app.transporter.transporter_' if not transporter.startswith(default_prefix): alternatives.append('%s%s' % (default_prefix, transporter)) for module_name in alternatives: try: module = __import__(module_name, globals(), locals(), ["TRANSPORTER_CLASS"], -1) except ImportError: import traceback traceback.print_exc() pass if not module: msg = "The transporter module '%s' could not be found." % transporter if len(alternatives) > 1: msg = '%s Tried (%s)' % (msg, ', '.join(alternatives)) self.logger.error(msg) else: try: classname = module.TRANSPORTER_CLASS module = __import__(module_name, globals(), locals(), [classname]) transporter_class = getattr(module, classname) except AttributeError: self.logger.error("The Transporter module '%s' was found, but its Transporter class '%s' could not be found." % (module_name, classname)) return transporter_class
def test_dir(completion_test_dir, thirdparty=False): global tests_fail for f_name in os.listdir(completion_test_dir): files_to_execute = [a for a in test_files.items() if a[0] in f_name] lines_to_execute = reduce(lambda x, y: x + y[1], files_to_execute, []) if f_name.endswith(".py") and (not test_files or files_to_execute): # for python2.5 certain tests are not being done, because it # only has these features partially. if is_py25 and f_name in ['generators.py', 'types.py']: continue if thirdparty: try: # there is always an underline at the end. # It looks like: completion/thirdparty/pylab_.py __import__(f_name.replace('_.py', '')) except ImportError: summary.append('Thirdparty-Library %s not found.' % f_name) continue path = os.path.join(completion_test_dir, f_name) f = open(path) num_tests, fails = run_test(f.read(), f_name, lines_to_execute) global test_sum test_sum += num_tests s = 'run %s tests with %s fails (%s)' % (num_tests, fails, f_name) tests_fail += fails print(s) summary.append(s)
def init_func(self, creator_fd, raw_socket_fd, tun_fd, cs, caddr, debug=True): self.__debug = debug self.__caddr = caddr name = "freenet.lib.crypto.%s" % fns_config.configs["tcp_crypto_module"]["name"] __import__(name) crypto = sys.modules[name] args = fns_config.configs["tcp_crypto_module"]["args"] self.__encrypt = crypto.encrypt(*args) self.__decrypt = crypto.decrypt(*args) self.__creator_fd = creator_fd self.__traffic_send_fd = raw_socket_fd self.__vlan_ips = [] self.__udp_natp_to_fd = {} self.__tun_fd = tun_fd self.set_socket(cs) self.register(self.fileno) self.add_evt_read(self.fileno) self.set_timeout(self.fileno, self.__TIMEOUT_NO_AUTH) self.print_access_log("connect") return self.fileno
def wrap(f): if modules: # attach import to function setattr(f, 'imports', modules) for alternatives in modules: # alternatives are comma seperated alternatives = alternatives.split(',') # we import the part of the import X.Y.Z -> Z mod_name = alternatives[0].split('.')[-1] for mod in alternatives: mod = mod.strip().split('.') try: if len(mod) == 1: module = __import__(mod[0]) else: module = getattr(__import__('.'.join(mod[:-1]), \ fromlist=[mod[-1]]), mod[-1]) f.func_globals[mod_name] = module break # import only one except ImportError: pass else: if forgive: # no break -> no import warnings.warn('Failed to import %s' % alternatives) else: raise ImportError('Failed to import %s' % alternatives) return f
def cython_pyximport(self, line, cell): """Compile and import a Cython code cell using pyximport. The contents of the cell are written to a `.pyx` file in the current working directory, which is then imported using `pyximport`. This magic requires a module name to be passed:: %%cython_pyximport modulename def f(x): return 2.0*x The compiled module is then imported and all of its symbols are injected into the user's namespace. For most purposes, we recommend the usage of the `%%cython` magic. """ module_name = line.strip() if not module_name: raise ValueError('module name must be given') fname = module_name + '.pyx' with io.open(fname, 'w', encoding='utf-8') as f: f.write(cell) if 'pyximport' not in sys.modules: import pyximport pyximport.install(reload_support=True) if module_name in self._reloads: module = self._reloads[module_name] reload(module) else: __import__(module_name) module = sys.modules[module_name] self._reloads[module_name] = module self._import_all(module)
def get_versions(module_list=None): if not module_list: return {} ext_module_list = set() for m in module_list: parts = m.split('.') ext_module_list.update('.'.join(parts[:idx]) for idx in range(1, len(parts) + 1)) versions = {} for module_name in ext_module_list: if module_name not in _VERSION_CACHE: try: __import__(module_name) except ImportError: continue try: app = sys.modules[module_name] except KeyError: continue try: version = get_version_from_app(module_name, app) except Exception as e: logger.exception(e) version = None _VERSION_CACHE[module_name] = version else: version = _VERSION_CACHE[module_name] if version is not None: versions[module_name] = version return versions
def import_object(import_str): """Returns an object including a module or module and class""" try: __import__(import_str) return sys.modules[import_str] except ImportError: return import_class(import_str)
def load_module(module_name): """Imports a module given its name and returns a handle to it.""" try: __import__(module_name) except ImportError: raise ImportError('No module named %s' % module_name) return sys.modules[module_name]
def patch_thread(threading=True, _threading_local=True): """Replace the standard :mod:`thread` module to make it greenlet-based. If *threading* is true (the default), also patch ``threading.local``. If *_threading_local* is true (the default), also patch ``_threading_local.local``. """ from gevent import thread as green_thread thread = __import__('thread') if thread.exit is not green_thread.exit: thread.get_ident = green_thread.get_ident thread.start_new_thread = green_thread.start_new_thread thread.LockType = green_thread.LockType thread.allocate_lock = green_thread.allocate_lock thread.exit = green_thread.exit if hasattr(green_thread, 'stack_size'): thread.stack_size = green_thread.stack_size from gevent.local import local thread._local = local if threading: if noisy and 'threading' in sys.modules: sys.stderr.write("gevent.monkey's warning: 'threading' is already imported\n\n") threading = __import__('threading') threading.local = local if _threading_local: _threading_local = __import__('_threading_local') _threading_local.local = local
def module_exists(module_name): try: __import__(module_name) except ImportError: return False else: return True
def init_app(app): # Load all core APIs import udata.core.spatial.api import udata.core.metrics.api import udata.core.user.api import udata.core.dataset.api import udata.core.issues.api import udata.core.discussions.api import udata.core.reuse.api import udata.core.organization.api import udata.core.followers.api import udata.core.jobs.api import udata.core.site.api import udata.core.tags.api import udata.core.topic.api import udata.core.post.api import udata.features.transfer.api # Load plugins API for plugin in app.config['PLUGINS']: name = 'udata.ext.{0}.api'.format(plugin) try: __import__(name) except ImportError: pass except Exception as e: log.error('Error importing %s: %s', name, e) # api.init_app(app) app.register_blueprint(apidoc) app.register_blueprint(apiv1) oauth2.init_app(app)
def import_module(module_name): """ Imports a module. A single point of truth for importing modules to be documented by `pdoc`. In particular, it makes sure that the top module in `module_name` can be imported by using only the paths in `pdoc.import_path`. If a module has already been imported, then its corresponding entry in `sys.modules` is returned. This means that modules that have changed on disk cannot be re-imported in the same process and have its documentation updated. """ if import_path != sys.path: # Such a kludge. Only restrict imports if the `import_path` has # been changed. We don't want to always restrict imports, since # providing a path to `imp.find_module` stops it from searching # in special locations for built ins or frozen modules. # # The problem here is that this relies on the `sys.path` not being # independently changed since the initialization of this module. # If it is changed, then some packages may fail. # # Any other options available? # Raises an exception if the parent module cannot be imported. # This hopefully ensures that we only explicitly import modules # contained in `pdoc.import_path`. imp.find_module(module_name.split('.')[0], import_path) if module_name in sys.modules: return sys.modules[module_name] else: __import__(module_name) return sys.modules[module_name]
def load_plugins(cls, modules, config, debug=None): """Load plugins from `modules`. Returns a list of loaded and configured plugins. """ plugins = cls() plugins.debug = debug for module in modules: plugins.current_module = module __import__(module) mod = sys.modules[module] coverage_init = getattr(mod, "coverage_init", None) if not coverage_init: raise CoverageException( "Plugin module %r didn't define a coverage_init function" % module ) options = config.get_plugin_options(module) coverage_init(plugins, options) plugins.current_module = None return plugins
def wrap(*args, **kwargs): __import__('time').sleep(seconds) try: result = target(*args, **kwargs) except: result = None return result
def load_backend(backend_name=None): if not backend_name: backend_name = settings.HAYSTACK_SEARCH_ENGINE try: # Most of the time, the search backend will be one of the # backends that ships with haystack, so look there first. return __import__("haystack.backends.%s_backend" % backend_name, {}, {}, [""]) except ImportError, e: # If the import failed, we might be looking for a search backend # distributed external to haystack. So we'll try that next. try: return __import__("%s_backend" % backend_name, {}, {}, [""]) except ImportError, e_user: # The search backend wasn't found. Display a helpful error message # listing all possible (built-in) database backends. backend_dir = os.path.join(__path__[0], "backends") available_backends = [ os.path.splitext(f)[0].split("_backend")[0] for f in os.listdir(backend_dir) if f != "base.py" and not f.startswith("_") and not f.startswith(".") and not f.endswith(".pyc") ] available_backends.sort() if backend_name not in available_backends: raise ImproperlyConfigured, "%r isn't an available search backend. Available options are: %s" % ( backend_name, ", ".join(map(repr, available_backends)), ) else: raise # If there's some other error, this must be an error in Django itself.
def find_controller(self, controller): """Locates a controller by attempting to import it then grab the SomeController instance from the imported module. Override this to change how the controller object is found once the URL has been resolved. """ # Check to see if we've cached the class instance for this name if controller in self.controller_classes: return self.controller_classes[controller] # Pull the controllers class name, import controller full_module_name = self.package_name + '.controllers.' \ + controller.replace('/', '.') # Hide the traceback here if the import fails (bad syntax and such) __traceback_hide__ = 'before_and_this' __import__(full_module_name) if hasattr(sys.modules[full_module_name], '__controller__'): mycontroller = getattr( sys.modules[full_module_name], sys.modules[full_module_name].__controller__) else: module_name = controller.split('/')[-1] class_name = class_name_from_module_name(module_name) + \ 'Controller' if self.log_debug: log.debug("Found controller, module: '%s', class: '%s'", full_module_name, class_name) mycontroller = getattr(sys.modules[full_module_name], class_name) self.controller_classes[controller] = mycontroller return mycontroller
def migrations_dir(self): """ Returns the full path of the migrations directory. If it doesn't exist yet, returns where it would exist, based on the app's migrations module (defaults to app.migrations) """ module_path = self.migrations_module() try: module = __import__(module_path, {}, {}, ['']) except ImportError: # There's no migrations module made yet; guess! try: parent = __import__(".".join(module_path.split(".")[:-1]), {}, {}, ['']) except ImportError: # The parent doesn't even exist, that's an issue. raise exceptions.InvalidMigrationModule( application = self.application.__name__, module = module_path, ) else: # Good guess. return os.path.join(os.path.dirname(parent.__file__), module_path.split(".")[-1]) else: # Get directory directly return os.path.dirname(module.__file__)
def run(self): if self._show_help: # extra printout, --help-plugins disables _log() print('Installed jobs:') self._log('--- begin') self._log('--- loading plugins:') plugin_path = [os.path.join(os.path.dirname(os.path.abspath(__file__)), 'plugins'), self._conf.plugin_dir] for p in plugin_path: if not os.path.exists(p): self._log('!!! skipping "{0}" - no such file or directory'.format(p)) continue sys.path.append(p) for (importer, name, ispkg) in pkgutil.walk_packages([p]): if name.startswith('ImpJob'): try: __import__(name) except Exception, e: self._log('!!! unable to import "{0}": {1}: {2}'.format( name, e.__class__.__name__, e))
def load_plugins(): with open(__config_file__, "r") as fh: for line in fh: line = line.strip() if line.startswith("#") or line == "": continue # just load the whole shit... try: __import__(pluginPath+"."+line, globals(), locals(), [], -1) except NecessaryModuleNotFound as e: logger.critical("Failed loading plugin due to missing module: "+str(e)) except ApiKeyNotFoundException as e: logger.critical("Failed loading plugin due to missing API key: "+str(e)) except: logger.exception("Plugin loading failed") # as they are loaded in the order in the file we will have the same order in __subclasses__()... I hope for clazz in Plugin.__subclasses__(): # look at all functions of a class lets filter them first methods = filter(lambda x: type(x) == FunctionType, clazz.__dict__.values()) # now we check if the method is decorated by register for method in methods: if __criteria_key__ in method.__dict__: criterias = method.__dict__[__criteria_key__] for lang, regex in criterias.items(): if not lang in plugins: plugins[lang] = [] # yeah... save the regex, the clazz and the method, shit just got loaded... plugins[lang].append((regex, clazz, method))
def app(root=None, redirect_to_fallback=True, fallback_url=None, password_file=None): import sys, os from pypiserver import core sys.modules.pop("pypiserver._app", None) __import__("pypiserver._app") _app = sys.modules["pypiserver._app"] import bottle if root is None: root = os.path.expanduser("~/packages") if fallback_url is None: fallback_url="http://pypi.python.org/simple" #os.listdir(root) _app.configure(root=root, redirect_to_fallback=redirect_to_fallback, fallback_url=fallback_url, password_file=password_file) _app.app.module = _app bottle.debug(True) return _app.app
def import_app(module): parts = module.split(":", 1) if len(parts) == 1: module, obj = module, "application" else: module, obj = parts[0], parts[1] try: __import__(module) except ImportError: if module.endswith(".py") and os.path.exists(module): raise ImportError("Failed to find application, did " "you mean '%s:%s'?" % (module.rsplit(".", 1)[0], obj)) else: raise mod = sys.modules[module] try: app = eval(obj, mod.__dict__) except NameError: raise AppImportError("Failed to find application: %r" % module) if app is None: raise AppImportError("Failed to find application object: %r" % obj) return app
def getargs(self,moduleName,className,method) : ''' This will return the list of arguments in a method of python module of class. It accepts method list as an argument. ''' print "Message : Argument list is being obtained for each method" methodArgsDict = {} if className == None: moduleList = moduleName.split(".") for index,name in enumerate(method) : Module = __import__(moduleList[len(moduleList) -1], globals(), locals(), [moduleList[len(moduleList) -2]], -1) try : names = vars(Module)[name] except KeyError: print "Message : method '" + name + "'does not exists,Continued with including it. " return False argumentList = inspect.getargspec(names) #inspect.getargvalues(name) methodArgsDict[name] = argumentList[0] else : moduleList = moduleName.split(".") for index,name in enumerate(method) : Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(), [className], -1) Class = getattr(Module, className) try : names = vars(Class)[name] except KeyError : print "Message : method '" + name + "'does not exists,Continued with include it." return False argumentList = inspect.getargspec(names) #inspect.getargvalues(name) methodArgsDict[name] = argumentList[0] return methodArgsDict
def test_package_import__semantics(self): # Generate a couple of broken modules to try importing. # ...try loading the module when there's a SyntaxError self.rewrite_file('for') try: __import__(self.module_name) except SyntaxError: pass else: raise RuntimeError, 'Failed to induce SyntaxError' self.assertNotIn(self.module_name, sys.modules) self.assertFalse(hasattr(sys.modules[self.package_name], 'foo')) # ...make up a variable name that isn't bound in __builtins__ var = 'a' while var in dir(__builtins__): var += random.choose(string.letters) # ...make a module that just contains that self.rewrite_file(var) try: __import__(self.module_name) except NameError: pass else: raise RuntimeError, 'Failed to induce NameError.' # ...now change the module so that the NameError doesn't # happen self.rewrite_file('%s = 1' % var) module = __import__(self.module_name).foo self.assertEqual(getattr(module, var), 1)
def getmethods(self,modulePath,Class) : ''' This will get the list of methods in given module or class. It accepts the module path and class name. If there is no class name then it has be mentioned as None. ''' methodList = [] moduleList = modulePath.split("/") newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) print "Message : Method list is being obatined , Please wait ..." try : if Class : Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(), [Class], -1) ClassList = [x.__name__ for x in Module.__dict__.values() if inspect.isclass(x)] self.ClassList = ClassList Class = vars(Module)[Class] methodList = [x.__name__ for x in Class.__dict__.values() if inspect.isfunction(x)] else : Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(),[moduleList[len(moduleList) - 2]], -1) methodList = [x.__name__ for x in Module.__dict__.values() if inspect.isfunction(x)] ClassList = [x.__name__ for x in Module.__dict__.values() if inspect.isclass(x)] self.ClassList = ClassList except : print "Error : " +str(sys.exc_info()[1]) self.method = methodList return self.method
def import_backends(): """ Import files in the duplicity/backends directory where the filename ends in 'backend.py' and ignore the rest. @rtype: void @return: void """ path = duplicity.backends.__path__[0] assert path.endswith("duplicity/backends"), duplicity.backends.__path__ files = os.listdir(path) files.sort() for fn in files: if fn.endswith("backend.py"): fn = fn[:-3] imp = "duplicity.backends.%s" % (fn,) try: __import__(imp) res = "Succeeded" except Exception: res = "Failed: " + str(sys.exc_info()[1]) log.Log(_("Import of %s %s") % (imp, res), log.INFO) else: continue
def main(listener_fd, alive_r, preload, main_path=None, sys_path=None): '''Run forkserver.''' if preload: if '__main__' in preload and main_path is not None: process.current_process()._inheriting = True try: spawn.import_main_path(main_path) finally: del process.current_process()._inheriting for modname in preload: try: __import__(modname) except ImportError: pass util._close_stdin() # ignoring SIGCHLD means no need to reap zombie processes # letting SIGINT through avoids KeyboardInterrupt tracebacks handlers = { signal.SIGCHLD: signal.SIG_IGN, signal.SIGINT: signal.SIG_DFL, } old_handlers = {sig: signal.signal(sig, val) for (sig, val) in handlers.items()} with socket.socket(socket.AF_UNIX, fileno=listener_fd) as listener, \ selectors.DefaultSelector() as selector: _forkserver._forkserver_address = listener.getsockname() selector.register(listener, selectors.EVENT_READ) selector.register(alive_r, selectors.EVENT_READ) while True: try: while True: rfds = [key.fileobj for (key, events) in selector.select()] if rfds: break if alive_r in rfds: # EOF because no more client processes left assert os.read(alive_r, 1) == b'' raise SystemExit assert listener in rfds with listener.accept()[0] as s: code = 1 if os.fork() == 0: try: _serve_one(s, listener, alive_r, old_handlers) except Exception: sys.excepthook(*sys.exc_info()) sys.stderr.flush() finally: os._exit(code) except OSError as e: if e.errno != errno.ECONNABORTED: raise
def check_config(dbdriver, dbtype, dbhost, dbuser, dbpasswd, testdb): global DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB, DBSCHEMA, SQL_FILE DBDRIVER = dbdriver DBTYPE = dbtype DBHOST = dbhost DBUSER = dbuser DBPASSWD = dbpasswd TESTDB = testdb #Check the database driver is installed: try: __import__(DBDRIVER) except ImportError: message = "Install %s if you want to use %s with BioSQL " % (DBDRIVER, DBTYPE) raise MissingExternalDependencyError(message) try: if DBDRIVER in ["sqlite3"]: server = BioSeqDatabase.open_database(driver = DBDRIVER, db = TESTDB) else: server = BioSeqDatabase.open_database(driver = DBDRIVER, user = DBUSER, passwd = DBPASSWD, host = DBHOST) server.close() del server except Exception, e: message = "Connection failed, check settings if you plan to use BioSQL: %s" % str(e) raise MissingExternalDependencyError(message)
def get_config(config_path): __import__('errbot.config-template') # - is on purpose, it should not be imported normally ;) template = sys.modules['errbot.config-template'] config_fullpath = config_path if not path.exists(config_fullpath): log.error( 'I cannot find the file %s \n' '(You can change this path with the -c parameter see --help)' % config_path ) log.info( 'You can use the template %s as a base and copy it to %s. \nYou can then customize it.' % ( path.dirname(template.__file__) + sep + 'config-template.py', config_path + sep) ) exit(-1) # noinspection PyBroadException try: config = __import__(path.splitext(path.basename(config_fullpath))[0]) diffs = [item for item in set(dir(template)) - set(dir(config)) if not item.startswith('_')] if diffs: log.error('You are missing configs defined from the template :') for diff in diffs: log.error('Missing config : %s' % diff) exit(-1) except Exception as _: log.exception('I could not import your config from %s, please check the error below...' % config_fullpath) exit(-1) log.info('Config check passed...') return config
def setup(self): try: __import__('multiprocessing') except ImportError: raise SkipTest('multiprocessing not supported') from celery.concurrency.prefork import TaskPool self.TaskPool = TaskPool
def test_module_docstring(self): """Test for existence of module docstring""" print("testing module docstring...") result = len(__import__('models.user').__doc__) self.assertTrue(result > 0, True)
def test(self): M1 = Migrations(__import__("fakeapp", {}, {}, [''])) self.assertEqual(M1, Migrations("fakeapp")) self.assertEqual(M1, Migrations(self.create_fake_app("fakeapp")))
def build_op(build_type, json_str): """ call op functions with function name and input args json_str Args: build_type : op function name json_str (str): op function input args Raises: Exception: If specific keyword is not found. """ kernel_info = json.loads(json_str) check_kernel_info(kernel_info) # import module op_name = kernel_info['op_info']['name'] try: custom_flag = False if 'impl_path' in kernel_info and kernel_info['impl_path'] is not None: impl_path = os.path.realpath(kernel_info['impl_path']) if os.path.isfile(impl_path): path, file_name = os.path.split(impl_path) op_name, _ = os.path.splitext(file_name) impl_path = path custom_flag = True else: impl_path = "" _initialize(impl_path) inputs_args = get_args(kernel_info['op_info'], 'inputs') outputs_args = get_args(kernel_info['op_info'], 'outputs') attrs_args = get_args(kernel_info['op_info'], 'attrs') kernel_name = kernel_info['op_info']['kernel_name'] if custom_flag: op_module = __import__(op_name) else: op_module = __import__("impl." + op_name, globals(), locals(), [op_name], 0) # get function if build_type == op_pre_build: # set op parameter op_build_cfg_dis() set_current_op_func_name(op_name) set_current_op_name(kernel_name) init_op_pattern() set_op_params(*outputs_args, *attrs_args, kernel_name=kernel_name) set_op_build_type('prebuild') if custom_flag: py_fn_name = kernel_info['op_info']['name'] else: py_fn_name = op_name elif build_type == op_build: if custom_flag: py_fn_name = kernel_info['op_info']['name'] else: py_fn_name = op_name else: raise ValueError( "function {} is not supported by Tbe op {}.".format( build_type, op_name)) op_func = getattr(op_module, py_fn_name, None) if op_func is None: raise ValueError( "Op:{} function {} is not supported by Tbe.".format( op_name, build_type)) # pre build if build_type == op_pre_build: op_func(*inputs_args, *outputs_args, *attrs_args, kernel_name) # disable only pattern configuration op_build_cfg_en() return get_op_pattern() # call function if kernel_name[0:19] == "bounding_box_encode": return op_func(*inputs_args, *outputs_args, *attrs_args, kernel_name_val=kernel_name) return op_func(*inputs_args, *outputs_args, *attrs_args, kernel_name=kernel_name) except Exception as e: if build_type == op_pre_build: op_build_cfg_en() raise RuntimeError(e)
def test_openapi_arguments(self) -> None: """This end-to-end API documentation test compares the arguments defined in the actual code using @has_request_variables and REQ(), with the arguments declared in our API documentation for every API endpoint in Zulip. First, we import the fancy-Django version of zproject/urls.py by doing this, each has_request_variables wrapper around each imported view function gets called to generate the wrapped view function and thus filling the global arguments_map variable. Basically, we're exploiting code execution during import. Then we need to import some view modules not already imported in urls.py. We use this different syntax because of the linters complaining of an unused import (which is correct, but we do this for triggering the has_request_variables decorator). At the end, we perform a reverse mapping test that verifies that every url pattern defined in the openapi documentation actually exists in code. """ from zproject import urls as urlconf # We loop through all the API patterns, looking in particular # for those using the rest_dispatch decorator; we then parse # its mapping of (HTTP_METHOD -> FUNCTION). for p in urlconf.v1_api_and_json_patterns + urlconf.v1_api_mobile_patterns: if p.lookup_str != 'zerver.lib.rest.rest_dispatch': # Endpoints not using rest_dispatch don't have extra data. methods_endpoints = dict(GET=p.lookup_str, ) else: methods_endpoints = p.default_args # since the module was already imported and is now residing in # memory, we won't actually face any performance penalties here. for method, value in methods_endpoints.items(): if isinstance(value, str): function_name = value tags: Set[str] = set() else: function_name, tags = value if function_name == 'zerver.tornado.views.get_events': # Work around the fact that the registered # get_events view function isn't where we do # @has_request_variables. # # TODO: Make this configurable via an optional argument # to has_request_variables, e.g. # @has_request_variables(view_func_name="zerver.tornado.views.get_events") function_name = 'zerver.tornado.views.get_events_backend' lookup_parts = function_name.split('.') module = __import__('.'.join(lookup_parts[:-1]), {}, {}, ['']) function = getattr(module, lookup_parts[-1]) # Our accounting logic in the `has_request_variables()` # code means we have the list of all arguments # accepted by every view function in arguments_map. accepted_arguments = set(arguments_map[function_name]) regex_pattern = p.pattern.regex.pattern url_pattern = self.convert_regex_to_url_pattern(regex_pattern) if "intentionally_undocumented" in tags: self.ensure_no_documentation_if_intentionally_undocumented( url_pattern, method) continue if url_pattern in self.pending_endpoints: # HACK: After all pending_endpoints have been resolved, we should remove # this segment and the "msg" part of the `ensure_no_...` method. msg = f""" We found some OpenAPI documentation for {method} {url_pattern}, so maybe we shouldn't include it in pending_endpoints. """ self.ensure_no_documentation_if_intentionally_undocumented( url_pattern, method, msg) continue try: # Don't include OpenAPI parameters that live in # the path; these are not extracted by REQ. openapi_parameters = get_openapi_parameters( url_pattern, method, include_url_parameters=False) except Exception: # nocoverage raise AssertionError( "Could not find OpenAPI docs for %s %s" % (method, url_pattern)) # We now have everything we need to understand the # function as defined in our urls.py: # # * method is the HTTP method, e.g. GET, POST, or PATCH # # * p.pattern.regex.pattern is the URL pattern; might require # some processing to match with OpenAPI rules # # * accepted_arguments is the full set of arguments # this method accepts (from the REQ declarations in # code). # # * The documented parameters for the endpoint as recorded in our # OpenAPI data in zerver/openapi/zulip.yaml. # # We now compare these to confirm that the documented # argument list matches what actually appears in the # codebase. openapi_parameter_names = { parameter['name'] for parameter in openapi_parameters } if len(accepted_arguments - openapi_parameter_names) > 0: # nocoverage print("Undocumented parameters for", url_pattern, method, function_name) print(" +", openapi_parameter_names) print(" -", accepted_arguments) assert (url_pattern in self.buggy_documentation_endpoints) elif len(openapi_parameter_names - accepted_arguments) > 0: # nocoverage print("Documented invalid parameters for", url_pattern, method, function_name) print(" -", openapi_parameter_names) print(" +", accepted_arguments) assert (url_pattern in self.buggy_documentation_endpoints) else: self.assertEqual(openapi_parameter_names, accepted_arguments) self.check_argument_types(function, openapi_parameters) self.checked_endpoints.add(url_pattern) self.check_for_non_existant_openapi_endpoints()
# multiple revisions is given via stdin whitelist = modulewhitelist(sys.stdin) assert whitelist, "module whitelist is empty" # build up module whitelist check from file names given at runtime perfpypats[0].append( # this matching pattern assumes importing modules from # "mercurial" package in the current style below, for simplicity # # from mercurial import ( # foo, # bar, # baz # ) ((r'from mercurial import [(][a-z0-9, \n#]*\n(?! *%s,|^[ #]*\n|[)])' % ',| *'.join(whitelist)), "import newer module separately in try clause for early Mercurial" )) # import contrib/check-code.py as checkcode assert 'RUNTESTDIR' in os.environ, "use check-perf-code.py in *.t script" contribpath = os.path.join(os.environ['RUNTESTDIR'], '..', 'contrib') sys.path.insert(0, contribpath) checkcode = __import__('check-code') # register perf.py specific entry with "checks" in check-code.py checkcode.checks.append(('perf.py', r'contrib/perf.py$', '', checkcode.pyfilters, perfpypats)) sys.exit(checkcode.main())
A forward pass: >>> from numpy import ones >>> r = n.activate(ones(24)) >>> len(r) 12 The result should be symmetric (although the weights are random) >>> r[0]-r[-1] 0.0 Check its gradient: >>> from pybrain.tests import gradientCheck >>> gradientCheck(n) Perfect gradient True """ __author__ = 'Tom Schaul, [email protected]' from pybrain.structure.networks import BidirectionalNetwork #@UnusedImport from pybrain.tests import runModuleTestSuite if __name__ == "__main__": runModuleTestSuite(__import__('__main__'))
#C* Copyright (c) Schrodinger, LLC. #D* ------------------------------------------------------------------- #E* It is unlawful to modify or remove this copyright notice. #F* ------------------------------------------------------------------- #G* Please see the accompanying LICENSE file for further information. #H* ------------------------------------------------------------------- #I* Additional authors of this source file include: #-* #-* #-* #Z* ------------------------------------------------------------------- # abstract (external or internal) gui control interface import pymol cmd = __import__("sys").modules["pymol.cmd"] def get_pmgapp(): ''' Returns the PMGApp instance. ''' if pymol._ext_gui is None: pymol._ext_gui = createlegacypmgapp() return pymol._ext_gui def get_qtwindow(): ''' Returns the PyMOLQtGUI/QMainWindow instance, or None if not available. '''
def optional_import( module: str, version: str = "", version_checker: Callable[..., bool] = min_version, name: str = "", descriptor: str = OPTIONAL_IMPORT_MSG_FMT, version_args=None, allow_namespace_pkg: bool = False, ) -> Tuple[Any, bool]: """ Imports an optional module specified by `module` string. Any importing related exceptions will be stored, and exceptions raise lazily when attempting to use the failed-to-import module. Args: module: name of the module to be imported. version: version string used by the version_checker. version_checker: a callable to check the module version, Defaults to monai.utils.min_version. name: a non-module attribute (such as method/class) to import from the imported module. descriptor: a format string for the final error message when using a not imported module. version_args: additional parameters to the version checker. allow_namespace_pkg: whether importing a namespace package is allowed. Defaults to False. Returns: The imported module and a boolean flag indicating whether the import is successful. Examples:: >>> torch, flag = optional_import('torch', '1.1') >>> print(torch, flag) <module 'torch' from 'python/lib/python3.6/site-packages/torch/__init__.py'> True >>> the_module, flag = optional_import('unknown_module') >>> print(flag) False >>> the_module.method # trying to access a module which is not imported OptionalImportError: import unknown_module (No module named 'unknown_module'). >>> torch, flag = optional_import('torch', '42', exact_version) >>> torch.nn # trying to access a module for which there isn't a proper version imported OptionalImportError: import torch (requires version '42' by 'exact_version'). >>> conv, flag = optional_import('torch.nn.functional', '1.0', name='conv1d') >>> print(conv) <built-in method conv1d of type object at 0x11a49eac0> >>> conv, flag = optional_import('torch.nn.functional', '42', name='conv1d') >>> conv() # trying to use a function from the not successfully imported module (due to unmatched version) OptionalImportError: from torch.nn.functional import conv1d (requires version '42' by 'min_version'). """ tb = None exception_str = "" if name: actual_cmd = f"from {module} import {name}" else: actual_cmd = f"import {module}" try: pkg = __import__(module) # top level module the_module = import_module(module) if not allow_namespace_pkg: is_namespace = getattr(the_module, "__file__", None) is None and hasattr( the_module, "__path__") assert not is_namespace if name: # user specified to load class/function/... from the module the_module = getattr(the_module, name) except Exception as import_exception: # any exceptions during import tb = import_exception.__traceback__ exception_str = f"{import_exception}" else: # found the module if version_args and version_checker(pkg, f"{version}", version_args): return the_module, True if not version_args and version_checker(pkg, f"{version}"): return the_module, True # preparing lazy error message msg = descriptor.format(actual_cmd) if version and tb is None: # a pure version issue msg += f" (requires '{module} {version}' by '{version_checker.__name__}')" if exception_str: msg += f" ({exception_str})" class _LazyRaise: def __init__(self, *_args, **_kwargs): _default_msg = ( f"{msg}." + "\n\nFor details about installing the optional dependencies, please visit:" + "\n https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies" ) if tb is None: self._exception = OptionalImportError(_default_msg) else: self._exception = OptionalImportError( _default_msg).with_traceback(tb) def __getattr__(self, name): """ Raises: OptionalImportError: When you call this method. """ raise self._exception def __call__(self, *_args, **_kwargs): """ Raises: OptionalImportError: When you call this method. """ raise self._exception return _LazyRaise(), False
"""The :mod:`zmq` module wraps the :class:`Socket` and :class:`Context` found in :mod:`pyzmq <zmq>` to be non blocking """ __zmq__ = __import__('zmq') from eventlet import sleep from eventlet.hubs import trampoline, get_hub __patched__ = ['Context', 'Socket'] globals().update( dict([(var, getattr(__zmq__, var)) for var in __zmq__.__all__ if not (var.startswith('__') or var in __patched__)])) def get_hub_name_from_instance(hub): """Get the string name the eventlet uses to refer to hub :param hub: An eventlet hub """ return hub.__class__.__module__.rsplit('.', 1)[-1] def Context(io_threads=1): """Factory function replacement for :class:`zmq.core.context.Context` This factory ensures the :class:`zeromq hub <eventlet.hubs.zeromq.Hub>` is the active hub, and defers creation (or retreival) of the ``Context`` to the hub's :meth:`~eventlet.hubs.zeromq.Hub.get_context` method It's a factory function due to the fact that there can only be one :class:`_Context` instance per thread. This is due to the way :class:`zmq.core.poll.Poller` works """
# -*- coding: utf-8 -*- """ Created on Wed Dec 4 17:56:50 2019 @author: Aditya Luthfi """ lib = __import__('1184090csv') lib.bukaModeListCsv() lib.bukaModeDictCsv() lib.tulisCsv() lib = __import__('1184090pandas') lib.bukaModeListPandas() lib.bukaModeDictPandas() lib.tulisCsvPandas()
import yfinance as yf import pandas as pd from datetime import datetime args = __import__('sys').argv symbol = args[1] data = yf.Ticker(symbol) history = data.history(period='1d', start='2010-1-1', end=datetime.today().strftime('%Y-%m-%d')) with pd.option_context('display.max_rows', None, 'display.max_columns', None): print(history)
#!/usr/bin/env python try: __import__('scrypt') __import__('requests') except: # Write more about how they might install it print( 'The scrypt and requests modules are required, please install in your environment' ) quit() import json, scrypt, requests, getpass, random, string def nonce_gen(length): return ''.join([ str(random.SystemRandom().choice(string.ascii_lowercase + string.digits)) for i in range(length) ]) user = input('What is your username associated with Keybase: ') js = json.loads( requests.get( 'https://keybase.io/_/api/1.0/getsalt.json?email_or_username='******'status']['code'] is 0: print( 'Bad username (be sure NOT to use your email, this method is depreciated)\nEnding process'
#!/usr/bin/env python3 """ Tests for the optimum number of clusters by variance """ import numpy as np kmeans = __import__('1-kmeans').kmeans variance = __import__('2-variance').variance def optimum_k(X, kmin=1, kmax=None, iterations=1000): """ Tests for the optimum number of clusters by variance :param X: numpy.ndarray of shape (n, d) containing the data set :param kmin: positive integer containing the minimum number of clusters to check for (inclusive) :param kmax: positive integer containing the maximum number of clusters to check for (inclusive) :param iterations: positive integer containing the maximum number of iterations for K-means :return: results, d_vars, or None, None on failure results is a list containing the outputs of K-means for each cluster size d_vars is a list containing the difference in variance from the smallest cluster size for each cluster size """ if type(X) is not np.ndarray or len(X.shape) != 2: return None, None if type(iterations) is not int or iterations <= 0: return None, None if type(kmin) is not int or kmin < 1: return None, None
def _import_module(name): """Import module, returning the module after the last dot.""" __import__(name) return sys.modules[name]
def get_server_mixin(server_name): from . import __name__ as base x = __import__('{}.servers.{}'.format(base, server_name), fromlist=['']) return x.ServerMixin
import time import datetime import threading from math import sqrt #import sys,os #import serial import logging logger = logging.getLogger("hydrosys4."+__name__) global ISRPI try: __import__("smbus") except ImportError: ISRPI=False else: import Adafruit_DHT #humidity temperature sensor import Adafruit_BMP.BMP085 as BMP085 #pressure sensor #from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor from stepperDOUBLEmod import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor import spidev import smbus import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) ISRPI=True HWCONTROLLIST=["tempsensor","humidsensor","pressuresensor","analogdigital","lightsensor","pulse","pinstate","servo","stepper","stepperstatus","photo","mail+info+link","mail+info","returnzero","stoppulse","readinputpin"]
def onClick(self, id): # Valores por defecto if id == 10006: if self.custom_button is not None: if '.' in self.callback: package, self.callback = self.callback.rsplit('.', 1) else: package = 'channels.%s' % self.channel try: cb_channel = __import__(package, None, None, [package]) except ImportError: logger.error('Imposible importar %s' % package) else: self.return_value = getattr( cb_channel, self.custom_button['function'])(self.item) if self.custom_button["close"]: self.close() else: for c in self.list_controls: if c["type"] == "text": c["control"].setText(c["default"]) self.values[c["id"]] = c["default"] if c["type"] == "bool": c["control"].setSelected(c["default"]) self.values[c["id"]] = c["default"] if c["type"] == "list": c["label"].setLabel(c["lvalues"][c["default"]]) self.values[c["id"]] = c["default"] self.evaluate_conditions() self.dispose_controls(self.index, force=True) self.check_default() self.check_ok() # Boton Cancelar y [X] if id == 10003 or id == 10005: self.close() # Boton Aceptar if id == 10004: if not self.callback: for v in self.values: config.set_setting(v, self.values[v], self.channel) self.close() else: self.close() if '.' in self.callback: package, self.callback = self.callback.rsplit('.', 1) else: package = 'channels.%s' % self.channel cb_channel = None try: cb_channel = __import__(package, None, None, [package]) except ImportError: logger.error('Imposible importar %s' % package) self.return_value = getattr(cb_channel, self.callback)(self.item, self.values) # Controles de ajustes, si se cambia el valor de un ajuste, cambiamos el valor guardado en el diccionario de # valores # Obtenemos el control sobre el que se ha echo click control = self.getControl(id) # Lo buscamos en el listado de controles for cont in self.list_controls: # Si el control es un "downBtn" o "upBtn" son los botones del "list" # en este caso cambiamos el valor del list if cont["type"] == "list" and (cont["downBtn"] == control or cont["upBtn"] == control): # Para bajar una posicion if cont["downBtn"] == control: index = cont["lvalues"].index(cont["label"].getLabel()) if index > 0: cont["label"].setLabel(cont["lvalues"][index - 1]) # Para subir una posicion elif cont["upBtn"] == control: index = cont["lvalues"].index(cont["label"].getLabel()) if index < len(cont["lvalues"]) - 1: cont["label"].setLabel(cont["lvalues"][index + 1]) # Guardamos el nuevo valor en el diccionario de valores self.values[cont["id"]] = cont["lvalues"].index( cont["label"].getLabel()) # Si esl control es un "bool", guardamos el nuevo valor True/False if cont["type"] == "bool" and cont["control"] == control: self.values[cont["id"]] = bool(cont["control"].isSelected()) # Si esl control es un "text", guardamos el nuevo valor if cont["type"] == "text" and cont["control"] == control: # Versiones antiguas requieren abrir el teclado manualmente if xbmcgui.ControlEdit == ControlEdit: import xbmc keyboard = xbmc.Keyboard(cont["control"].getText(), cont["control"].getLabel(), cont["control"].isPassword) keyboard.setHiddenInput(cont["control"].isPassword) keyboard.doModal() if keyboard.isConfirmed(): cont["control"].setText(keyboard.getText()) self.values[cont["id"]] = cont["control"].getText() self.evaluate_conditions() self.dispose_controls(self.index, force=True) self.check_default() self.check_ok()
def test_lazy_import(): class O(object): math = lazy_import('math') assert O.math is __import__('math')
Setup package ~~~~~~~~~~~~~~~~~~~~ Setuptools/distutils commands to package installation. :license: Other/Proprietary, see LICENSE for details. """ # pylint: HOOK-IGNORED import os from setuptools import setup from setuptools import find_packages import subprocess project_name = 'ANP Reader' __version__ = __import__(project_name).__version__ __author__ = __import__(project_name).__author__ __author_email__ = __import__(project_name).__author_email__ __author_username__ = __import__(project_name).__author_username__ __description__ = __import__(project_name).__description__ def cmd_output(args, **kwds): kwds.setdefault("stdout", subprocess.PIPE) kwds.setdefault("stderr", subprocess.STDOUT) p = subprocess.Popen(args, **kwds) return p.communicate()[0] def version_tuple(version): return tuple([int(num) for num in version.split('.')])
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ''' import components.scanner.flatevaluators.result as FlatEvltResult import components.scanner.logicevaluators.result as LogicEvltResult flatModuleList = [ ('FileItem', 'files_hash'), ] logicModuleList = [] flatEvaluatorList = {} logicEvaluatorList = {} for document, module in flatModuleList: flatEvaluatorList[document] = getattr( __import__('components.scanner.flatevaluators.%s' % module, fromlist=['Evaluator']), 'Evaluator') for document, module in logicModuleList: logicEvaluatorList[document] = getattr( __import__('components.scanner.logicevaluators.%s' % module, fromlist=['Evaluator']), 'Evaluator')
#!/usr/bin/env python3 """ Tranformer Encoder Block (https://www.tensorflow.org/tutorials/text/transformer#encoder_layer) """ import tensorflow as tf MultiHeadAttention = __import__('6-multihead_attention').MultiHeadAttention class EncoderBlock(tf.keras.layers.Layer): """ class Encoder block """ def __init__(self, dm, h, hidden, drop_rate=0.1): """ dm - the dimensionality of the model h - the number of heads hidden - the number of hidden units in the fully connected layer drop_rate - the dropout rate Sets the following public instance attributes: mha - a MultiHeadAttention layer dense_hidden - the hidden dense layer with hidden units an relu activation dense_output - the output dense layer with dm units layernorm1 - the first layer norm layer, with epsilon=1e-6 layernorm2 - the second layer norm layer, with epsilon=1e-6 dropout1 - the first dropout layer dropout2 - the second dropout layer """
def importobj(modpath, attrname): module = __import__(modpath, None, None, ['__doc__']) return getattr(module, attrname)
def namespaced_import(name, namespace=None, environment_path=None): """A function to import compiled soy modules using the Soy namespace. This function attempts to first import the module directly. If it isn't found in the matching package as the Soy Namespace, it will walk the sys.path structure open any module with a matching name and test its SOY_NAMESPACE attribute. If it matches it will load that instead. Multiple files can share the same soy namespace. In that instance, all of these files will be loaded, combined, and loaded as one module. Note: If multiple files share the same namespace, they still require that the module name begins with the last part of the namespace (e.g. soy.examples.delegates will load delegates0.py, delegatesxyz.py, etc.). TODO(dcphillips): See if there's any way we can avoid this limitation without blowing up load times. Args: name: The name of the module to import. namespace: The namespace of the module to import. environment_path: A custom environment module path for interacting with the runtime environment. Returns: The Module object. """ full_namespace = '%s.%s' % (namespace, name) if namespace else name try: # Try searching for the module directly return importlib.import_module(full_namespace) except ImportError: # If the module isn't found, search without the namespace and check the # namespaces. if namespace: namespace_key = "SOY_NAMESPACE: '%s'." % full_namespace module = None if environment_path: file_loader = importlib.import_module( environment_path).file_loader else: file_loader = environment.file_loader for sys_path, f_path, f_name in _find_modules(name): # Verify the file namespace by comparing the 5th line. with file_loader(f_path, f_name, 'r') as f: for _ in range(4): next(f) if namespace_key != next(f).rstrip(): continue # Strip the root path and the file extension. module_path = six.ensure_str(os.path.relpath( f_path, sys_path)).replace('/', '.') module_name = os.path.splitext(f_name)[0] # Python 2 performs relative or absolute imports. Beginning with # Python 3.3, only absolute imports are possible. Compare the # docs for the default value of the `level` argument of `__import__`: # https://docs.python.org/2/library/functions.html#__import__ # https://docs.python.org/3/library/functions.html#__import__ module = getattr( __import__(module_path, globals(), locals(), [module_name]), module_name) break if module: # Add this to the global modules list for faster loading in the future. _cache_module(full_namespace, module) return module raise
Package containing implementation of all the standard Distutils commands.""" __revision__ = "$Id: __init__.py,v 1.3 2005/05/16 11:08:49 pearu Exp $" distutils_all = [ #'build_py', 'clean', 'install_clib', 'install_scripts', 'bdist', 'bdist_dumb', 'bdist_wininst', ] __import__('distutils.command', globals(), locals(), distutils_all) __all__ = [ 'build', 'config_compiler', 'config', 'build_src', 'build_py', 'build_ext', 'build_clib', 'build_scripts', 'install', 'install_data', 'install_headers', 'install_lib', 'bdist_rpm',
#!/usr/bin/env python3 """ Contains the inception_network function """ import tensorflow.keras as K inception_block = __import__('0-inception_block').inception_block def inception_network(): """ Builds the inception network """ initializer = K.initializers.he_normal(seed=None) X = K.Input(shape=(224, 224, 3)) my_layer = K.layers.Conv2D( filters=64, kernel_size=(7, 7), strides=(2, 2), padding='same', activation='relu', kernel_initializer=initializer, )(X) my_layer = K.layers.MaxPool2D(pool_size=(3, 3), padding='same', strides=(2, 2))(my_layer) my_layer = K.layers.Conv2D( filters=64,
#!/usr/bin/python3 print_reversed_list_integer = __import__( '3-print_reversed_list_integer').print_reversed_list_integer my_list = [1, 2, 3, 4, 5] print_reversed_list_integer(my_list) print_reversed_list_integer(my_list2)
from codecs import open from os import path here = path.abspath(path.dirname(__file__)) # Get the long description from the README file with open(path.join(here, 'README.rst'), encoding='utf-8') as f: long_description = f.read() setup( name='pymysql_split_tool', # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # https://packaging.python.org/en/latest/single_source_version.html version=__import__('pymysql_split_tool').__version__, description='mysql spliting/sharding tools', long_description=long_description, # The project's main homepage. url='https://github.com/echoma/pymysql_split_tool', # Author details author='Echo Ma', author_email='*****@*****.**', # Choose your license license='Apache 2.0', # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
# Copyright (C) 2019 by eHealth Africa : http://www.eHealthAfrica.org # # See the NOTICE file distributed with this work for additional information # regarding copyright ownership. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. __path__ = __import__('pkgutil').extend_path(__path__, __name__)
def visit_file(dir, name): # Try to match the regexp pattern, if specified. if configuration.regexp: if not re.search(configuration.regexp, name): # We didn't match the regex, we're done. return if configuration.skip_tests: for file_regexp in configuration.skip_tests: if re.search(file_regexp, name): return # We found a match for our test. Add it to the suite. # Update the sys.path first. if not sys.path.count(dir): sys.path.insert(0, dir) base = os.path.splitext(name)[0] # Thoroughly check the filterspec against the base module and admit # the (base, filterspec) combination only when it makes sense. def check(obj, parts): for part in parts: try: parent, obj = obj, getattr(obj, part) except AttributeError: # The filterspec has failed. return False return True module = __import__(base) def iter_filters(): for filterspec in configuration.filters: parts = filterspec.split('.') if check(module, parts): yield filterspec elif parts[0] == base and len(parts) > 1 and check(module, parts[1:]): yield '.'.join(parts[1:]) else: for key,value in module.__dict__.items(): if check(value, parts): yield key + '.' + filterspec filtered = False for filterspec in iter_filters(): filtered = True print("adding filter spec %s to module %s" % (filterspec, repr(module))) tests = unittest2.defaultTestLoader.loadTestsFromName(filterspec, module) configuration.suite.addTests(tests) # Forgo this module if the (base, filterspec) combo is invalid if configuration.filters and not filtered: return if not filtered: # Add the entire file's worth of tests since we're not filtered. # Also the fail-over case when the filterspec branch # (base, filterspec) combo doesn't make sense. configuration.suite.addTests( unittest2.defaultTestLoader.loadTestsFromName(base))
def _bundle_extensions(objs: Sequence[Model | Document], resources: Resources) -> List[ExtensionEmbed]: names: Set[str] = set() bundles: List[ExtensionEmbed] = [] extensions = [".min.js", ".js"] if resources.minified else [".js"] for obj in _all_objs( objs ) if objs is not None else Model.model_class_reverse_map.values(): if hasattr(obj, "__implementation__"): continue name = obj.__view_module__.split(".")[0] if name == "bokeh": continue if name in names: continue names.add(name) module = __import__(name) this_file = abspath(module.__file__) base_dir = dirname(this_file) dist_dir = join(base_dir, "dist") ext_path = join(base_dir, "bokeh.ext.json") if not exists(ext_path): continue server_prefix = f"{resources.root_url}static/extensions" package_path = join(base_dir, "package.json") pkg: Pkg | None = None if exists(package_path): with open(package_path) as io: try: pkg = json.load(io) except json.decoder.JSONDecodeError: pass artifact_path: str server_url: str cdn_url: str | None = None if pkg is not None: pkg_name: str | None = pkg.get("name", None) if pkg_name is None: raise ValueError("invalid package.json; missing package name") pkg_version = pkg.get("version", "latest") pkg_main = pkg.get("module", pkg.get("main", None)) if pkg_main is not None: cdn_url = f"{_default_cdn_host}/{pkg_name}@{pkg_version}/{pkg_main}" else: pkg_main = join(dist_dir, f"{name}.js") artifact_path = join(base_dir, normpath(pkg_main)) artifacts_dir = dirname(artifact_path) artifact_name = basename(artifact_path) server_path = f"{name}/{artifact_name}" if not settings.dev: sha = hashlib.sha256() sha.update(pkg_version.encode()) vstring = sha.hexdigest() server_path = f"{server_path}?v={vstring}" else: for ext in extensions: artifact_path = join(dist_dir, f"{name}{ext}") artifacts_dir = dist_dir server_path = f"{name}/{name}{ext}" if exists(artifact_path): break else: raise ValueError( f"can't resolve artifact path for '{name}' extension") extension_dirs[name] = artifacts_dir server_url = f"{server_prefix}/{server_path}" embed = ExtensionEmbed(artifact_path, server_url, cdn_url) bundles.append(embed) return bundles