Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ searchindex.js
search.html
pylint.log
pyflakes.log
.idea/
venv/
8 changes: 5 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

try:
from setuptools import setup
except ImportError:
Expand All @@ -7,17 +8,17 @@
NAME = 'sparql-client'
VERSION = open('version.txt').read().strip()


setup(name=NAME,
version=VERSION,
description='Python API to query a SPARQL endpoint',
long_description=open('README.rst').read() + "\n\n" + \
open(os.path.join("docs", "HISTORY.txt")).read(),
long_description=open('README.rst').read() + "\n\n"
+ open(os.path.join("docs", "HISTORY.txt")).read(),
classifiers=[
'Environment :: Console',
'Intended Audience :: Developers',
"Programming Language :: Python",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 2.7",
'Operating System :: OS Independent',
'Topic :: Software Development :: Libraries :: Python Modules',
Expand All @@ -37,6 +38,7 @@
extras_require={
'test': [
'mock',
'python-dateutil',
]
},

Expand Down
20 changes: 11 additions & 9 deletions sparql-client/tests/testn3parse.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import unittest
import sparql

import six

import sparql

try:
from testdatatypes import _literal_data
except ImportError:
from .testdatatypes import _literal_data

_string_literals = [
('""', ''), # empty string
("''", ''), # empty string
('""""""', ''), # triple quotes (")
("''''''", ''), # triple quotes (')
('" "', ' '), # one space
('""', ''), # empty string
("''", ''), # empty string
('""""""', ''), # triple quotes (")
("''''''", ''), # triple quotes (')
('" "', ' '), # one space
('"hi"', 'hi'),
("'hi'", 'hi'),
("'some\\ntext'", 'some\ntext'), # newline
("'some\\ttext'", 'some\ttext'), # tab
("'some\\ntext'", 'some\ntext'), # newline
("'some\\ttext'", 'some\ttext'), # tab
("'''some\ntext\n with spaces'''", 'some\ntext\n with spaces'),
]

Expand Down Expand Up @@ -93,4 +95,4 @@ def test_evil_literals(self):
parse = sparql.parse_n3_term
self.assertRaises(ValueError, parse, '"hello" + " world"')
self.assertRaises(ValueError, parse, '"hello"\nx = " world"')
self.assertRaises(ValueError, parse, 'hello')
self.assertRaises(ValueError, parse, 'hello')
47 changes: 26 additions & 21 deletions sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@
except Exception:
__version__ = "2.6"

USER_AGENT = "sparql-client/%s +https://www.eionet.europa.eu/software/sparql-client/" % __version__
USER_AGENT = \
"sparql-client/%s +https://www.eionet.europa.eu/software/sparql-client/" \
% __version__

CONTENT_TYPE = {
'turtle': "application/turtle",
Expand Down Expand Up @@ -316,21 +318,13 @@ def parse_n3_term(src):
raise ValueError
value = value_node.value
else:
# Don't allow any extra tokens in the AST
if len(ast.body) != 1:
raise ValueError
assign_node = ast.body[0]

if len(assign_node._fields) != 2:
if len(ast.body) != 1 \
or not isinstance(ast.body[0], astcompiler.Assign):
raise ValueError

value_node = assign_node.value
if len(value_node._fields) != 1:
assign_node = cast(astcompiler.Assign, ast.body[0])
if not isinstance(assign_node.value, astcompiler.Constant):
raise ValueError

# if value_node.__class__ != ast.Constant():
# raise ValueError
value = getattr(value_node, value_node._fields[0])
value = cast(astcompiler.Constant, assign_node.value).value

if type(value) is not six.text_type:
raise ValueError
Expand Down Expand Up @@ -420,8 +414,10 @@ def query(self, query, timeout=0, raw=False):

def authenticate(self, username, password):
# self._headers_map['Authorization'] = "Basic %s" % replace(
# encodestring("%s:%s" % (username, password)), "\012", "")
head = "Basic %s" % encodestring("%s:%s" % (username, password)).replace("\012", "")
# encodebytes("%s:%s" % (username, password)), "\012", "")
head = "Basic %s" % encodebytes(
"%s:%s" % (username, password)
).replace("\012", "")
self._headers_map['Authorization'] = head


Expand Down Expand Up @@ -582,12 +578,16 @@ def _queryString(self, statement):
Creates the REST query string from the statement and graphs.
"""
args = []
# refs #72876 removing the replace of newline to allow the comments in sparql queries
# refs #72876 removing the replace of newline to allow the comments
# in sparql queries
# statement = statement.replace("\n", " ").encode('utf-8')
# not needed py3
# statement = statement.encode('utf-8')

pref = ' '.join(["PREFIX %s: <%s> " % (p, self._prefix_map[p]) for p in self._prefix_map])
pref = ' '.join([
"PREFIX %s: <%s> " % (p, self._prefix_map[p])
for p in self._prefix_map
])
if six.PY2:
statement = pref + statement
else:
Expand All @@ -607,7 +607,8 @@ def _queryString(self, statement):

class RedirectHandler(ev_request.HTTPRedirectHandler):
"""
Subclass the HTTPRedirectHandler to re-contruct request when follow redirect
Subclass the HTTPRedirectHandler to re-contruct request
when follow redirect
"""
def redirect_request(self, req, fp, code, msg, headers, newurl):
if code in (301, 302, 303, 307):
Expand Down Expand Up @@ -692,7 +693,9 @@ def fetchone(self):
if node.tagName == 'result':
self._vals = [None] * len(self.variables)
elif node.tagName == 'binding':
idx = self.variables.index(node.attributes['name'].value)
idx = self.variables.index(
node.attributes['name'].value
)
elif node.tagName == 'uri':
self.events.expandNode(node)
data = ''.join(t.data for t in node.childNodes)
Expand All @@ -701,7 +704,8 @@ def fetchone(self):
self.events.expandNode(node)
data = ''.join(t.data for t in node.childNodes)
lang = node.getAttribute('xml:lang') or None
datatype = Datatype(node.getAttribute('datatype')) or None
datatype = \
Datatype(node.getAttribute('datatype')) or None
self._vals[idx] = Literal(data, datatype, lang)
elif node.tagName == 'bnode':
self.events.expandNode(node)
Expand Down Expand Up @@ -780,6 +784,7 @@ def __init__(self, code, message):
self.code = code
self.message = message


if __name__ == '__main__':
import sys
import codecs
Expand Down