<s> from __future__ import unicode_literals <EOL> import glob <EOL> import os . path <EOL> import simplejson <EOL> from bravado_core . spec import Spec , build_http_handlers <EOL> from six . moves . urllib import parse as urlparse <EOL> from . api import build_swagger_12_endpoints <EOL> from . load_schema import load_schema <EOL> from . model import SwaggerSchema <EOL> from . spec import API_DOCS_FILENAME , validate_swagger_schema <EOL> class ResourceListingNotFoundError ( Exception ) : <EOL> pass <EOL> class ApiDeclarationNotFoundError ( Exception ) : <EOL> pass <EOL> class ResourceListingGenerationError ( Exception ) : <EOL> pass <EOL> def find_resource_names ( api_docs_json ) : <EOL> return [ api [ '<STR_LIT:path>' ] . lstrip ( '<STR_LIT:/>' ) for api in api_docs_json [ '<STR_LIT>' ] ] <EOL> def find_resource_paths ( schema_dir ) : <EOL> """<STR_LIT>""" <EOL> def not_api_doc_file ( filename ) : <EOL> return not filename . endswith ( API_DOCS_FILENAME ) <EOL> def not_swagger_dot_json ( filename ) : <EOL> return not os . path . basename ( filename ) == '<STR_LIT>' <EOL> def filename_to_path ( filename ) : <EOL> filename , _ext = os . path . splitext ( os . path . basename ( filename ) ) <EOL> return '<STR_LIT:/>' + filename <EOL> filenames = glob . glob ( '<STR_LIT>' . format ( schema_dir ) ) <EOL> return map ( filename_to_path , <EOL> filter ( not_swagger_dot_json , <EOL> filter ( not_api_doc_file , sorted ( filenames ) ) ) ) <EOL> def build_schema_mapping ( schema_dir , resource_listing ) : <EOL> """<STR_LIT>""" <EOL> def resource_name_to_filepath ( name ) : <EOL> return os . path . join ( schema_dir , '<STR_LIT>' . format ( name ) ) <EOL> return dict ( <EOL> ( resource , resource_name_to_filepath ( resource ) ) <EOL> for resource in find_resource_names ( resource_listing ) <EOL> ) <EOL> def _load_resource_listing ( resource_listing ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> with open ( resource_listing ) as resource_listing_file : <EOL> return simplejson . load ( resource_listing_file ) <EOL> except IOError : <EOL> raise ResourceListingNotFoundError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( resource_listing , API_DOCS_FILENAME ) <EOL> ) <EOL> def generate_resource_listing ( schema_dir , listing_base ) : <EOL> if '<STR_LIT>' in listing_base : <EOL> raise ResourceListingGenerationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( schema_dir , API_DOCS_FILENAME ) ) <EOL> return dict ( <EOL> listing_base , <EOL> apis = [ { '<STR_LIT:path>' : path } for path in find_resource_paths ( schema_dir ) ] <EOL> ) <EOL> def get_resource_listing ( schema_dir , should_generate_resource_listing ) : <EOL> """<STR_LIT>""" <EOL> listing_filename = os . path . join ( schema_dir , API_DOCS_FILENAME ) <EOL> resource_listing = _load_resource_listing ( listing_filename ) <EOL> if not should_generate_resource_listing : <EOL> return resource_listing <EOL> return generate_resource_listing ( schema_dir , resource_listing ) <EOL> def compile_swagger_schema ( schema_dir , resource_listing ) : <EOL> """<STR_LIT>""" <EOL> mapping = build_schema_mapping ( schema_dir , resource_listing ) <EOL> resource_validators = ingest_resources ( mapping , schema_dir ) <EOL> endpoints = list ( build_swagger_12_endpoints ( resource_listing , mapping ) ) <EOL> return SwaggerSchema ( endpoints , resource_validators ) <EOL> def get_swagger_schema ( settings ) : <EOL> """<STR_LIT>""" <EOL> schema_dir = settings . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> resource_listing = get_resource_listing ( <EOL> schema_dir , <EOL> settings . get ( '<STR_LIT>' , False ) <EOL> ) <EOL> if settings . get ( '<STR_LIT>' , True ) : <EOL> validate_swagger_schema ( schema_dir , resource_listing ) <EOL> return compile_swagger_schema ( schema_dir , resource_listing ) <EOL> def get_swagger_spec ( settings ) : <EOL> """<STR_LIT>""" <EOL> schema_dir = settings . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> schema_filename = settings . get ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> schema_path = os . path . join ( schema_dir , schema_filename ) <EOL> schema_url = urlparse . urljoin ( '<STR_LIT>' , os . path . abspath ( schema_path ) ) <EOL> handlers = build_http_handlers ( None ) <EOL> file_handler = handlers [ '<STR_LIT:file>' ] <EOL> spec_dict = file_handler ( schema_url ) <EOL> return Spec . from_dict ( <EOL> spec_dict , <EOL> config = create_bravado_core_config ( settings ) , <EOL> origin_url = schema_url ) <EOL> def create_bravado_core_config ( settings ) : <EOL> """<STR_LIT>""" <EOL> config_keys = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> bravado_core_config_defaults = { <EOL> '<STR_LIT>' : False <EOL> } <EOL> return dict ( bravado_core_config_defaults , ** dict ( <EOL> ( bravado_core_key , settings [ pyramid_swagger_key ] ) <EOL> for pyramid_swagger_key , bravado_core_key in config_keys . items ( ) <EOL> if pyramid_swagger_key in settings ) ) <EOL> def ingest_resources ( mapping , schema_dir ) : <EOL> """<STR_LIT>""" <EOL> ingested_resources = [ ] <EOL> for name , filepath in mapping . items ( ) : <EOL> try : <EOL> ingested_resources . append ( load_schema ( filepath ) ) <EOL> except IOError : <EOL> raise ApiDeclarationNotFoundError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( filepath , name , schema_dir ) <EOL> ) <EOL> return ingested_resources </s>
