From: Berker Peksag Date: Mon, 28 Jul 2014 10:27:19 +0000 (+0300) Subject: Cleanup mediagoblin._compat and fix translation tests. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=cf3b5926f49a59d1547c2e4f098b9917c70168d0;p=mediagoblin.git Cleanup mediagoblin._compat and fix translation tests. --- diff --git a/mediagoblin/_compat.py b/mediagoblin/_compat.py index 65fb5140..5a3fac53 100644 --- a/mediagoblin/_compat.py +++ b/mediagoblin/_compat.py @@ -1,23 +1,15 @@ -from six import PY3, iteritems - -from mediagoblin import mg_globals +from six import PY3 if PY3: from email.mime.text import MIMEText - from urllib import parse as urlparse - # TODO(berker): Rename to gettext and ungettext instead? - ugettext = mg_globals.thread_scope.translations.gettext - ungettext = mg_globals.thread_scope.translations.ngettext else: from email.MIMEText import MIMEText - import urlparse - ugettext = mg_globals.thread_scope.translations.ugettext - ungettext = mg_globals.thread_scope.translations.ungettext # taken from # https://github.com/django/django/blob/master/django/utils/encoding.py def py2_unicode(klass): + # TODO: Add support for __repr__ if not PY3: if '__str__' not in klass.__dict__: raise ValueError("@py2_unicode cannot be applied " diff --git a/mediagoblin/db/open.py b/mediagoblin/db/open.py index 9cf9e578..34f0bffa 100644 --- a/mediagoblin/db/open.py +++ b/mediagoblin/db/open.py @@ -18,9 +18,10 @@ from sqlalchemy import create_engine, event import logging +import six + from mediagoblin.db.base import Base, Session from mediagoblin import mg_globals -from mediagoblin._compat import iteritems _log = logging.getLogger(__name__) @@ -29,7 +30,7 @@ class DatabaseMaster(object): def __init__(self, engine): self.engine = engine - for k, v in iteritems(Base._decl_class_registry): + for k, v in six.iteritems(Base._decl_class_registry): setattr(self, k, v) def commit(self): diff --git a/mediagoblin/plugins/oauth/views.py b/mediagoblin/plugins/oauth/views.py index 9e3b87c9..8ca73521 100644 --- a/mediagoblin/plugins/oauth/views.py +++ b/mediagoblin/plugins/oauth/views.py @@ -17,7 +17,7 @@ import logging -from urllib import urlencode +from six.moves.urllib.parse import urlencode import six diff --git a/mediagoblin/storage/filestorage.py b/mediagoblin/storage/filestorage.py index 404d24c3..917c7703 100644 --- a/mediagoblin/storage/filestorage.py +++ b/mediagoblin/storage/filestorage.py @@ -14,16 +14,16 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import os +import shutil + +from six.moves.urllib.parse import urlparse + from mediagoblin.storage import ( StorageInterface, clean_listy_filepath, NoWebServing) -import os -import shutil - -from mediagoblin._compat import urlparse - class BasicFileStorage(StorageInterface): """ diff --git a/mediagoblin/tools/template.py b/mediagoblin/tools/template.py index 359ddd38..b01196fd 100644 --- a/mediagoblin/tools/template.py +++ b/mediagoblin/tools/template.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import six import jinja2 from jinja2.ext import Extension @@ -33,8 +34,6 @@ from mediagoblin.tools.pluginapi import get_hook_templates, hook_transform from mediagoblin.tools.timesince import timesince from mediagoblin.meddleware.csrf import render_csrf_form_token -from mediagoblin._compat import ugettext, ungettext - SETUP_JINJA_ENVS = {} @@ -67,7 +66,12 @@ def get_jinja_env(template_loader, locale): 'jinja2.ext.i18n', 'jinja2.ext.autoescape', TemplateHookExtension] + local_exts) - template_env.install_gettext_callables(ugettext, ungettext) + if six.PY2: + template_env.install_gettext_callables(mg_globals.thread_scope.translations.ugettext, + mg_globals.thread_scope.translations.ungettext) + else: + template_env.install_gettext_callables(mg_globals.thread_scope.translations.gettext, + mg_globals.thread_scope.translations.ngettext) # All templates will know how to ... # ... fetch all waiting messages and remove them from the queue diff --git a/mediagoblin/tools/translate.py b/mediagoblin/tools/translate.py index e6df612d..f8a59aee 100644 --- a/mediagoblin/tools/translate.py +++ b/mediagoblin/tools/translate.py @@ -17,12 +17,12 @@ import gettext import pkg_resources +import six from babel import localedata from babel.support import LazyProxy from mediagoblin import mg_globals -from mediagoblin._compat import ugettext, ungettext ################### # Translation tools @@ -147,7 +147,9 @@ def pass_to_ugettext(*args, **kwargs): The reason we can't have a global ugettext method is because mg_globals gets swapped out by the application per-request. """ - return ugettext(*args, **kwargs) + if six.PY2: + return mg_globals.thread_scope.translations.ugettext(*args, **kwargs) + return mg_globals.thread_scope.translations.gettext(*args, **kwargs) def pass_to_ungettext(*args, **kwargs): """ @@ -156,7 +158,9 @@ def pass_to_ungettext(*args, **kwargs): The reason we can't have a global ugettext method is because mg_globals gets swapped out by the application per-request. """ - return ungettext(*args, **kwargs) + if six.PY2: + return mg_globals.thread_scope.translations.ungettext(*args, **kwargs) + return mg_globals.thread_scope.translations.ngettext(*args, **kwargs) def lazy_pass_to_ugettext(*args, **kwargs):