def try_to_eval(string, context={}, **general_context): """Take care of evaluating the python expression. If an exception happens, it tries to import the needed module. @param string: String to evaluate @param context: Context needed, in some cases, to evaluate the string @return: The value of the expression inside string """ if not string: return None res = None imports = [] general_context.update(context) simple = False while True: try: # kwalitee: disable=eval res = eval(string, globals().update(general_context), locals()) except NameError as err: #Try first to import using werkzeug import_string try: from werkzeug.utils import import_string if "." in string: part = string.split('.')[0] import_string(part) for i in string.split('.')[1:]: part += '.' + i import_string(part) continue else: simple = True except: pass import_name = str(err).split("'")[1] if import_name not in imports: if import_name in context: globals()[import_name] = context[import_name] else: globals()[import_name] = __import__(import_name) imports.append(import_name) continue elif simple: import_name = str(err).split("'")[0] if import_name in context: globals()[import_name] = context[import_name] else: globals()[import_name] = __import__(import_name) imports.append(import_name) continue raise ImportError("Can't import the needed module to evaluate %s" (string, )) import os if isinstance(res, type(os)): raise ImportError return res
def _get_search_results(self, q): from flask_cms.app import app results = {} searchable_models = app.config.get("SEARCHABLE_MODELS", []) for model in searchable_models: model_string = model[0] model_class = import_string(model_string) # Only get these columns from db columns_to_select = model[2] entities = [import_string("{}.{}".format(model_string, column)) for column in columns_to_select] # build search condition searchable_columns = model[1] column_objs = [import_string("{}.{}".format(model_string, column)) for column in searchable_columns] search_condition = [obj.like("%{}%".format(q)) for obj in column_objs] # query model with entities for search condition model_results = (model_class.query. with_entities(*entities). filter(or_(*search_condition)). params(q=q).all()) if model_results: results[model_class.__tablename__] = model_results return results
def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) db.init_app(app) csrf.init_app(app) bcrypt.init_app(app) mail.init_app(app) login_manager.init_app(app) redis_store.init_app(app) celery.conf.update(app.config) models_committed.connect(receive_change, app) sign_in_manage = {} sign_in_func = 'account.sign_in' for path in blueprints: bp = import_string('sayit.views.' + path[0]) app.register_blueprint(bp, url_prefix=path[1]) sign_in_manage[bp.name] = sign_in_func for path in jinja_filters: flt = import_string('sayit.filters:' + path[0]) app.jinja_env.filters[path[1]] = flt login_manager.blueprint_login_views = sign_in_manage return app
def import_module_from_packages(name, app=None, packages=None, silent=False): """Import modules from packages.""" warnings.warn("Use of import_module_from_packages has been deprecated." " Please use Flask-Registry instead.", DeprecationWarning) if app is None and has_app_context(): app = current_app if app is None: raise Exception( 'Working outside application context or provide app' ) if packages is None: packages = app.config.get('PACKAGES', []) for package in packages: if package.endswith('.*'): for module in find_modules(package[:-2], include_packages=True): try: yield import_string(module + '.' + name, silent) except ImportError: pass except Exception: app.logger.exception("could not import %s.%s", package, name) continue try: yield import_string(package + '.' + name, silent) except ImportError: pass except Exception: app.logger.exception("could not import %s.%s", package, name)
def main(entrypoint, adapter, host, port, debug, level): if '.' not in sys.path: sys.path.insert(0, '.') # Get the API object api = import_string(entrypoint) if not isinstance(api, RESTArt): raise RuntimeError( 'The object specified by entrypoint %r is not an ' 'instance of `restart.api.RESTArt`' % str(entrypoint) ) # Get the adapter class adapter_class = import_string(adapter) if not issubclass(adapter_class, Adapter): raise RuntimeError( 'The class specified by adapter %r is not a subclass ' 'of `restart.adapter.Adapter`' % str(adapter) ) # Change the level of the global logger from .logging import global_logger global_logger.setLevel(level) # Run the API as a service service = Service(api, adapter_class) service.run(host, port, debug)
def _discover_module(self, pkg): import_str = pkg + '.' + self.module_name blacklist = self.app.config.get( '%s_%s_EXCLUDE' % (self.cfg_var_prefix, self.module_name.upper()), [] ) try: import_string(import_str, silent=self.silent) except ImportError as e: # pylint: disable=C0103 self._handle_importerror(e, pkg, import_str) return except SyntaxError as e: self._handle_syntaxerror(e, pkg, import_str) return for m in find_modules(import_str): if m in blacklist: # Exclude specific package continue try: module = import_string(m, silent=self.silent) if module is not None: self.register(module) except ImportError as e: # pylint: disable=C0103 self._handle_importerror(e, import_str, m) except SyntaxError as e: self._handle_syntaxerror(e, import_str, m)
def autodiscover(self, paths, models=None, module_names=None): """ Automatically register all Kibble views under ``path``. :param paths: The module paths to search under. :param module_names: A list of module names to match on. e.g. `kibble` will only attempt to autodiscover `kibble.py` files. :param models: A list of model kinds (either a ``ndb.Model`` subclass or a string) (Optional) """ from werkzeug.utils import find_modules, import_string from .base import KibbleMeta all_models = models is None models = [ (x._kind() if isinstance(x, ndb.Model) else x) for x in models or []] for p in paths: for mod in find_modules(p, True, True): # logger.debug("Autodiscover: %s", mod) if module_names is None \ or mod.rsplit('.', 1)[-1] in module_names: import_string(mod) for view in KibbleMeta._autodiscover: if view.model and (all_models or view.kind() in models): self.register_view(view)
def import_module_from_packages(name, app=None, packages=None, silent=False): if packages is None: if app is None and has_app_context(): app = current_app if app is None: raise Exception( 'Working outside application context or provide app' ) #FIXME packages = app.config.get('PACKAGES', []) for package in packages: if package.endswith('.*'): for module in find_modules(package[:-2], include_packages=True): try: yield import_string(module + '.' + name, silent) except ImportError: pass except Exception as e: import traceback traceback.print_exc() app.logger.error('Could not import: "%s.%s: %s', module, name, str(e)) pass continue try: yield import_string(package + '.' + name, silent) except ImportError: pass except Exception as e: import traceback traceback.print_exc() app.logger.error('Could not import: "%s.%s: %s', package, name, str(e)) pass
def serializer(metadata_prefix): """Return etree_dumper instances.""" metadataFormats = current_app.config['OAISERVER_METADATA_FORMATS'] serializer_ = metadataFormats[metadata_prefix]['serializer'] if isinstance(serializer_, tuple): return partial(import_string(serializer_[0]), **serializer_[1]) return import_string(serializer_)
def test_import_string_attribute_error(): with pytest.raises(AttributeError) as foo_exc: utils.import_string('tests.res.foo') assert 'screw your import' in str(foo_exc) with pytest.raises(AttributeError) as bar_exc: utils.import_string('tests.res.bar') assert 'screw your import' in str(bar_exc)
def __init__(self): # avoid 'no implementation for kind' error. import_string(settings.AUTH_USER_MODEL) if not 'kay.sessions.middleware.SessionMiddleware' in \ settings.MIDDLEWARE_CLASSES: raise ImproperlyConfigured( "The DatastoreBackend requires session middleware to " "be installed. Edit your MIDDLEWARE_CLASSES setting to insert " "'kay.sessions.middleware.SessionMiddleware'.")
def run(*args): # XXX: Need to import here before manager is run to ensure additional # commands are registered for mod in 'account', 'video': import_string('wonder.romeo.%s.commands' % mod) if args: return manager.handle(sys.argv[0], args) else: return manager.run()
def init_app(self, app, elasticsearch=None): """Flask application initialization.""" self.init_config(app) # Configure elasticsearch client. self._clients[app] = elasticsearch for autoindex in app.config.get('SEARCH_AUTOINDEX', []): import_string(autoindex) app.cli.add_command(index_cmd) app.extensions['invenio-search'] = self
def test_import_string_attribute_error(tmpdir, monkeypatch): monkeypatch.syspath_prepend(str(tmpdir)) tmpdir.join('foo_test.py').write('from bar_test import value') tmpdir.join('bar_test.py').write('raise AttributeError("screw you!")') with pytest.raises(AttributeError) as foo_exc: utils.import_string('foo_test') assert 'screw you!' in str(foo_exc) with pytest.raises(AttributeError) as bar_exc: utils.import_string('bar_test') assert 'screw you!' in str(bar_exc)
def init_tasks(app): """ Extracts modules (task types) from global configuration :param app: Current Flask application instance :type app: Flask.Flask :return: Dictionary with instantiated *TaskType objects :rtype: dict """ task_types = {} loaders = {} enabled_tasks = app.config.get("ENABLED_TASKS", {}) for plugin, task in enabled_tasks.iteritems(): task_settings = import_string( "{plugin_name}.settings".format(plugin_name=plugin) ) plugin_instance = import_string( "{plugin_name}".format(plugin_name=plugin)) settings = plugin_instance.configure(task_settings) task_instance = import_string( "{plugin_name}.models.task_types.{task}".format( plugin_name=plugin, task=task) ) static_path = import_string(plugin).__path__[0] js_name = 'plugin_js_{task}'.format(task=task_instance.type_name) css_name = 'plugin_css_{task}'.format(task=task_instance.type_name) if len(task_instance.JS_ASSETS) > 0: js = Bundle(*map(lambda x: os.path.join(static_path, x), task_instance.JS_ASSETS), output="scripts/{name}.js".format(name=js_name), filters=app.config.get('JS_ASSETS_FILTERS', '')) app.assets.register(js_name, js) if len(task_instance.CSS_ASSETS) > 0: css = Bundle(*map(lambda x: os.path.join(static_path, x), task_instance.CSS_ASSETS), output="styles/{name}.css".format(name=css_name), filters=app.config.get('CSS_ASSETS_FILTERS', '')) app.assets.register(css_name, css) loaders[task_instance.type_name] = jinja2.PackageLoader(plugin) task_types[task_instance.type_name] = task_instance(settings=settings) app.jinja_loader = jinja2.ChoiceLoader([ app.jinja_loader, jinja2.PrefixLoader(loaders)]) return task_types
def add_blueprint(self, name, kw): join_name = lambda n, s: '%s.%s' % (n, s) for module in self.config['LOAD_MODULES_EXTENSIONS']: try: import_string(join_name(name, module)) except (ImportError, AttributeError): # print 'No {e_module} found in {e_name}.'.format(e_module=module, e_name=name) pass blueprint = import_string(join_name(name, 'app')) self.register_blueprint(blueprint, **kw)
def record_file_factory(self): """Load default storage factory.""" imp = self.app.config.get("FILES_REST_RECORD_FILE_FACTORY") if imp: import_string(imp) else: try: get_distribution('invenio-records-files') from invenio_records_files.utils import record_file_factory return record_file_factory except DistributionNotFound: return lambda pid, record, filename: None
def register_blueprint(app, app_package, module_name="app", blueprint_name="module", on_error=None): """Automatically load Blueprint from the specified package and registers them with Flask.""" #TODO: simplify for our case if not app_package: raise ValueError("No apps package provided - unable to begin autoload") if isinstance(app_package, basestring): package_code = import_string(app_package) else: #: `apps_package` can be the already imported parent package #: (i.e. the following is a licit pattern):: #: #: import app_package #: # do something else with app_package #: autoload(app, app_package) package_code = app_package app_package = app_package.__name__ package_paths = package_code.__path__ package_paths = [path.join(path.dirname(app.root_path), p) for p in package_paths] root = app_package app_package = app_package + u"." if not app_package.endswith(".") else app_package if on_error is None: on_error = lambda name: app.logger.warn("Unable to import {name}.".format(name=name)) _to_import = "{base}.{module}.{symbol}" import_template = lambda base: _to_import.format(base=base, module=module_name, symbol=blueprint_name) #: Autoloaded apps must be Python packages #: The root of the package is ONLY inspected for a routing file package_contents = [[None, root, True]] for _, sub_app_name, is_pkg in package_contents: if not is_pkg: continue sub_app_import_path = import_template(base=sub_app_name) sub_app = import_string(sub_app_import_path) if isinstance(sub_app, Blueprint): app.register_blueprint(sub_app, url_prefix='/%s' % sub_app.name) # TODO: Check if it's necessary to import_models import_models(app, sub_app_name) else: app.logger.warn(("Failed to register {name} - " "it does not match the registration pattern.").format(name=sub_app_name))
def create_app(): app = Flask(__name__) app.config.from_object('envcfg.json.envision') for extension_name in extensions: extension = import_string(extension_name) extension.init_app(app) for blueprint_name in blueprints: blueprint = import_string(blueprint_name) app.register_blueprint(blueprint) return app
def general_menu(): menu_items = list() blueprints = app.blueprints for k, v in blueprints.items(): try: config = import_string('%s.config' % import_string(v.import_name).__package__) except ImportError, e: print e else: if hasattr(config, 'RUS_NAME'): menu_items.append(dict(module=v.name, name=config.RUS_NAME)) else: menu_items.append(dict(module=v.name, name=v.name))
def import_tests(apps_path): pkg_list = os.listdir(apps_path) base_pkg = os.path.basename(apps_path) test_pkgs = list() if pkg_list: for pkg_path in pkg_list: if os.path.isdir(os.path.join(apps_path, pkg_path)): if os.path.isdir(os.path.join(apps_path, pkg_path, 'tests')): for _test_pkg in os.listdir(os.path.join(apps_path, pkg_path, 'tests')): test_pkgs.append(import_string('%s.%s.tests.%s' % (base_pkg, pkg_path, os.path.splitext(_test_pkg)[0]))) else: test_pkgs.append(import_string('%s.%s.tests' % (base_pkg, pkg_path))) return test_pkgs
def send_registration_confirm(request, registration_key): from kay.registration.models import RegistrationProfile import_string(settings.AUTH_USER_MODEL) p = db.get(registration_key) subject = render_to_string('registration/activation_email_subject.txt', {'appname': settings.APP_NAME}) subject = ''.join(subject.splitlines()) message = render_to_string( 'registration/activation_email.txt', {'activation_key': registration_key, 'appname': settings.APP_NAME}) mail.send_mail(subject=subject, body=message, sender=settings.DEFAULT_MAIL_FROM, to=p.parent().email) return Response("OK")
def _get_records(kevent): """Unpack records from a Kinesis event.""" events, shard_id = utils.unpack_kinesis_event( kevent, deserializer="{{kinesis_deserializer}}" \ and "{{kinesis_desearializer}}" != "None" \ and import_string("{{kinesis_deserializer}}"), unpacker="{{kinesis_unpacker}}" \ and "{{kinesis_unpacker}}" != "None" \ and import_string("{{kinesis_unpacker}}"), embed_timestamp="{{received_at_field}}" ) return events, shard_id
def runscript(script_path): ''' Run Function. e.g.: cheapflight.scripts.rader:main or cheapflight/scripts/rader.py with main() ''' if ":" not in script_path: script_path = os.path.expanduser(os.path.expandvars(script_path)) script_path = os.path.relpath(script_path) script_path = script_path.rstrip(".py") script_path = script_path.replace(os.path.sep, ".") script_path = script_path.lstrip(".") script_path = "%s:main" % script_path import_string(script_path)()
def __call__(self): try: endpoint = self.endpoint except NotFound: return super(URLMapAdapterMixin, self).__call__() endpoint = import_string(endpoint) return self.application.dispatch_to_endpoint(self, endpoint)
def main(): parser = ArgumentParser(description=__doc__) parser.add_argument('app_module', metavar='APP_MODULE', help='Python importible flask application e.g. my.module:app') parser.add_argument('-u', '--username', required=True, help='Ldap login with this username') parser.add_argument('-p', '--password', required=True, help='Ldap login with this password') args = parser.parse_args() if ':' in args.app_module: import_name, appname = args.app_module.split(':', 1) else: import_name, appname = args.app_module, 'app' module = import_string(import_name) app = getattr(module, appname) userdata = app.ldap_login_manager.ldap_login(args.username, args.password) if userdata is None: print "Invalid username/password" else: print "Got userdata for %s" % args.username pprint(userdata)
def setup_app(app): """Initialize Flask-Security.""" app.config.setdefault('SECURITY_DATASTORE_FACTORY', None) app.config.setdefault('SECURITY_CHANGEABLE', True) app.config.setdefault('SECURITY_CONFIRMABLE', True) app.config.setdefault('SECURITY_PASSWORD_HASH', 'pbkdf2_sha512') app.config.setdefault('SECURITY_RECOVERABLE', True) app.config.setdefault('SECURITY_REGISTERABLE', True) app.config.setdefault('SECURITY_TRACKABLE', True) app.config.setdefault('SECURITY_LOGIN_WITHOUT_CONFIRMATION', True) # Get datastore factory method create_datastore = import_string(app.config.get( 'SECURITY_DATASTORE_FACTORY')) if create_datastore is None: raise RuntimeError("Please specify SECURITY_DATASTORE_FACTORY") datastore = create_datastore(app) # Create user datastore and initialize extension. state = security.init_app(app, datastore=datastore) # Use Celery task for sending emails. @state.send_mail_task def delay_security_email(msg): send_security_email.delay(msg)
def _upgrade_recipe_find_path(import_str, create=True): """Determine repository name and path for new upgrade. It is based on package import path. """ try: # Import package m = import_string(import_str) # Check if package or module if m.__package__ is not None and m.__package__ != m.__name__: raise RuntimeError( "Expected package but found module at '%s'." % import_str ) # Create upgrade directory if it does not exists path = os.path.join(os.path.dirname(m.__file__), "upgrades") if not os.path.exists(path) and create: os.makedirs(path) # Create init file if it does not exists init = os.path.join(path, "__init__.py") if not os.path.exists(init) and create: open(init, 'a').close() repository = m.__name__.split(".")[-1] return (path, repository) except ImportError: raise RuntimeError("Could not find module '%s'." % import_str) except SyntaxError: raise RuntimeError("Module '%s' has syntax errors." % import_str)
def from_object(self, obj): """Updates the values from the given object. An object can be of one of the following two types: - a string: in this case the object with that name will be imported - an actual object reference: that object is used directly Objects are usually either modules or classes. Just the uppercase variables in that object are stored in the config. Example usage:: app.config.from_object('yourapplication.default_config') from yourapplication import default_config app.config.from_object(default_config) You should not use this function to load the actual configuration but rather configuration defaults. The actual config should be loaded with :meth:`from_pyfile` and ideally from a location not within the package because the package might be installed system wide. :param obj: an import name or object """ if isinstance(obj, basestring): obj = import_string(obj) for key in dir(obj): if key.isupper(): self[key] = getattr(obj, key)
def _import_model_if_not(self, model_name): if isinstance(self.models_dict[model_name], basestring): self.models_dict[model_name] = import_string( self.models_dict[model_name]) if self.model_handlers[model_name] is None: self.model_handlers[model_name] = ModelHandler( model_name, self.models_dict[model_name])
def auto_discovery(): for mod in find_modules("apps", recursive=True): if str(mod).endswith("tasks"): import_string(mod) elif str(mod).endswith("views"): import_string(mod)
def register_blueprints(app): for bp in bps: app.register_blueprint(import_string(bp))
def create_app(import_name, app_path, enabled_plugins, after_app_created=None, get_user_by_id=None, env=None): app_dot_path = app_path.replace('/', '.') # app and openapi app = None openapi_app = None if enabled_plugins.get('openapi'): import connexion openapi_app = connexion.FlaskApp( import_name, specification_dir=f'{app_path.strip("/")}/openapi/') app = openapi_app.app app.openapi_app = openapi_app else: app = Flask(import_name) app.app_path = app_path app.app_dot_path = app_dot_path app.enabled_plugins = enabled_plugins if after_app_created and not after_app_created(): return app # config config = get_config(app_dot_path, env) app.config.from_object(config) app.secret_key = app.config['SECRET_KEY'] app.static_url_path = '/static' # custom json encoder from .plugins.improved_json_encoder import ImprovedJsonEncoder app.json_encoder = ImprovedJsonEncoder # proxy_fix if enabled_plugins.get('proxy_fix'): from werkzeug.middleware.proxy_fix import ProxyFix ''' Needed when app behind proxy, such as nginx or vue proxy server. If not fixed, url_for and session will not work properly. ''' app.wsgi_app = ProxyFix(app.wsgi_app) # data validation DataValidator = import_string(f'{app_dot_path}.validator:DataValidator', silent=True) app.DataValidator = DataValidator or BaseDataValidator # cassandra if enabled_plugins.get('cassandra'): from cassandra.cqlengine import connection connection.setup([app.config['DB_HOST']], app.config['DB_KEYSPACE'], lazy_connect=True) from .plugins import cassandra_cqlengine, cassandra_cqlengine_elastic # mysql if enabled_plugins.get('mysql'): from flask_sqlalchemy import SQLAlchemy global _db # mysql://username:password@server:port/db?charset=utf8 user = app.config['DB_USER'] password = app.config['DB_PASS'] server = app.config['DB_HOST'] db_name = app.config['DB_NAME'] charset = app.config.get('DB_CHARSET', 'utf8') port = app.config.get('DB_PORT', 3306) app.config[ 'SQLALCHEMY_DATABASE_URI'] = f'mysql+pymysql://{user}:{password}@{server}:{port}/{db_name}?charset={charset}' _db = db = SQLAlchemy(app) # with app.app_context(): # cache # Check Configuring Flask-Cache section for more details if enabled_plugins.get('cache'): from flask_caching import Cache global _cache _cache = cache = Cache(config={'CACHE_TYPE': 'simple'}) # oldSet = cache.set def custom_cache_set(key, value, timeout=None): ''' timeout, unit minute ''' if timeout != None: timeout *= 60 return oldSet(key, value, timeout) cache.set = custom_cache_set cache.init_app(app) # schedule_task if enabled_plugins.get('schedule_task'): from flask_apscheduler import APScheduler scheduler = APScheduler() scheduler.init_app(app) scheduler.start() app.scheduler = scheduler import_string(f'{app_dot_path}.schedule_tasks', silent=True) # flask_login if enabled_plugins.get('flask_login'): from flask_login import LoginManager, user_loaded_from_request from flask.sessions import SecureCookieSessionInterface User = import_string(f'{app_dot_path}.models:User') def get_user_by_id2(id): if enabled_plugins.get('cassandra'): return User.objects(id=id).first() elif get_user_by_id: return get_user_by_id(id) else: raise Exception( "Can't get user by id. The argument get_user_by_id is required." ) # session is also inited login_manager = LoginManager() login_manager.init_app(app) app.login_manager = login_manager if app.config.get('FLASK_LOGIN_DISABLE_COOKIE_LOGIN'): # with token @login_manager.request_loader def load_user_from_request(request): from .plugins.jwt import decode_token token = request.headers.get('Authorization') if token: prefix = 'Bearer ' if token.startswith(prefix): token = token[len(prefix):] r = decode_token(token) if r['success']: user_id = r['data'] return get_user_by_id2(user_id) return None # disable cookie class CustomSessionInterface(SecureCookieSessionInterface): """Prevent creating session from API requests.""" def save_session(self, *args, **kwargs): if g.get('login_via_header'): return return super(CustomSessionInterface, self).save_session(*args, **kwargs) app.session_interface = CustomSessionInterface() # @user_loaded_from_request.connect def user_loaded_from_request_func(self, user=None): g.login_via_header = True else: # with cookie @login_manager.user_loader def load_user(user_id): return get_user_by_id2(user_id) # route if enabled_plugins.get('route'): route = import_string(f'{app_dot_path}.net', silent=True) # template global variables @app.context_processor def inject_global_variables_to_templates(): return dict(setting=setting) return app
def view(self): actual_view = import_string(self.import_name) if self.view_name: actual_view = actual_view.as_view(self.view_name) return actual_view
try: run_simple(ip, port, app, use_debugger=use_debugger, use_reloader=use_reloader, threaded=threaded) break except (OSError, socket.error): port += 1 if __name__ == '__main__': sys.path.insert(0, '.') if len(sys.argv) > 1: module_name = sys.argv[1] module_name = module_name.split('.py')[0] if ':' not in module_name: module_name += ':app' app = import_string(module_name) html_dirs = os.getenv("PICO_HTML_DIR") or {'/': 'static'} if html_dirs: html_dirs = {"/{}".format(os.path.basename(p)): p.replace("\\", "/") for p in html_dirs.split(os.pathsep)} for d, p in html_dirs.items(): assert os.path.isdir( p), "Not existing directory at path: {}".format(p) log.info("> html_dirs: {}".format(html_dirs)) ip = os.getenv("PICO_IP", '127.0.0.1') if ip and ip.startswith('http'): ip = ip.replace("http://", "")
def mod(self): """Load the module""" return import_string(self.path)
def to_operation_dict(self) -> OperationSpecType: """Generate the openapi spec part of this endpoint. The result needs to be added to the `apispec` instance manually. """ assert self.func is not None, "This object must be used in a decorator environment." assert self.operation_id is not None, "This object must be used in a decorator environment." module_obj = import_string(self.func.__module__) module_name = module_obj.__name__ headers: Dict[str, OpenAPIParameter] = {} if self.etag in ('output', 'both'): etag_header = to_openapi([ETAG_HEADER_PARAM], 'header')[0] del etag_header['in'] headers[etag_header.pop('name')] = etag_header responses: ResponseType = {} # We don't(!) support any endpoint without an output schema. # Just define one! if self.response_schema is not None: responses['200'] = { 'content': { self.content_type: { 'schema': self.response_schema }, }, 'description': apispec.utils.dedent(self.response_schema.__doc__ or ''), 'headers': headers, } if self.will_do_redirects: responses['302'] = { 'description': ('Either the resource has moved or has not yet completed. Please see this ' 'resource for further information.') } # Actually, iff you don't want to give out anything, then we don't need a schema. if self.output_empty: responses['204'] = { 'description': 'Operation done successfully. No further output.', 'headers': headers, } tag_obj: OpenAPITag = { 'name': module_name, } docstring_name = _docstring_name(module_obj.__doc__) if docstring_name: tag_obj['x-displayName'] = docstring_name docstring_desc = _docstring_description(module_obj.__doc__) if docstring_desc: tag_obj['description'] = docstring_desc _add_tag(tag_obj, tag_group=self.tag_group) operation_spec: OperationSpecType = { 'operationId': self.operation_id, 'tags': [module_name], 'description': '', 'responses': { 'default': { 'description': 'Any unsuccessful or unexpected result.', 'content': { 'application/problem+json': { 'schema': self.error_schema, } } } }, } header_params: List[RawParameter] = [] query_params: Sequence[ RawParameter] = self.query_params if self.query_params is not None else [] path_params: Sequence[ RawParameter] = self.path_params if self.path_params is not None else [] if self.etag in ('input', 'both'): header_params.append(ETAG_IF_MATCH_HEADER) operation_spec['parameters'] = coalesce_schemas([ ('header', header_params), ('query', query_params), ('path', path_params), ]) operation_spec['responses'].update(responses) if self.request_schema is not None: operation_spec['requestBody'] = { 'required': self.request_body_required, 'content': { 'application/json': { 'schema': self.request_schema, } } } operation_spec['x-codeSamples'] = code_samples(self) # If we don't have any parameters we remove the empty list, so the spec will not have it. if not operation_spec['parameters']: del operation_spec['parameters'] docstring_name = _docstring_name(self.func.__doc__) if docstring_name: operation_spec['summary'] = docstring_name else: raise RuntimeError(f"Please put a docstring onto {self.operation_id}") docstring_desc = _docstring_description(self.func.__doc__) if docstring_desc: operation_spec['description'] = docstring_desc apispec.utils.deepupdate(operation_spec, self.options) return {self.method: operation_spec} # type: ignore[misc]
def _add_api_spec(func): module_obj = import_string(func.__module__) module_name = module_obj.__name__ operation_id = func.__module__ + "." + func.__name__ if not output_empty and response_schema is None: raise ValueError( "%s: 'response_schema' required when output is to be sent!" % operation_id) if output_empty and response_schema: raise ValueError( "%s: On empty output 'output_schema' may not be used." % operation_id) # We don't(!) support any endpoint without an output schema. # Just define one! if response_schema is not None: path_item = { '200': { 'content': { content_type: { 'schema': response_schema }, }, 'description': apispec.utils.dedent(response_schema.__doc__ or ''), } } # Actually, iff you don't want to give out anything, then we don't need a schema. if output_empty: path_item = { '204': { 'description': 'Operation done successfully. No further output.' } } tag_obj = {'name': module_name} tag_obj.update( _docstring_keys(module_obj.__doc__, 'x-displayName', 'description')) _add_tag(tag_obj, tag_group='Endpoints') operation_spec = { 'operationId': operation_id, 'tags': [module_name], 'description': '', 'responses': { 'default': { 'description': 'Any unsuccessful or unexpected result.', 'content': { 'application/problem+json': { 'schema': error_schema, } } } }, 'parameters': [], } if param_names: operation_spec['parameters'].extend(parameters) if etag in ('input', 'both'): operation_spec['parameters'].append(ETAG_IF_MATCH_HEADER) if etag in ('output', 'both'): # We can't put this completely in a schema because 'headers' is a map. We therefore # have to duplicate it every time. # NOTE: Be aware that this block only works under the assumption that only one(!) # http_status defined `operation_spec`. If this assumption no longer holds this block # needs to be refactored. only_key = list(path_item.keys())[0] path_item[only_key].setdefault('headers', {}) path_item[only_key]['headers'].update(ETAG_HEADER_PARAM) operation_spec['responses'].update(path_item) if request_schema is not None: tag = _tag_from_schema(request_schema) _add_tag(tag, tag_group='Request Schemas') operation_spec['requestBody'] = { 'required': request_body_required, 'content': { 'application/json': { 'schema': request_schema, } } } operation_spec['x-code-samples'] = code_samples( path, method, request_schema) # If we don't have any parameters we remove the empty list, so the spec will not have it. if not operation_spec['parameters']: del operation_spec['parameters'] operation_spec.update( _docstring_keys(func.__doc__, 'summary', 'description')) apispec.utils.deepupdate(operation_spec, options) add_operation(path, method, operation_spec) if response_schema is None and request_schema is None: return func return wrap_with_validation(func, request_schema, response_schema)
def next_action(activity_id='0', action_id=0): """Next action.""" work_activity = WorkActivity() history = WorkActivityHistory() post_json = request.get_json() activity = dict(activity_id=activity_id, action_id=action_id, action_version=post_json.get('action_version'), action_status=ActionStatusPolicy.ACTION_DONE, commond=post_json.get('commond')) action = Action().get_action_detail(action_id) action_endpoint = action.action_endpoint if action_endpoint == 'begin_action': return jsonify(code=0, msg=_('success')) if action_endpoint == 'end_action': work_activity.end_activity(activity) return jsonify(code=0, msg=_('success')) if action_endpoint == 'item_login': register_hdl(activity_id) activity_detail = work_activity.get_activity_detail(activity_id) item_id = None recid = None record = None pid_without_ver = None if activity_detail and activity_detail.item_id: item_id = activity_detail.item_id current_pid = PersistentIdentifier.get_by_object(pid_type='recid', object_type='rec', object_uuid=item_id) recid = get_record_identifier(current_pid.pid_value) record = WekoDeposit.get_record(item_id) if record: pid_without_ver = get_record_without_version(current_pid) deposit = WekoDeposit(record, record.model) if post_json.get('temporary_save') == 1 \ and action_endpoint != 'identifier_grant': if 'journal' in post_json: work_activity.create_or_update_action_journal( activity_id=activity_id, action_id=action_id, journal=post_json.get('journal')) else: work_activity.upt_activity_action_comment( activity_id=activity_id, action_id=action_id, comment=post_json.get('commond')) return jsonify(code=0, msg=_('success')) elif post_json.get('journal'): work_activity.create_or_update_action_journal( activity_id=activity_id, action_id=action_id, journal=post_json.get('journal')) if action_endpoint == 'approval' and item_id: item = ItemsMetadata.get_record(id_=item_id) pid_identifier = PersistentIdentifier.get_by_object( pid_type='depid', object_type='rec', object_uuid=item.id) record_class = import_string('weko_deposit.api:WekoRecord') resolver = Resolver(pid_type='recid', object_type='rec', getter=record_class.get_record) _pid, _approval_record = resolver.resolve(pid_identifier.pid_value) action_feedbackmail = work_activity.get_action_feedbackmail( activity_id=activity_id, action_id=ITEM_REGISTRATION_ACTION_ID) if action_feedbackmail: FeedbackMailList.update( item_id=item_id, feedback_maillist=action_feedbackmail.feedback_maillist) if not recid and pid_without_ver: FeedbackMailList.update( item_id=pid_without_ver.object_uuid, feedback_maillist=action_feedbackmail.feedback_maillist) if record: deposit.update_feedback_mail() deposit.update_jpcoar_identifier() # TODO: Make private as default. # UpdateItem.publish(pid, approval_record) if action_endpoint == 'item_link' and record: current_pid = PersistentIdentifier.get_by_object(pid_type='recid', object_type='rec', object_uuid=item_id) if not pid_without_ver: pid_without_ver = get_record_without_version(current_pid) item_link = ItemLink(pid_without_ver.pid_value) relation_data = post_json.get('link_data') if relation_data: errors = item_link.update(relation_data) if errors: return jsonify(code=-1, msg=_(errors)) # save pidstore_identifier to ItemsMetadata identifier_select = post_json.get('identifier_grant') if 'identifier_grant' == action_endpoint and identifier_select: idf_grant_jalc_doi_manual = post_json.get( 'identifier_grant_jalc_doi_suffix') idf_grant_jalc_cr_doi_manual = post_json.get( 'identifier_grant_jalc_cr_doi_suffix') idf_grant_jalc_dc_doi_manual = post_json.get( 'identifier_grant_jalc_dc_doi_suffix') idf_grant_ndl_jalc_doi_manual = post_json.get( 'identifier_grant_ndl_jalc_doi_suffix') # If is action identifier_grant, then save to to database identifier_grant = { 'action_identifier_select': identifier_select, 'action_identifier_jalc_doi': idf_grant_jalc_doi_manual, 'action_identifier_jalc_cr_doi': idf_grant_jalc_cr_doi_manual, 'action_identifier_jalc_dc_doi': idf_grant_jalc_dc_doi_manual, 'action_identifier_ndl_jalc_doi': idf_grant_ndl_jalc_doi_manual } work_activity.create_or_update_action_identifier( activity_id=activity_id, action_id=action_id, identifier=identifier_grant) error_list = item_metadata_validation(item_id, identifier_select) if post_json.get('temporary_save') == 1: return jsonify(code=0, msg=_('success')) if isinstance(error_list, str): return jsonify(code=-1, msg=_(error_list)) sessionstore = RedisStore( redis.StrictRedis.from_url('redis://{host}:{port}/1'.format( host=os.getenv('INVENIO_REDIS_HOST', 'localhost'), port=os.getenv('INVENIO_REDIS_PORT', '6379')))) if error_list: sessionstore.put('updated_json_schema_{}'.format(activity_id), json.dumps(error_list).encode('utf-8'), ttl_secs=300) return previous_action(activity_id=activity_id, action_id=action_id, req=-1) else: if sessionstore.redis.exists( 'updated_json_schema_{}'.format(activity_id)): sessionstore.delete( 'updated_json_schema_{}'.format(activity_id)) if identifier_select != IDENTIFIER_GRANT_SELECT_DICT['NotGrant'] \ and item_id is not None: record_without_version = item_id if record and pid_without_ver and not recid: record_without_version = pid_without_ver.object_uuid saving_doi_pidstore(item_id, record_without_version, post_json, int(identifier_select)) rtn = history.create_activity_history(activity) if not rtn: return jsonify(code=-1, msg=_('error')) # next action work_activity.upt_activity_action_status( activity_id=activity_id, action_id=action_id, action_status=ActionStatusPolicy.ACTION_DONE) work_activity.upt_activity_action_comment(activity_id=activity_id, action_id=action_id, comment='') flow = Flow() next_flow_action = flow.get_next_flow_action( activity_detail.flow_define.flow_id, action_id) if next_flow_action and len(next_flow_action) > 0: next_action_endpoint = next_flow_action[0].action.action_endpoint if 'end_action' == next_action_endpoint: new_activity_id = None if record: deposit.publish() updated_item = UpdateItem() # publish record without version ID when registering newly if recid: # new record attached version ID ver_attaching_record = deposit.newversion(current_pid) new_activity_id = ver_attaching_record.model.id ver_attaching_deposit = WekoDeposit( ver_attaching_record, ver_attaching_record.model) ver_attaching_deposit.publish() record_bucket_id = merge_buckets_by_records( current_pid.object_uuid, ver_attaching_record.model.id, sub_bucket_delete=True) if not record_bucket_id: return jsonify(code=-1, msg=_('error')) # Record without version: Make status Public as default updated_item.publish(record) else: # update to record without version ID when editing new_activity_id = record.model.id if pid_without_ver: record_without_ver = WekoDeposit.get_record( pid_without_ver.object_uuid) deposit_without_ver = WekoDeposit( record_without_ver, record_without_ver.model) deposit_without_ver['path'] = deposit.get('path', []) parent_record = deposit_without_ver.\ merge_data_to_record_without_version(current_pid) deposit_without_ver.publish() set_bucket_default_size(new_activity_id) merge_buckets_by_records(new_activity_id, pid_without_ver.object_uuid) updated_item.publish(parent_record) delete_unregister_buckets(new_activity_id) activity.update( action_id=next_flow_action[0].action_id, action_version=next_flow_action[0].action_version, item_id=new_activity_id, ) work_activity.end_activity(activity) else: next_action_id = next_flow_action[0].action_id work_activity.upt_activity_action( activity_id=activity_id, action_id=next_action_id, action_status=ActionStatusPolicy.ACTION_DOING) # delete session value if session.get('itemlogin_id'): del session['itemlogin_id'] del session['itemlogin_activity'] del session['itemlogin_item'] del session['itemlogin_steps'] del session['itemlogin_action_id'] del session['itemlogin_cur_step'] del session['itemlogin_record'] del session['itemlogin_res_check'] del session['itemlogin_pid'] del session['itemlogin_community_id'] return jsonify(code=0, msg=_('success'))
def handle_collection(self, content): """ :param content: :return: """ for item in content['item']: try: # 注意这里是直接取postman数据,不改变数据类型,因此body是dict。 if len(item['name']) < 64: name = item['name'] else: name = item['name'][0:64] header = item['request']['header'] method = item['request']['method'].lower() host = item['request']['url']['protocol'] + '://' + \ '.'.join(item['request']['url']['host']) if 'port' in item['request']['url']: host += ':' + item['request']['url']['port'] # path默认是首页请求,如果不是则进行拼接 path = '/' if 'path' in item['request']['url']: path += '/'.join(item['request']['url']['path']) if 'query' in item['request']['url']: params = item['request']['url']['query'] else: params = [] if 'body' in item['request']: if item['request']['body'].get('mode') == 'formdata': body = { 'mode': item['request']['body']['mode'], 'data': item['request']['body']['formdata'] } elif item['request']['body'].get('mode') == 'urlencoded': body = { 'mode': item['request']['body']['mode'], 'data': item['request']['body']['urlencoded'] } elif item['request']['body'].get('mode') == 'file': body = { 'mode': item['request']['body']['mode'], 'data': item['request']['body']['file'] } else: body = { 'mode': item['request']['body'].get('mode') or 'raw', 'data': item['request']['body'].get('raw') or '' } else: body = {'mode': 'raw', 'data': ''} host = self.change_postman_variable_to_clover(host) path = self.change_postman_variable_to_clover(path) header = self.change_postman_variable_to_clover(header) params = self.change_postman_variable_to_clover(params) body = self.change_postman_variable_to_clover(body) interface = { 'team': self.team, 'project': self.project, 'name': name, 'method': method, 'host': host, 'path': path, 'header': header, 'params': params, 'body': body, 'verify': [], 'extract': [], } plugin = 'clover.interface.service:InterfaceService' service = import_string(plugin)() _, status, _, _ = service.create(interface) if status == 0: self.result['success'] += 1 else: self.result['failed'] += 1 except Exception: self.result['failed'] += 1 self.result['total'] += 1
# limitations under the License. """Copy of Data Commons Python Client API Core without pandas dependency.""" import base64 import collections import json import logging import os import zlib import urllib import requests from werkzeug.utils import import_string if os.environ.get('FLASK_ENV') == 'test': cfg = import_string('configmodule.TestConfig')() elif os.environ.get('FLASK_ENV') == 'webdriver': cfg = import_string('configmodule.WebdriverConfig')() elif os.environ.get('FLASK_ENV') == 'production': cfg = import_string('configmodule.ProductionConfig')() elif os.environ.get('FLASK_ENV') == 'staging': cfg = import_string('configmodule.StagingConfig')() else: cfg = import_string('configmodule.DevelopmentConfig')() API_ROOT = cfg.API_ROOT API_PROJECT = cfg.API_PROJECT DC_API_KEY = 'api-key' # if (os.environ.get('FLASK_ENV') == 'test' or # os.environ.get('FLASK_ENV') == 'webdriver'):
class Resource(object): """The core class that represents a REST resource. :param action_map: the mapping of request methods to resource actions. """ #: The name of the resource. name = None #: The parser classes used for parsing request data. parser_classes = tuple( import_string(parser_class) for parser_class in config.PARSER_CLASSES) #: The renderer classes used for rendering response data. renderer_classes = tuple( import_string(renderer_class) for renderer_class in config.RENDERER_CLASSES) #: The class used to select the proper parser or renderer negotiator_class = Negotiator #: The resource-level middleware classes middleware_classes = () def __init__(self, action_map): self.action_map = action_map @locked_cached_classproperty(name='_middlewares') def middlewares(cls): """The instances of all middleware classes. The final order of middleware classes: The first is global middleware classes (in order) and then is resource-level middleware classes (in order). """ global_middleware_classes = tuple( import_string(middleware_class_string) for middleware_class_string in config.MIDDLEWARE_CLASSES) middleware_classes = global_middleware_classes + cls.middleware_classes return tuple(middleware_class() for middleware_class in middleware_classes) @property def logger(self): """A :class:`logging.Logger` object for this API.""" from .logging import global_logger return global_logger def _get_head(self): """Get the head message for logging.""" query_string = self.request.environ['QUERY_STRING'] separator = '?' if query_string else '' head = '[%s %s%s%s]' % (self.request.method, self.request.path, separator, query_string) return head def log_message(self, msg): """Logs a message with `DEBUG` level. :param msg: the message to be logged. """ if self.request.method in config.LOGGER_METHODS: self.logger.debug('%s %s' % (self._get_head(), msg)) def log_exception(self, exc): """Logs an exception with `ERROR` level. :param exc: the exception to be logged. """ self.logger.exception('Exception on %s' % self._get_head()) def get_parser_context(self, request, args, kwargs): """Return a dictionary that represents a parser context. :param request: the request object. :param args: the positional arguments captured from the URI. :param kwargs: the keyword arguments captured from the URI. """ return { 'resource': self, 'request': request, 'args': args, 'kwargs': kwargs } def get_renderer_context(self, request, args, kwargs, response): """Return a dictionary that represents a renderer context. :param request: the request object. :param args: the positional arguments captured from the URI. :param kwargs: the keyword arguments captured from the URI. :param response: the response object. """ return { 'resource': self, 'request': request, 'args': args, 'kwargs': kwargs, 'response': response } def dispatch_request(self, request, *args, **kwargs): """Does the request dispatching. Matches the HTTP method and return the return value of the bound action. :param request: the request object. :param args: the positional arguments captured from the URI. :param kwargs: the keyword arguments captured from the URI. """ negotiator = self.negotiator_class() self.format_suffix = kwargs.pop('format', None) parser_context = self.get_parser_context(request, args, kwargs) self.request = request try: request.parse(negotiator, self.parser_classes, parser_context) self.log_message('<Request> %s' % request.data) rv = self.perform_action(*args, **kwargs) except Exception as exc: rv = self.handle_exception(exc) response = self.make_response(rv) self.log_message('<Response> %s %s' % (response.status, response.data)) renderer_context = self.get_renderer_context(request, args, kwargs, response) return response.render(negotiator, self.renderer_classes, self.format_suffix, renderer_context) def http_method_not_allowed(self, request, *args, **kwargs): """The default action handler if the corresponding action for `request.method` is not implemented. See :meth:`dispatch_request` for the meanings of the parameters. """ raise exceptions.MethodNotAllowed() def find_action(self, request): """Find the appropriate action according to the request method. :param request: the request object. """ try: action_name = self.action_map[request.method] except KeyError as exc: exc.args = ('Config `ACTION_MAP` has no mapping for %r' % request.method, ) raise action = getattr(self, action_name, self.http_method_not_allowed) return action def perform_action(self, *args, **kwargs): """Perform the appropriate action. Also apply all possible `process_*` methods of middleware instances in `self.middlewares`. During request phase: :meth:`process_request` methods are called on each request, before RESTArt calls the `action`, in order. It should return :attr:`None` or any other value that :attr:`~restart.resource.Resource.make_response` can recognize. If it returns :attr:`None`, RESTArt will continue processing the request, executing any other :meth:`process_request` and, then, the `action`. If it returns any other value (e.g. a :class:`~restart.response.Response` object), RESTArt won't bother calling any other middleware or the `action`. During response phase: :meth:`process_response` methods are called on all responses before they'are returned to the client, in reverse order. It must return a value that can be converted to a :class:`~restart.response.Response` object by :attr:`~restart.resource.Resource.make_response`. It could alter and return the given `response`, or it could create and return a brand-new value. Unlike :meth:`process_request` methods, the :meth:`process_response` method is always called, even if the :meth:`process_request` of the same middleware were skipped (because an earlier middleware method returned a :class:`~restart.response.Response`). :param args: a list of positional arguments that will be passed to the action. :param kwargs: a dictionary of keyword arguments that will be passed to the action. """ rv = None # Call possible `process_request` methods of middlewares for middleware in self.middlewares: if hasattr(middleware, 'process_request'): rv = middleware.process_request(self.request) if rv is not None: break # Call the `action` if rv is None: action = self.find_action(self.request) rv = action(self.request, *args, **kwargs) # Call all `process_response` methods of middlewares for middleware in reversed(self.middlewares): if hasattr(middleware, 'process_response'): # Ensure that the second parameter passed to the # `process_response` method is a `Response` object response = self.make_response(rv) rv = middleware.process_response(self.request, response) return rv def handle_exception(self, exc): """Handle any exception that occurs, by returning an appropriate response, or re-raising the error. :param exc: the exception to be handled. """ if isinstance(exc, exceptions.HTTPException): # Always render `HTTPException` messages into JSON from .renderers import JSONRenderer self.renderer_classes = (JSONRenderer, ) headers = dict(exc.get_headers(self.request.environ)) rv = ({'message': exc.description}, exc.code, headers) return rv else: self.log_exception(exc) raise def make_response(self, rv): """Converts the return value to a real response object that is an instance of :class:`~restart.response.Response`. The following types are allowed for `rv`: ====================== ============================================ :class:`Response` the object is returned unchanged :class:`str` the string becomes the response body :class:`unicode` the unicode string becomes the response body :class:`tuple` A tuple in the form ``(data, status)`` or ``(data, status, headers)`` where `data` is the response body, `status` is an integer and `headers` is a dictionary with header values. ====================== ============================================ """ status = 200 headers = None if isinstance(rv, tuple): rv_len = len(rv) if rv_len == 2: rv, status = rv elif rv_len == 3: rv, status, headers = rv else: raise ValueError('Resource action return a wrong response') if rv is None: raise ValueError('Resource action did not return a response') elif not isinstance(rv, Response): rv = Response(rv, status=status, headers=headers) return rv
def load(self): """Mock load entry point.""" if self.name == 'importfail': raise ImportError() else: return import_string(self.name)
def server(self): """ All in one endpoints. This property is created automaticly if you have implemented all the getters and setters. However, if you are not satisfied with the getter and setter, you can create a validator with :class:`OAuth2RequestValidator`:: class MyValidator(OAuth2RequestValidator): def validate_client_id(self, client_id): # do something return True And assign the validator for the provider:: oauth._validator = MyValidator() """ expires_in = self.app.config.get('OAUTH2_PROVIDER_TOKEN_EXPIRES_IN') token_generator = self.app.config.get( 'OAUTH2_PROVIDER_TOKEN_GENERATOR', None) if token_generator and not callable(token_generator): token_generator = import_string(token_generator) refresh_token_generator = self.app.config.get( 'OAUTH2_PROVIDER_REFRESH_TOKEN_GENERATOR', None) if refresh_token_generator and not callable(refresh_token_generator): refresh_token_generator = import_string(refresh_token_generator) if hasattr(self, '_validator'): return Server( self._validator, token_expires_in=expires_in, token_generator=token_generator, refresh_token_generator=refresh_token_generator, ) if hasattr(self, '_clientgetter') and \ hasattr(self, '_tokengetter') and \ hasattr(self, '_tokensetter') and \ hasattr(self, '_grantgetter') and \ hasattr(self, '_grantsetter'): usergetter = None if hasattr(self, '_usergetter'): usergetter = self._usergetter validator = OAuth2RequestValidator( clientgetter=self._clientgetter, tokengetter=self._tokengetter, grantgetter=self._grantgetter, usergetter=usergetter, tokensetter=self._tokensetter, grantsetter=self._grantsetter, ) self._validator = validator return Server( validator, token_expires_in=expires_in, token_generator=token_generator, refresh_token_generator=refresh_token_generator, ) raise RuntimeError('application not bound to required getters')
except Exception as e: raise 'backend_dir listdir failed!' # 目录为空,或者不是list if not backend_dir or not isinstance(backend_dir, list): raise 'backend_dir is empty!' from werkzeug.utils import import_string # 遍历注册 Blueprint for blue_dir in backend_dir: # 不是目录,忽略 if not os.path.isdir(os.path.join(PATH_BACKEND, blue_dir)): continue # 缓存目录,忽略 if blue_dir.startswith('_'): continue # 导入并注册 class_blueprint = import_string('%s.%s.%s_blueprint' % ('backend', blue_dir, blue_dir)) backend_server.register_blueprint(class_blueprint, url_prefix='/%s' % blue_dir) if __name__ == '__main__': ''' 启动服务器 python backend_server.py ''' backend_server.run(host='127.0.0.1', port=8577, threaded=True, debug=True)
def iter_suites(): """Yields all testsuites.""" for module in find_modules(__name__): mod = import_string(module) if hasattr(mod, 'suite'): yield mod.suite()
def register_blueprints(app): for name in find_modules("src.handler"): mod = import_string(name) if hasattr(mod, "blueprint"): app.register_blueprint(mod.blueprint) return None
def to_operation_dict(self) -> OperationSpecType: """Generate the openapi spec part of this endpoint. The result needs to be added to the `apispec` instance manually. """ assert self.func is not None, "This object must be used in a decorator environment." assert self.operation_id is not None, "This object must be used in a decorator environment." module_obj = import_string(self.func.__module__) response_headers: Dict[str, OpenAPIParameter] = {} content_type_header = to_openapi([CONTENT_TYPE], "header")[0] del content_type_header["in"] response_headers[content_type_header.pop("name")] = content_type_header if self.etag in ("output", "both"): etag_header = to_openapi([ETAG_HEADER_PARAM], "header")[0] del etag_header["in"] response_headers[etag_header.pop("name")] = etag_header responses: ResponseType = {} if self.tag_group == "Setup": responses["403"] = self._path_item( 403, "Configuration via WATO is disabled") if 404 in self._expected_status_codes: responses["404"] = self._path_item( 404, "The requested object has not been found.") if 422 in self._expected_status_codes: responses["422"] = self._path_item( 422, "The request could not be processed.") if 405 in self._expected_status_codes: responses["405"] = _path_item( 405, "Method not allowed: This request is only allowed " "with other HTTP methods") if 409 in self._expected_status_codes: responses["409"] = self._path_item( 409, "The request is in conflict with the stored resource.", ) if 415 in self._expected_status_codes: responses["415"] = self._path_item( 415, "The submitted content-type is not supported.") if 302 in self._expected_status_codes: responses["302"] = self._path_item( 302, "Either the resource has moved or has not yet completed. Please see this " "resource for further information.", ) if 400 in self._expected_status_codes: responses["400"] = self._path_item( 400, "Parameter or validation failure.") # We don't(!) support any endpoint without an output schema. # Just define one! if 200 in self._expected_status_codes: if self.response_schema: content: ContentObject content = {self.content_type: {"schema": self.response_schema}} elif self.content_type == "application/octet-stream" or self.content_type.startswith( "image/"): content = { self.content_type: { "schema": { "type": "string", "format": "binary", } } } else: raise ValueError( f"Unknown content-type: {self.content_type} Please add condition." ) responses["200"] = self._path_item( 200, "The operation was done successfully.", content=content, headers=response_headers, ) if 204 in self._expected_status_codes: responses["204"] = self._path_item( 204, "Operation done successfully. No further output.") if 412 in self._expected_status_codes: responses["412"] = self._path_item( 412, "The value of the If-Match header doesn't match the object's ETag.", ) if 428 in self._expected_status_codes: responses["428"] = self._path_item( 428, "The required If-Match header is missing.") docstring_name = _docstring_name(module_obj.__doc__) tag_obj: OpenAPITag = { "name": docstring_name, "x-displayName": docstring_name, } docstring_desc = _docstring_description(module_obj.__doc__) if docstring_desc: tag_obj["description"] = docstring_desc _add_tag(tag_obj, tag_group=self.tag_group) operation_spec: OperationSpecType = { "operationId": self.operation_id, "tags": [docstring_name], "description": "", } header_params: List[RawParameter] = [] query_params: Sequence[RawParameter] = ( self.query_params if self.query_params is not None else []) path_params: Sequence[RawParameter] = ( self.path_params if self.path_params is not None else []) if config.rest_api_etag_locking and self.etag in ("input", "both"): header_params.append(ETAG_IF_MATCH_HEADER) if self.request_schema: header_params.append(CONTENT_TYPE) # While we define the parameters separately to be able to use them for validation, the # OpenAPI spec expects them to be listed in on place, so here we bunch them together. operation_spec["parameters"] = coalesce_schemas([ ("header", header_params), ("query", query_params), ("path", path_params), ]) operation_spec["responses"] = responses if self.request_schema is not None: operation_spec["requestBody"] = { "required": True, "content": { "application/json": { "schema": self.request_schema, } }, } operation_spec["x-codeSamples"] = code_samples( self, header_params=header_params, path_params=path_params, query_params=query_params, ) # If we don't have any parameters we remove the empty list, so the spec will not have it. if not operation_spec["parameters"]: del operation_spec["parameters"] docstring_name = _docstring_name(self.func.__doc__) if docstring_name: operation_spec["summary"] = docstring_name else: raise RuntimeError( f"Please put a docstring onto {self.operation_id}") docstring_desc = _docstring_description(self.func.__doc__) if docstring_desc: operation_spec["description"] = docstring_desc apispec.utils.deepupdate(operation_spec, self.options) return {self.method: operation_spec} # type: ignore[misc]
def view(self): return import_string(self.import_name)
def cache(self): """Return a cache instance.""" cache = self._cache or self.app.config.get('ACCESS_CACHE') return import_string(cache) if isinstance(cache, six.string_types) \ else cache
def create_app(config_name): app = Flask(config_name) app.config.from_object(config[config_name]) # 全局响应头 @app.after_request def after_request(response): if "Access-Control-Allow-Origin" not in response.headers.keys(): response.headers['Access-Control-Allow-Origin'] = '*' if request.method == 'OPTIONS': response.headers[ 'Access-Control-Allow-Methods'] = 'DELETE, GET, POST, PUT' headers = request.headers.get('Access-Control-Request-Headers') if headers: response.headers['Access-Control-Allow-Headers'] = headers return response # 自定义错误日志 handler = logging.FileHandler('app.log', encoding='UTF-8') logging_format = logging.Formatter( '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s' ) handler.setFormatter(logging_format) app.logger.addHandler(handler) def init_login(): login_manager = login.LoginManager() login_manager.setup_app(app) # Create user loader function @login_manager.user_loader def load_user(user_id): return AdminUser.objects(id=user_id).first() @app.errorhandler(404) def page_not_found(e): return jsonify(msg='404 page not found') # 注册所有蓝图 for bp_name in blueprints: bp = import_string(bp_name) app.register_blueprint(bp) config[config_name].init_app(app) babel = Babel(app) db.init_app(app) docs.init_app(app) admin.init_app(app) init_login() limiter.init_app(app) celery.init_app(app) redis.init_app(app) celery.conf.update(app.config) db.register_connection(db='ultrabear_homework', alias="home", port=PORT, username=NAME, password=PWD) # 跨域 CORS(app, supports_credentials=True) return app
def lazy_import(name): """ Lazy import of name using `Werzeug.local.import_string`. """ return LocalProxy(lambda: import_string(name))
# REANA is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. """Flask application configuration.""" import os from reana_commons.config import REANA_COMPONENT_PREFIX from werkzeug.utils import import_string SHARED_VOLUME_PATH_ROOT = os.getenv("SHARED_VOLUME_PATH_ROOT", "/var/reana") """Root path of the shared volume .""" COMPUTE_BACKENDS = { "kubernetes": lambda: import_string( "reana_job_controller.kubernetes_job_manager.KubernetesJobManager"), "htcondorcern": lambda: import_string( "reana_job_controller.htcondorcern_job_manager.HTCondorJobManagerCERN" ), "slurmcern": lambda: import_string( "reana_job_controller.slurmcern_job_manager.SlurmJobManagerCERN"), } """Supported job compute backends and corresponding management class.""" JOB_MONITORS = { "kubernetes": lambda: import_string( "reana_job_controller.job_monitor.JobMonitorKubernetes"), "htcondorcern":
def import_bp_string(module_name): import_name = '%s.bps.%s:bp' % (__package__, module_name) return import_string(import_name)
import os from werkzeug.utils import import_string from service.config import set_config set_config( import_string(os.environ.get('APP_CONFIG', 'service.config.TestConfig')))
from flask import Flask from flask_login import LoginManager from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate, MigrateCommand from flask_script import Manager from flask_bootstrap import Bootstrap from flask_uploads import UploadSet, configure_uploads, IMAGES from werkzeug.utils import import_string app = Flask(__name__) if app.config["ENV"] == "production": cfg = import_string('config.config.ProductionConfig')() app.config.from_object(cfg) else: cfg = import_string('config.config.DevelopmentConfig')() app.config.from_object(cfg) print(f' Flask ENV is set to: {app.config["ENV"]}') photos = UploadSet('photos', IMAGES) configure_uploads(app, photos) db = SQLAlchemy(app) migrate = Migrate(app, db) manager = Manager(app) manager.add_command('db', MigrateCommand) login_manager = LoginManager(app) login_manager.login_view = 'login'
def translation_path_from_config(self): try: return import_string( current_app.config.get("OY_TRANSLATIONS_PATH", "")).__path__[0] except ValueError: return
def configure_admin(app, admin): # noqa custom_index = app.config.get('ADMIN_INDEX_VIEW') if custom_index: admin.index_view = import_string(custom_index)() if isinstance(admin._views[0], BaseIndexView): del admin._views[0] admin._views.insert(0, admin.index_view) admin_config = app.config.get('ADMIN', { 'name': 'Quokka Admin', 'url': '/admin' }) for k, v in list(admin_config.items()): setattr(admin, k, v) for entry in app.config.get('FILE_ADMIN', []): try: admin.add_view( FileAdmin( entry['path'], entry['url'], name=_l(entry['name']), category=_l(entry['category']), endpoint=entry['endpoint'], roles_accepted=entry.get('roles_accepted'), editable_extensions=entry.get('editable_extensions'))) except Exception as e: app.logger.info(e) # register all themes in file manager for k, theme in app.theme_manager.themes.items(): try: if k == app.config.get('DEFAULT_THEME'): suffix = "(Site theme)" elif k == app.config.get('ADMIN_THEME'): suffix = "(Admin theme)" else: suffix = "Theme" admin.add_view( FileAdmin(theme.static_path, "/_themes/{0}/".format(theme.identifier), name="{0}: {1} static files".format( suffix, theme.identifier), category=_l("Files"), endpoint="{0}_static_files".format(theme.identifier), roles_accepted=('admin', "editor"), editable_extensions=app.config.get( 'DEFAULT_EDITABLE_EXTENSIONS'))) admin.add_view( FileAdmin( theme.templates_path, "/theme_template_files/{0}/".format(theme.identifier), name="{0}: {1} template files".format( suffix, theme.identifier), category=_l("Files"), endpoint="{0}_template_files".format(theme.identifier), roles_accepted=('admin', "editor"), editable_extensions=app.config.get( 'DEFAULT_EDITABLE_EXTENSIONS'))) except Exception as e: app.logger.warning('Error registering %s folder to file admin %s' % (theme.identifier, e)) # adding views admin.add_view(InspectorView(category=_l("Settings"), name=_l("Inspector"))) # adding extra views extra_views = app.config.get('ADMIN_EXTRA_VIEWS', []) for view in extra_views: admin.add_view( import_string(view['module'])(category=_l(view.get('category')), name=_l(view.get('name')))) # adding model views admin.register(Link, LinkAdmin, category=_l("Content"), name=_l("Link")) admin.register(Config, ConfigAdmin, category=_l("Settings"), name=_l("Config")) admin.register(SubContentPurpose, SubContentPurposeAdmin, category=_l("Settings"), name=_l("Sub content purposes")) admin.register(ChannelType, ChannelTypeAdmin, category=_l("Settings"), name=_l("Channel type")) admin.register(ContentTemplateType, ContentTemplateTypeAdmin, category=_l("Settings"), name=_l("Template type")) admin.register(Channel, ChannelAdmin, category=_l("Content"), name=_l("Channel")) # avoid registering twice if admin.app is None: admin.init_app(app) return admin
def display_activity(activity_id=0): """Display activity.""" activity = WorkActivity() activity_detail = activity.get_activity_detail(activity_id) item = None if activity_detail and activity_detail.item_id: try: item = ItemsMetadata.get_record(id_=activity_detail.item_id) except NoResultFound as ex: current_app.logger.exception(str(ex)) item = None steps = activity.get_activity_steps(activity_id) history = WorkActivityHistory() histories = history.get_activity_history_list(activity_id) workflow = WorkFlow() workflow_detail = workflow.get_workflow_by_id(activity_detail.workflow_id) if activity_detail.activity_status == \ ActivityStatusPolicy.ACTIVITY_FINALLY \ or activity_detail.activity_status == \ ActivityStatusPolicy.ACTIVITY_CANCEL: activity_detail.activity_status_str = _('End') else: activity_detail.activity_status_str = \ request.args.get('status', 'ToDo') cur_action = activity_detail.action action_endpoint = cur_action.action_endpoint action_id = cur_action.id temporary_comment = activity.get_activity_action_comment( activity_id=activity_id, action_id=action_id) # display_activity of Identifier grant identifier_setting = None if action_endpoint == 'identifier_grant' and item: community_id = request.args.get('community', None) if not community_id: community_id = 'Root Index' identifier_setting = get_identifier_setting(community_id) # valid date pidstore_identifier data if identifier_setting: text_empty = '<Empty>' if not identifier_setting.jalc_doi: identifier_setting.jalc_doi = text_empty if not identifier_setting.jalc_crossref_doi: identifier_setting.jalc_crossref_doi = text_empty if not identifier_setting.jalc_datacite_doi: identifier_setting.jalc_datacite_doi = text_empty if not identifier_setting.ndl_jalc_doi: identifier_setting.ndl_jalc_doi = text_empty temporary_identifier_select = 0 temporary_identifier_inputs = [] last_identifier_setting = activity.get_action_identifier_grant( activity_id=activity_id, action_id=action_id) if last_identifier_setting: temporary_identifier_select = last_identifier_setting.get( 'action_identifier_select') temporary_identifier_inputs.append( last_identifier_setting.get('action_identifier_jalc_doi')) temporary_identifier_inputs.append( last_identifier_setting.get('action_identifier_jalc_cr_doi')) temporary_identifier_inputs.append( last_identifier_setting.get('action_identifier_jalc_dc_doi')) temporary_identifier_inputs.append( last_identifier_setting.get('action_identifier_ndl_jalc_doi')) temporary_journal = activity.get_action_journal(activity_id=activity_id, action_id=action_id) if temporary_journal: temporary_journal = temporary_journal.action_journal cur_step = action_endpoint step_item_login_url = None approval_record = [] pid = None record = {} need_file = False need_billing_file = False json_schema = '' schema_form = '' item_save_uri = '' files = [] endpoints = {} links = None need_thumbnail = False files_thumbnail = [] allow_multi_thumbnail = False show_autofill_metadata = True is_hidden_pubdate_value = False item_type_name = get_item_type_name(workflow_detail.itemtype_id) if 'item_login' == action_endpoint or 'file_upload' == action_endpoint: activity_session = dict(activity_id=activity_id, action_id=activity_detail.action_id, action_version=cur_action.action_version, action_status=ActionStatusPolicy.ACTION_DOING, commond='') show_autofill_metadata = is_show_autofill_metadata(item_type_name) is_hidden_pubdate_value = is_hidden_pubdate(item_type_name) session['activity_info'] = activity_session # get item edit page info. step_item_login_url, need_file, need_billing_file, \ record, json_schema, schema_form,\ item_save_uri, files, endpoints, need_thumbnail, files_thumbnail, \ allow_multi_thumbnail \ = item_login(item_type_id=workflow_detail.itemtype_id) if item: # Remove the unused local variable # _pid_identifier = PersistentIdentifier.get_by_object( # pid_type='depid', object_type='rec', object_uuid=item.id) record = item sessionstore = RedisStore( redis.StrictRedis.from_url('redis://{host}:{port}/1'.format( host=os.getenv('INVENIO_REDIS_HOST', 'localhost'), port=os.getenv('INVENIO_REDIS_PORT', '6379')))) if sessionstore.redis.exists( 'updated_json_schema_{}'.format(activity_id)) \ and sessionstore.get( 'updated_json_schema_{}'.format(activity_id)): json_schema = (json_schema + "/{}").format(activity_id) # if 'approval' == action_endpoint: if item: # get record data for the first time access to editing item screen pid_identifier = PersistentIdentifier.get_by_object( pid_type='depid', object_type='rec', object_uuid=item.id) record_class = import_string('weko_deposit.api:WekoRecord') resolver = Resolver(pid_type='recid', object_type='rec', getter=record_class.get_record) pid, approval_record = resolver.resolve(pid_identifier.pid_value) files = to_files_js(approval_record) # get files data after click Save btn sessionstore = RedisStore( redis.StrictRedis.from_url('redis://{host}:{port}/1'.format( host=os.getenv('INVENIO_REDIS_HOST', 'localhost'), port=os.getenv('INVENIO_REDIS_PORT', '6379')))) if sessionstore.redis.exists('activity_item_' + str(activity_id)): item_str = sessionstore.get('activity_item_' + str(activity_id)) item_json = json.loads(item_str.decode('utf-8')) if 'files' in item_json: files = item_json.get('files') if not files: deposit = WekoDeposit.get_record(item.id) if deposit: files = to_files_js(deposit) if files and not files_thumbnail: files_thumbnail = [ i for i in files if 'is_thumbnail' in i.keys() and i['is_thumbnail'] ] from weko_deposit.links import base_factory links = base_factory(pid) res_check = check_authority_action(str(activity_id), str(action_id)) getargs = request.args ctx = {'community': None} community_id = "" if 'community' in getargs: comm = GetCommunity.get_community_by_id(request.args.get('community')) ctx = {'community': comm} community_id = comm.id # be use for index tree and comment page. if 'item_login' == action_endpoint or 'file_upload' == action_endpoint: session['itemlogin_id'] = activity_id session['itemlogin_activity'] = activity_detail session['itemlogin_item'] = item session['itemlogin_steps'] = steps session['itemlogin_action_id'] = action_id session['itemlogin_cur_step'] = cur_step session['itemlogin_record'] = approval_record session['itemlogin_histories'] = histories session['itemlogin_res_check'] = res_check session['itemlogin_pid'] = pid session['itemlogin_community_id'] = community_id from weko_theme.utils import get_design_layout # Get the design for widget rendering page, render_widgets = get_design_layout( community_id or current_app.config['WEKO_THEME_DEFAULT_COMMUNITY']) list_license = get_list_licence() if item and item.get('pid'): pid_without_ver = item['pid']['value'].split('.')[0] item_link = ItemLink.get_item_link_info(pid_without_ver) ctx['item_link'] = item_link return render_template( 'weko_workflow/activity_detail.html', page=page, render_widgets=render_widgets, activity=activity_detail, item=item, steps=steps, action_id=action_id, cur_step=cur_step, temporary_comment=temporary_comment, temporary_journal=temporary_journal, temporary_idf_grant=temporary_identifier_select, temporary_idf_grant_suffix=temporary_identifier_inputs, idf_grant_data=identifier_setting, idf_grant_input=IDENTIFIER_GRANT_LIST, idf_grant_method=IDENTIFIER_GRANT_SUFFIX_METHOD, record=approval_record, records=record, step_item_login_url=step_item_login_url, need_file=need_file, need_billing_file=need_billing_file, jsonschema=json_schema, schemaform=schema_form, id=workflow_detail.itemtype_id, item_save_uri=item_save_uri, files=files, endpoints=endpoints, error_type='item_login_error', links=links, histories=histories, res_check=res_check, pid=pid, community_id=community_id, need_thumbnail=need_thumbnail, files_thumbnail=files_thumbnail, allow_multi_thumbnail=allow_multi_thumbnail, enable_feedback_maillist=current_app. config['WEKO_WORKFLOW_ENABLE_FEEDBACK_MAIL'], enable_contributor=current_app. config['WEKO_WORKFLOW_ENABLE_CONTRIBUTOR'], show_automatic_metadata_input=show_autofill_metadata, is_hidden_pubdate=is_hidden_pubdate_value, list_license=list_license, **ctx)
def setup(): """Load query object for given database type.""" from werkzeug.utils import import_string from invenio.config import CFG_DATABASE_TYPE return import_string( 'invenio.legacy.dbquery_{0}'.format(CFG_DATABASE_TYPE))