From 307678c6c7b1bd8b26df332adf97d91369106190 Mon Sep 17 00:00:00 2001 From: Artem Popov Date: Thu, 13 Mar 2014 09:30:45 +0700 Subject: [PATCH] Misc py3 compatibility fixes. --- rdflib/graph.py | 39 +++++++++++++++++++++++++++------------ rdflib/namespace.py | 14 ++++++++++++-- rdflib/parser.py | 17 ++++++++++++++--- rdflib/plugin.py | 2 +- rdflib/query.py | 6 +++++- rdflib/store.py | 17 ++++++++++++----- rdflib/term.py | 15 +++++++++++++-- rdflib/util.py | 6 +++++- 8 files changed, 89 insertions(+), 27 deletions(-) diff --git a/rdflib/graph.py b/rdflib/graph.py index 9b412981b..1bdfde728 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -274,7 +274,11 @@ import os import shutil import tempfile -from urlparse import urlparse + +try: + from urlparse import urlparse +except ImportError: + from urllib.parse import urlparse __all__ = [ 'Graph', 'ConjunctiveGraph', 'QuotedGraph', 'Seq', @@ -386,7 +390,8 @@ def close(self, commit_pending_transaction=False): self.__store.close( commit_pending_transaction=commit_pending_transaction) - def add(self, (s, p, o)): + def add(self, triple): + (s, p, o) = triple """Add a triple with self as context""" assert isinstance(s, Node), \ "Subject %s must be an rdflib term" % (s,) @@ -405,7 +410,8 @@ def addN(self, quads): and _assertnode(s,p,o) ) - def remove(self, (s, p, o)): + def remove(self, triple): + (s, p, o) = triple """Remove a triple from the graph If the triple does not provide a context attribute, removes the triple @@ -413,7 +419,8 @@ def remove(self, (s, p, o)): """ self.__store.remove((s, p, o), context=self) - def triples(self, (s, p, o)): + def triples(self, triple): + (s, p, o) = triple """Generator over the triple store Returns triples that match the given triple pattern. If triple pattern @@ -642,7 +649,8 @@ def predicate_objects(self, subject=None): for s, p, o in self.triples((subject, None, None)): yield p, o - def triples_choices(self, (subject, predicate, object_), context=None): + def triples_choices(self, triple, context=None): + (subject, predicate, object_) = triple for (s, p, o), cg in self.store.triples_choices( (subject, predicate, object_), context=self): yield (s, p, o) @@ -1379,7 +1387,8 @@ def quads(self, triple_or_quad=None): for ctx in cg: yield s, p, o, ctx - def triples_choices(self, (s, p, o), context=None): + def triples_choices(self, triple, context=None): + (s, p, o) = triple """Iterate over all the triples in the entire conjunctive graph""" if context is None: @@ -1640,7 +1649,8 @@ class QuotedGraph(Graph): def __init__(self, store, identifier): super(QuotedGraph, self).__init__(store, identifier) - def add(self, (s, p, o)): + def add(self, triple): + (s, p, o) = triple """Add a triple with self as context""" assert isinstance(s, Node), \ "Subject %s must be an rdflib term" % (s,) @@ -1794,16 +1804,19 @@ def close(self): for graph in self.graphs: graph.close() - def add(self, (s, p, o)): + def add(self, triple): + (s, p, o) = triple raise ModificationException() def addN(self, quads): raise ModificationException() - def remove(self, (s, p, o)): + def remove(self, triple): + (s, p, o) = triple raise ModificationException() - def triples(self, (s, p, o)): + def triples(self, triple): + (s, p, o) = triple for graph in self.graphs: if isinstance(p, Path): for s, o in p.eval(self, s, o): @@ -1822,7 +1835,8 @@ def __contains__(self, triple_or_quad): return True return False - def quads(self, (s, p, o)): + def quads(self, triple): + (s, p, o) = triple """Iterate over all the quads in the entire aggregate graph""" for graph in self.graphs: for s1, p1, o1 in graph.triples((s, p, o)): @@ -1852,7 +1866,8 @@ def __isub__(self, other): # Conv. methods - def triples_choices(self, (subject, predicate, object_), context=None): + def triples_choices(self, triple, context=None): + (subject, predicate, object_) = triple for graph in self.graphs: choices = graph.triples_choices((subject, predicate, object_)) for (s, p, o) in choices: diff --git a/rdflib/namespace.py b/rdflib/namespace.py index 14d7b8243..9f4f49fd6 100644 --- a/rdflib/namespace.py +++ b/rdflib/namespace.py @@ -56,14 +56,24 @@ """) +import sys +(py_v_major, py_v_minor, py_v_micro, py_v_final, py_v_serial) = sys.version_info + import logging _logger = logging.getLogger(__name__) import os -from urlparse import urljoin, urldefrag -from urllib import pathname2url +if py_v_major >= 3: + from urllib.parse import urljoin, urldefrag + from urllib.request import pathname2url + unicode = str + basestring = str + long = int +else: + from urlparse import urljoin, urldefrag + from urllib import pathname2url from rdflib.term import URIRef, Variable, _XSD_PFX, _is_valid_uri diff --git a/rdflib/parser.py b/rdflib/parser.py index e662667bf..048698c89 100644 --- a/rdflib/parser.py +++ b/rdflib/parser.py @@ -12,9 +12,20 @@ import os import sys -from urllib import pathname2url, url2pathname -from urllib2 import urlopen, Request -from urlparse import urljoin +(py_v_major, py_v_minor, py_v_micro, py_v_final, py_v_serial) = sys.version_info + +if py_v_major >= 3: + from urllib.request import pathname2url, url2pathname + from urllib.request import urlopen, Request + from urllib.parse import urljoin + unicode = str + basestring = str + long = int +else: + from urllib import pathname2url, url2pathname + from urllib2 import urlopen, Request + from urlparse import urljoin + from rdflib.py3compat import PY3 if PY3: from io import BytesIO diff --git a/rdflib/plugin.py b/rdflib/plugin.py index b5d2efeba..481b5f9a4 100644 --- a/rdflib/plugin.py +++ b/rdflib/plugin.py @@ -110,7 +110,7 @@ def get(name, kind): pass # TODO: log a message else: # add the plugins specified via pkg_resources' EntryPoints. - for entry_point, kind in entry_points.iteritems(): + for entry_point, kind in entry_points.items(): for ep in iter_entry_points(entry_point): _plugins[(ep.name, kind)] = PKGPlugin(ep.name, kind, ep) diff --git a/rdflib/query.py b/rdflib/query.py index d4b8e3613..94b8aec96 100644 --- a/rdflib/query.py +++ b/rdflib/query.py @@ -5,7 +5,11 @@ import warnings import types -from urlparse import urlparse +try: + from urlparse import urlparse +except ImportError: + from urllib.parse import urlparse + try: from io import BytesIO assert BytesIO diff --git a/rdflib/store.py b/rdflib/store.py index 05f6286ff..76996a292 100644 --- a/rdflib/store.py +++ b/rdflib/store.py @@ -68,7 +68,11 @@ class TripleRemovedEvent(Event): - the ``graph`` from which the triple was removed """ -from cPickle import Pickler, Unpickler, UnpicklingError +try: + from cPickle import Pickler, Unpickler, UnpicklingError +except ImportError: + from pickle import Pickler, Unpickler, UnpicklingError + try: from io import BytesIO assert BytesIO @@ -97,7 +101,7 @@ def loads(self, s): up.persistent_load = self._get_object try: return up.load() - except KeyError, e: + except KeyError as e: raise UnpicklingError("Could not find Node class for %s" % e) def dumps(self, obj, protocol=None, bin=None): @@ -198,7 +202,8 @@ def gc(self): pass # RDF APIs - def add(self, (subject, predicate, object), context, quoted=False): + def add(self, triple, context, quoted=False): + (subject, predicate, object) = triple """ Adds the given statement to a specific context or to the model. The quoted argument is interpreted by formula-aware stores to indicate @@ -223,13 +228,15 @@ def addN(self, quads): "Context associated with %s %s %s is None!" % (s, p, o) self.add((s, p, o), c) - def remove(self, (subject, predicate, object), context=None): + def remove(self, triple, context=None): + (subject, predicate, object) = triple """ Remove the set of triples matching the pattern from the store """ self.dispatcher.dispatch( TripleRemovedEvent( triple=(subject, predicate, object), context=context)) - def triples_choices(self, (subject, predicate, object_), context=None): + def triples_choices(self, triple, context=None): + (subject, predicate, object_) = triple """ A variant of triples that can take a list of terms instead of a single term in any slot. Stores can implement this to optimize the response diff --git a/rdflib/term.py b/rdflib/term.py index ae3a4f86e..2b02d2dfd 100644 --- a/rdflib/term.py +++ b/rdflib/term.py @@ -35,6 +35,9 @@ 'Statement', ] +import sys +(py_v_major, py_v_minor, py_v_micro, py_v_final, py_v_serial) = sys.version_info + import logging import warnings @@ -43,7 +46,14 @@ import base64 import xml.dom.minidom -from urlparse import urlparse, urljoin, urldefrag +if py_v_major >= 3 : + from urllib.parse import urlparse, urlunparse, urlsplit, urljoin + unicode = str + basestring = str + long = int +else : + from urlparse import urlparse, urlunparse, urlsplit, urljoin + from datetime import date, time, datetime from re import sub, compile from collections import defaultdict @@ -1494,7 +1504,8 @@ def __reduce__(self): class Statement(Node, tuple): - def __new__(cls, (subject, predicate, object), context): + def __new__(cls, triple, context): + (subject, predicate, object) = triple warnings.warn( "Class Statement is deprecated, and will be removed in " + "the future. If you use this please let rdflib-dev know!", diff --git a/rdflib/util.py b/rdflib/util.py index 4df659c64..f0cd43877 100644 --- a/rdflib/util.py +++ b/rdflib/util.py @@ -38,7 +38,11 @@ from time import timezone from os.path import splitext -from StringIO import StringIO + +try: + from StringIO import StringIO +except ImportError: + from io import StringIO from rdflib.exceptions import ContextTypeError from rdflib.exceptions import ObjectTypeError