Skip to content

Commit

Permalink
be more compatible when importing elementtree, and only in one place
Browse files Browse the repository at this point in the history
fixes #606
  • Loading branch information
gromgull committed Jan 12, 2017
1 parent 3e74b39 commit 86f31ed
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
27 changes: 27 additions & 0 deletions rdflib/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
4 changes: 2 additions & 2 deletions rdflib/plugins/sparql/results/xmlresults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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))
Expand Down
27 changes: 5 additions & 22 deletions rdflib/plugins/stores/sparqlstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import re
import collections
import urllib2
import warnings

try:
Expand All @@ -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
Expand All @@ -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 = {}

Expand Down Expand Up @@ -89,7 +72,7 @@ def injectPrefixes(self, query):
BNODE_IDENT_PATTERN = re.compile('(?P<label>_\:[^\s]+)')
SPARQL_NS = Namespace('http://www.w3.org/2005/sparql-results#')
sparqlNsBindings = {u'sparql': SPARQL_NS}
ElementTree._namespace_map["sparql"] = SPARQL_NS
etree._namespace_map["sparql"] = SPARQL_NS


def _node_from_result(node):
Expand Down Expand Up @@ -418,7 +401,7 @@ def triples(self, (s, p, o), context=None):
self.timeout = self._timeout
self.setQuery(query)

doc = ElementTree.parse(SPARQLWrapper.query(self).response)
doc = etree.parse(SPARQLWrapper.query(self).response)
# ElementTree.dump(doc)
for rt, vars in _traverse_sparql_result_dom(
doc,
Expand Down Expand Up @@ -449,7 +432,7 @@ def __len__(self, context=None):
if self._is_contextual(context):
self.addParameter("default-graph-uri", context.identifier)
self.setQuery(q)
doc = ElementTree.parse(SPARQLWrapper.query(self).response)
doc = etree.parse(SPARQLWrapper.query(self).response)
rt, vars = iter(
_traverse_sparql_result_dom(
doc,
Expand Down Expand Up @@ -485,7 +468,7 @@ def contexts(self, triple=None):
else:
self.setQuery('SELECT ?name WHERE { GRAPH ?name {} }')

doc = ElementTree.parse(SPARQLWrapper.query(self).response)
doc = etree.parse(SPARQLWrapper.query(self).response)

return (
rt.get(Variable("name"))
Expand Down

0 comments on commit 86f31ed

Please sign in to comment.