diff --git a/rdflib/compat.py b/rdflib/compat.py index 6c71489497..310d9ff027 100644 --- a/rdflib/compat.py +++ b/rdflib/compat.py @@ -23,3 +23,36 @@ 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") + +try: + etree_register_namespace = etree.register_namespace +except AttributeError: + def etree_register_namespace(prefix, uri): + etree._namespace_map[uri] = prefix diff --git a/rdflib/plugins/sparql/results/xmlresults.py b/rdflib/plugins/sparql/results/xmlresults.py index 85c33882e9..b31f9513b4 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 d3557b3839..aff8c3d606 100644 --- a/rdflib/plugins/stores/sparqlstore.py +++ b/rdflib/plugins/stores/sparqlstore.py @@ -13,9 +13,8 @@ import re import collections -import contextlib -import urllib2 import warnings +import contextlib try: from SPARQLWrapper import SPARQLWrapper, XML, POST, GET, URLENCODED, POSTDIRECTLY @@ -24,20 +23,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, etree_register_namespace from rdflib.plugins.stores.regexmatching import NATIVE_REGEX from rdflib.store import Store @@ -46,9 +32,6 @@ from rdflib.graph import DATASET_DEFAULT_GRAPH_ID from rdflib.term import Node -import httplib -import urlparse - class NSSPARQLWrapper(SPARQLWrapper): nsBindings = {} @@ -90,7 +73,7 @@ def injectPrefixes(self, query): BNODE_IDENT_PATTERN = re.compile('(?P