Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: handle instantiation of named individual #29

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Own environment
py2/
py3/
.pytest_cache
/.tox
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio
Expand Down
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
language: python
python:
- "2.6"
- "2.7"
- "3.4"
- "3.6"
- "pypy"
# command to install dependencies
install:
- pip install -r requirements.txt
- pip install --upgrade pytest
- pip install --upgrade pytest-ordering
- pip install --upgrade setuptools pytest pytest-ordering
- python setup.py install
# command to run tests
script: py.test surf
script: py.test surf
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
rdflib>=4.2.1
SPARQLWrapper>=1.7.6
six>=1.10.0
six>=1.10.0
future
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
deps = [
'rdflib>=4.2.1',
'SPARQLWrapper>=1.7.6',
"future"
]

test_deps = [
Expand Down Expand Up @@ -92,6 +93,7 @@
'Operating System :: OS Independent',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Topic :: Software Development :: Libraries :: Python Modules',
],
keywords='Python SPARQL RDF resource mapper ORM query Semantic Web RDFS rdflib Object-Oriented',
Expand Down
1 change: 1 addition & 0 deletions surf/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
# OF THE POSSIBILITY OF SUCH DAMAGE.

# -*- coding: utf-8 -*-
from builtins import map
from collections import namedtuple

__author__ = 'Cosmin Basca'
Expand Down
1 change: 1 addition & 0 deletions surf/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
from past.builtins import basestring
__author__ = 'Cosmin Basca'

LOGGER_NAME = 'surf'
Expand Down
14 changes: 8 additions & 6 deletions surf/namespace.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import str
# Copyright (c) 2009, Digital Enterprise Research Institute (DERI),
# NUI Galway
# All rights reserved.
Expand Down Expand Up @@ -34,6 +35,7 @@

# -*- coding: utf-8 -*-

from past.builtins import basestring
__author__ = ['Cosmin Basca', 'Peteris Caune']

from copy import deepcopy
Expand Down Expand Up @@ -114,22 +116,22 @@

# Fix for http://code.google.com/p/rdflib/issues/detail?id=154
def _unicode(namespace):
uri = unicode(namespace)
uri = str(namespace)
if not isinstance(uri, basestring) and hasattr(namespace, 'uri'):
uri = unicode(namespace.uri)
uri = str(namespace.uri)
return uri

# an internal inverted dict - for fast access
_INVERTED = {}
for k, v in sys.modules[__name__].__dict__.items():
for k, v in list(sys.modules[__name__].__dict__.items()):
if isinstance(v, (Namespace, ClosedNamespace)):
if k == "_fallback_namespace":
# no, this is not a namespace prefix, this is just a variable name
continue
_INVERTED[_unicode(v)] = k

__DIRECT__ = {}
for k, v in sys.modules[__name__].__dict__.items():
for k, v in list(sys.modules[__name__].__dict__.items()):
if isinstance(v, (Namespace, ClosedNamespace)):
__DIRECT__[k] = v

Expand Down Expand Up @@ -193,7 +195,7 @@ def register(**namespaces):
for key in namespaces:
uri = namespaces[key]
prefix = key.upper()
if not type(uri) in [Namespace, ClosedNamespace]:
if not isinstance(uri, (Namespace, ClosedNamespace)):
uri = Namespace(uri)

ns_dict[prefix] = uri
Expand Down Expand Up @@ -242,7 +244,7 @@ def get_namespace(base):
global _anonymous_count
ns_dict = sys.modules[__name__].__dict__

if not type(base) in [str, unicode]:
if not isinstance(base, (str, str)):
base = str(base)

try:
Expand Down
7 changes: 5 additions & 2 deletions surf/noconflict.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
# Recipe 204197: SOLVING THE METACLASS CONFLICT
# http://code.activestate.com/recipes/204197/

import inspect, types, __builtin__
from future import standard_library
standard_library.install_aliases()
from builtins import map
import inspect, types, builtins

