Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,)
Expand All @@ -405,15 +410,17 @@ 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
from all contexts.
"""
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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,)
Expand Down Expand Up @@ -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):
Expand All @@ -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)):
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 12 additions & 2 deletions rdflib/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 14 additions & 3 deletions rdflib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion rdflib/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions rdflib/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 13 additions & 2 deletions rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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!",
Expand Down
6 changes: 5 additions & 1 deletion rdflib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down