Permalink
Browse files

get rid of six dependency by adding a translationstring.compat module

  • Loading branch information...
1 parent 18c956a commit d6e2590b8228fc6517c5cf8060ca73e6864d3b32 @mcdonc mcdonc committed Jul 21, 2011
View
@@ -12,12 +12,10 @@
README = ''
CHANGES = ''
-requires = ['six']
-
setup(name='translationstring',
version='0.3',
description=('Utility library for i18n relied on by various Repoze '
- 'packages'),
+ 'and Pyramid packages'),
long_description=README + '\n\n' + CHANGES,
classifiers=[
"Intended Audience :: Developers",
@@ -33,8 +31,6 @@
packages=find_packages(),
include_package_data=True,
zip_safe=False,
- tests_require = requires,
- install_requires = requires,
test_suite="translationstring",
)
View
@@ -1,6 +1,6 @@
[tox]
envlist =
- py24,py25,py26,py27,jython,pypy,cover
+ py24,py25,py26,py27,py32,jython,pypy,cover
[testenv]
commands =
@@ -20,11 +20,11 @@ commands =
deps =
Babel
nose
- coverage<3.4
+ coverage==3.4
nosexcover
# we separate coverage into its own testenv because a) "last run wins" wrt
# cobertura jenkins reporting and b) pypy and jython can't handle any
# combination of versions of coverage and nosexcover that i can find.
-# coverage <3.4 is required by nosexcover 1.0.4.
+# coverage==3.4 is required by nosexcover.
@@ -1,13 +1,15 @@
import re
-import six
from gettext import NullTranslations
+from translationstring.compat import text_type
+from translationstring.compat import string_types
+from translationstring.compat import PY3
NAME_RE = r"[a-zA-Z][-a-zA-Z0-9_]*"
_interp_regex = re.compile(r'(?<!\$)(\$(?:(%(n)s)|{(%(n)s)}))'
% ({'n': NAME_RE}))
-class TranslationString(six.text_type):
+class TranslationString(text_type):
"""
The constructor for a :term:`translation string`. A translation
string is a Unicode-like object that has some extra metadata.
@@ -62,15 +64,15 @@ def __new__(self, msgid, domain=None, default=None, mapping=None):
# identity* of a non-``None`` but empty ``default`` value
# provided to it. See the comment in ChameleonTranslate.
- self = six.text_type.__new__(self, msgid)
+ self = text_type.__new__(self, msgid)
if isinstance(msgid, self.__class__):
domain = domain or msgid.domain and msgid.domain[:]
default = default or msgid.default and msgid.default[:]
mapping = mapping or msgid.mapping and msgid.mapping.copy()
- msgid = six.text_type(msgid)
+ msgid = text_type(msgid)
self.domain = domain
if default is None:
- default = six.text_type(msgid)
+ default = text_type(msgid)
self.default = default
self.mapping = mapping
return self
@@ -98,7 +100,7 @@ def interpolate(self, translated=None):
if self.mapping and translated:
def replace(match):
whole, param1, param2 = match.groups()
- return six.text_type(self.mapping.get(param1 or param2, whole))
+ return text_type(self.mapping.get(param1 or param2, whole))
translated = _interp_regex.sub(replace, translated)
return translated
@@ -107,7 +109,7 @@ def __reduce__(self):
return self.__class__, self.__getstate__()
def __getstate__(self):
- return six.text_type(self), self.domain, self.default, self.mapping
+ return text_type(self), self.domain, self.default, self.mapping
def TranslationStringFactory(domain):
""" Create a factory which will generate translation strings
@@ -175,7 +177,7 @@ def translate(msgid, domain=None, mapping=None, context=None,
# preserving ``default`` in the aforementioned case. So we
# spray these indignant comments all over this module. ;-)
- if not isinstance(msgid, six.string_types):
+ if not isinstance(msgid, string_types):
return msgid
tstring = msgid
@@ -196,7 +198,7 @@ def ugettext_policy(translations, tstring, domain):
""" A translator policy function which unconditionally uses the
``ugettext`` API on the translations object."""
- if six.PY3: # pragma: no cover
+ if PY3: # pragma: no cover
_gettext = translations.gettext
else: # pragma: no cover
_gettext = translations.ugettext
@@ -213,7 +215,7 @@ def dugettext_policy(translations, tstring, domain):
if getattr(translations, 'dugettext', None) is not None:
return translations.dugettext(domain, tstring)
- if six.PY3: # pragma: no cover
+ if PY3: # pragma: no cover
_gettext = translations.gettext
else: # pragma: no cover
_gettext = translations.ugettext
@@ -265,7 +267,7 @@ def ungettext_policy(translations, singular, plural, n, domain):
""" A pluralizer policy function which unconditionally uses the
``ungettext`` API on the translations object."""
- if six.PY3: # pragma: no cover
+ if PY3: # pragma: no cover
_gettext = translations.ngettext
else: # pragma: no cover
_gettext = translations.ungettext
@@ -283,7 +285,7 @@ def dungettext_policy(translations, singular, plural, n, domain):
if getattr(translations, 'dungettext', None) is not None:
return translations.dungettext(domain, singular, plural, n)
- if six.PY3: # pragma: no cover
+ if PY3: # pragma: no cover
_gettext = translations.ngettext
else: # pragma: no cover
_gettext = translations.ungettext
@@ -326,7 +328,8 @@ def pluralizer(singular, plural, n, domain=None, mapping=None):
translations = NullTranslations()
def pluralizer(singular, plural, n, domain=None, mapping=None):
""" Pluralize this object """
- translated = six.text_type(policy(translations, singular, plural, n, domain))
+ translated = text_type(
+ policy(translations, singular, plural, n, domain))
if translated and '$' in translated and mapping:
return TranslationString(translated, mapping=mapping).interpolate()
return translated
@@ -1,5 +1,6 @@
import unittest
-import six
+from translationstring import text_type
+from translationstring.compat import u
class TestTranslationString(unittest.TestCase):
def _getTargetClass(self):
@@ -12,7 +13,7 @@ def _makeOne(self, msgid, **kw):
def test_is_text_type_subclass(self):
inst = self._makeOne('msgid')
- self.failUnless(isinstance(inst, six.text_type))
+ self.failUnless(isinstance(inst, text_type))
def test_msgid_is_translation_string(self):
another = self._makeOne('msgid', domain='domain', default='default',
@@ -46,55 +47,56 @@ def test_interpolate_substitution(self):
inst = self._makeOne('This is $name version ${version}.',
mapping=mapping)
result = inst.interpolate()
- self.assertEqual(result, six.u('This is Zope version 3.'))
+ self.assertEqual(result, u('This is Zope version 3.'))
def test_interpolate_subsitution_more_than_once(self):
mapping = {"name": "Zope", "version": 3}
inst = self._makeOne(
- six.u("This is $name version ${version}. ${name} $version!"),
+ u("This is $name version ${version}. ${name} $version!"),
mapping=mapping)
result = inst.interpolate()
- self.assertEqual(result, six.u('This is Zope version 3. Zope 3!'))
+ self.assertEqual(result, u('This is Zope version 3. Zope 3!'))
def test_interpolate_double_dollar_escape(self):
mapping = {"name": "Zope", "version": 3}
inst = self._makeOne('$$name', mapping=mapping)
result = inst.interpolate()
- self.assertEqual(result, six.u('$$name'))
+ self.assertEqual(result, u('$$name'))
def test_interpolate_missing_not_interpolated(self):
mapping = {"name": "Zope", "version": 3}
inst = self._makeOne(
- six.u("This is $name $version. $unknown $$name $${version}."),
+ u("This is $name $version. $unknown $$name $${version}."),
mapping=mapping)
result = inst.interpolate()
self.assertEqual(result,
- six.u('This is Zope 3. $unknown $$name $${version}.'))
+ u('This is Zope 3. $unknown $$name $${version}.'))
def test_interpolate_missing_mapping(self):
- inst = self._makeOne(six.u("This is ${name}"))
+ inst = self._makeOne(u("This is ${name}"))
result = inst.interpolate()
- self.assertEqual(result, six.u('This is ${name}'))
+ self.assertEqual(result, u('This is ${name}'))
def test_interpolate_passed_translated(self):
mapping = {"name": "Zope", "version": 3}
- inst = self._makeOne(six.u("This is ${name}"), mapping = mapping)
+ inst = self._makeOne(u("This is ${name}"), mapping = mapping)
result = inst.interpolate('That is ${name}')
- self.assertEqual(result, six.u('That is Zope'))
+ self.assertEqual(result, u('That is Zope'))
def test___reduce__(self):
klass = self._getTargetClass()
inst = self._makeOne('msgid', default='default', domain='domain',
mapping='mapping')
result = inst.__reduce__()
- self.assertEqual(result, (klass, (six.u('msgid'), 'domain', six.u('default'),
+ self.assertEqual(result, (klass, (u('msgid'), 'domain', u('default'),
'mapping')))
def test___getstate__(self):
inst = self._makeOne('msgid', default='default', domain='domain',
mapping='mapping')
result = inst.__getstate__()
- self.assertEqual(result, (six.u('msgid'), 'domain', six.u('default'), 'mapping'))
+ self.assertEqual(result,
+ (u('msgid'), 'domain', u('default'), 'mapping'))
class TestTranslationStringFactory(unittest.TestCase):
def _makeOne(self, domain):
@@ -136,10 +138,10 @@ def test_msgid_translationstring_translator_is_None(self):
self.assertEqual(result, 'interpolated')
def test_msgid_text_type_translator_is_None(self):
- msgid = six.u('foo')
+ msgid = u('foo')
translate = self._makeOne(None)
result = translate(msgid)
- self.assertEqual(result, six.u('foo'))
+ self.assertEqual(result, u('foo'))
def test_msgid_translationstring_translator_is_not_None(self):
msgid = DummyTranslationString()
@@ -350,10 +352,10 @@ def dungettext(self, domain, singular, plural, n): # pragma: no cover
self.asked_domain = domain
return self.result
-class DummyTranslationString(six.text_type):
+class DummyTranslationString(text_type):
def __new__(cls, msgid='', domain=None, default=None, mapping=None):
- self = six.text_type.__new__(cls, msgid)
- six.text_type.__init__(self, msgid)
+ self = text_type.__new__(cls, msgid)
+ text_type.__init__(self, msgid)
self.domain = domain
self.mapping = mapping
if default is None:
@@ -1,4 +1,3 @@
-import gettext
import unittest
from translationstring.tests.translations import Translations
@@ -84,4 +83,4 @@ def test_pluralizer_dungettext_policy(self):
domain='messages',
mapping={'users':'users'})
self.assertEqual(result,
- "Benutzer konnte nicht gefunden werden: users")
+ "Benutzer konnte nicht gefunden werden: users")
@@ -1,5 +1,5 @@
import gettext
-import six
+from translationstring.compat import PY3
class Translations(gettext.GNUTranslations, object):
"""An extended translation catalog class."""
@@ -39,7 +39,7 @@ def dugettext(self, domain, message):
"""Like ``ugettext()``, but look the message up in the specified
domain.
"""
- if six.PY3: # pragma: no cover
+ if PY3: # pragma: no cover
return self._domains.get(domain, self).gettext(message)
else: # pragma: no cover
return self._domains.get(domain, self).ugettext(message)
@@ -48,10 +48,12 @@ def dungettext(self, domain, singular, plural, num):
"""Like ``ungettext()`` but look the message up in the specified
domain.
"""
- if six.PY3: # pragma: no cover
- return self._domains.get(domain, self).ngettext(singular, plural, num)
+ if PY3: # pragma: no cover
+ return self._domains.get(domain, self).ngettext(
+ singular, plural, num)
else: # pragma: no cover
- return self._domains.get(domain, self).ungettext(singular, plural, num)
+ return self._domains.get(domain, self).ungettext(
+ singular, plural, num)
# Most of the downwards code, until it get's included in stdlib, from:
# http://bugs.python.org/file10036/gettext-pgettext.patch

0 comments on commit d6e2590

Please sign in to comment.