'''
.. note:: This module appears `as is` and is based on the `Recipe 204197:
Expand All @@ -27,7 +30,7 @@ def skip_redundant(iterable, skipset=None):


def remove_redundant(metaclasses):
skipset = set([types.ClassType])
skipset = set([type])
for meta in metaclasses: # determines the metaclasses to be skipped
skipset.update(inspect.getmro(meta)[1:])
return tuple(skip_redundant(metaclasses, skipset))
Expand Down
6 changes: 3 additions & 3 deletions surf/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@
# OF THE POSSIBILITY OF SUCH DAMAGE.

# -*- coding: utf-8 -*-
from builtins import object
from abc import ABCMeta
import logging
from future.utils import with_metaclass

__author__ = 'Cosmin Basca'


class Plugin(object):
class Plugin(with_metaclass(ABCMeta, object)):
"""
Super class for all SuRF plugins, provides basic instantiation and `logging`.
"""

__metaclass__ = ABCMeta

def __init__(self, *args, **kwargs):
super(Plugin, self).__init__()
self._inference = False
Expand Down
4 changes: 2 additions & 2 deletions surf/plugin/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def registered_readers():
:return: the registered reader plugins
:rtype: list or set
"""
return _readers.keys()
return list(_readers.keys())


def registered_writers():
Expand All @@ -133,7 +133,7 @@ def registered_writers():
:return: the registered writer plugins
:rtype: list or set
"""
return _writers.keys()
return list(_writers.keys())


def get_reader(reader_id, *args, **kwargs):
Expand Down
13 changes: 7 additions & 6 deletions surf/plugin/query_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
# OF THE POSSIBILITY OF SUCH DAMAGE.

# -*- coding: utf-8 -*-
from builtins import range
from past.builtins import basestring
from abc import ABCMeta, abstractmethod

from surf.plugin.reader import RDFReader
from surf.query import Query, Union
from surf.query import a, ask, select, optional_group, named_group
from surf.rdf import URIRef
from surf.log import *
from future.utils import with_metaclass

__author__ = 'Cosmin Basca'

Expand Down Expand Up @@ -155,7 +158,7 @@ def order_terms(s, p, o):
return (s, p, o) if direct else (o, p, s)

for attribute, values, direct in params["get_by"]:
if hasattr(values, "__iter__"):
if not isinstance(values, basestring) and hasattr(values, "__iter__"):
where_clause = Union()
for value in values:
where_clause.append(order_terms("?s", attribute, value))
Expand Down Expand Up @@ -190,17 +193,15 @@ def order_terms(s, p, o):
return query


class RDFQueryReader(RDFReader):
class RDFQueryReader(with_metaclass(ABCMeta, RDFReader)):
"""
Super class for SuRF Reader plugins that wrap queryable `stores`.
"""

__metaclass__ = ABCMeta

def __init__(self, *args, **kwargs):
super(RDFQueryReader, self).__init__(*args, **kwargs)
self.use_subqueries = kwargs.get('use_subqueries', False)
if isinstance(self.use_subqueries, str):
if isinstance(self.use_subqueries, basestring):
self.use_subqueries = (self.use_subqueries.lower() == 'true')
elif not isinstance(self.use_subqueries, bool):
raise ValueError('The use_subqueries parameter must be a bool or a string set to "true" or "false"')
Expand Down Expand Up @@ -244,7 +245,7 @@ def _get_by(self, params):
query.optional_group(("?s", a, "?c"))

context = params.get("context", None)
if not (context is None):
if context is not None:
query.from_(context)

# Load just subjects and their types
Expand Down
10 changes: 6 additions & 4 deletions surf/plugin/rdflib/reader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import str
# Copyright (c) 2009, Digital Enterprise Research Institute (DERI),
# NUI Galway
# All rights reserved.
Expand Down Expand Up @@ -34,9 +35,10 @@

# -*- coding: utf-8 -*-

from builtins import zip
try:
from json import loads
except ImportError, e:
except ImportError as e:
from simplejson import loads
from surf.plugin.query_reader import RDFQueryReader
from surf.rdf import ConjunctiveGraph
Expand Down Expand Up @@ -79,17 +81,17 @@ def _to_table(self, result):
# Elements in result.selectionF are instances of rdflib.Variable,
# rdflib.Variable is subclass of unicode. We convert them to
# unicode here anyway to hide rdflib internals from clients.
vars = [unicode(var) for var in result.vars]
vars = [str(var) for var in result.vars]

# Convert each row to dict: { var->value, ... }
return [dict(zip(vars, row)) for row in result]
return [dict(list(zip(vars, row))) for row in result]

def _ask(self, result):
# askAnswer is list with boolean values, we want first value.
return result.askAnswer[0]

def _execute(self, query):
q_string = unicode(query)
q_string = str(query)
debug(q_string)
return self._graph.query(q_string)

Expand Down
4 changes: 2 additions & 2 deletions surf/plugin/rdflib/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _save(self, *resources):
for resource in resources:
s = resource.subject
self._remove_from_graph(s)
for p, objs in resource.rdf_direct.items():
for p, objs in list(resource.rdf_direct.items()):
for o in objs:
self.__add(s, p, o)

Expand All @@ -95,7 +95,7 @@ def _update(self, *resources):
s = resource.subject
for p in resource.rdf_direct:
self._remove_from_graph(s, p)
for p, objs in resource.rdf_direct.items():
for p, objs in list(resource.rdf_direct.items()):
for o in objs:
self.__add(s, p, o)

Expand Down
5 changes: 2 additions & 3 deletions surf/plugin/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@
from abc import ABCMeta, abstractmethod

from surf.plugin import Plugin
from future.utils import with_metaclass

__author__ = 'Cosmin Basca'


class RDFReader(Plugin):
class RDFReader(with_metaclass(ABCMeta, Plugin)):
"""
Super class for all surf Reader plugins.
"""

__metaclass__ = ABCMeta

@abstractmethod
def _get(self, subject, attribute, direct, context):
"""
Expand Down
18 changes: 10 additions & 8 deletions surf/plugin/sparql_protocol/reader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import str
# Copyright (c) 2009, Digital Enterprise Research Institute (DERI),
# NUI Galway
# All rights reserved.
Expand Down Expand Up @@ -33,6 +34,7 @@
# OF THE POSSIBILITY OF SUCH DAMAGE.

# -*- coding: utf-8 -*-
from future.utils import raise_
__author__ = 'Cosmin Basca'

import sys
Expand Down Expand Up @@ -83,7 +85,7 @@ def _to_table(self, result):
converted = []
for binding in result["results"]["bindings"]:
rdf_item = {}
for key, obj in binding.items():
for key, obj in list(binding.items()):
try:
rdf_item[key] = json_to_rdflib(obj)
except ValueError:
Expand All @@ -103,15 +105,15 @@ def execute_sparql(self, q_string, format='JSON'):
debug(q_string)
self._sparql_wrapper.setQuery(q_string)
return self._sparql_wrapper.query().convert()
except EndPointNotFound, _:
raise SparqlReaderException("Endpoint not found"), None, sys.exc_info()[2]
except QueryBadFormed, _:
raise SparqlReaderException("Bad query: %s" % q_string), None, sys.exc_info()[2]
except Exception, e:
raise SparqlReaderException("Exception: %s" % e), None, sys.exc_info()[2]
except EndPointNotFound as _:
raise_(SparqlReaderException, "Endpoint not found", sys.exc_info()[2])
except QueryBadFormed as _:
raise_(SparqlReaderException, "Bad query: %s" % q_string, sys.exc_info()[2])
except Exception as e:
raise_(SparqlReaderException, "Exception: %s" % e, sys.exc_info()[2])

def _execute(self, query):
return self.execute_sparql(unicode(query))
return self.execute_sparql(str(query))

def close(self):
pass
Loading