jsonhyperschema-codec-1.0.3/0000755000076500000240000000000013245276432017455 5ustar tomchristiestaff00000000000000jsonhyperschema-codec-1.0.3/PKG-INFO0000644000076500000240000000105413245276432020552 0ustar tomchristiestaff00000000000000Metadata-Version: 1.1 Name: jsonhyperschema-codec Version: 1.0.3 Summary: A JSON Hyper-Schema codec for Core API. Home-page: http://github.com/core-api/python-jsonhyperschema-codec/ Author: Tom Christie Author-email: tom@tomchristie.com License: BSD Description-Content-Type: UNKNOWN Description: UNKNOWN Platform: UNKNOWN Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 3 jsonhyperschema-codec-1.0.3/jsonhyperschema_codec/0000755000076500000240000000000013245276432024014 5ustar tomchristiestaff00000000000000jsonhyperschema-codec-1.0.3/jsonhyperschema_codec/__init__.py0000644000076500000240000000733713245276422026136 0ustar tomchristiestaff00000000000000# coding: utf-8 from coreapi.codecs.base import BaseCodec from coreapi.compat import urlparse from coreapi.document import Document, Link, Field from coreapi.exceptions import ParseError from jsonhyperschema_codec.utils import _get_string, _get_list, _get_dict, get_dicts, _dereference import json import uritemplate try: from urllib.parse import unquote except ImportError: from urllib import unquote __version__ = "1.0.3" def _get_content(data, base_url, ref): content = {} links = _get_list(data, 'links') properties = _get_dict(data, 'properties') if properties: for key, value in properties.items(): if not isinstance(value, dict): continue if list(value.keys()) == ['$ref']: value = _dereference(value['$ref'], ref) sub_content = _get_content(value, base_url, ref) if sub_content: content[key] = sub_content if links: for link in get_dicts(links): rel = _get_string(link, 'rel') if rel: href = _get_string(link, 'href') method = _get_string(link, 'method') schema = _get_dict(link, 'schema') schema_type = _get_list(schema, 'type') schema_properties = _get_dict(schema, 'properties') schema_required = _get_list(schema, 'required') fields = [] url = urlparse.urljoin(base_url, href) templated = uritemplate.variables(url) for item in templated: orig = item if item.startswith('(') and item.endswith(')'): item = unquote(item.strip('(').rstrip(')')) if item.startswith('#/'): components = [ component for component in item.strip('#/').split('/') if component != 'definitions' ] item = '_'.join(components).replace('-', '_') url = url.replace(orig, item) fields.append(Field(name=item, location='path', required=True)) if schema_type == ['object'] and schema_properties: fields += [ Field(name=key, required=(key in schema_required)) for key in schema_properties.keys() ] if rel == 'self': rel = 'read' content[rel] = Link(url=url, action=method, fields=fields) return content def _primative_to_document(data, base_url): url = base_url # Determine if the document contains a self URL. links = _get_list(data, 'links') for link in get_dicts(links): href = _get_string(link, 'href') rel = _get_string(link, 'rel') if rel == 'self' and href: url = urlparse.urljoin(url, href) # Load the document content. title = _get_string(data, 'title') content = _get_content(data, url, ref=data) return Document(title=title, url=url, content=content) class JSONHyperSchemaCodec(BaseCodec): """ JSON Hyper-Schema. """ media_type = 'application/schema+json' supports = ['decoding'] def load(self, bytes, **kwargs): """ Takes a bytestring and returns a document. """ base_url = kwargs.get('base_url', None) try: data = json.loads(bytes.decode('utf-8')) except ValueError as exc: raise ParseError('Malformed JSON. %s' % exc) doc = _primative_to_document(data, base_url) if not (isinstance(doc, Document)): raise ParseError('Top level node must be a document.') return doc jsonhyperschema-codec-1.0.3/jsonhyperschema_codec/utils.py0000644000076500000240000000122412741654561025530 0ustar tomchristiestaff00000000000000from coreapi.compat import string_types def _get_string(item, key): value = item.get(key) if isinstance(value, string_types): return value return '' def _get_dict(item, key): value = item.get(key) if isinstance(value, dict): return value return {} def _get_list(item, key): value = item.get(key) if isinstance(value, list): return value return [] def get_dicts(item): return [value for value in item if isinstance(value, dict)] def _dereference(value, ref): keys = value.strip('#/').split('/') node = ref for key in keys: node = _get_dict(node, key) return node jsonhyperschema-codec-1.0.3/MANIFEST.in0000644000076500000240000000010512741654561021212 0ustar tomchristiestaff00000000000000global-exclude __pycache__ global-exclude *.pyc global-exclude *.pyo jsonhyperschema-codec-1.0.3/README.md0000644000076500000240000000100112741670307020723 0ustar tomchristiestaff00000000000000# JSON Hyper-Schema Codec **A JSON Hyper-Schema codec for Core API.** [![travis-image]][travis] [![pypi-image]][pypi] ## Installation Install using pip: $ pip install jsonhyperschema-codec [travis-image]: https://secure.travis-ci.org/core-api/python-jsonhyperschema-codec.svg?branch=master [travis]: http://travis-ci.org/core-api/python-jsonhyperschema-codec?branch=master [pypi-image]: https://img.shields.io/pypi/v/jsonhyperschema-codec.svg [pypi]: https://pypi.python.org/pypi/jsonhyperschema-codec jsonhyperschema-codec-1.0.3/setup.py0000755000076500000240000000431412741672403021172 0ustar tomchristiestaff00000000000000#!/usr/bin/env python # -*- coding: utf-8 -*- from setuptools import setup import re import os import sys def get_version(package): """ Return package version as listed in `__version__` in `init.py`. """ init_py = open(os.path.join(package, '__init__.py')).read() return re.search("__version__ = ['\"]([^'\"]+)['\"]", init_py).group(1) def get_packages(package): """ Return root package and all sub-packages. """ return [dirpath for dirpath, dirnames, filenames in os.walk(package) if os.path.exists(os.path.join(dirpath, '__init__.py'))] def get_package_data(package): """ Return all files under the root package, that are not in a package themselves. """ walk = [(dirpath.replace(package + os.sep, '', 1), filenames) for dirpath, dirnames, filenames in os.walk(package) if not os.path.exists(os.path.join(dirpath, '__init__.py'))] filepaths = [] for base, filenames in walk: filepaths.extend([os.path.join(base, filename) for filename in filenames]) return {package: filepaths} version = get_version('jsonhyperschema_codec') if sys.argv[-1] == 'publish': os.system("python setup.py sdist upload") print("You probably want to also tag the version now:") print(" git tag -a %s -m 'version %s'" % (version, version)) print(" git push --tags") sys.exit() setup( name='jsonhyperschema-codec', version=version, url='http://github.com/core-api/python-jsonhyperschema-codec/', license='BSD', description='A JSON Hyper-Schema codec for Core API.', author='Tom Christie', author_email='tom@tomchristie.com', packages=get_packages('jsonhyperschema_codec'), package_data=get_package_data('jsonhyperschema_codec'), install_requires=['coreapi'], classifiers=[ 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Python', 'Programming Language :: Python :: 3', ], entry_points={ 'coreapi.codecs': [ 'jsonhyperschema=jsonhyperschema_codec:JSONHyperSchemaCodec', ] } ) jsonhyperschema-codec-1.0.3/setup.cfg0000644000076500000240000000004613245276432021276 0ustar tomchristiestaff00000000000000[egg_info] tag_build = tag_date = 0 jsonhyperschema-codec-1.0.3/jsonhyperschema_codec.egg-info/0000755000076500000240000000000013245276432025506 5ustar tomchristiestaff00000000000000jsonhyperschema-codec-1.0.3/jsonhyperschema_codec.egg-info/PKG-INFO0000644000076500000240000000105413245276432026603 0ustar tomchristiestaff00000000000000Metadata-Version: 1.1 Name: jsonhyperschema-codec Version: 1.0.3 Summary: A JSON Hyper-Schema codec for Core API. Home-page: http://github.com/core-api/python-jsonhyperschema-codec/ Author: Tom Christie Author-email: tom@tomchristie.com License: BSD Description-Content-Type: UNKNOWN Description: UNKNOWN Platform: UNKNOWN Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 3 jsonhyperschema-codec-1.0.3/jsonhyperschema_codec.egg-info/SOURCES.txt0000644000076500000240000000055713245276432027401 0ustar tomchristiestaff00000000000000MANIFEST.in README.md setup.py jsonhyperschema_codec/__init__.py jsonhyperschema_codec/utils.py jsonhyperschema_codec.egg-info/PKG-INFO jsonhyperschema_codec.egg-info/SOURCES.txt jsonhyperschema_codec.egg-info/dependency_links.txt jsonhyperschema_codec.egg-info/entry_points.txt jsonhyperschema_codec.egg-info/requires.txt jsonhyperschema_codec.egg-info/top_level.txtjsonhyperschema-codec-1.0.3/jsonhyperschema_codec.egg-info/entry_points.txt0000644000076500000240000000011713245276432031003 0ustar tomchristiestaff00000000000000[coreapi.codecs] jsonhyperschema = jsonhyperschema_codec:JSONHyperSchemaCodec jsonhyperschema-codec-1.0.3/jsonhyperschema_codec.egg-info/requires.txt0000644000076500000240000000001013245276432030075 0ustar tomchristiestaff00000000000000coreapi jsonhyperschema-codec-1.0.3/jsonhyperschema_codec.egg-info/top_level.txt0000644000076500000240000000002613245276432030236 0ustar tomchristiestaff00000000000000jsonhyperschema_codec jsonhyperschema-codec-1.0.3/jsonhyperschema_codec.egg-info/dependency_links.txt0000644000076500000240000000000113245276432031554 0ustar tomchristiestaff00000000000000