diff options
author | Jason Madden <jamadden@gmail.com> | 2017-12-18 09:26:59 -0600 |
---|---|---|
committer | Jason Madden <jamadden@gmail.com> | 2017-12-18 09:59:12 -0600 |
commit | 5b0ee8a60ad660039591d48e755168a87a33c921 (patch) | |
tree | 2fd934a05077916f0e2286568f45d67c26cc8c87 | |
parent | a455b5485dee424f8e96b51c33fae4f41ba08470 (diff) | |
download | zope-i18n-5b0ee8a60ad660039591d48e755168a87a33c921.tar.gz |
require python-gettext in install_requires
-rw-r--r-- | setup.py | 5 | ||||
-rw-r--r-- | src/zope/i18n/__init__.py | 12 | ||||
-rw-r--r-- | src/zope/i18n/compile.py | 100 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_compile.py | 3 |
4 files changed, 56 insertions, 64 deletions
@@ -40,7 +40,9 @@ def alltests(): return unittest.TestSuite(suites) COMPILE_REQUIRES = [ - 'python-gettext' + # python-gettext used to be here, but it's now + # a fixed requirement. Keep the extra to avoid + # breaking downstream installs. ] ZCML_REQUIRES = [ @@ -94,6 +96,7 @@ setup( namespace_packages=['zope',], install_requires=[ 'setuptools', + 'python-gettext', 'pytz', 'zope.schema', 'zope.i18nmessageid', diff --git a/src/zope/i18n/__init__.py b/src/zope/i18n/__init__.py index 79db793..3088074 100644 --- a/src/zope/i18n/__init__.py +++ b/src/zope/i18n/__init__.py @@ -140,20 +140,20 @@ def translate(msgid, domain=None, mapping=None, context=None, :hide: >>> from zope import i18n - >>> old_negatiate = i18n + >>> old_negotiate = i18n.negotiate - >>> def test_negatiate(context): - ... print("Negatiating for %r" % (context,)) + >>> def test_negotiate(context): + ... print("Negotiating for %r" % (context,)) ... return 'en' - >>> i18n.negotiate = test_negatiate + >>> i18n.negotiate = test_negotiate >>> print(translate('eek', 'your.domain', context='context')) - Negatiating for 'context' + Negotiating for 'context' test-from-your.domain .. doctest:: :hide: - >>> i18n.negotiate = old_negatiate + >>> i18n.negotiate = old_negotiate """ if isinstance(msgid, Message): diff --git a/src/zope/i18n/compile.py b/src/zope/i18n/compile.py index e4f3be2..2856628 100644 --- a/src/zope/i18n/compile.py +++ b/src/zope/i18n/compile.py @@ -1,61 +1,53 @@ - +from contextlib import closing +import os.path +from os.path import join import logging + +from pythongettext.msgfmt import Msgfmt +from pythongettext.msgfmt import PoSyntaxError + logger = logging.getLogger('zope.i18n') -def _cannot_compile_mo_file(domain, lc_messages_path): - logger.critical("Unable to compile messages: Python `gettext` library missing.") - return -try: - from pythongettext.msgfmt import Msgfmt - from pythongettext.msgfmt import PoSyntaxError -except ImportError: # pragma: no cover - HAS_PYTHON_GETTEXT = False - compile_mo_file = _cannot_compile_mo_file -else: - from contextlib import closing - import os.path - from os.path import join +HAS_PYTHON_GETTEXT = True + +def _safe_mtime(path): + try: + return os.path.getmtime(path) + except (IOError, OSError): + return None + +def compile_mo_file(domain, lc_messages_path): + """Creates or updates a mo file in the locales folder.""" + + base = join(lc_messages_path, domain) + pofile = str(base + '.po') + mofile = str(base + '.mo') + + po_mtime = _safe_mtime(pofile) + mo_mtime = _safe_mtime(mofile) if os.path.exists(mofile) else 0 - HAS_PYTHON_GETTEXT = True + if po_mtime is None or mo_mtime is None: + logger.debug("Unable to access either %s (%s) or %s (%s)", + pofile, po_mtime, mofile, mo_mtime) + return - def _safe_mtime(path): + if po_mtime > mo_mtime: try: - return os.path.getmtime(path) - except (IOError, OSError): - return None - - def compile_mo_file(domain, lc_messages_path): - """Creates or updates a mo file in the locales folder.""" - - base = join(lc_messages_path, domain) - pofile = str(base + '.po') - mofile = str(base + '.mo') - - po_mtime = _safe_mtime(pofile) - mo_mtime = _safe_mtime(mofile) if os.path.exists(mofile) else 0 - - if po_mtime is None or mo_mtime is None: - logger.debug("Unable to access either %s (%s) or %s (%s)", - pofile, po_mtime, mofile, mo_mtime) - return - - if po_mtime > mo_mtime: - try: - # Msgfmt.getAsFile returns io.BytesIO on Python 3, and cStringIO.StringIO - # on Python 2; sadly StringIO isn't a proper context manager, so we have to - # wrap it with `closing`. Also, Msgfmt doesn't properly close a file - # it opens for reading if you pass the path, but it does if you pass - # the file. - with open(pofile, 'rb') as pofd: - with closing(Msgfmt(pofd, domain).getAsFile()) as mo: - with open(mofile, 'wb') as fd: - fd.write(mo.read()) - # For testing we return distinct values for each scenario - return True - except PoSyntaxError as err: - logger.warning('Syntax error while compiling %s (%s).', pofile, err.msg) - return 0 - except (IOError, OSError) as err: - logger.warning('Error while compiling %s (%s).', pofile, err) - return False + # Msgfmt.getAsFile returns io.BytesIO on Python 3, and cStringIO.StringIO + # on Python 2; sadly StringIO isn't a proper context manager, so we have to + # wrap it with `closing`. Also, Msgfmt doesn't properly close a file + # it opens for reading if you pass the path, but it does if you pass + # the file. + with open(pofile, 'rb') as pofd: + with closing(Msgfmt(pofd, domain).getAsFile()) as mo: + with open(mofile, 'wb') as fd: + fd.write(mo.read()) + # For testing we return distinct values for each scenario + return True + except PoSyntaxError as err: + logger.warning('Syntax error while compiling %s (%s).', pofile, err.msg) + return 0 + except (IOError, OSError) as err: + logger.warning('Error while compiling %s (%s).', pofile, err) + return False diff --git a/src/zope/i18n/tests/test_compile.py b/src/zope/i18n/tests/test_compile.py index 4a09ee4..1aa7bd9 100644 --- a/src/zope/i18n/tests/test_compile.py +++ b/src/zope/i18n/tests/test_compile.py @@ -63,6 +63,3 @@ class TestCompile(unittest.TestCase): self.assertIs( False, compile.compile_mo_file('foo', td)) - - def test_cannot_compile(self): - self.assertIsNone(compile._cannot_compile_mo_file(None, None)) |