Skip to content

Commit

Permalink
A small refactor of ingest.py to flatten out the calls from compile_s…
Browse files Browse the repository at this point in the history
…wagger_schema
  • Loading branch information
Daniel Nephin committed Feb 20, 2015
1 parent 896d7a1 commit 2b092b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 60 deletions.
77 changes: 27 additions & 50 deletions pyramid_swagger/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,45 @@ class ApiDeclarationNotFoundError(Exception):


def find_resource_names(api_docs_json):
return [
api['path'].lstrip('/')
for api in api_docs_json['apis']
]
return [api['path'].lstrip('/') for api in api_docs_json['apis']]


def build_schema_mapping(schema_dir):
def build_schema_mapping(schema_dir, listing_json):
"""Discovers schema file locations and relations.
:param schema_dir: the directory schema files live inside
:type schema_dir: string
:returns: A tuple of (resource listing filepath, mapping) where the mapping
is between resource name and file path
:rtype: (string, dict)
:param listing_json: the contents of the listing document
:type listing_json: string
:returns: a mapping from resource name to file path
:rtype: dict
"""
def resource_name_to_filepath(name):
return os.path.join(schema_dir, '{0}.json'.format(name))

listing, listing_json = _load_resource_listing(schema_dir)

return (
listing,
dict(
(resource, resource_name_to_filepath(resource))
for resource in find_resource_names(listing_json)
)
return dict(
(resource, resource_name_to_filepath(resource))
for resource in find_resource_names(listing_json)
)


def _load_resource_listing(schema_dir):
def _load_resource_listing(resource_listing):
"""Load the resource listing from file, handling errors.
:param schema_dir: the directory schema files live inside
:type schema_dir: string
:returns: (resource listing filepath, resource listing json)
:param resource_listing: path to the api-docs resource listing file
:type resource_listing: string
:returns: contents of the resource listing file
:rtype: dict
"""
resource_listing = os.path.join(schema_dir, API_DOCS_FILENAME)
try:
with open(resource_listing) as resource_listing_file:
resource_listing_json = simplejson.load(resource_listing_file)
return simplejson.load(resource_listing_file)
# If not found, raise a more user-friendly error.
except IOError:
raise ResourceListingNotFoundError(
'No resource listing found at {0}. Note that your json file '
'must be named {1}'.format(resource_listing, API_DOCS_FILENAME)
)
return resource_listing, resource_listing_json


def compile_swagger_schema(schema_dir, should_validate_schemas):
Expand All @@ -78,35 +70,27 @@ def compile_swagger_schema(schema_dir, should_validate_schemas):
:type should_validate_schemas: boolean
:returns: a SwaggerSchema object
"""
listing, mapping = build_schema_mapping(schema_dir)
schema_resolvers = ingest_resources(
listing,
mapping,
schema_dir,
should_validate_schemas,
)
return SwaggerSchema(
listing,
mapping,
schema_resolvers,
)
listing_filename = os.path.join(schema_dir, API_DOCS_FILENAME)
listing_json = _load_resource_listing(listing_filename)
mapping = build_schema_mapping(schema_dir, listing_json)
schema_resolvers = ingest_resources(mapping, schema_dir)

if should_validate_schemas:
validate_swagger_schemas(listing_filename, mapping.values())

return SwaggerSchema(listing_filename, mapping, schema_resolvers)

def ingest_resources(listing, mapping, schema_dir, should_validate_schemas):

def ingest_resources(mapping, schema_dir):
"""Consume the Swagger schemas and produce a queryable datastructure.
:param listing: Filepath to a resource listing
:type listing: string
:param mapping: Map from resource name to filepath of its api declaration
:type mapping: dict
:param schema_dir: the directory schema files live inside
:type schema_dir: string
:param should_validate_schemas: if True, check schemas for correctness
:type should_validate_schemas: boolean
:returns: A list of SchemaAndResolver objects
:returns: A list of :class:`pyramid_swagger.load_schema.SchemaAndResolver`
objects
"""
resource_filepaths = mapping.values()

ingested_resources = []
for name, filepath in mapping.items():
try:
Expand All @@ -120,11 +104,4 @@ def ingest_resources(listing, mapping, schema_dir, should_validate_schemas):
'your resource name and API declaration file do not '
'match?'.format(filepath, name, schema_dir)
)

if should_validate_schemas:
validate_swagger_schemas(
listing,
resource_filepaths
)

return ingested_resources
1 change: 0 additions & 1 deletion pyramid_swagger/tween.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def validator_tween(request):


def load_settings(registry):
# By default, assume cwd contains the swagger schemas.
schema_dir = registry.settings.get(
'pyramid_swagger.schema_directory',
'api_docs/'
Expand Down
12 changes: 3 additions & 9 deletions tests/ingest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,17 @@


def test_proper_error_on_missing_resource_listing():
filename = 'tests/sample_schemas/missing_resource_listing/api_docs.json'
with pytest.raises(ResourceListingNotFoundError) as exc:
_load_resource_listing(
'tests/sample_schemas/missing_resource_listing',
)
assert(
'tests/sample_schemas/missing_resource_listing/api_docs.json'
in str(exc)
)
_load_resource_listing(filename)
assert filename in str(exc)
assert 'must be named {0}'.format(API_DOCS_FILENAME) in str(exc)


def test_proper_error_on_missing_api_declaration():
with pytest.raises(ApiDeclarationNotFoundError) as exc:
ingest_resources(
'fake/fake_resource_listing.json',
{'sample_resource': 'fake/sample_resource.json'},
'fake',
False,
)
assert 'fake/sample_resource.json' in str(exc)

0 comments on commit 2b092b1

Please sign in to comment.