flask_rdf-0.1.8/0000755002352500234200000000000012645053765015263 5ustar hufmanDomain Users00000000000000flask_rdf-0.1.8/README.rst0000644002352500234200000000426312645051276016752 0ustar hufmanDomain Users00000000000000Flask_rdf ========== A Flask decorator to output RDF using content negotiation. Apply the ``@flask_rdf`` decorator to a view function and return an rdflib Graph object. Flask_rdf will automatically format it into an RDF output format, depending on what the request's Accept header says. If the view function returns something besides an rdflib graph, it will be passed through without modification. Custom formats can be registered easily. After registering the new serializer with rdflib's plugin support, use the ``decide_format`` method to register a new mimetype request to use the new formatter. API --- - ``add_format`` Registers a new format to be recognized for content negotiation. It accepts arguments ``mimetype``, ``serialize_format``, and is used to add any custom rdflib serializer plugins to be used for the content negotiation. A third argument, requires_context, will restrict this serializer to only be used by graphs that are ``context_aware`` - ``decide_format`` Given an Accept header, return a (``mimetype``, ``format``) tuple that would best satisfy the client's request. A second argument, context_aware, may be used to allow formats that require a ``context_aware`` graph - ``flask_rdf`` Decorator for a Flask view function to use the Flask request's Accept header. It handles converting an rdflib Graph object to the proper Flask response, depending on the content negotiation. Other content is returned without modification. Example ------- .. code:: python #!/usr/bin/env python from rdflib import Graph, BNode, Literal, URIRef from rdflib.namespace import FOAF from flask import Flask from flask_rdf import flask_rdf import random app = Flask(__name__) @app.route('/') @app.route('/') @flask_rdf def random_age(path=''): graph = Graph('IOMemory', BNode()) graph.add((URIRef(path), FOAF.age, Literal(random.randint(20, 50)))) return graph if __name__ == '__main__': app.run(host='0.0.0.0', debug=True) .. image:: https://travis-ci.org/hufman/flask_rdf.svg?branch=master :alt: Build Status :target: https://travis-ci.org/hufman/flask_rdf flask_rdf-0.1.8/flask_rdf/0000755002352500234200000000000012645053765017216 5ustar hufmanDomain Users00000000000000flask_rdf-0.1.8/flask_rdf/__init__.py0000644002352500234200000000014312344242447021316 0ustar hufmanDomain Users00000000000000from .format import add_format, decide_format from .flask_decorator import flask_rdf, output_flask flask_rdf-0.1.8/flask_rdf/flask_decorator.py0000644002352500234200000000376412645046245022737 0ustar hufmanDomain Users00000000000000from .format import decide_format from flask import request, make_response def _get_graph(output): """ Given a Flask response, find the rdflib Graph """ if hasattr(output, 'serialize'): # single graph object return output if hasattr(output, '__getitem__'): # indexable tuple if len(output) > 0 and \ hasattr(output[0], 'serialize'): # graph object return output[0] def _set_graph(output, newgraph): """ Replace the rdflib Graph in a Flask response """ if hasattr(output, 'serialize'): # single graph object return newgraph if hasattr(output, '__getitem__'): # indexable tuple if len(output) > 0 and \ hasattr(output[0], 'serialize'): # graph object return (newgraph,) + output[1:] def output_flask(output, accepts): """ Formats a response from a Flask view to handle any RDF graphs If a view function returns a single RDF graph, serialize it based on Accept header If it's an RDF graph plus some extra headers, pass those along If it's not an RDF graph at all, return it without any special handling """ graph = _get_graph(output) if graph is not None: # decide the format output_mimetype, output_format = decide_format(accepts, graph.context_aware) if 'text' in output_mimetype: output_mimetype = output_mimetype + '; charset=utf-8' # format the new response serialized = graph.serialize(format=output_format) final_output = _set_graph(output, serialized) response = make_response(final_output) response.headers['Content-Type'] = output_mimetype return response else: return make_response(output) def flask_rdf(view): """ Wraps a Flask view function to return formatted RDF graphs Uses content negotiation to serialize the graph to the client-preferred format Passes other content through unmodified """ from flask import request, make_response from functools import wraps @wraps(view) def decorated(*args, **kwargs): output = view(*args, **kwargs) return output_flask(output, request.headers.get('Accept', '')) return decorated flask_rdf-0.1.8/flask_rdf/format.py0000644002352500234200000000320412645053103021041 0ustar hufmanDomain Users00000000000000import mimeparse # What formats we support for serialization formats = { 'application/x-turtle': 'turtle', 'text/turtle': 'turtle', 'application/rdf+xml': 'xml', 'application/trix': 'trix', 'application/n-quads': 'nquads', 'application/n-triples': 'nt', 'text/n-triples': 'nt', 'text/rdf+nt': 'nt', 'application/n3': 'n3', 'text/n3': 'n3', 'text/rdf+n3': 'n3' } # the list of any mimetypes, unlocked if we have a context all_mimetypes = list(formats.keys()) # the list of mimetypes that don't require a context ctxless_mimetypes = [m for m in all_mimetypes if 'n-quads' not in m] def add_format(mimetype, format, requires_context=False): """ Registers a new format to be used in a graph's serialize call If you've installed an rdflib serializer plugin, use this to add it to the content negotiation system Set requires_context=True if this format requires a context-aware graph """ global formats formats[mimetype] = format if not requires_context: ctxless_mimetypes.append(mimetype) all_mimetypes.append(mimetype) def decide_format(accepts, context_aware = False): """ Returns what (mimetype,format) the client wants to receive Parses the given Accept header and picks the best one that we know how to output Returns (mimetype, format) An unknown Accept will default to rdf+xml context_aware=True will allow nquad serialization """ if context_aware: mimetype = mimeparse.best_match(all_mimetypes, accepts) else: mimetype = mimeparse.best_match(ctxless_mimetypes, accepts) if mimetype: return (mimetype, formats[mimetype]) else: return ('application/rdf+xml', 'xml') flask_rdf-0.1.8/PKG-INFO0000644002352500234200000000720212645053765016361 0ustar hufmanDomain Users00000000000000Metadata-Version: 1.1 Name: flask_rdf Version: 0.1.8 Summary: Flask decorator to output RDF using content negotiation Home-page: https://github.com/hufman/flask_rdf Author: Walter Huf Author-email: hufman@gmail.com License: BSD Description: Flask_rdf ========== A Flask decorator to output RDF using content negotiation. Apply the ``@flask_rdf`` decorator to a view function and return an rdflib Graph object. Flask_rdf will automatically format it into an RDF output format, depending on what the request's Accept header says. If the view function returns something besides an rdflib graph, it will be passed through without modification. Custom formats can be registered easily. After registering the new serializer with rdflib's plugin support, use the ``decide_format`` method to register a new mimetype request to use the new formatter. API --- - ``add_format`` Registers a new format to be recognized for content negotiation. It accepts arguments ``mimetype``, ``serialize_format``, and is used to add any custom rdflib serializer plugins to be used for the content negotiation. A third argument, requires_context, will restrict this serializer to only be used by graphs that are ``context_aware`` - ``decide_format`` Given an Accept header, return a (``mimetype``, ``format``) tuple that would best satisfy the client's request. A second argument, context_aware, may be used to allow formats that require a ``context_aware`` graph - ``flask_rdf`` Decorator for a Flask view function to use the Flask request's Accept header. It handles converting an rdflib Graph object to the proper Flask response, depending on the content negotiation. Other content is returned without modification. Example ------- .. code:: python #!/usr/bin/env python from rdflib import Graph, BNode, Literal, URIRef from rdflib.namespace import FOAF from flask import Flask from flask_rdf import flask_rdf import random app = Flask(__name__) @app.route('/') @app.route('/') @flask_rdf def random_age(path=''): graph = Graph('IOMemory', BNode()) graph.add((URIRef(path), FOAF.age, Literal(random.randint(20, 50)))) return graph if __name__ == '__main__': app.run(host='0.0.0.0', debug=True) .. image:: https://travis-ci.org/hufman/flask_rdf.svg?branch=master :alt: Build Status :target: https://travis-ci.org/hufman/flask_rdf Platform: Any Classifier: Development Status :: 4 - Beta Classifier: Framework :: Flask Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content :: CGI Tools/Libraries Classifier: Topic :: Software Development :: Libraries :: Python Modules flask_rdf-0.1.8/flask_rdf.egg-info/0000755002352500234200000000000012645053765020710 5ustar hufmanDomain Users00000000000000flask_rdf-0.1.8/flask_rdf.egg-info/SOURCES.txt0000644002352500234200000000037612645053765022602 0ustar hufmanDomain Users00000000000000README.rst setup.py flask_rdf/__init__.py flask_rdf/flask_decorator.py flask_rdf/format.py flask_rdf.egg-info/PKG-INFO flask_rdf.egg-info/SOURCES.txt flask_rdf.egg-info/dependency_links.txt flask_rdf.egg-info/requires.txt flask_rdf.egg-info/top_level.txtflask_rdf-0.1.8/flask_rdf.egg-info/requires.txt0000644002352500234200000000003512645053761023302 0ustar hufmanDomain Users00000000000000Flask python-mimeparse==0.1.4flask_rdf-0.1.8/flask_rdf.egg-info/dependency_links.txt0000644002352500234200000000000112645053761024752 0ustar hufmanDomain Users00000000000000 flask_rdf-0.1.8/flask_rdf.egg-info/PKG-INFO0000644002352500234200000000720212645053761022002 0ustar hufmanDomain Users00000000000000Metadata-Version: 1.1 Name: flask-rdf Version: 0.1.8 Summary: Flask decorator to output RDF using content negotiation Home-page: https://github.com/hufman/flask_rdf Author: Walter Huf Author-email: hufman@gmail.com License: BSD Description: Flask_rdf ========== A Flask decorator to output RDF using content negotiation. Apply the ``@flask_rdf`` decorator to a view function and return an rdflib Graph object. Flask_rdf will automatically format it into an RDF output format, depending on what the request's Accept header says. If the view function returns something besides an rdflib graph, it will be passed through without modification. Custom formats can be registered easily. After registering the new serializer with rdflib's plugin support, use the ``decide_format`` method to register a new mimetype request to use the new formatter. API --- - ``add_format`` Registers a new format to be recognized for content negotiation. It accepts arguments ``mimetype``, ``serialize_format``, and is used to add any custom rdflib serializer plugins to be used for the content negotiation. A third argument, requires_context, will restrict this serializer to only be used by graphs that are ``context_aware`` - ``decide_format`` Given an Accept header, return a (``mimetype``, ``format``) tuple that would best satisfy the client's request. A second argument, context_aware, may be used to allow formats that require a ``context_aware`` graph - ``flask_rdf`` Decorator for a Flask view function to use the Flask request's Accept header. It handles converting an rdflib Graph object to the proper Flask response, depending on the content negotiation. Other content is returned without modification. Example ------- .. code:: python #!/usr/bin/env python from rdflib import Graph, BNode, Literal, URIRef from rdflib.namespace import FOAF from flask import Flask from flask_rdf import flask_rdf import random app = Flask(__name__) @app.route('/') @app.route('/') @flask_rdf def random_age(path=''): graph = Graph('IOMemory', BNode()) graph.add((URIRef(path), FOAF.age, Literal(random.randint(20, 50)))) return graph if __name__ == '__main__': app.run(host='0.0.0.0', debug=True) .. image:: https://travis-ci.org/hufman/flask_rdf.svg?branch=master :alt: Build Status :target: https://travis-ci.org/hufman/flask_rdf Platform: Any Classifier: Development Status :: 4 - Beta Classifier: Framework :: Flask Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content :: CGI Tools/Libraries Classifier: Topic :: Software Development :: Libraries :: Python Modules flask_rdf-0.1.8/flask_rdf.egg-info/top_level.txt0000644002352500234200000000001212645053761023427 0ustar hufmanDomain Users00000000000000flask_rdf flask_rdf-0.1.8/setup.py0000755002352500234200000000260012645052114016761 0ustar hufmanDomain Users00000000000000#!/usr/bin/env python from distutils.core import setup try: from setuptools import setup except: pass requirements = ['Flask', 'python-mimeparse==0.1.4'] test_requirements = ['nose==1.3.3', 'rdflib'] long_description = open('README.rst').read() setup(name='flask_rdf', version='0.1.8', description='Flask decorator to output RDF using content negotiation', author='Walter Huf', author_email='hufman@gmail.com', url='https://github.com/hufman/flask_rdf', packages=['flask_rdf'], install_requires=requirements, test_requires=requirements + test_requirements, long_description=long_description, classifiers=[ 'Development Status :: 4 - Beta', 'Framework :: Flask', 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Topic :: Internet :: WWW/HTTP :: Dynamic Content :: CGI Tools/Libraries', 'Topic :: Software Development :: Libraries :: Python Modules' ], license='BSD', platforms=['Any'] ) flask_rdf-0.1.8/setup.cfg0000644002352500234200000000007312645053765017104 0ustar hufmanDomain Users00000000000000[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0