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

Python3 support - non-working #8

Closed
wants to merge 3 commits into from
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
27 changes: 27 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.*
*.egg-info
*.orig
*.pyc
__pycache__
build
dist
run-docker-tests

docs/_build

# Go language doesn't support **-globbing

*/*.egg-info
*/*.orig
*/*.pyc
*/__pycache__

*/*.egg-info
*/*/*.orig
*/*/*.pyc
*/*/__pycache__

*/*/*.egg-info
*/*/*/*.orig
*/*/*/*.pyc
*/*/*/__pycache__
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM funkyfuture/nest-of-serpents

ENTRYPOINT tox
WORKDIR /src

RUN pyp install flake8 pytest rdflib six Sphinx tox \
&& mkdir /home/tox \
&& mv /root/.cache /home/tox/

# this will be set to the user's id who's running a script that uses this
# image in order to have file-ownerships on the host-system
# at some point there should be native Docker-implementation for this
ENV TOX_USER_ID=1000
RUN useradd --uid=$TOX_USER_ID -m tox \
&& chown -R tox.tox /home/tox/.cache

ADD . .
RUN chown -R tox.tox .

USER tox
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,12 @@ Testing
$ tox
$ # or run the tests in surf
$ py.test surf


If you have host running *Docker* you can easily test against different interpreters with

.. code:: sh

$ ./run-docker-tests.sh

If you have a ``.tox`` folder in the project-directory, it will be used to keep the tox-environments.
63 changes: 12 additions & 51 deletions ez_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

This file can also be run as a script to install or upgrade setuptools.
"""
from hashlib import md5
import os
import sys


DEFAULT_VERSION = "0.6c11"
DEFAULT_URL = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3]

md5_data = {
MD5_DATA = {
'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca',
'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb',
'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b',
Expand Down Expand Up @@ -62,21 +65,19 @@
'setuptools-0.6c9-py2.6.egg': 'ca37b1ff16fa2ede6e19383e7b59245a',
}

import sys, os
try: from hashlib import md5
except ImportError: from md5 import md5

def _validate_md5(egg_name, data):
if egg_name in md5_data:
if egg_name in MD5_DATA:
digest = md5(data).hexdigest()
if digest != md5_data[egg_name]:
if digest != MD5_DATA[egg_name]:
print >>sys.stderr, (
"md5 validation of %s failed! (Possible download problem?)"
% egg_name
)
sys.exit(2)
return data


def use_setuptools(
version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
download_delay=15
Expand All @@ -100,10 +101,10 @@ def do_download():
try:
import pkg_resources
except ImportError:
return do_download()
return do_download()
try:
pkg_resources.require("setuptools>="+version); return
except pkg_resources.VersionConflict, e:
except pkg_resources.VersionConflict as e:
if was_imported:
print >>sys.stderr, (
"The required version of setuptools (>=%s) is not available, and\n"
Expand Down Expand Up @@ -166,40 +167,6 @@ def download_setuptools(
return os.path.realpath(saveto)




































def main(argv, version=DEFAULT_VERSION):
"""Install or upgrade setuptools and EasyInstall"""
try:
Expand Down Expand Up @@ -249,10 +216,10 @@ def update_md5(filenames):
for name in filenames:
base = os.path.basename(name)
f = open(name,'rb')
md5_data[base] = md5(f.read()).hexdigest()
MD5_DATA[base] = md5(f.read()).hexdigest()
f.close()

data = [" %r: %r,\n" % it for it in md5_data.items()]
data = [" %r: %r,\n" % it for it in MD5_DATA.items()]
data.sort()
repl = "".join(data)

Expand All @@ -276,9 +243,3 @@ def update_md5(filenames):
update_md5(sys.argv[2:])
else:
main(sys.argv[1:])






17 changes: 17 additions & 0 deletions run-docker-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

cd "$(dirname ${0})"

cp Dockerfile Dockerfile.orig
sed -i 's,^ENV TOX_USER_ID=.*\$,ENV TOX_USER_ID='$UID',g' Dockerfile

docker build -t surf-tox . && \
if [ -d ".tox" ]; then
docker run --rm --entrypoint="tox" -v "$(pwd)/.tox:/src/.tox" surf-tox "${@}"
else
docker run --rm --entrypoint="tox" surf-tox "${@}"
fi

mv Dockerfile.orig Dockerfile

cd -
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# in the documentation and/or other materials provided with
# the distribution.
# * Neither the name of DERI nor the
# names of its contributors may be used to endorse or promote
# names of its contributors may be used to endorse or promote
# products derived from this software without specific prior
# written permission.

Expand Down Expand Up @@ -92,6 +92,10 @@
'Operating System :: OS Independent',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language:: Python:: 3.2',
'Programming Language:: Python:: 3.3',
'Programming Language:: Python:: 3.4',
'Programming Language:: Python:: 3.5',
'Topic :: Software Development :: Libraries :: Python Modules',
],
keywords='Python SPARQL RDF resource mapper ORM query Semantic Web RDFS rdflib Object-Oriented',
Expand Down
6 changes: 5 additions & 1 deletion surf/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
# 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.

import six


__author__ = 'Cosmin Basca'

LOGGER_NAME = 'surf'
Expand Down Expand Up @@ -113,7 +117,7 @@ def set_logger_level(level, name=None):
logging._acquireLock()
try:
logger = logging.getLogger(name) if name else logging.root
if isinstance(level, basestring):
if isinstance(level, six.string_types):
level = level.upper()
logger.setLevel(level)
finally:
Expand Down
46 changes: 22 additions & 24 deletions surf/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
__author__ = ['Cosmin Basca', 'Peteris Caune']

from copy import deepcopy
import six
import sys
from surf.rdf import ClosedNamespace, Namespace, RDF, RDFS

Expand Down Expand Up @@ -113,40 +114,37 @@

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

# an internal inverted dict - for fast access
_INVERTED = {}
for k, v in 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():
if isinstance(v, (Namespace, ClosedNamespace)):
__DIRECT__[k] = v

namespaces = [(_unicode(obj), name) for name, obj in globals().items()
if isinstance(obj, (Namespace, ClosedNamespace)) and name != "_fallback_namespace"]
_INVERTED = dict(namespaces)

namespaces = [(name, obj) for name, obj in globals().items() if isinstance(obj, (Namespace, ClosedNamespace))]
__DIRECT__ = dict(namespaces)

del namespaces


def __add_inverted(prefix):
ns_dict = sys.modules[__name__].__dict__
_INVERTED[_unicode(ns_dict[prefix])] = prefix

def __add_direct(prefix):
ns_dict = sys.modules[__name__].__dict__
__DIRECT__[prefix] = ns_dict[prefix]

def all(copy=False):
""" Return all registered namespaces as a dict, if `copy` is `True` than a copy
of the dictionary is returned. By default, the actual internal dict is returned,
and therefore any changes are reflected in the namespace manager.
"""
return deepcopy(__DIRECT__) if copy else __DIRECT__

def base(property):
""" Return the base part of a URI, `property` is a string denoting a URI.

Expand Down Expand Up @@ -194,9 +192,9 @@ def register(**namespaces):
prefix = key.upper()
if not type(uri) in [Namespace, ClosedNamespace]:
uri = Namespace(uri)

ns_dict[prefix] = uri

# Also keep inverted dict up-to-date.
__add_inverted(prefix)
__add_direct(prefix)
Expand Down Expand Up @@ -227,7 +225,7 @@ def get_fallback_namespace():
def get_namespace(base):
""" Return the `namespace` short hand notation and the URI based on the
URI `base`.

The namespace is a `rdf.namespace.Namespace`

.. code-block:: python
Expand All @@ -240,8 +238,8 @@ def get_namespace(base):

global _anonymous_count
ns_dict = sys.modules[__name__].__dict__
if not type(base) in [str, unicode]:

if not isinstance(base, six.string_types):
base = str(base)

try:
Expand Down
18 changes: 8 additions & 10 deletions surf/plugin/rdflib/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# in the documentation and/or other materials provided with
# the distribution.
# * Neither the name of DERI nor the
# names of its contributors may be used to endorse or promote
# names of its contributors may be used to endorse or promote
# products derived from this software without specific prior
# written permission.

Expand All @@ -34,10 +34,8 @@

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

try:
from json import loads
except ImportError, e:
from simplejson import loads
from json import loads
import six
from surf.plugin.query_reader import RDFQueryReader
from surf.rdf import ConjunctiveGraph
from surf.log import *
Expand Down Expand Up @@ -77,19 +75,19 @@ def commit_pending_transaction_on_close(self):

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]
# rdflib.Variable is subclass of unicode strings. We convert them to
# unicode strings here anyway to hide rdflib internals from clients.
vars = [six.text_type(var) for var in result.vars]

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

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

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

Expand Down
Loading