Skip to content

Commit

Permalink
Improve python3 compatibility (#9)
Browse files Browse the repository at this point in the history
* Fix python3 compatibility for `str(translation)` and `bool(translation)`
* Add tags for supported pythons
* Update CI configuration to test supported pythons
* Bump version to 1.3.0
  • Loading branch information
youtux committed Feb 10, 2020
1 parent 066e37c commit 056bcb7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 6 deletions.
14 changes: 12 additions & 2 deletions .travis.yml
@@ -1,13 +1,23 @@
language: python
python:
- "2.7"
- "3.5"
- "3.6"
- "3.7"
- "3.8"

# command to install dependencies
install:
- pip install python-coveralls virtualenv tox
# # command to run tests
- pip install coveralls virtualenv tox tox-travis

# command to run tests
script: tox

after_success:
- pip install -r requirements-testing.txt -e .
- py.test --cov=traduki --cov-report=term-missing tests
- coveralls

notifications:
email:
- opensource-tests@paylogic.com
6 changes: 6 additions & 0 deletions CHANGES.rst
@@ -1,6 +1,12 @@
Changelog
=========

1.3.0
-----

* Fix python 3 compatibility
* Declare support for python 2.7, 3.5, 3.6, 3.7, 3.8

1.2.0
-----

Expand Down
16 changes: 15 additions & 1 deletion setup.py
Expand Up @@ -12,16 +12,30 @@
name='traduki',
description='SQLAlchemy internationalisation',
long_description='\n'.join(long_description),
version='1.2.0',
version='1.3.0',
author='Paylogic International',
author_email='developers@paylogic.com',
license='MIT',
url='https://github.com/paylogic/traduki',
install_requires=[
'SQLAlchemy',
'six>=1.9.0',
],
packages=find_packages(exclude=['ez_setup', 'tests']),
dependency_links=[],
include_package_data=True,
keywords='sqlalchemy i18n internationalisation',
classifiers=[
'Development Status :: 5 - Production/Stable',

'Intended Audience :: Developers',

'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
]
)
27 changes: 27 additions & 0 deletions tests/test_i18n.py
@@ -1,6 +1,7 @@
"""Test SQLAlchemy i18n."""
from __future__ import unicode_literals
import pytest
import six

from sqlalchemy import Column, Integer, UnicodeText, String
from sqlalchemy import create_engine
Expand Down Expand Up @@ -110,6 +111,32 @@ def test_set(model):
assert model.title.get_dict() == {'en': 'New'}


@pytest.mark.parametrize(
'model_title, expected',
[
({'en': 'english title', 'nl': 'dutch title'}, 'english title'),
({'nl': 'dutch title'}, 'dutch title'),
({}, ''),
]
)
def test_stringification(model, expected):
"""Test that str(model) returns a string with the translated text."""
assert six.text_type(model.title) == expected


@pytest.mark.parametrize(
'model_title, expected',
[
({'en': 'title'}, True),
({'en': ''}, False),
({}, False),
]
)
def test_bool(model, expected):
"""Test that bool(model) returns only when there is an actual translation"""
assert bool(model.title) is expected


def test_startswith(session, model, model_class):
"""Test startswith comparator."""
assert model in session.query(model_class).filter(model_class.title.startswith('En Title'))
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
@@ -1,6 +1,6 @@
[tox]
distshare = {homedir}/.tox/distshare
envlist = py27, py33, py34
envlist = py27, py35, py36, py37, py38

[testenv]
commands = py.test --junitxml={envlogdir}/junit-{envname}.xml
Expand Down
10 changes: 8 additions & 2 deletions traduki/sqla.py
Expand Up @@ -8,6 +8,7 @@
"""
import collections

import six
from sqlalchemy import Column, Integer, ForeignKey, UnicodeText, event
from sqlalchemy.exc import ArgumentError
from sqlalchemy.orm import relationship, mapper
Expand All @@ -22,6 +23,7 @@
Attributes = collections.namedtuple('Attributes', ['Translation', 'i18n_column', 'i18n_relation'])


@six.python_2_unicode_compatible
class TranslationMixin(object):
"""Helper for future translation class which is created during initialization."""

Expand Down Expand Up @@ -52,11 +54,15 @@ def get_text(self, code=None, chain=None):
"""
return helpers.get_text_from_dict(self.get_dict(), code=code, chain=chain)

def __unicode__(self):
def __str__(self):
return self.get_text() or u''

def __bool__(self):
return bool(six.text_type(self))

# Python 2 compatibility
def __nonzero__(self):
return bool(unicode(self))
return self.__bool__()


def initialize(base, languages, get_current_language_callback, get_language_chain_callback, attributes=None):
Expand Down

0 comments on commit 056bcb7

Please sign in to comment.