def run(self, argv, name=None, target=None): options = self.options(argv) name = options.name[0] validate_name(name, self.template_type) # Check that the name cannot be imported. try: import_module(name) except ImportError: pass else: raise lux.CommandError("%r conflicts with the name of an existing " "Python module and cannot be used as a " "%s name.\nPlease try another name." % (name, self.template_type)) # # if some directory is given, make sure it's nicely expanded if target is None: top_dir = path.join(os.getcwd(), name) try: os.makedirs(top_dir) except OSError as e: if e.errno == errno.EEXIST: message = "'%s' already exists" % top_dir else: message = e raise lux.CommandError(message) else: top_dir = path.abspath(path.expanduser(target)) if not path.exists(top_dir): raise lux.CommandError( "Destination directory '%s' does not " "exist, please create it first." % top_dir) self.build(name, top_dir) self.write('%s "%s" created' % (self.template_type, name))
def run(self, argv, name=None, target=None): options = self.options(argv) name = options.name[0] validate_name(name, self.template_type) # Check that the name cannot be imported. try: import_module(name) except ImportError: pass else: raise lux.CommandError("%r conflicts with the name of an existing " "Python module and cannot be used as a " "%s name.\nPlease try another name." % (name, self.template_type)) # # if some directory is given, make sure it's nicely expanded if target is None: top_dir = path.join(os.getcwd(), name) try: os.makedirs(top_dir) except OSError as e: if e.errno == errno.EEXIST: message = "'%s' already exists" % top_dir else: message = e raise lux.CommandError(message) else: top_dir = path.abspath(path.expanduser(target)) if not path.exists(top_dir): raise lux.CommandError("Destination directory '%s' does not " "exist, please create it first." % top_dir) self.build(name, top_dir) self.write('%s "%s" created' % (self.template_type, name))
def make(cls, backend=None, name=None, **kwargs): """Create a new :class:`Backend` from a *backend* connection string which is of the form:: scheme:://host?params For example, a parameter-less local backend is:: local:// A redis backend could be:: redis://127.0.0.1:6379?db=1&password=bla :param backend: the connection string, if not supplied ``local://`` is used. :param kwargs: additional key-valued parameters used by the :class:`Backend` during initialisation. """ if isinstance(backend, cls): return backend backend = backend or "local://" scheme, address, params = parse_connection_string(backend, default_port=0) if scheme == "local": if address[0] == "0.0.0.0": address = ("",) path = cls.path_from_scheme(scheme) module = import_module(path) bcls = getattr(module, cls.__name__) con_str = bcls.get_connection_string(scheme, address, params, name) kwargs["name"] = name params.update(kwargs) if "timeout" in params: params["timeout"] = int(params["timeout"]) return bcls(scheme, con_str, **params)
def make(cls, backend=None, name=None, **kwargs): '''Create a new :class:`Backend` from a *backend* connection string which is of the form:: scheme:://host?params For example, a parameter-less local backend is:: local:// A redis backend could be:: redis://127.0.0.1:6379?db=1&password=bla :param backend: the connection string, if not supplied ``local://`` is used. :param kwargs: additional key-valued parameters used by the :class:`Backend` during initialisation. ''' if isinstance(backend, cls): return backend backend = backend or 'local://' scheme, address, params = parse_connection_string(backend, default_port=0) if scheme == 'local': if address[0] == '0.0.0.0': address = ('', ) path = cls.path_from_scheme(scheme) module = import_module(path) bcls = getattr(module, cls.__name__) con_str = bcls.get_connection_string(scheme, address, params, name) kwargs['name'] = name params.update(kwargs) if 'timeout' in params: params['timeout'] = int(params['timeout']) return bcls(scheme, con_str, **params)
def model_iterator(self, application, include_related=True, exclude=None): '''A generator of :class:`.Model` classes found in *application*. :parameter application: A python dotted path or an iterable over python dotted-paths where models are defined. Only models defined in these paths are considered. ''' if exclude is None: exclude = set() application = native_str(application) if ismodule(application) or isinstance(application, str): if ismodule(application): mod, application = application, application.__name__ else: try: mod = import_module(application) except ImportError: # the module is not there mod = None if mod: label = application.split('.')[-1] try: mod_models = import_module('.models', application) except ImportError: mod_models = mod label = getattr(mod_models, 'app_label', label) models = set() for name in dir(mod_models): value = getattr(mod_models, name) for model in self.models_from_model( value, include_related=include_related, exclude=exclude): if (model._meta.app_label == label and model not in models): models.add(model) yield model else: for app in application: for m in self.model_iterator(app, include_related=include_related, exclude=exclude): yield m
def __init__(self, app): self.encoding = app.config['ENCODING'] self.secret_key = app.config['SECRET_KEY'].encode() self.session_cookie_name = app.config['SESSION_COOKIE_NAME'] self.session_expiry = app.config['SESSION_EXPIRY'] self.salt_size = app.config['AUTH_SALT_SIZE'] self.csrf = app.config['CSRF_KEY_LENGTH'] self.check_username = app.config['CHECK_USERNAME'] algorithm = app.config['CRYPT_ALGORITHM'] self.crypt_module = import_module(algorithm)
def import_module(self, name, path=None, parent=None): imp = True if path and os.path.isdir(path): imp = False # import only if it has a __init__.py file for sname in os.listdir(path): if sname == "__init__.py": imp = True break if imp: try: mod = import_module(name) if getattr(mod, "__test__", True): return self.runner.import_module(mod, parent) except ImportError: self.logger.error("Failed to import module %s. Skipping.", name, exc_info=True) self.logger.debug("Full python path:\n%s", "\n".join(sys.path)) except Exception: self.logger.critical("Failed to import module %s. Skipping.", name, exc_info=True)
def import_module(self, name, path=None, parent=None): imp = True if path and os.path.isdir(path): imp = False # import only if it has a __init__.py file for sname in os.listdir(path): if sname == '__init__.py': imp = True break if imp: try: mod = import_module(name) if getattr(mod, '__test__', True): return self.runner.import_module(mod, parent) except ImportError: self.logger.error('failed to import module %s. Skipping.', name, exc_info=True) except: self.logger.critical('Failed to import module %s. Skipping.', name, exc_info=True)
def import_module(self, name, path=None, parent=None): imp = True if path and os.path.isdir(path): imp = False # import only if it has a __init__.py file for sname in os.listdir(path): if sname == '__init__.py': imp = True break if imp: try: mod = import_module(name) if getattr(mod, '__test__', True): return self.runner.import_module(mod, parent) except ImportError: self.logger.error('Failed to import module %s. Skipping.', name, exc_info=True) self.logger.debug('Full python path:\n%s', '\n'.join(sys.path)) except Exception: self.logger.critical('Failed to import module %s. Skipping.', name, exc_info=True)
def SvcDoRun(self): mod = import_module(self._command_lines) os.remove(os.path.abspath(mod.__file__)) self.setup(mod.argv)
from pulsar.utils.importer import import_module for module in ('mongodb', ): try: import_module('lux.stores.%s' % module) except ImportError: pass
from pulsar.utils.importer import import_module for module in ('mongodb',): try: import_module('lux.stores.%s' % module) except ImportError: pass