Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 port #8

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0.2
: Port to Python 3
0.1
: Original LOD application
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

rdflib-web
==========
Two RDFLib web-apps on top of the Flask web-microframework:
Expand All @@ -13,22 +12,33 @@ Documentation on ReadTheDocs: http://rdflib-web.readthedocs.org/en/latest/
Installation
------------

Stable version:
```bash
pip install rdflib-web
pip3 install https://github.com/RDFLib/rdflib-web/archive/master.zip
```

or most recent:
Running
-------
Run after installing with pip:

```bash
pip install https://github.com/RDFLib/rdflib-web/archive/master.zip
rdflodapp <RDF-file>
rdfsparqlapp <RDF-file>
```

or run dev server in cloned instance:

```bash
FLASK_APP=rdflib_web FLASK_ENV=development flask run
```

Requirements
------------

These are installed automatically if you install with pip

* For the Web-apps: Flask, http://flask.pocoo.org/
* (which in turn requires Jinja2 and Werkzeug)
* For correct content-negotiation: mimeparse (fallback without conneg)
* For correct content-negotiation: python-mimeparse (fallback without conneg)

Status
------
See [TODO file](TODO.txt).
5 changes: 5 additions & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
2021-03-24 FIXME: the star-resource picker is not working (after python3 port)
2021-03-24 FIXME: rdflib, flask and python-mimeparse where not installed automatically with pip3
2021-04-15 TODO: review <setup.old> (Py2.7 setup.py) and remove

# ex:ft=todo.txt:
16 changes: 8 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
master_doc = 'index'

# General information about the project.
project = u'rdflib-web'
copyright = u'2013, RDFLib Team'
project = 'rdflib-web'
copyright = '2013, RDFLib Team'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down Expand Up @@ -194,8 +194,8 @@ def find_version(filename):
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
('index', 'rdflib-web.tex', u'rdflib-web Documentation',
u'RDFLib Team', 'manual'),
('index', 'rdflib-web.tex', 'rdflib-web Documentation',
'RDFLib Team', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -224,8 +224,8 @@ def find_version(filename):
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'rdflib-web', u'rdflib-web Documentation',
[u'RDFLib Team'], 1)
('index', 'rdflib-web', 'rdflib-web Documentation',
['RDFLib Team'], 1)
]

# If true, show URL addresses after external links.
Expand All @@ -238,8 +238,8 @@ def find_version(filename):
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'rdflib-web', u'rdflib-web Documentation',
u'RDFLib Team', 'rdflib-web', 'One line description of project.',
('index', 'rdflib-web', 'rdflib-web Documentation',
'RDFLib Team', 'rdflib-web', 'One line description of project.',
'Miscellaneous'),
]

Expand Down
7 changes: 6 additions & 1 deletion rdflib_web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@
"""


__version__ = "0.1"
__version__ = "0.2"

from . import lod
from . import bookdb

app = lod.get(bookdb.bookdb)
6 changes: 3 additions & 3 deletions rdflib_web/bookdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"""

import rdflib
import StringIO
import io

