def __parser_for_url(url, resolve, backend, strict, encoding): # noqa: N802 """Return a parser instance for the URL and the given parameters.""" # Try the URL formatted = click.format_filename(url) click.echo('Processing "%s"...' % (formatted, )) from prance.util import fs fsurl = fs.abspath(url) import os.path if os.path.exists(fs.from_posix(fsurl)): url = fsurl # Create parser to use parser = None if resolve: click.echo(' -> Resolving external references.') parser = prance.ResolvingParser(url, lazy = True, backend = backend, strict = strict, encoding = encoding) else: click.echo(' -> Not resolving external references.') parser = prance.BaseParser(url, lazy = True, backend = backend, strict = strict, encoding = encoding) # XXX maybe enable tihs in debug mode or something. # click.echo(' -> Using backend: {0.backend}'.format(parser)) return parser, formatted
def __parser_for_url(url, resolve, backend, strict): # noqa: N802 """Return a parser instance for the URL and the given parameters.""" # Try the URL formatted = click.format_filename(url) click.echo('Processing "%s"...' % (formatted, )) from prance.util import fs fsurl = fs.abspath(url) import os.path if os.path.exists(fs.from_posix(fsurl)): url = fsurl # Create parser to use if resolve: click.echo(' -> Resolving external references.') return prance.ResolvingParser(url, lazy=True, backend=backend, strict=strict), formatted else: click.echo(' -> Not resolving external references.') return prance.BaseParser(url, lazy=True, backend=backend, strict=strict), formatted
def init_app_common(cfg: Type[Config] = Config, is_csm: bool = False) -> Flask: """ Initializes common Flask parts, e.g. CORS, configuration, database, migrations and custom corpora.""" spec_dir: str = Config.CSM_DIRECTORY if is_csm else Config.MC_SERVER_DIRECTORY connexion_app: FlaskApp = connexion.FlaskApp( __name__, port=(cfg.CORPUS_STORAGE_MANAGER_PORT if is_csm else cfg.HOST_PORT), specification_dir=spec_dir) spec_path: str = Config.API_SPEC_CSM_FILE_PATH if is_csm else Config.API_SPEC_MCSERVER_FILE_PATH parser = prance.ResolvingParser( spec_path, lazy=True, strict=False) # str(Path(spec_path).absolute()) parser.parse() connexion_app.add_api(parser.specification) apply_event_handlers(connexion_app) app: Flask = connexion_app.app # allow CORS requests for all API routes CORS(app) # , resources=r"/*" app.config.from_object(cfg) app.app_context().push() db.init_app(app) migrate.init_app(app, db) if is_csm or cfg.TESTING: db.create_all() if is_csm: from mcserver.app.services.databaseService import DatabaseService DatabaseService.init_db_alembic() from mcserver.app.services.textService import TextService TextService.init_proper_nouns_list() TextService.init_stop_words_latin() if is_csm: full_init(app, cfg) return app
def validate(resolve, strict, output_file, urls): """ Validate the given spec or specs. If the --resolve option is set, references will be resolved before validation. Note that this can yield unexpected results. The swagger-spec-validator used as a basis for prance cannot deal with relative file path references, and will report them. However, resolving these references before validation will skip these errors. If the --output-file option is given, the validated spec is written to that file. Please note that with that option given, only one input file may be specified. """ # Ensure that when an output file is given, only one input file exists. if output_file and len(urls) > 1: raise click.UsageError('If --output-file is given, only one input URL ' 'is allowed!') import os.path from prance.util import fs # Process files for url in urls: formatted = click.format_filename(url) click.echo('Processing "%s"...' % (formatted, )) fsurl = fs.abspath(url) if os.path.exists(fs.from_posix(fsurl)): url = fsurl # Create parser to use if resolve: click.echo(' -> Resolving external references.') parser = prance.ResolvingParser(url, lazy=True, strict=strict) else: click.echo(' -> Not resolving external references.') parser = prance.BaseParser(url, lazy=True, strict=strict) # Try parsing from prance.util.url import ResolutionError from swagger_spec_validator.common import SwaggerValidationError try: parser.parse() except (ResolutionError, SwaggerValidationError) as err: msg = 'ERROR in "%s" [%s]: %s' % (formatted, type(err).__name__, str(err)) click.secho(msg, err=True, fg='red') import sys sys.exit(1) # All good, next file. click.echo('Validates OK as Swagger/OpenAPI 2.0!') # If an output file is given, write the specs to it. if output_file: __write_to_file(output_file, parser.specification)
def get_bundled_specs(main_file: Path) -> Dict[str, Any]: """ Get bundled specs :param main_file: Swagger file path :return: """ parser = prance.ResolvingParser(str(main_file.absolute()), lazy=True, backend="openapi-spec-validator") parser.parse() return parser.specification
def aggregate_specs(main_file: Path) -> Dict[str, Any]: """This function glues all seperate API Spec YML files together. This enales we keep a set of small YML files while being able to use something like $ref: 'another.yaml#/components/schemas/Foo' in the YML files. """ parser = prance.ResolvingParser(str(main_file.absolute()), lazy=True, strict=True) parser.parse() return parser.specification
def bundled(spec_file: str) -> Dict[str, Any]: """ Resolve multi-file OpenAPI specifications. See https://github.com/zalando/connexion/issues/254 """ path = Path(__file__, "../../openapi", spec_file).resolve().absolute() parser = prance.ResolvingParser(str(path), strict=True) parser.parse() return parser.specification
def create_app(): """Create Connexion/Flask application.""" root = os.path.dirname(rhub.__path__[0]) connexion_app = connexion.App(__name__) flask_app = connexion_app.app flask_app.url_map.strict_slashes = False if os.getenv('PROMETHEUS_MULTIPROC_DIR'): GunicornInternalPrometheusMetrics(flask_app) from . import _config flask_app.config.from_object(_config) parser = prance.ResolvingParser( os.path.join(root, 'openapi', 'openapi.yml')) connexion_app.add_api( parser.specification, validate_responses=True, strict_validation=True, pythonic_params=True, ) # Enable CORS (Cross-Origin Resource Sharing) # https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS CORS(flask_app) flask_app.cli.add_command(init_command) db.init_app(flask_app) migrate.init_app(flask_app, db) try: import coloredlogs coloredlogs.install(level=flask_app.config['LOG_LEVEL']) except ImportError: logging.basicConfig(level=flask_app.config['LOG_LEVEL']) RHUB_RETURN_INITIAL_FLASK_APP = os.getenv('RHUB_RETURN_INITIAL_FLASK_APP', 'False') if str(RHUB_RETURN_INITIAL_FLASK_APP).lower() == 'true': return flask_app FlaskInjector( app=flask_app, injector=di, modules=[ KeycloakModule(flask_app), VaultModule(flask_app), SchedulerModule(flask_app), ], ) # Try to retrieve Tower notification webhook creds from vault try: with flask_app.app_context(): vault = di.get(Vault) webhookCreds = vault.read(flask_app.config['WEBHOOK_VAULT_PATH']) if webhookCreds: flask_app.config['WEBHOOK_USER'] = webhookCreds['username'] flask_app.config['WEBHOOK_PASS'] = webhookCreds['password'] else: raise Exception( 'Missing tower webhook notification credentials; ' f'{vault} {flask_app.config["WEBHOOK_VAULT_PATH"]}') except Exception as e: logger.error( f'Failed to load {flask_app.config["WEBHOOK_VAULT_PATH"]} tower' f' webhook notification credentials {e!s}.') return flask_app
def load(path): global _swagger_spec _swagger_spec = prance.ResolvingParser(path).specification return _swagger_spec
def get_spec(path_to_spec: Path): parser = prance.ResolvingParser(str(path_to_spec.resolve())) parser.parse() return parser.specification
def load(path): spec = prance.ResolvingParser(path).specification return apply(spec)
def get_bundled_specs(main_file: Path) -> Dict[str, Any]: parser = prance.ResolvingParser(str(main_file.absolute()), lazy = True, strict = True) parser.parse() return parser.specification
def get_bundled_specs(main_file: Path) -> Dict[str, Any]: parser = prance.ResolvingParser(str(main_file.absolute()), lazy=False, backend="openapi-spec-validator") parser.parse() return parser.specification
def __init__(self, uri: str) -> None: self._resolver = prance.ResolvingParser(uri, backend=OPENAPI_SPEC_VALIDATOR, strict=False, lazy=True)
def get_bundled_specs(main_file): parser = prance.ResolvingParser(url=str(main_file.absolute()), lazy=True, backend='openapi-spec-validator') parser.parse() return parser.specification