diff --git a/.gitignore b/.gitignore index c4784b0..05dce64 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ searchindex.js search.html pylint.log pyflakes.log +.idea/ +venv/ diff --git a/setup.py b/setup.py index 9df4e3b..b6512e1 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,5 @@ import os + try: from setuptools import setup except ImportError: @@ -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', @@ -37,6 +38,7 @@ extras_require={ 'test': [ 'mock', + 'python-dateutil', ] }, diff --git a/sparql-client/tests/testn3parse.py b/sparql-client/tests/testn3parse.py index a626729..b39c31b 100644 --- a/sparql-client/tests/testn3parse.py +++ b/sparql-client/tests/testn3parse.py @@ -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'), ] @@ -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') \ No newline at end of file + self.assertRaises(ValueError, parse, 'hello') diff --git a/sparql.py b/sparql.py index 5fa63cb..aaf6f89 100755 --- a/sparql.py +++ b/sparql.py @@ -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", @@ -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 @@ -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 @@ -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: @@ -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): @@ -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) @@ -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) @@ -780,6 +784,7 @@ def __init__(self, code, message): self.code = code self.message = message + if __name__ == '__main__': import sys import codecs