bookrdf="""
bookrdf=b"""
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix book: <http://example.org/book/> .
Expand Down Expand Up @@ -70,4 +70,4 @@
"""

bookdb=rdflib.Graph()
bookdb.parse(data=bookrdf,format='n3')
bookdb.parse(data=bookrdf.decode('utf-8'),format='n3')
2 changes: 1 addition & 1 deletion rdflib_web/caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def wrapper(*args, **kwds):
# purge least frequently used cache entry
if len(cache) > maxsize:
for key, _ in nsmallest(maxsize // 10,
use_count.iteritems(),
iter(use_count.items()),
key=itemgetter(1)):
try:
del cache[key], use_count[key]
Expand Down
13 changes: 7 additions & 6 deletions rdflib_web/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import time
import traceback

import mimeutils
from . import mimeutils

from rdflib_web import htmlresults
from rdflib_web import __version__
Expand Down Expand Up @@ -88,7 +88,7 @@ def query():

results=g.generic.ds.query(q).serialize(format=format)
if format=='html':
response=make_response(render_template("results.html", results=Markup(unicode(results,"utf-8")), q=q))
response=make_response(render_template("results.html", results=Markup(str(results,"utf-8")), q=q))
else:
response=make_response(results)

Expand All @@ -104,7 +104,7 @@ def graph_store_do(graph_identifier):
if mimetype == "multipart/form-data":
body = []
force_mimetype = args.get('mimetype')
for _, data_file in request.files.items():
for _, data_file in list(request.files.items()):
data = data_file.read()
mt = force_mimetype or data_file.mimetype or rdflib.guess_format(data_file.filename)
body.append({'data': data, 'mimetype': mt})
Expand All @@ -119,7 +119,7 @@ def graph_store_do(graph_identifier):
code, headers, body = result

response = make_response(body or '', code)
for k, v in headers.items():
for k, v in list(headers.items()):
response.headers[k] = v
return response

Expand Down Expand Up @@ -148,6 +148,7 @@ def __create_generic_endpoint():
coin_url=lambda: url_for("graph_store_direct", path=str(rdflib.BNode()), _external=True)
)

# FIXME: these do not seem to be called with newer flask?
@endpoint.before_request
def __start():
g.start=time.time()
Expand All @@ -156,7 +157,7 @@ def __start():
def __end(response):
diff = time.time() - g.start
if response.response and response.content_type.startswith("text/html") and response.status_code==200:
response.response[0]=response.response[0].replace('__EXECUTION_TIME__', "%.3f"%diff)
response.response[0]=response.response[0].replace(b'__EXECUTION_TIME__', b"%.3f"%diff)
response.headers["Content-Length"]=len(response.response[0])
return response

Expand Down Expand Up @@ -193,7 +194,7 @@ def _main(g, out, opts):
import sys

if 'x' in opts:
import bookdb
from . import bookdb
g=bookdb.bookdb

serve(g, True)
Expand Down
2 changes: 1 addition & 1 deletion rdflib_web/generic_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def negotiate(self, resulttype, accept_header):
if resulttype == self.RESULT_GRAPH:
available = ['application/n-triples', 'text/n3', 'text/turtle', 'application/rdf+xml']
assert available, "Invalid resulttype"
import mimeutils
from . import mimeutils
best = mimeutils.best_match(available, accept_header) or available[-1]
return best, best

Expand Down
32 changes: 16 additions & 16 deletions rdflib_web/htmlresults.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""

SPARQL Result Serializer and RDF Serializer
SPARQL Result Serializer and RDF Serializer
for nice human readable HTML tables

>>> from rdflib import RDF, RDFS, XSD, Graph, Literal
Expand Down Expand Up @@ -32,24 +32,24 @@

def qname(ctx, t):
try:
if "graph" in ctx:
if "graph" in ctx:
l=ctx["graph"].namespace_manager.compute_qname(t, False)
else:
else:
l=nm.compute_qname(t, False)
return u'%s:%s'%(l[0],l[2])
except:
return '%s:%s'%(l[0],l[2])
except:
return t


@contextfilter
def term_to_string(ctx, t):
def term_to_string(ctx, t):
if isinstance(t, rdflib.URIRef):
l=qname(ctx,t)
return Markup(u"<a href='%s'>%s</a>"%(t,l))
elif isinstance(t, rdflib.Literal):
if t.language:
return Markup("<a href='%s'>%s</a>"%(t,l))
elif isinstance(t, rdflib.Literal):
if t.language:
return '"%s"@%s'%(t,t.language)
elif t.datatype:
elif t.datatype:
return '"%s"^^&lt;%s&gt;'%(t,qname(ctx,t.datatype))
else:
return '"%s"'%t
Expand All @@ -59,7 +59,7 @@ def term_to_string(ctx, t):
env.filters["term_to_string"]=term_to_string


GRAPH_TEMPLATE=u"""
GRAPH_TEMPLATE="""
<table>
<thead>
<tr>
Expand All @@ -81,7 +81,7 @@ def term_to_string(ctx, t):

"""

SELECT_TEMPLATE=u"""
SELECT_TEMPLATE="""
<table>
<thead>
<tr>
Expand All @@ -106,7 +106,7 @@ def term_to_string(ctx, t):

class HTMLResultSerializer(ResultSerializer):

def __init__(self, result):
def __init__(self, result):
ResultSerializer.__init__(self, result)

def serialize(self, stream, encoding="utf-8"):
Expand All @@ -118,7 +118,7 @@ def serialize(self, stream, encoding="utf-8"):
stream.write(template.render(result=self.result))






Expand All @@ -136,7 +136,7 @@ def serialize(self, stream, base=None, encoding=None, **args):
template = env.from_string(GRAPH_TEMPLATE)
res=template.render(graph=self.store)
if not encoding: encoding="utf-8"

res=res.encode(encoding)
stream.write(res)

Expand All @@ -150,4 +150,4 @@ def serialize(self, stream, base=None, encoding=None, **args):
g.add((rdflib.RDF.Property, rdflib.RDFS.label, rdflib.Literal('Eigenschaft', lang='de')))

s=g.query('select * where { ?s ?p ?o . }').serialize(format='html')

print(s)
Loading