From 7bc5c9ad5c1c51803508215d388cf8b076f584df Mon Sep 17 00:00:00 2001 From: EEA Jenkins Date: Wed, 17 Jun 2020 14:12:22 +0300 Subject: [PATCH 01/23] Back to devel --- docs/HISTORY.txt | 3 +++ version.txt | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/HISTORY.txt b/docs/HISTORY.txt index d897ea8..c7afe24 100644 --- a/docs/HISTORY.txt +++ b/docs/HISTORY.txt @@ -1,6 +1,9 @@ Changelog ========= +3.8-dev0 - (unreleased) +--------------------------- + 3.7 - (2020-06-17) --------------------------- * Feature: added the documentation originally found under eionet.euripa.eu diff --git a/version.txt b/version.txt index 475ba51..37f4172 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -3.7 +3.8-dev0 From 6cf44d6a3075578935b49b4ed6aeac1f302ba8de Mon Sep 17 00:00:00 2001 From: alecghica Date: Wed, 17 Jun 2020 15:08:37 +0000 Subject: [PATCH 02/23] PEP8 and linting, refs #111217 --- docs/HISTORY.txt | 2 ++ setup.py | 4 ++-- sparql.py | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/HISTORY.txt b/docs/HISTORY.txt index c7afe24..6482aff 100644 --- a/docs/HISTORY.txt +++ b/docs/HISTORY.txt @@ -3,6 +3,8 @@ Changelog 3.8-dev0 - (unreleased) --------------------------- +* Feature: PEP8 and linting + [alecghica refs #111217] 3.7 - (2020-06-17) --------------------------- diff --git a/setup.py b/setup.py index 6542dd6..b84ade2 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup(name=NAME, version=VERSION, description='Python API to query a SPARQL endpoint', - long_description = open('README.rst').read() + "\n\n" + + long_description = open('README.rst').read() + "\n\n" + \ open(os.path.join("docs", "HISTORY.txt")).read(), classifiers=[ 'Environment :: Console', @@ -26,7 +26,7 @@ long_description_content_type='text/x-rst', url='https://github.com/eea/sparql-client', license="MPL", - py_modules =['sparql'], + py_modules=['sparql'], install_requires=[ 'eventlet', 'six' ], diff --git a/sparql.py b/sparql.py index 083bdcc..baff867 100755 --- a/sparql.py +++ b/sparql.py @@ -71,7 +71,7 @@ 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" , From 8f935734cc88536e40fc279697c40d5aca81073f Mon Sep 17 00:00:00 2001 From: EEA Jenkins Date: Wed, 17 Jun 2020 18:13:43 +0300 Subject: [PATCH 03/23] Updated version to 3.8 --- version.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.txt b/version.txt index 37f4172..cc1923a 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -3.8-dev0 +3.8 From 169cb3cc690ba1aeaf12bc6b541e27ce79cce80c Mon Sep 17 00:00:00 2001 From: EEA Jenkins Date: Wed, 17 Jun 2020 18:16:39 +0300 Subject: [PATCH 04/23] Updated changelog - removed develop information --- docs/HISTORY.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/HISTORY.txt b/docs/HISTORY.txt index 6482aff..f50fbf0 100644 --- a/docs/HISTORY.txt +++ b/docs/HISTORY.txt @@ -1,7 +1,7 @@ Changelog ========= -3.8-dev0 - (unreleased) +3.8 - (2020-06-17) --------------------------- * Feature: PEP8 and linting [alecghica refs #111217] From 38cc0e378c92857641047b9d32584c1e02d28f47 Mon Sep 17 00:00:00 2001 From: alecghica Date: Thu, 18 Jun 2020 09:00:54 +0000 Subject: [PATCH 05/23] cosmetics, refs #111217 --- Jenkinsfile | 4 ---- README.rst | 1 + docs/HISTORY.txt | 2 +- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 7805396..4e9d2d2 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -125,17 +125,13 @@ pipeline { "Plone5 & Python3": { node(label: 'docker') { - catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') { sh '''docker run -i --rm --name="$BUILD_TAG-plone5py3" -e GIT_BRANCH="$BRANCH_NAME" -e ADDONS="$GIT_NAME[test]" -e DEVELOP="src/$GIT_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plone-test:5-python3 -v -vv -s $GIT_NAME''' - } } }, "PloneSaaS": { node(label: 'docker') { - catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') { sh '''docker run -i --rm --name="$BUILD_TAG-plonesaas" -e GIT_NAME="$GIT_NAME" -e GIT_BRANCH="$BRANCH_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plonesaas-devel /debug.sh bin/test --test-path /plone/instance/src/$GIT_NAME -v -vv -s $GIT_NAME''' - } } } diff --git a/README.rst b/README.rst index 8208b7e..0828d44 100644 --- a/README.rst +++ b/README.rst @@ -102,6 +102,7 @@ Literals. These can take a data type or a language code. Blank node. Similar to IRI but lacks a stable identifier. Query utilities +=============== ``class sparql.Service(endpoint, qs_encoding='utf-8')`` diff --git a/docs/HISTORY.txt b/docs/HISTORY.txt index f50fbf0..989f4f5 100644 --- a/docs/HISTORY.txt +++ b/docs/HISTORY.txt @@ -8,7 +8,7 @@ Changelog 3.7 - (2020-06-17) --------------------------- -* Feature: added the documentation originally found under eionet.euripa.eu +* Feature: added the documentation originally found under eionet.europa.eu [alecghica refs #111217] 3.6 - (2020-03-03) From 47152c754a0e6d98dba31a2bc00f8c3bcaba2616 Mon Sep 17 00:00:00 2001 From: Petchesi-Iulian Date: Fri, 19 Jun 2020 17:27:06 +0200 Subject: [PATCH 06/23] Refs #111217 Fix sparql tests. --- sparql-client/tests/testdatatypes.py | 18 ++++++++-- sparql-client/tests/testhttp.py | 12 +++++-- sparql-client/tests/testn3parse.py | 13 ++++++-- sparql-client/tests/testparser.py | 16 +++++++-- sparql.py | 50 ++++++++++++++++++++-------- 5 files changed, 85 insertions(+), 24 deletions(-) diff --git a/sparql-client/tests/testdatatypes.py b/sparql-client/tests/testdatatypes.py index 9d18785..f32eac3 100644 --- a/sparql-client/tests/testdatatypes.py +++ b/sparql-client/tests/testdatatypes.py @@ -3,6 +3,7 @@ import unittest import sparql +import six class TestLiterals(unittest.TestCase): @@ -46,7 +47,10 @@ def test_convert_to_unicode(self): class SomeType(object): def __unicode__(self): return u"hello world" - l = sparql.Literal(SomeType()) + if six.PY2: + l = sparql.Literal(SomeType()) + else: + l = sparql.Literal(SomeType().__unicode__()) self.assertEqual(str(l), "hello world") def test_repr(self): @@ -111,8 +115,11 @@ def test_convert_to_unicode(self): class SomeType(object): def __unicode__(self): return u"hello world" - l = sparql.Literal(SomeType(),u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD") - self.assertEqual(str(l), "hello world") + if six.PY2: + lt = sparql.Literal(SomeType(),u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD") + else: + lt = sparql.Literal(SomeType().__unicode__(),u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD") + self.assertEqual(str(lt), "hello world") class TestIRIs(unittest.TestCase): @@ -144,6 +151,7 @@ def test_compare_with_non_iri(self): (u"ascii-unicode" , '"ascii-unicode"'), (u"̈Ünɨcøðé" , '"\\u0308\\u00dcn\\u0268c\\u00f8\\u00f0\\u00e9"'), (u"\u6f22\u5b57(kanji)" , '"\u6f22\u5b57(kanji)"'), + # '"\\u6f22\\u5b57(kanji)"' ] class TestNotation3(unittest.TestCase): @@ -151,6 +159,10 @@ class TestNotation3(unittest.TestCase): def test_literal(self): """ Notation3 representation of a literal """ for value, expected in _literal_data: + # if 'kanji' in value: + # # import pdb; pdb.set_trace() # TODO: + # x = sparql.Literal(value) + # x.n3() self.assertEqual(sparql.Literal(value).n3(), expected) self.assertEqual(sparql.Literal(value, lang='en').n3(), expected+'@en') diff --git a/sparql-client/tests/testhttp.py b/sparql-client/tests/testhttp.py index b410522..5aa012d 100644 --- a/sparql-client/tests/testhttp.py +++ b/sparql-client/tests/testhttp.py @@ -1,5 +1,6 @@ import unittest import sparql +import six QUERIES = { "SELECT * WHERE {?s ?p ?o} LIMIT 2": """\ @@ -46,7 +47,10 @@ def getcode(self): class MockQuery(sparql._Query): def _get_response(self, opener, request, buf, timeout): - self.querystring = request.get_data() + if six.PY2: + self.querystring = request.get_data() + else: + self.querystring = request.data.decode() return MockResponse() def _read_response(self, response, buf, timeout): @@ -55,7 +59,11 @@ def _read_response(self, response, buf, timeout): except ImportError: from cgi import parse_qs query = parse_qs(self.querystring).get('query', [''])[0] - buf.write(QUERIES[query]) + if not six.PY2: + value = QUERIES[query].encode() + else: + value = QUERIES[query] + buf.write(value) class TestSparqlEndpoint(unittest.TestCase): diff --git a/sparql-client/tests/testn3parse.py b/sparql-client/tests/testn3parse.py index d7d2971..09ff2ad 100644 --- a/sparql-client/tests/testn3parse.py +++ b/sparql-client/tests/testn3parse.py @@ -1,5 +1,11 @@ import unittest import sparql +import six + +try: + from testdatatypes import _literal_data +except ImportError: + from .testdatatypes import _literal_data _string_literals = [ ('""', ''), # empty string @@ -14,7 +20,6 @@ ("'''some\ntext\n with spaces'''", 'some\ntext\n with spaces'), ] -from testdatatypes import _literal_data for _value, _n3 in _literal_data: _string_literals.append((_n3, _value)) @@ -26,7 +31,11 @@ def test_unicode(self): class Tricky(object): def __unicode__(self): return '<%s>' % value - self.assertEqual(sparql.parse_n3_term(Tricky()), sparql.IRI(value)) + if six.PY2: + parsed = sparql.parse_n3_term(Tricky()) + else: + parsed = sparql.parse_n3_term(Tricky().__unicode__()) + self.assertEqual(parsed, sparql.IRI(value)) def test_parse_IRI(self): value = 'http://example.com/some_iri' diff --git a/sparql-client/tests/testparser.py b/sparql-client/tests/testparser.py index 376a5ab..4c6cb48 100644 --- a/sparql-client/tests/testparser.py +++ b/sparql-client/tests/testparser.py @@ -51,7 +51,10 @@ def test_fetchmany(self): rows = result.fetchmany(2) self.assertEqual(2, len(rows)) row0 = rows[0] - self.assertEqual("http://rdfdata.eionet.europa.eu/eea/countries/BE", str(row0[0])) + if six.PY2: + self.assertEqual("http://rdfdata.eionet.europa.eu/eea/countries/BE", str(row0[0])) + else: + self.assertEqual("http://rdfdata.eionet.europa.eu/eea/countries/BE", row0[0].value) rows = result.fetchmany(2) self.assertEqual(1, len(rows)) row0 = rows[0] @@ -77,7 +80,10 @@ def test_w3_example(self): self.assertEqual([u'x', u'hpage', u'name', u'mbox', u'age', u'blurb', u'friend'], result.variables) rows = result.fetchall() row0 = rows[0] - self.assertEqual("http://work.example.org/alice/", str(row0[1])) + if six.PY2: + self.assertEqual("http://work.example.org/alice/", str(row0[1])) + else: + self.assertEqual("http://work.example.org/alice/", row0[1].value) def test_hasresult(self): """ Check that http://www.w3.org/TR/rdf-sparql-XMLres/output2.srx works """ @@ -93,7 +99,11 @@ def test_national(self): rows = result.fetchall() row0 = rows[0] - self.assertEqual("http://aims.fao.org/aos/geopolitical.owl#Germany", str(row0[0])) + if six.PY2: + self.assertEqual("http://aims.fao.org/aos/geopolitical.owl#Germany", str(row0[0])) + else: + self.assertEqual("http://aims.fao.org/aos/geopolitical.owl#Germany", row0[0].value) + self.assertEqual(sparql.IRI(u"http://aims.fao.org/aos/geopolitical.owl#Germany"), row0[0]) self.assertEqual(u"Германия", six.text_type(row0[2])) diff --git a/sparql.py b/sparql.py index baff867..ddf036b 100755 --- a/sparql.py +++ b/sparql.py @@ -62,6 +62,7 @@ import compiler else: from eventlet.green.urllib import request as ev_request + import ast as astcompiler from six.moves import map import six from six.moves import input @@ -286,26 +287,39 @@ def parse_n3_term(src): ast = compiler.parse("value = u" + src) else: # should be fixed due to #111217 - ast = compile("value = u" + src) + ast = astcompiler.parse("value = " + src) except: raise ValueError - # Don't allow any extra tokens in the AST - if len(ast.node.getChildNodes()) != 1: - raise ValueError - assign_node = ast.node.getChildNodes()[0] - if len(assign_node.getChildNodes()) != 2: - raise ValueError - value_node = assign_node.getChildNodes()[1] - if value_node.getChildNodes(): - raise ValueError if six.PY2: + # Don't allow any extra tokens in the AST + if len(ast.node.getChildNodes()) != 1: + raise ValueError + assign_node = ast.node.getChildNodes()[0] + if len(assign_node.getChildNodes()) != 2: + raise ValueError + value_node = assign_node.getChildNodes()[1] + if value_node.getChildNodes(): + raise ValueError if value_node.__class__ != compiler.ast.Const: raise ValueError + value = value_node.value else: - if value_node.__class__ != ast.Constant(): + # 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: raise ValueError - value = value_node.value + + value_node = assign_node.value + if len(value_node._fields) != 1: + raise ValueError + + # if value_node.__class__ != ast.Constant(): + # raise ValueError + value = getattr(value_node, value_node._fields[0]) if type(value) is not six.text_type: raise ValueError @@ -669,7 +683,11 @@ def fetchone(self): #print "rtn:", len(self._vals), self._vals yield tuple(self._vals) except SAXParseException as e: - faultString = 'The data is ' + e.message + if six.PY2: + message = e.message + else: + message = e.getMessage() + faultString = 'The data is ' + message print(faultString) yield tuple() @@ -763,5 +781,9 @@ def __init__(self, code, message): for row in result.fetchone(): print("\t".join(map(six.text_type, row))) except SparqlException as e: - faultString = e.message + if six.PY2: + message = e.message + else: + message = e.getMessage() + faultString = message print(faultString) From 2c15add59f7931428f6d4204f8ff88e144fe4f3e Mon Sep 17 00:00:00 2001 From: Petchesi-Iulian Date: Mon, 22 Jun 2020 12:44:22 +0200 Subject: [PATCH 07/23] Use different test data depending on py2/3. --- sparql-client/tests/testdatatypes.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sparql-client/tests/testdatatypes.py b/sparql-client/tests/testdatatypes.py index f32eac3..e98ad23 100644 --- a/sparql-client/tests/testdatatypes.py +++ b/sparql-client/tests/testdatatypes.py @@ -149,20 +149,20 @@ def test_compare_with_non_iri(self): ("new\nlines" , '"new\\nlines"'), ("ta\tbs" , '"ta\\tbs"'), (u"ascii-unicode" , '"ascii-unicode"'), - (u"̈Ünɨcøðé" , '"\\u0308\\u00dcn\\u0268c\\u00f8\\u00f0\\u00e9"'), - (u"\u6f22\u5b57(kanji)" , '"\u6f22\u5b57(kanji)"'), - # '"\\u6f22\\u5b57(kanji)"' + (u"̈Ünɨcøðé" , '"\\u0308\\u00dcn\\u0268c\\u00f8\\u00f0\\u00e9"') ] +if six.PY2: + _literal_data.append((u"\u6f22\u5b57(kanji)" , '"\u6f22\u5b57(kanji)"')) +else: + _literal_data.append((u"\u6f22\u5b57(kanji)" , '"\\u6f22\\u5b57(kanji)"')) + + class TestNotation3(unittest.TestCase): def test_literal(self): """ Notation3 representation of a literal """ for value, expected in _literal_data: - # if 'kanji' in value: - # # import pdb; pdb.set_trace() # TODO: - # x = sparql.Literal(value) - # x.n3() self.assertEqual(sparql.Literal(value).n3(), expected) self.assertEqual(sparql.Literal(value, lang='en').n3(), expected+'@en') From 058ce6c4cf71b160c54177032b7276f6362ba868 Mon Sep 17 00:00:00 2001 From: Petchesi-Iulian Date: Mon, 22 Jun 2020 13:59:59 +0200 Subject: [PATCH 08/23] Pep8 fixes. --- __init__.py | 1 - docs/conf.py | 2 +- setup.py | 4 +- sparql.py | 138 ++++++++++++++++++++++++++++++--------------------- 4 files changed, 85 insertions(+), 60 deletions(-) diff --git a/__init__.py b/__init__.py index 8b13789..e69de29 100644 --- a/__init__.py +++ b/__init__.py @@ -1 +0,0 @@ - diff --git a/docs/conf.py b/docs/conf.py index f7b2c95..1532813 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +from sparql import __version__ as version extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx'] @@ -9,7 +10,6 @@ project = u'sparql-client' copyright = u'2011, European Environment Agency' -from sparql import __version__ as version release = version exclude_patterns = ['_build'] diff --git a/setup.py b/setup.py index b84ade2..044a573 100644 --- a/setup.py +++ b/setup.py @@ -11,8 +11,8 @@ 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', diff --git a/sparql.py b/sparql.py index ddf036b..9d34be8 100755 --- a/sparql.py +++ b/sparql.py @@ -48,6 +48,8 @@ """ from base64 import encodestring +from six.moves import input +from six.moves import map from six.moves.urllib.parse import urlencode from xml.dom import pulldom from xml.sax import SAXParseException @@ -63,9 +65,6 @@ else: from eventlet.green.urllib import request as ev_request import ast as astcompiler -from six.moves import map -import six -from six.moves import input try: __version__ = open('version.txt').read().strip() @@ -75,18 +74,18 @@ USER_AGENT = "sparql-client/%s +https://www.eionet.europa.eu/software/sparql-client/" % __version__ CONTENT_TYPE = { - 'turtle' : "application/turtle" , - 'n3' :"application/n3", - 'rdfxml' : "application/rdf+xml" , - 'ntriples' : "application/n-triples" , - 'xml' : "application/xml" + 'turtle': "application/turtle" , + 'n3': "application/n3", + 'rdfxml': "application/rdf+xml" , + 'ntriples': "application/n-triples" , + 'xml': "application/xml" } RESULTS_TYPES = { - 'xml' : "application/sparql-results+xml" , - 'xmlschema' : "application/x-ms-access-export+xml", - 'json' : "application/sparql-results+json" + 'xml': "application/sparql-results+xml" , + 'xmlschema': "application/x-ms-access-export+xml", + 'json': "application/sparql-results+json" } # The purpose of this construction is to use shared strings when @@ -106,22 +105,26 @@ datatype_dict = { '': '', - XSD_STRING : XSD_STRING, - XSD_INT : XSD_INT, - XSD_LONG : XSD_LONG, - XSD_DOUBLE : XSD_DOUBLE, - XSD_FLOAT : XSD_FLOAT, - XSD_INTEGER : XSD_INTEGER, - XSD_DECIMAL : XSD_DECIMAL, - XSD_DATETIME : XSD_DATETIME, - XSD_DATE : XSD_DATE, - XSD_TIME : XSD_TIME, - XSD_BOOLEAN : XSD_BOOLEAN + XSD_STRING: XSD_STRING, + XSD_INT: XSD_INT, + XSD_LONG: XSD_LONG, + XSD_DOUBLE: XSD_DOUBLE, + XSD_FLOAT: XSD_FLOAT, + XSD_INTEGER: XSD_INTEGER, + XSD_DECIMAL: XSD_DECIMAL, + XSD_DATETIME: XSD_DATETIME, + XSD_DATE: XSD_DATE, + XSD_TIME: XSD_TIME, + XSD_BOOLEAN: XSD_BOOLEAN } # allow import from RestrictedPython -__allow_access_to_unprotected_subobjects__ = {'Datatype': 1, 'unpack_row': 1, - 'RDFTerm': 1, 'IRI': 1, 'Literal': 1, 'BlankNode': 1} +__allow_access_to_unprotected_subobjects__ = { + 'Datatype': 1, 'unpack_row': 1, + 'RDFTerm': 1, 'IRI': 1, + 'Literal': 1, 'BlankNode': 1 +} + def Datatype(value): """ @@ -129,7 +132,7 @@ def Datatype(value): intern() only works for plain strings - not unicode. We make it look like a class, because it conceptually could be. """ - if value==None: + if value is None: r = None elif value in datatype_dict: r = datatype_dict[value] @@ -137,6 +140,7 @@ def Datatype(value): r = datatype_dict[value] = value return r + class RDFTerm(object): """ Super class containing methods to override. :class:`IRI`, @@ -160,6 +164,7 @@ def n3(self): def __repr__(self): return '<%s %s>' % (type(self).__name__, self.n3()) + class IRI(RDFTerm): """ An RDF resource. """ @@ -172,7 +177,8 @@ def __str__(self): def __eq__(self, other): if type(self) != type(other): return False - if self.value == other.value: return True + if self.value == other.value: + return True return False def n3(self): @@ -185,6 +191,8 @@ def n3(self): '\t': '\\t', '\\': '\\\\', } + + def _n3_quote(string): def escape(m): ch = m.group() @@ -194,6 +202,7 @@ def escape(m): return "\\u%04x" % ord(ch) return '"' + _n3_quote_char.sub(escape, string) + '"' + class Literal(RDFTerm): """ Literals. These can take a data type or a language code. @@ -204,16 +213,16 @@ def __init__(self, value, datatype=None, lang=None): self.datatype = datatype def __eq__(self, other): - if type(self) != type(other): - return False + if type(self) != type(other): + return False - elif (self.value == other.value and - self.lang == other.lang and - self.datatype == other.datatype): - return True + elif (self.value == other.value and + self.lang == other.lang and + self.datatype == other.datatype): + return True - else: - return False + else: + return False def n3(self): n3_value = _n3_quote(self.value) @@ -226,17 +235,18 @@ def n3(self): return n3_value + class BlankNode(RDFTerm): """ Blank node. Similar to `IRI` but lacks a stable identifier. """ def __init__(self, value): self.value = value def __eq__(self, other): - if type(self) != type(other): - return False - if self.value == other.value: - return True - return False + if type(self) != type(other): + return False + if self.value == other.value: + return True + return False def n3(self): return '_:%s' % self.value @@ -244,6 +254,7 @@ def n3(self): _n3parser_lang = re.compile(r'@(?P\w+)$') _n3parser_datatype = re.compile(r'\^\^<(?P[^\^"\'>]+)>$') + def parse_n3_term(src): """ Parse a Notation3 value into a RDFTerm object (IRI or Literal). @@ -331,6 +342,8 @@ def parse_n3_term(src): # _ServiceMixin # ######################################### + + class _ServiceMixin(object): def __init__(self, endpoint, method="POST", accept=RESULTS_TYPES['xml']): self._method = method @@ -346,7 +359,8 @@ def __init__(self, endpoint, method="POST", accept=RESULTS_TYPES['xml']): def _setMethod(self, method): if method in ("GET", "POST"): self._method = method - else: raise ValueError("Only GET or POST is allowed") + else: + raise ValueError("Only GET or POST is allowed") def _getMethod(self): return self._method @@ -380,14 +394,15 @@ def headers(self): # ######################################### + class Service(_ServiceMixin): """ This is the main entry to the library. The user creates a :class:`Service`, then sends a query to it. If we want to have persistent connections, then open them here. """ - def __init__(self, endpoint, qs_encoding = "utf-8", method = "POST", - accept = "application/sparql-results+xml"): + def __init__(self, endpoint, qs_encoding="utf-8", method="POST", + accept="application/sparql-results+xml"): _ServiceMixin.__init__(self, endpoint, method, accept) self.qs_encoding = qs_encoding @@ -399,7 +414,7 @@ def createQuery(self): q._prefix_map = copy.deepcopy(self._prefix_map) return q - def query(self, query, timeout = 0, raw=False): + def query(self, query, timeout=0, raw=False): q = self.createQuery() return q.query(query, timeout, raw=raw) @@ -409,6 +424,7 @@ def authenticate(self, username, password): head = "Basic %s" % encodestring("%s:%s" % (username, password)).replace("\012", "") self._headers_map['Authorization'] = head + def _parseBoolean(val): if val.lower() in ('true', '1'): return True @@ -422,8 +438,8 @@ def _parseBoolean(val): XSD_LONG: int, XSD_DOUBLE: float, XSD_FLOAT: float, - XSD_INTEGER: int, # INTEGER is a DECIMAL, but Python `int` has no size - # limit, so it's safe to use + XSD_INTEGER: int, # INTEGER is a DECIMAL, but Python `int` has no size + # limit, so it's safe to use XSD_DECIMAL: decimal.Decimal, XSD_BOOLEAN: _parseBoolean, } @@ -436,6 +452,7 @@ def _parseBoolean(val): except ImportError: pass + def unpack_row(row, convert=None, convert_type={}): """ Convert values in the given row from :class:`RDFTerm` objects to plain @@ -478,6 +495,8 @@ def unpack_row(row, convert=None, convert_type={}): # _Query # ######################################### + + class _Query(_ServiceMixin): def __init__(self, service): @@ -525,7 +544,7 @@ def _read_response(self, response, buf, timeout): def _build_response(self, query, opener, buf, timeout): request = self._build_request(query) return self._get_response(opener, request, buf, - timeout if timeout > 0 else None) + timeout if timeout > 0 else None) def _request(self, statement, timeout=0): """ @@ -563,7 +582,7 @@ def _queryString(self, statement): """ args = [] # refs #72876 removing the replace of newline to allow the comments in sparql queries - #statement = statement.replace("\n", " ").encode('utf-8') + # statement = statement.replace("\n", " ").encode('utf-8') # not needed py3 # statement = statement.encode('utf-8') @@ -597,8 +616,13 @@ class _ResultsParser(object): Parse the XML result. """ - __allow_access_to_unprotected_subobjects__ = {'fetchone': 1, - 'fetchmany': 1, 'fetchall': 1, 'hasresult': 1, 'variables': 1} + __allow_access_to_unprotected_subobjects__ = { + 'fetchone': 1, + 'fetchmany': 1, + 'fetchall': 1, + 'hasresult': 1, + 'variables': 1 + } def __init__(self, fp): self.__fp = fp @@ -625,7 +649,7 @@ def _fetchhead(self): self.events.expandNode(node) self._hasResult = (node.firstChild.data == 'true') elif node.tagName == 'result': - return # We should not arrive here + return # We should not arrive here elif event == pulldom.END_ELEMENT: if node.tagName == 'head' and self.variables: return @@ -661,7 +685,7 @@ def fetchone(self): for (event, node) in self.events: if event == pulldom.START_ELEMENT: if node.tagName == 'result': - self._vals = [None] * len(self.variables) + self._vals = [None] * len(self.variables) elif node.tagName == 'binding': idx = self.variables.index(node.attributes['name'].value) elif node.tagName == 'uri': @@ -680,7 +704,7 @@ def fetchone(self): self._vals[idx] = BlankNode(data) elif event == pulldom.END_ELEMENT: if node.tagName == 'result': - #print "rtn:", len(self._vals), self._vals + # print "rtn:", len(self._vals), self._vals yield tuple(self._vals) except SAXParseException as e: if six.PY2: @@ -705,9 +729,11 @@ def fetchmany(self, num): for row in self.fetchone(): result.append(row) num -= 1 - if num <= 0: return result + if num <= 0: + return result return result + def query(endpoint, query, timeout=0, qs_encoding="utf-8", method="POST", accept="application/sparql-results+xml", raw=False): """ @@ -718,6 +744,7 @@ def query(endpoint, query, timeout=0, qs_encoding="utf-8", method="POST", s = Service(endpoint, qs_encoding, method, accept) return s.query(query, timeout, raw=raw) + def _interactive(endpoint): while True: try: @@ -759,11 +786,10 @@ def __init__(self, code, message): c = codecs.getwriter('ascii') sys.stdout = c(sys.stdout, 'replace') - parser = OptionParser(usage="%prog [-i] endpoint", - version="%prog " + str(__version__)) + version="%prog " + str(__version__)) parser.add_option("-i", dest="interactive", action="store_true", - help="Enables interactive mode") + help="Enables interactive mode") (options, args) = parser.parse_args() From a46dd09ac820e24bc697ddd60ff5752f8dd7a4c9 Mon Sep 17 00:00:00 2001 From: Petchesi-Iulian Date: Mon, 22 Jun 2020 14:33:13 +0200 Subject: [PATCH 09/23] Pylint fixes for tests. --- sparql-client/tests/genquery.py | 29 +++-- sparql-client/tests/testconversion.py | 2 +- sparql-client/tests/testdatatypes.py | 146 +++++++++++++++++--------- sparql-client/tests/testhttp.py | 39 +++---- sparql-client/tests/testn3parse.py | 13 ++- sparql-client/tests/testparser.py | 133 ++++++++++++++++------- 6 files changed, 244 insertions(+), 118 deletions(-) diff --git a/sparql-client/tests/genquery.py b/sparql-client/tests/genquery.py index b55df0a..e10dce2 100644 --- a/sparql-client/tests/genquery.py +++ b/sparql-client/tests/genquery.py @@ -1,18 +1,27 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- -import six.moves.urllib.request, six.moves.urllib.parse, six.moves.urllib.error,six.moves.urllib.request,six.moves.urllib.error,six.moves.urllib.parse +import six.moves.urllib.request +import six.moves.urllib.parse +import six.moves.urllib.error +import six.moves.urllib.request +import six.moves.urllib.error +import six.moves.urllib.parse -statement = open("code.rq").read() -query = { 'query': statement, - 'format':'xml' } +statement = open('code.rq').read() +query = {'query': statement, 'format': 'xml'} qs = six.moves.urllib.parse.urlencode(query) -print(qs) -url = "http://dbpedia.org/sparql?" + six.moves.urllib.parse.urlencode(query) +print qs +url = 'http://dbpedia.org/sparql?' \ + + six.moves.urllib.parse.urlencode(query) -opener = six.moves.urllib.request.build_opener(six.moves.urllib.request.HTTPHandler) +opener = \ + six.moves.urllib.request.build_opener(six.moves.urllib.request.HTTPHandler) six.moves.urllib.request.install_opener(opener) req = six.moves.urllib.request.Request(url) -#req.add_header("Accept", "application/xml") + +# req.add_header("Accept", "application/xml") try: conn = six.moves.urllib.request.urlopen(req, timeout=10) @@ -20,8 +29,8 @@ conn = None if not conn: - raise IOError("Failure in open") + raise IOError('Failure in open') data = conn.read() conn.close() -print(data) +print data diff --git a/sparql-client/tests/testconversion.py b/sparql-client/tests/testconversion.py index 94e681e..e8189d7 100644 --- a/sparql-client/tests/testconversion.py +++ b/sparql-client/tests/testconversion.py @@ -3,8 +3,8 @@ import unittest from datetime import datetime, date, time -import sparql import os.path +import sparql from mock import Mock, patch from six.moves import map diff --git a/sparql-client/tests/testdatatypes.py b/sparql-client/tests/testdatatypes.py index e98ad23..1501116 100644 --- a/sparql-client/tests/testdatatypes.py +++ b/sparql-client/tests/testdatatypes.py @@ -10,32 +10,36 @@ class TestLiterals(unittest.TestCase): def test_literal_same(self): """ Two literals with same language must be the same """ - l1 = sparql.Literal(u"Hello world",lang=u"en") - l2 = sparql.Literal(u"Hello world",lang=u"en") + + l1 = sparql.Literal(u'Hello world', lang=u'en') + l2 = sparql.Literal(u'Hello world', lang=u'en') self.assertEqual(l1, l2) def test_literal_notsame1(self): """ Two literals different language must be different """ - l1 = sparql.Literal(u"Hello world",lang=u"en") - l2 = sparql.Literal(u"Hello world",lang=u"en-US") + + l1 = sparql.Literal(u'Hello world', lang=u'en') + l2 = sparql.Literal(u'Hello world', lang=u'en-US') self.assertNotEqual(l1, l2) def test_literal_notsame2(self): """ Difference on both value and language """ - l1 = sparql.Literal(u"Hello world",lang=u"en") - l2 = sparql.Literal(u"Hallo Welt",lang=u"de") + + l1 = sparql.Literal(u'Hello world', lang=u'en') + l2 = sparql.Literal(u'Hallo Welt', lang=u'de') self.assertNotEqual(l1, l2) def test_literal_notsame3(self): """ Two literals with same language must be the same """ - l1 = sparql.Literal(u"Hello world",lang=u"en") - self.assertNotEqual(u"Hello world", l1) - self.assertNotEqual(l1, u"Hello world") + + l1 = sparql.Literal(u'Hello world', lang=u'en') + self.assertNotEqual(u'Hello world', l1) + self.assertNotEqual(l1, u'Hello world') def test_compare_with_non_literal(self): - l1 = sparql.Literal("hi") - self.assertFalse(l1 == "hi") - self.assertFalse(l1 == u"hi") + l1 = sparql.Literal('hi') + self.assertFalse(l1 == 'hi') + self.assertFalse(l1 == u'hi') self.assertFalse(l1 == None) self.assertFalse(l1 == 13) self.assertFalse(l1 == 13.0) @@ -44,66 +48,97 @@ def test_compare_with_non_literal(self): def test_convert_to_unicode(self): """ Literals should convert values to unicode when saving them """ + class SomeType(object): + def __unicode__(self): - return u"hello world" + return u'hello world' + if six.PY2: l = sparql.Literal(SomeType()) else: l = sparql.Literal(SomeType().__unicode__()) - self.assertEqual(str(l), "hello world") + self.assertEqual(str(l), 'hello world') def test_repr(self): """ repr should return the literal in N3 syntax """ - l = sparql.Literal(u"Hello world") + + l = sparql.Literal(u'Hello world') self.assertEqual(u'', repr(l)) + class TestTypedLiterals(unittest.TestCase): def test_isinstance(self): """ Type literals are instances of RDFTerm """ - l = sparql.Literal(u"Hello world",u"http://www.w3.org/2001/XMLSchema#string") + + l = sparql.Literal(u'Hello world', + u'http://www.w3.org/2001/XMLSchema#string') assert isinstance(l, sparql.RDFTerm) def test_repr(self): """ repr should return the literal in N3 syntax """ - l = sparql.Literal(u"Hello world",u"http://www.w3.org/2001/XMLSchema#string") - self.assertEqual(u'>', repr(l)) + + l = sparql.Literal(u'Hello world', + u'http://www.w3.org/2001/XMLSchema#string') + self.assertEqual(u'>' + , repr(l)) def test_str(self): """ str should return the literal without type """ - l = sparql.Literal(u"Hello world",u"http://www.w3.org/2001/XMLSchema#string") - assert str(l) == u"Hello world" + + l = sparql.Literal(u'Hello world', + u'http://www.w3.org/2001/XMLSchema#string') + assert str(l) == u'Hello world' def test_literal_same(self): """ Two literals with same language must be the same """ - l1 = sparql.Literal(u"Hello world",u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD") - l2 = sparql.Literal(u"Hello world",u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD") + + l1 = sparql.Literal(u'Hello world', + u'http://aims.fao.org/aos/geopolitical.owl#MillionUSD' + ) + l2 = sparql.Literal(u'Hello world', + u'http://aims.fao.org/aos/geopolitical.owl#MillionUSD' + ) self.assertEqual(l1, l2) def test_literal_notsame1(self): """ Two literals different language must be different """ - l1 = sparql.Literal(u"Hello world",u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD") - l2 = sparql.Literal(u"Hello world",u"http://www.w3.org/2001/XMLSchema#string") + + l1 = sparql.Literal(u'Hello world', + u'http://aims.fao.org/aos/geopolitical.owl#MillionUSD' + ) + l2 = sparql.Literal(u'Hello world', + u'http://www.w3.org/2001/XMLSchema#string') self.assertNotEqual(l1, l2) def test_literal_notsame2(self): """ Difference on both value and language """ - l1 = sparql.Literal(u"Hello world",u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD") - l2 = sparql.Literal(u"Hallo Welt",u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD") + + l1 = sparql.Literal(u'Hello world', + u'http://aims.fao.org/aos/geopolitical.owl#MillionUSD' + ) + l2 = sparql.Literal(u'Hallo Welt', + u'http://aims.fao.org/aos/geopolitical.owl#MillionUSD' + ) self.assertNotEqual(l1, l2) def test_literal_notsame3(self): """ Two literals with same language must be the same """ - l1 = sparql.Literal(u"Hello world",u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD") - self.assertNotEqual(u"Hello world", l1) - self.assertNotEqual(l1, u"Hello world") - assert l1 != u"Hello world" + + l1 = sparql.Literal(u'Hello world', + u'http://aims.fao.org/aos/geopolitical.owl#MillionUSD' + ) + self.assertNotEqual(u'Hello world', l1) + self.assertNotEqual(l1, u'Hello world') + assert l1 != u'Hello world' def test_compare_with_non_literal(self): - l1 = sparql.Literal("hi",u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD") - self.assertFalse(l1 == "hi") - self.assertFalse(l1 == u"hi") + l1 = sparql.Literal('hi', + u'http://aims.fao.org/aos/geopolitical.owl#MillionUSD' + ) + self.assertFalse(l1 == 'hi') + self.assertFalse(l1 == u'hi') self.assertFalse(l1 == None) self.assertFalse(l1 == 13) self.assertFalse(l1 == 13.0) @@ -112,32 +147,43 @@ def test_compare_with_non_literal(self): def test_convert_to_unicode(self): """ Literals should convert values to unicode when saving them """ + class SomeType(object): + def __unicode__(self): - return u"hello world" + return u'hello world' + if six.PY2: - lt = sparql.Literal(SomeType(),u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD") + lt = sparql.Literal(SomeType(), + u'http://aims.fao.org/aos/geopolitical.owl#MillionUSD' + ) else: - lt = sparql.Literal(SomeType().__unicode__(),u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD") - self.assertEqual(str(lt), "hello world") + lt = sparql.Literal(SomeType().__unicode__(), + u'http://aims.fao.org/aos/geopolitical.owl#MillionUSD' + ) + self.assertEqual(str(lt), 'hello world') + class TestIRIs(unittest.TestCase): def test_repr(self): """ repr should return the literal in N3 syntax """ - i = sparql.IRI("http://example.com/asdf") - self.assertEqual(repr(i), ">") + + i = sparql.IRI('http://example.com/asdf') + self.assertEqual(repr(i), '>') def test_compare_with_non_iri(self): - i1 = sparql.IRI("http://example.com/asdf") - self.assertFalse(i1 == "http://example.com/asdf") - self.assertFalse(i1 == u"http://example.com/asdf") + i1 = sparql.IRI('http://example.com/asdf') + self.assertFalse(i1 == 'http://example.com/asdf') + self.assertFalse(i1 == u'http://example.com/asdf') self.assertFalse(i1 == None) self.assertFalse(i1 == 13) self.assertFalse(i1 == 13.0) self.assertFalse(i1 == ['http://example.com/asdf']) - self.assertFalse(i1 == {'http://example.com/asdf': - 'http://example.com/asdf'}) + self.assertFalse(i1 + == {'http://example.com/asdf': 'http://example.com/asdf' + }) + _literal_data = [ ('' , '""'), @@ -162,16 +208,20 @@ class TestNotation3(unittest.TestCase): def test_literal(self): """ Notation3 representation of a literal """ - for value, expected in _literal_data: + + for (value, expected) in _literal_data: self.assertEqual(sparql.Literal(value).n3(), expected) - self.assertEqual(sparql.Literal(value, lang='en').n3(), expected+'@en') + self.assertEqual(sparql.Literal(value, lang='en').n3(), + expected + '@en') def test_typed_literal(self): """ N3 notation of a typed literal """ - datatype = u"http://www.w3.org/2001/XMLSchema#string" - for value, expected in _literal_data: + + datatype = u'http://www.w3.org/2001/XMLSchema#string' + for (value, expected) in _literal_data: tl = sparql.Literal(value, datatype) self.assertEqual(tl.n3(), '%s^^<%s>' % (expected, datatype)) + if __name__ == '__main__': unittest.main() diff --git a/sparql-client/tests/testhttp.py b/sparql-client/tests/testhttp.py index 5aa012d..dadbecc 100644 --- a/sparql-client/tests/testhttp.py +++ b/sparql-client/tests/testhttp.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + import unittest import sparql import six @@ -46,24 +49,24 @@ def getcode(self): class MockQuery(sparql._Query): - def _get_response(self, opener, request, buf, timeout): - if six.PY2: - self.querystring = request.get_data() - else: - self.querystring = request.data.decode() - return MockResponse() - - def _read_response(self, response, buf, timeout): - try: - from six.moves.urllib.parse import parse_qs - except ImportError: - from cgi import parse_qs - query = parse_qs(self.querystring).get('query', [''])[0] - if not six.PY2: - value = QUERIES[query].encode() - else: - value = QUERIES[query] - buf.write(value) + def _get_response(self, opener, request, buf, timeout): + if six.PY2: + self.querystring = request.get_data() + else: + self.querystring = request.data.decode() + return MockResponse() + + def _read_response(self, response, buf, timeout): + try: + from six.moves.urllib.parse import parse_qs + except ImportError: + from cgi import parse_qs + query = parse_qs(self.querystring).get('query', [''])[0] + if not six.PY2: + value = QUERIES[query].encode() + else: + value = QUERIES[query] + buf.write(value) class TestSparqlEndpoint(unittest.TestCase): diff --git a/sparql-client/tests/testn3parse.py b/sparql-client/tests/testn3parse.py index 09ff2ad..a626729 100644 --- a/sparql-client/tests/testn3parse.py +++ b/sparql-client/tests/testn3parse.py @@ -28,9 +28,12 @@ class N3ParsingTest(unittest.TestCase): def test_unicode(self): value = 'http://example.com/some_iri' + class Tricky(object): + def __unicode__(self): return '<%s>' % value + if six.PY2: parsed = sparql.parse_n3_term(Tricky()) else: @@ -54,7 +57,7 @@ def test_IRI_error(self): self.assertRaises(ValueError, parse, 'ri>') def test_literal(self): - for n3_value, value in _string_literals: + for (n3_value, value) in _string_literals: result = sparql.parse_n3_term(n3_value) self.assertTrue(type(result) is sparql.Literal) self.assertEqual(result.lang, None) @@ -64,7 +67,7 @@ def test_literal(self): self.assertEqual(sparql.parse_n3_term(l.n3()), l) def test_literal_with_lang(self): - for n3_value, value in _string_literals: + for (n3_value, value) in _string_literals: n3_value_with_lang = n3_value + '@en' result = sparql.parse_n3_term(n3_value_with_lang) self.assertTrue(type(result) is sparql.Literal) @@ -75,8 +78,8 @@ def test_literal_with_lang(self): self.assertEqual(sparql.parse_n3_term(l.n3()), l) def test_typed_literals(self): - million_uri = u"http://aims.fao.org/aos/geopolitical.owl#MillionUSD" - for n3_value, value in _string_literals: + million_uri = u'http://aims.fao.org/aos/geopolitical.owl#MillionUSD' + for (n3_value, value) in _string_literals: n3_value_with_type = n3_value + '^^<' + million_uri + '>' result = sparql.parse_n3_term(n3_value_with_type) self.assertTrue(type(result) is sparql.Literal) @@ -90,4 +93,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') \ No newline at end of file diff --git a/sparql-client/tests/testparser.py b/sparql-client/tests/testparser.py index 4c6cb48..34abcc8 100644 --- a/sparql-client/tests/testparser.py +++ b/sparql-client/tests/testparser.py @@ -9,65 +9,104 @@ from six.moves import map import six + def _open_datafile(name): return open(os.path.join(os.path.dirname(__file__), name)) -XSD_FAO_MILLION = "http://aims.fao.org/aos/geopolitical.owl#MillionUSD" + +XSD_FAO_MILLION = 'http://aims.fao.org/aos/geopolitical.owl#MillionUSD' + class TestParser(unittest.TestCase): def test_simple(self): """ Simple query with unbound variables """ - resultfp = _open_datafile("countries.srx") + + resultfp = _open_datafile('countries.srx') result = sparql._ResultsParser(resultfp) - self.assertEqual([u'eeaURI', u'gdpTotal', u'eeacode', u'nutscode', u'faocode', u'gdp', u'name'], result.variables) + self.assertEqual([ + u'eeaURI', + u'gdpTotal', + u'eeacode', + u'nutscode', + u'faocode', + u'gdp', + u'name', + ], result.variables) rows = result.fetchall() row0 = rows[0] - self.assertEqual(sparql.IRI(u"http://rdfdata.eionet.europa.eu/eea/countries/BE"), row0[0]) - self.assertEqual(sparql.Literal("471161.0", XSD_FAO_MILLION), row0[1]) - self.assertEqual(sparql.Literal("44.252934", sparql.XSD_FLOAT), row0[5]) + self.assertEqual(sparql.IRI(u'http://rdfdata.eionet.europa.eu/eea/countries/BE' + ), row0[0]) + self.assertEqual(sparql.Literal('471161.0', XSD_FAO_MILLION), + row0[1]) + self.assertEqual(sparql.Literal('44.252934', sparql.XSD_FLOAT), + row0[5]) def test_unpack(self): - resultfp = _open_datafile("countries.srx") + resultfp = _open_datafile('countries.srx') result = sparql._ResultsParser(resultfp) - self.assertEqual([u'eeaURI', u'gdpTotal', u'eeacode', u'nutscode', u'faocode', u'gdp', u'name'], result.variables) + self.assertEqual([ + u'eeaURI', + u'gdpTotal', + u'eeacode', + u'nutscode', + u'faocode', + u'gdp', + u'name', + ], result.variables) rows = list(map(sparql.unpack_row, result.fetchall())) row0 = rows[0] - self.assertEqual(u"http://rdfdata.eionet.europa.eu/eea/countries/BE", row0[0]) + self.assertEqual(u'http://rdfdata.eionet.europa.eu/eea/countries/BE' + , row0[0]) + # XSD_FAO_MILLION unpacked as string - self.assertEqual("471161.0", row0[1]) + + self.assertEqual('471161.0', row0[1]) + # XSD_FLOAT unpacked as float - self.assertNotEqual("44.252934", row0[5]) + + self.assertNotEqual('44.252934', row0[5]) self.assertEqual(44.252934, row0[5]) def test_fetchmany(self): """ Simple query with unbound variables """ - resultfp = _open_datafile("countries.srx") + + resultfp = _open_datafile('countries.srx') result = sparql._ResultsParser(resultfp) - self.assertEqual([u'eeaURI', u'gdpTotal', u'eeacode', u'nutscode', u'faocode', u'gdp', u'name'], result.variables) + self.assertEqual([ + u'eeaURI', + u'gdpTotal', + u'eeacode', + u'nutscode', + u'faocode', + u'gdp', + u'name', + ], result.variables) rows = result.fetchmany(2) self.assertEqual(2, len(rows)) row0 = rows[0] if six.PY2: - self.assertEqual("http://rdfdata.eionet.europa.eu/eea/countries/BE", str(row0[0])) + self.assertEqual('http://rdfdata.eionet.europa.eu/eea/countries/BE' + , str(row0[0])) else: - self.assertEqual("http://rdfdata.eionet.europa.eu/eea/countries/BE", row0[0].value) + self.assertEqual('http://rdfdata.eionet.europa.eu/eea/countries/BE' + , row0[0].value) rows = result.fetchmany(2) self.assertEqual(1, len(rows)) row0 = rows[0] - assert str(row0[6]) == "Japan" + assert str(row0[6]) == 'Japan' def test_ask_query(self): """ Check that http://www.w3.org/TR/rdf-sparql-XMLres/output2.srx works """ - resultfp = _open_datafile("w3-output2.srx") + + resultfp = _open_datafile('w3-output2.srx') result = sparql._ResultsParser(resultfp) rows = result.fetchall() assert len(rows) == 0 - # for row in result.fetchone(): # print row # row1 = result.fetchone() @@ -75,61 +114,83 @@ def test_ask_query(self): def test_w3_example(self): """ Check that http://www.w3.org/TR/rdf-sparql-XMLres/output.srx works """ - resultfp = _open_datafile("w3-output.srx") + + resultfp = _open_datafile('w3-output.srx') result = sparql._ResultsParser(resultfp) - self.assertEqual([u'x', u'hpage', u'name', u'mbox', u'age', u'blurb', u'friend'], result.variables) + self.assertEqual([ + u'x', + u'hpage', + u'name', + u'mbox', + u'age', + u'blurb', + u'friend', + ], result.variables) rows = result.fetchall() row0 = rows[0] if six.PY2: - self.assertEqual("http://work.example.org/alice/", str(row0[1])) + self.assertEqual('http://work.example.org/alice/', + str(row0[1])) else: - self.assertEqual("http://work.example.org/alice/", row0[1].value) + self.assertEqual('http://work.example.org/alice/', + row0[1].value) def test_hasresult(self): """ Check that http://www.w3.org/TR/rdf-sparql-XMLres/output2.srx works """ - resultfp = _open_datafile("w3-output2.srx") + + resultfp = _open_datafile('w3-output2.srx') result = sparql._ResultsParser(resultfp) assert result.hasresult() == True def test_national(self): """ Simple query with UTF-8 """ - resultfp = _open_datafile("national.srx") + + resultfp = _open_datafile('national.srx') result = sparql._ResultsParser(resultfp) - self.assertEqual([u'subj', u'nameen', u'nameru'], result.variables) + self.assertEqual([u'subj', u'nameen', u'nameru'], + result.variables) rows = result.fetchall() row0 = rows[0] if six.PY2: - self.assertEqual("http://aims.fao.org/aos/geopolitical.owl#Germany", str(row0[0])) + self.assertEqual('http://aims.fao.org/aos/geopolitical.owl#Germany' + , str(row0[0])) else: - self.assertEqual("http://aims.fao.org/aos/geopolitical.owl#Germany", row0[0].value) + self.assertEqual('http://aims.fao.org/aos/geopolitical.owl#Germany' + , row0[0].value) - self.assertEqual(sparql.IRI(u"http://aims.fao.org/aos/geopolitical.owl#Germany"), row0[0]) + self.assertEqual(sparql.IRI(u'http://aims.fao.org/aos/geopolitical.owl#Germany' + ), row0[0]) self.assertEqual(u"Германия", six.text_type(row0[2])) def test_big_text(self): + # `xml.dom.pulldom` may return several text nodes within a single # binding. This seems to be triggered especially by entities, e.g. # "<". - resultfp = _open_datafile("big_text.srx") + + resultfp = _open_datafile('big_text.srx') result = sparql._ResultsParser(resultfp) row0 = result.fetchall()[0] - self.assertEqual("multiple
paragraphs
here", row0[0].value) - self.assertEqual("http://example.com/", row0[1].value) - self.assertEqual("bnode.id", row0[2].value) + self.assertEqual('multiple
paragraphs
here', + row0[0].value) + self.assertEqual('http://example.com/', row0[1].value) + self.assertEqual('bnode.id', row0[2].value) def side_effect_fetchhead(): - fp = _open_datafile("invalid-result.srx") + fp = _open_datafile('invalid-result.srx') return pulldom.parse(fp) - @patch('sparql._ResultsParser._fetchhead', side_effect=side_effect_fetchhead) + @patch('sparql._ResultsParser._fetchhead', + side_effect=side_effect_fetchhead) def test_invalid_fetchone(self, mocked_element): """ Simple query with invalid characters """ - resultfp = _open_datafile("invalid-result.srx") + + resultfp = _open_datafile('invalid-result.srx') result = sparql._ResultsParser(resultfp) setattr(result, 'events', result._fetchhead()) for row in result.fetchone(): - print(row) + print(row) if __name__ == '__main__': From 645f7d2a6cabdcc03023df7146a6dcee57117162 Mon Sep 17 00:00:00 2001 From: Petchesi-Iulian Date: Mon, 22 Jun 2020 17:19:35 +0200 Subject: [PATCH 10/23] Dont encode query if py3 --- sparql.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sparql.py b/sparql.py index 9d34be8..2f77f06 100755 --- a/sparql.py +++ b/sparql.py @@ -597,7 +597,10 @@ def _queryString(self, statement): for uri in self.namedGraphs(): args.append(('named-graph-uri', uri)) - return urlencode(args).encode('utf-8') + if six.PY2: + return urlencode(args).encode('utf-8') + else: + return urlencode(args) class RedirectHandler(ev_request.HTTPRedirectHandler): From 86ae64a6bb7938af464d55cde6f677415c2cfe02 Mon Sep 17 00:00:00 2001 From: Petchesi-Iulian Date: Mon, 22 Jun 2020 17:23:37 +0200 Subject: [PATCH 11/23] Fix indentation error. --- sparql-client/tests/testhttp.py | 43 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/sparql-client/tests/testhttp.py b/sparql-client/tests/testhttp.py index dadbecc..cefce55 100644 --- a/sparql-client/tests/testhttp.py +++ b/sparql-client/tests/testhttp.py @@ -53,7 +53,10 @@ def _get_response(self, opener, request, buf, timeout): if six.PY2: self.querystring = request.get_data() else: - self.querystring = request.data.decode() + if not request.data: + self.querystring = request.selector.split('?')[1] + else: + self.querystring = request.data return MockResponse() def _read_response(self, response, buf, timeout): @@ -71,28 +74,28 @@ def _read_response(self, response, buf, timeout): class TestSparqlEndpoint(unittest.TestCase): - def setUp(self): - self.old_Query = sparql._Query - sparql._Query = MockQuery + def setUp(self): + self.old_Query = sparql._Query + sparql._Query = MockQuery - def tearDown(self): - sparql._Query = self.old_Query + def tearDown(self): + sparql._Query = self.old_Query - def test_simple_query(self): - from sparql import IRI - URI_LANG = 'http://rdfdata.eionet.europa.eu/eea/languages' - URI_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' - URI_LANG_TYPE = 'http://rdfdata.eionet.europa.eu/eea/ontology/Language' - endpoint = "http://cr3.eionet.europa.eu/sparql" + def test_simple_query(self): + from sparql import IRI + URI_LANG = 'http://rdfdata.eionet.europa.eu/eea/languages' + URI_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' + URI_LANG_TYPE = 'http://rdfdata.eionet.europa.eu/eea/ontology/Language' + endpoint = "http://cr3.eionet.europa.eu/sparql" + + result = sparql.query(endpoint, "SELECT * WHERE {?s ?p ?o} LIMIT 2") - result = sparql.query(endpoint, "SELECT * WHERE {?s ?p ?o} LIMIT 2") - - self.assertEqual(result.variables, ['s', 'p', 'o']) - self.assertEqual(list(result), [ - (IRI(URI_LANG+'/en'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), - (IRI(URI_LANG+'/da'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), - ]) + self.assertEqual(result.variables, ['s', 'p', 'o']) + self.assertEqual(list(result), [ + (IRI(URI_LANG+'/en'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), + (IRI(URI_LANG+'/da'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), + ]) if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file From 12c21010228ffd113ebe8bc7b9185cb26a79d15d Mon Sep 17 00:00:00 2001 From: Petchesi-Iulian Date: Mon, 22 Jun 2020 17:36:39 +0200 Subject: [PATCH 12/23] More pylint fixes. --- sparql-client/tests/testhttp.py | 84 ++++++++++++++++----------------- sparql.py | 27 ++++------- 2 files changed, 52 insertions(+), 59 deletions(-) diff --git a/sparql-client/tests/testhttp.py b/sparql-client/tests/testhttp.py index cefce55..f50701a 100644 --- a/sparql-client/tests/testhttp.py +++ b/sparql-client/tests/testhttp.py @@ -49,52 +49,52 @@ def getcode(self): class MockQuery(sparql._Query): - def _get_response(self, opener, request, buf, timeout): - if six.PY2: - self.querystring = request.get_data() - else: - if not request.data: - self.querystring = request.selector.split('?')[1] - else: - self.querystring = request.data - return MockResponse() - - def _read_response(self, response, buf, timeout): - try: - from six.moves.urllib.parse import parse_qs - except ImportError: - from cgi import parse_qs - query = parse_qs(self.querystring).get('query', [''])[0] - if not six.PY2: - value = QUERIES[query].encode() - else: - value = QUERIES[query] - buf.write(value) + def _get_response(self, opener, request, buf, timeout): + if six.PY2: + self.querystring = request.get_data() + else: + if not request.data: + self.querystring = request.selector.split('?')[1] + else: + self.querystring = request.data + return MockResponse() + + def _read_response(self, response, buf, timeout): + try: + from six.moves.urllib.parse import parse_qs + except ImportError: + from cgi import parse_qs + query = parse_qs(self.querystring).get('query', [''])[0] + if not six.PY2: + value = QUERIES[query].encode() + else: + value = QUERIES[query] + buf.write(value) class TestSparqlEndpoint(unittest.TestCase): - def setUp(self): - self.old_Query = sparql._Query - sparql._Query = MockQuery - - def tearDown(self): - sparql._Query = self.old_Query - - def test_simple_query(self): - from sparql import IRI - URI_LANG = 'http://rdfdata.eionet.europa.eu/eea/languages' - URI_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' - URI_LANG_TYPE = 'http://rdfdata.eionet.europa.eu/eea/ontology/Language' - endpoint = "http://cr3.eionet.europa.eu/sparql" - - result = sparql.query(endpoint, "SELECT * WHERE {?s ?p ?o} LIMIT 2") - - self.assertEqual(result.variables, ['s', 'p', 'o']) - self.assertEqual(list(result), [ - (IRI(URI_LANG+'/en'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), - (IRI(URI_LANG+'/da'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), - ]) + def setUp(self): + self.old_Query = sparql._Query + sparql._Query = MockQuery + + def tearDown(self): + sparql._Query = self.old_Query + + def test_simple_query(self): + from sparql import IRI + URI_LANG = 'http://rdfdata.eionet.europa.eu/eea/languages' + URI_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' + URI_LANG_TYPE = 'http://rdfdata.eionet.europa.eu/eea/ontology/Language' + endpoint = "http://cr3.eionet.europa.eu/sparql" + + result = sparql.query(endpoint, "SELECT * WHERE {?s ?p ?o} LIMIT 2") + + self.assertEqual(result.variables, ['s', 'p', 'o']) + self.assertEqual(list(result), [ + (IRI(URI_LANG+'/en'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), + (IRI(URI_LANG+'/da'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), + ]) if __name__ == '__main__': diff --git a/sparql.py b/sparql.py index 2f77f06..76930b5 100755 --- a/sparql.py +++ b/sparql.py @@ -48,8 +48,7 @@ """ from base64 import encodestring -from six.moves import input -from six.moves import map +from six.moves import input, map from six.moves.urllib.parse import urlencode from xml.dom import pulldom from xml.sax import SAXParseException @@ -74,16 +73,16 @@ USER_AGENT = "sparql-client/%s +https://www.eionet.europa.eu/software/sparql-client/" % __version__ CONTENT_TYPE = { - 'turtle': "application/turtle" , + 'turtle': "application/turtle", 'n3': "application/n3", - 'rdfxml': "application/rdf+xml" , - 'ntriples': "application/n-triples" , + 'rdfxml': "application/rdf+xml", + 'ntriples': "application/n-triples", 'xml': "application/xml" } RESULTS_TYPES = { - 'xml': "application/sparql-results+xml" , + 'xml': "application/sparql-results+xml", 'xmlschema': "application/x-ms-access-export+xml", 'json': "application/sparql-results+json" } @@ -198,8 +197,7 @@ def escape(m): ch = m.group() if ch in _n3_quote_map: return _n3_quote_map[ch] - else: - return "\\u%04x" % ord(ch) + return "\\u%04x" % ord(ch) return '"' + _n3_quote_char.sub(escape, string) + '"' @@ -221,8 +219,7 @@ def __eq__(self, other): self.datatype == other.datatype): return True - else: - return False + return False def n3(self): n3_value = _n3_quote(self.value) @@ -426,10 +423,7 @@ def authenticate(self, username, password): def _parseBoolean(val): - if val.lower() in ('true', '1'): - return True - else: - return False + return val.lower() in ('true', '1') # XMLSchema types and cast functions @@ -599,8 +593,7 @@ def _queryString(self, statement): args.append(('named-graph-uri', uri)) if six.PY2: return urlencode(args).encode('utf-8') - else: - return urlencode(args) + return urlencode(args) class RedirectHandler(ev_request.HTTPRedirectHandler): @@ -785,7 +778,7 @@ def __init__(self, code, message): try: c = codecs.getwriter(sys.stdout.encoding) - except: + except Exception: c = codecs.getwriter('ascii') sys.stdout = c(sys.stdout, 'replace') From 93c2914e3699e9041477afe73551b15de7fb71e5 Mon Sep 17 00:00:00 2001 From: alecghica Date: Tue, 23 Jun 2020 09:32:31 +0000 Subject: [PATCH 13/23] cleanup --- Jenkinsfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 4e9d2d2..cb409a9 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -125,13 +125,13 @@ pipeline { "Plone5 & Python3": { node(label: 'docker') { - sh '''docker run -i --rm --name="$BUILD_TAG-plone5py3" -e GIT_BRANCH="$BRANCH_NAME" -e ADDONS="$GIT_NAME[test]" -e DEVELOP="src/$GIT_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plone-test:5-python3 -v -vv -s $GIT_NAME''' + sh '''docker run -i --rm --name="$BUILD_TAG-plone5py3" -e GIT_BRANCH="$BRANCH_NAME" -e ADDONS="$GIT_NAME[test]" -e DEVELOP="src/$GIT_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plone-test:5-python3 -v -vv -s $GIT_NAME''' } }, "PloneSaaS": { node(label: 'docker') { - sh '''docker run -i --rm --name="$BUILD_TAG-plonesaas" -e GIT_NAME="$GIT_NAME" -e GIT_BRANCH="$BRANCH_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plonesaas-devel /debug.sh bin/test --test-path /plone/instance/src/$GIT_NAME -v -vv -s $GIT_NAME''' + sh '''docker run -i --rm --name="$BUILD_TAG-plonesaas" -e GIT_NAME="$GIT_NAME" -e GIT_BRANCH="$BRANCH_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plonesaas-devel /debug.sh bin/test --test-path /plone/instance/src/$GIT_NAME -v -vv -s $GIT_NAME''' } } From 3aa639bf5354965e160d97c5b3aea14972e3c823 Mon Sep 17 00:00:00 2001 From: Petchesi-Iulian Date: Thu, 25 Jun 2020 15:47:32 +0200 Subject: [PATCH 14/23] Fix sparql error caused by eea.ldapadmin --- sparql.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sparql.py b/sparql.py index 76930b5..ba0b7ec 100755 --- a/sparql.py +++ b/sparql.py @@ -537,6 +537,8 @@ def _read_response(self, response, buf, timeout): def _build_response(self, query, opener, buf, timeout): request = self._build_request(query) + if type(query) is not bytes and not six.PY2: + query = query.encode() return self._get_response(opener, request, buf, timeout if timeout > 0 else None) @@ -581,8 +583,10 @@ def _queryString(self, statement): # statement = statement.encode('utf-8') pref = ' '.join(["PREFIX %s: <%s> " % (p, self._prefix_map[p]) for p in self._prefix_map]) - - statement = pref + statement + if six.PY2: + statement = pref + statement + else: + statement = pref.encode() + statement args.append(('query', statement)) From 5162bc9f924d82ca5789da0985cf0e83acc74a3a Mon Sep 17 00:00:00 2001 From: Petchesi-Iulian Date: Thu, 9 Jul 2020 11:11:24 +0200 Subject: [PATCH 15/23] Fix sparql tests for py3. --- sparql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sparql.py b/sparql.py index ba0b7ec..67e73c6 100755 --- a/sparql.py +++ b/sparql.py @@ -586,7 +586,7 @@ def _queryString(self, statement): if six.PY2: statement = pref + statement else: - statement = pref.encode() + statement + statement = pref.encode() + statement.encode() args.append(('query', statement)) From f6c51de6ef78c29353784916c9edc4340f75cbef Mon Sep 17 00:00:00 2001 From: alecghica Date: Mon, 20 Jul 2020 13:07:30 +0000 Subject: [PATCH 16/23] Fixed Py3 query execution, refs #111217 --- docs/HISTORY.txt | 2 ++ sparql.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/docs/HISTORY.txt b/docs/HISTORY.txt index 989f4f5..9523f26 100644 --- a/docs/HISTORY.txt +++ b/docs/HISTORY.txt @@ -3,6 +3,8 @@ Changelog 3.8 - (2020-06-17) --------------------------- +* Bug fix: fixed Python3 query execution + [alecghica refs #111217] * Feature: PEP8 and linting [alecghica refs #111217] diff --git a/sparql.py b/sparql.py index 67e73c6..d2592d5 100755 --- a/sparql.py +++ b/sparql.py @@ -553,6 +553,8 @@ def _request(self, statement, timeout=0): opener = ev_request.build_opener(RedirectHandler) opener.addheaders = list(self.headers().items()) try: + if type(query) is not bytes and not six.PY2: + query = query.encode() response = self._build_response(query, opener, buf, timeout) except SparqlException as error: self.endpoint = error.message From a59ebba8efa035776fef4d00d967963a508fc124 Mon Sep 17 00:00:00 2001 From: Alin Voinea Date: Tue, 21 Jul 2020 18:31:30 +0300 Subject: [PATCH 17/23] Fix python3 syntax --- sparql-client/tests/testhttp.py | 78 ++++++++++++++++----------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/sparql-client/tests/testhttp.py b/sparql-client/tests/testhttp.py index f50701a..8fe87b8 100644 --- a/sparql-client/tests/testhttp.py +++ b/sparql-client/tests/testhttp.py @@ -6,7 +6,7 @@ import six QUERIES = { -"SELECT * WHERE {?s ?p ?o} LIMIT 2": """\ + "SELECT * WHERE {?s ?p ?o} LIMIT 2": """\ @@ -50,52 +50,52 @@ def getcode(self): class MockQuery(sparql._Query): def _get_response(self, opener, request, buf, timeout): - if six.PY2: - self.querystring = request.get_data() - else: - if not request.data: - self.querystring = request.selector.split('?')[1] - else: - self.querystring = request.data - return MockResponse() - + if six.PY2: + self.querystring = request.get_data() + else: + if not request.data: + self.querystring = request.selector.split('?')[1] + else: + self.querystring = request.data + return MockResponse() + def _read_response(self, response, buf, timeout): - try: - from six.moves.urllib.parse import parse_qs - except ImportError: - from cgi import parse_qs - query = parse_qs(self.querystring).get('query', [''])[0] - if not six.PY2: - value = QUERIES[query].encode() - else: - value = QUERIES[query] - buf.write(value) + try: + from six.moves.urllib.parse import parse_qs + except ImportError: + from cgi import parse_qs + query = parse_qs(self.querystring).get('query', [''])[0] + if not six.PY2: + value = QUERIES[query].encode() + else: + value = QUERIES[query] + buf.write(value) class TestSparqlEndpoint(unittest.TestCase): def setUp(self): - self.old_Query = sparql._Query - sparql._Query = MockQuery - + self.old_Query = sparql._Query + sparql._Query = MockQuery + def tearDown(self): - sparql._Query = self.old_Query - + sparql._Query = self.old_Query + def test_simple_query(self): - from sparql import IRI - URI_LANG = 'http://rdfdata.eionet.europa.eu/eea/languages' - URI_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' - URI_LANG_TYPE = 'http://rdfdata.eionet.europa.eu/eea/ontology/Language' - endpoint = "http://cr3.eionet.europa.eu/sparql" - - result = sparql.query(endpoint, "SELECT * WHERE {?s ?p ?o} LIMIT 2") - - self.assertEqual(result.variables, ['s', 'p', 'o']) - self.assertEqual(list(result), [ - (IRI(URI_LANG+'/en'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), - (IRI(URI_LANG+'/da'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), - ]) + from sparql import IRI + URI_LANG = 'http://rdfdata.eionet.europa.eu/eea/languages' + URI_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' + URI_LANG_TYPE = 'http://rdfdata.eionet.europa.eu/eea/ontology/Language' + endpoint = "http://cr3.eionet.europa.eu/sparql" + + result = sparql.query(endpoint, "SELECT * WHERE {?s ?p ?o} LIMIT 2") + + self.assertEqual(result.variables, ['s', 'p', 'o']) + self.assertEqual(list(result), [ + (IRI(URI_LANG+'/en'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), + (IRI(URI_LANG+'/da'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), + ]) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() From 97961723f4f442831206b3900e6f5a2e635b4625 Mon Sep 17 00:00:00 2001 From: Alin Voinea Date: Tue, 21 Jul 2020 18:33:12 +0300 Subject: [PATCH 18/23] Fix print syntax --- sparql-client/tests/genquery.py | 4 ++-- sparql.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sparql-client/tests/genquery.py b/sparql-client/tests/genquery.py index e10dce2..e29d9da 100644 --- a/sparql-client/tests/genquery.py +++ b/sparql-client/tests/genquery.py @@ -12,7 +12,7 @@ query = {'query': statement, 'format': 'xml'} qs = six.moves.urllib.parse.urlencode(query) -print qs +print(qs) url = 'http://dbpedia.org/sparql?' \ + six.moves.urllib.parse.urlencode(query) @@ -33,4 +33,4 @@ data = conn.read() conn.close() -print data +print(data) diff --git a/sparql.py b/sparql.py index d2592d5..1ec2807 100755 --- a/sparql.py +++ b/sparql.py @@ -30,7 +30,7 @@ >>> result = sparql.query(endpoint, query) >>> for row in result: - >>> print row + >>> print(row) Command-line use From 45754c91fc44d13f9b210ba9541adbbe32c27fa5 Mon Sep 17 00:00:00 2001 From: Alin Voinea Date: Tue, 21 Jul 2020 18:39:56 +0300 Subject: [PATCH 19/23] Fix tests python3 --- sparql-client/tests/testhttp.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sparql-client/tests/testhttp.py b/sparql-client/tests/testhttp.py index 8fe87b8..5402fd2 100644 --- a/sparql-client/tests/testhttp.py +++ b/sparql-client/tests/testhttp.py @@ -57,6 +57,8 @@ def _get_response(self, opener, request, buf, timeout): self.querystring = request.selector.split('?')[1] else: self.querystring = request.data + if isinstance(self.querystring, six.binary_type): + self.querystring = self.querystring.decode("utf-8") return MockResponse() def _read_response(self, response, buf, timeout): From 54068c21f9426d8193a4e9d765aac8977ed63ac0 Mon Sep 17 00:00:00 2001 From: Alin Voinea Date: Tue, 21 Jul 2020 21:54:07 +0300 Subject: [PATCH 20/23] [JENKINS] Fix dnspython version --- setup.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 044a573..19afedf 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,9 @@ license="MPL", py_modules=['sparql'], install_requires=[ - 'eventlet', 'six' + 'eventlet', + 'six', + 'dnspython < 2.0.0', ], extras_require={ 'test': [ From d61c191fa829f0623cd89b10527382e7c6fe6dd4 Mon Sep 17 00:00:00 2001 From: alecghica Date: Thu, 30 Jul 2020 09:41:52 +0000 Subject: [PATCH 21/23] cosmetics, refs #111217 --- setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup.py b/setup.py index 19afedf..9df4e3b 100644 --- a/setup.py +++ b/setup.py @@ -17,6 +17,8 @@ 'Environment :: Console', 'Intended Audience :: Developers', "Programming Language :: Python", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 2.7", 'Operating System :: OS Independent', 'Topic :: Software Development :: Libraries :: Python Modules', ], From e0b124975866987b59ae0866feebdc1b06fef889 Mon Sep 17 00:00:00 2001 From: Petchesi-Iulian Date: Thu, 30 Jul 2020 14:13:49 +0200 Subject: [PATCH 22/23] Refs #111217 Pull latest docker images for tests. --- Jenkinsfile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index cb409a9..0cc8bae 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -107,31 +107,31 @@ pipeline { "KGS": { node(label: 'docker') { - sh '''docker run -i --rm --name="$BUILD_TAG-kgs" -e GIT_NAME="$GIT_NAME" -e GIT_BRANCH="$BRANCH_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/kgs-devel /debug.sh bin/test --test-path /plone/instance/src/$GIT_NAME -v -vv -s $GIT_NAME''' + sh '''docker pull eeacms/kgs-devel;docker run -i --rm --name="$BUILD_TAG-kgs" -e GIT_NAME="$GIT_NAME" -e GIT_BRANCH="$BRANCH_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/kgs-devel /debug.sh bin/test --test-path /plone/instance/src/$GIT_NAME -v -vv -s $GIT_NAME''' } }, "Plone4": { node(label: 'docker') { - sh '''docker run -i --rm --name="$BUILD_TAG-plone4" -e GIT_BRANCH="$BRANCH_NAME" -e ADDONS="$GIT_NAME[test]" -e DEVELOP="src/$GIT_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plone-test:4 -v -vv -s $GIT_NAME''' + sh '''docker pull eeacms/plone-test:4;docker run -i --rm --name="$BUILD_TAG-plone4" -e GIT_BRANCH="$BRANCH_NAME" -e ADDONS="$GIT_NAME[test]" -e DEVELOP="src/$GIT_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plone-test:4 -v -vv -s $GIT_NAME''' } }, "Plone5 & Python2": { node(label: 'docker') { - sh '''docker run -i --rm --name="$BUILD_TAG-plone5py2" -e GIT_BRANCH="$BRANCH_NAME" -e ADDONS="$GIT_NAME[test]" -e DEVELOP="src/$GIT_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plone-test:5 -v -vv -s $GIT_NAME''' + sh '''docker pull eeacms/plone-test:5;docker run -i --rm --name="$BUILD_TAG-plone5py2" -e GIT_BRANCH="$BRANCH_NAME" -e ADDONS="$GIT_NAME[test]" -e DEVELOP="src/$GIT_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plone-test:5 -v -vv -s $GIT_NAME''' } }, "Plone5 & Python3": { node(label: 'docker') { - sh '''docker run -i --rm --name="$BUILD_TAG-plone5py3" -e GIT_BRANCH="$BRANCH_NAME" -e ADDONS="$GIT_NAME[test]" -e DEVELOP="src/$GIT_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plone-test:5-python3 -v -vv -s $GIT_NAME''' + sh '''docker pull eeacms/plone-test:5-python3;docker run -i --rm --name="$BUILD_TAG-plone5py3" -e GIT_BRANCH="$BRANCH_NAME" -e ADDONS="$GIT_NAME[test]" -e DEVELOP="src/$GIT_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plone-test:5-python3 -v -vv -s $GIT_NAME''' } }, "PloneSaaS": { node(label: 'docker') { - sh '''docker run -i --rm --name="$BUILD_TAG-plonesaas" -e GIT_NAME="$GIT_NAME" -e GIT_BRANCH="$BRANCH_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plonesaas-devel /debug.sh bin/test --test-path /plone/instance/src/$GIT_NAME -v -vv -s $GIT_NAME''' + sh '''docker pull eeacms/plonesaas-devel;docker run -i --rm --name="$BUILD_TAG-plonesaas" -e GIT_NAME="$GIT_NAME" -e GIT_BRANCH="$BRANCH_NAME" -e GIT_CHANGE_ID="$CHANGE_ID" eeacms/plonesaas-devel /debug.sh bin/test --test-path /plone/instance/src/$GIT_NAME -v -vv -s $GIT_NAME''' } } From a88181969c124b66809ca52e7563fb2737e52de2 Mon Sep 17 00:00:00 2001 From: Petchesi-Iulian Date: Mon, 3 Aug 2020 16:21:08 +0200 Subject: [PATCH 23/23] Trigger tests.