From 86f31edd01cad38f714538bbd9f41997dae60b88 Mon Sep 17 00:00:00 2001 From: Gunnar Aastrand Grimnes Date: Thu, 12 Jan 2017 12:35:28 +0100 Subject: [PATCH] be more compatible when importing elementtree, and only in one place fixes #606 --- rdflib/compat.py | 27 +++++++++++++++++++++ rdflib/plugins/sparql/results/xmlresults.py | 4 +-- rdflib/plugins/stores/sparqlstore.py | 27 ++++----------------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/rdflib/compat.py b/rdflib/compat.py index 6c7148949..f7ca4063b 100644 --- a/rdflib/compat.py +++ b/rdflib/compat.py @@ -23,3 +23,30 @@ def numeric_greater(a, b): def numeric_greater(a, b): return a > b + + +try: + from lxml import etree + print("running with lxml.etree") +except ImportError: + try: + # Python 2.5 + import xml.etree.cElementTree as etree + print("running with cElementTree on Python 2.5+") + except ImportError: + try: + # Python 2.5 + import xml.etree.ElementTree as etree + print("running with ElementTree on Python 2.5+") + except ImportError: + try: + # normal cElementTree install + import cElementTree as etree + print("running with cElementTree") + except ImportError: + try: + # normal ElementTree install + import elementtree.ElementTree as etree + print("running with ElementTree") + except ImportError: + print("Failed to import ElementTree from any known place") diff --git a/rdflib/plugins/sparql/results/xmlresults.py b/rdflib/plugins/sparql/results/xmlresults.py index 85c33882e..b31f9513b 100644 --- a/rdflib/plugins/sparql/results/xmlresults.py +++ b/rdflib/plugins/sparql/results/xmlresults.py @@ -3,7 +3,7 @@ from xml.dom import XML_NAMESPACE from xml.sax.xmlreader import AttributesNSImpl -from xml.etree import cElementTree as ElementTree +from rdflib.compat import etree from rdflib import Literal, URIRef, BNode, Graph, Variable from rdflib.query import ( @@ -42,7 +42,7 @@ def __init__(self, source): if isinstance(xmlstring, unicode): xmlstring = xmlstring.encode('utf-8') try: - tree = ElementTree.fromstring(xmlstring) + tree = etree.fromstring(xmlstring) except Exception, e: try: raise e.__class__("error parsing %r: %s" % (xmlstring, e)) diff --git a/rdflib/plugins/stores/sparqlstore.py b/rdflib/plugins/stores/sparqlstore.py index 25cfb61dc..481f68405 100644 --- a/rdflib/plugins/stores/sparqlstore.py +++ b/rdflib/plugins/stores/sparqlstore.py @@ -13,7 +13,6 @@ import re import collections -import urllib2 import warnings try: @@ -23,20 +22,7 @@ "SPARQLWrapper not found! SPARQL Store will not work." + "Install with 'easy_install SPARQLWrapper'") -import sys -if getattr(sys, 'pypy_version_info', None) is not None \ - or sys.platform.startswith('java') \ - or sys.version_info[:2] < (2, 6): - # import elementtree as etree - from elementtree import ElementTree - assert ElementTree -else: - try: - from xml.etree import ElementTree - assert ElementTree - except ImportError: - from elementtree import ElementTree - +from rdflib.compat import etree from rdflib.plugins.stores.regexmatching import NATIVE_REGEX from rdflib.store import Store @@ -45,9 +31,6 @@ from rdflib.graph import DATASET_DEFAULT_GRAPH_ID from rdflib.term import Node -import httplib -import urlparse - class NSSPARQLWrapper(SPARQLWrapper): nsBindings = {} @@ -89,7 +72,7 @@ def injectPrefixes(self, query): BNODE_IDENT_PATTERN = re.compile('(?P