Skip to content

Commit

Permalink
Make Python 3 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermendes committed May 23, 2018
1 parent e5a8021 commit c29efcb
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 15 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ settings. To change any settings make a copy of the template:
cp settings.py.tmpl settings.py
```

## Testing

Explicates is tested against Python 2.7 and 3.4.

```bash
# python 2
nosetests test/

# python 3
python3 -m "nose"
```

## Usage

Proper documentation to follow.
Expand Down
2 changes: 1 addition & 1 deletion alembic/versions/3b8038c6e43e_add_core_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def make_timestamp():


def make_uuid():
return unicode(uuid.uuid4())
return str(uuid.uuid4())


def upgrade():
Expand Down
22 changes: 11 additions & 11 deletions explicates/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from jsonschema import validate as validate_json
from jsonschema.exceptions import ValidationError
from sqlalchemy.exc import IntegrityError
from past.builtins import basestring

from explicates.core import repo
from explicates.model.annotation import Annotation
Expand All @@ -34,7 +35,6 @@ def _get_domain_object(self, model_cls, id, **kwargs):

def _get_iri(self, obj, **kwargs):
"""Get the IRI for an object."""

if isinstance(obj, Annotation):
kwargs.pop('iris', None)
return url_for('api.annotations', annotation_id=obj.id,
Expand Down Expand Up @@ -83,15 +83,15 @@ def _delete(self, obj):
repo.delete(model_cls, obj.key)
except (IntegrityError, TypeError) as err:
abort(400, err.message)

def _get_validated_data(self, model_cls):
data = request.get_json()
try:
self._validate_data(data, model_cls)
except ValidationError as err:
abort(400, err.message)
return data

def _validate_data(self, obj, model_cls):
"""Validate data according JSON schema for the model class."""
schema_fn = '{}.json'.format(model_cls.__name__.lower())
Expand Down Expand Up @@ -219,7 +219,7 @@ def _get_container(self, collection, items=None, total=None):
# results returned in fake containers
if total:
out['total'] = total

page = self._get_page_arg()
per_page = current_app.config.get('ANNOTATIONS_PER_PAGE')
n_pages = self._get_n_pages(items, out['total'], per_page)
Expand All @@ -229,24 +229,24 @@ def _get_container(self, collection, items=None, total=None):
if isinstance(page, int) and not items:
abort(404)
elif isinstance(page, int):
return self._get_page(page, n_pages, per_page, collection,
return self._get_page(page, n_pages, per_page, collection,
items, partof=out, **params)
elif minimal:
out['first'] = self._get_iri(collection, page=0, **params)
else:
out['first'] = self._get_page(0, n_pages, per_page, collection,
out['first'] = self._get_page(0, n_pages, per_page, collection,
items, **params)
if n_pages > 1:
out['last'] = self._get_iri(collection, page=n_pages - 1,
out['last'] = self._get_iri(collection, page=n_pages - 1,
**params)

return out

def _slice_annotations(self, items, per_page, page=0):
"""Return a slice of items. """
start = page * per_page if page > 0 else 0
start = page * per_page if page and page > 0 else 0
return items[start:start + per_page]

def _get_page_arg(self):
"""Return the page query param and check it's an int."""
page = request.args.get('page')
Expand All @@ -262,7 +262,7 @@ def _get_n_pages(self, items, total, per_page):
n = 0 if total <= 0 else (total - 1) // per_page
return n + 1

def _get_page(self, page, n_pages, per_page, collection, items,
def _get_page(self, page, n_pages, per_page, collection, items,
partof=None, **params):
"""Return an AnnotationPage."""
page_iri = self._get_iri(collection, page=page, **params)
Expand Down
2 changes: 1 addition & 1 deletion explicates/api/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _get_base_id(self, annotation):
iri = annotation.get('id')
if not iri:
abort(400, 'Invalid Annotation passed in request')
return unquote(iri.rstrip('/').split('/')[-1].encode())
return unquote(iri).rstrip('/').split('/')[-1]

def delete(self):
"""Batch delete items."""
Expand Down
2 changes: 1 addition & 1 deletion explicates/model/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def make_timestamp():

def make_uuid():
"""Return a Unicode UUID."""
return unicode(uuid.uuid4())
return str(uuid.uuid4())
3 changes: 2 additions & 1 deletion explicates/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sqlalchemy.sql import and_, or_
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.base import _entity_descriptor
from future.utils import iteritems


class Repository(object):
Expand Down Expand Up @@ -126,7 +127,7 @@ def _get_contains_clauses(self, model_cls, query):
def _get_relationship_clauses(self, model_cls, **kwargs):
"""Return relationship clauses."""
clauses = []
relationships = {k: v for k, v in kwargs.iteritems() if '.' in k}
relationships = {k: v for k, v in iteritems(kwargs) if '.' in k}
for k, v in relationships.items():
parts = k.split('.')
if len(parts) == 2:
Expand Down
2 changes: 2 additions & 0 deletions provisioning/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
- python-virtualenv
- python-setuptools
- python-pip
- python3-pip
- python3-nose

- name: upgrade pip
become_user: "{{explicates_user}}"
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"unidecode>=1.0.22, <2.0.0",
"zipstream>=1.1.4, <1.2.0",
"psycopg2>=2.5.2, <3.0",
"future>=0.16.0, <1.0.0",
"nose",
"mock",
"rednose",
Expand Down

0 comments on commit c29efcb

Please sign in to comment.