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 compatibility #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added dist/pymantic-0.1.1dev_r0-py3.5.egg
Binary file not shown.
26 changes: 18 additions & 8 deletions pymantic/sparql.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
"""Provide an interface to SPARQL query endpoints."""

from cStringIO import StringIO
try:
# py2
from StringIO import StringIO
import urlparse
except ImportError:
# py3
from io import StringIO
from urllib.parse import urlparse
from urllib.parse import urlencode

from builtins import str as text

import datetime
import urllib
import urlparse

import httplib2
from lxml import objectify
Expand Down Expand Up @@ -64,19 +74,19 @@ def execute(self):

if self.server.post_directly:
self.headers["Content-Type"] = self.directContentType() + "; charset=utf-8"
uri_params = urllib.urlencode(self.params, doseq=True)
uri_params = urlencode(self.params, doseq=True)
body = sparql
method='POST'
elif self.postQueries():
self.headers["Content-Type"] = "application/x-www-form-urlencoded"
uri_params = None
self.params[self.query_or_update()] = sparql
body = urllib.urlencode(self.params, doseq=True)
body = urlencode(self.params, doseq=True)
method='POST'
else:
# select only
self.params[self.query_or_update()] = sparql
uri_params = urllib.urlencode(self.params, doseq=True)
uri_params = urlencode(self.params, doseq=True)
body = None
method='GET'

Expand Down Expand Up @@ -142,7 +152,7 @@ def execute(self):
elif response['content-type'].startswith('application/sparql-results+json'):
# See http://stackoverflow.com/a/19366580/2276263
# for justification of unicode() below
return simplejson.loads(unicode(content, "utf-8"))
return simplejson.loads(text(content, "utf-8"))
elif response['content-type'].startswith('application/sparql-results+xml'):
return objectify.parse(StringIO(content))
else:
Expand Down Expand Up @@ -215,7 +225,7 @@ def __init__(self, query_url, dataset_url, param_style = True, **kwargs):

def request_url(self, graph_uri):
if self.param_style:
return self.dataset_url + '?' + urllib.urlencode({'graph': graph_uri})
return self.dataset_url + '?' + urlencode({'graph': graph_uri})
else:
return urlparse.urljoin(self.dataset_url, urllib.quote_plus(graph_uri))

Expand Down Expand Up @@ -270,7 +280,7 @@ def post(self, graph_uri, graph):
resp, content = h.request(uri = self.dataset_url, method = 'POST',
body = graph_triples,
headers = {'content-type': 'text/plain',},)
if resp['status'] != '201':
if resp['status'] not in ('200', '201', '204'):
raise Exception('Error from Graph Store (%s): %s' %\
(resp['status'], content))

Expand Down