[mousetrap/gnome3-wip: 237/240] More Python 3 compatibility fixes
- From: Heidi Ellis <heidiellis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [mousetrap/gnome3-wip: 237/240] More Python 3 compatibility fixes
- Date: Mon, 8 Sep 2014 15:32:29 +0000 (UTC)
commit 9f7d9c68aba107a1b3312143d13225a788a44689
Author: Kevin Brown <kevin kevinbrown in>
Date: Tue Jul 1 20:41:47 2014 -0400
More Python 3 compatibility fixes
src/mousetrap/compat.py | 12 ++++++++++++
src/mousetrap/config.py | 11 +++--------
src/mousetrap/i18n.py | 7 ++++++-
3 files changed, 21 insertions(+), 9 deletions(-)
---
diff --git a/src/mousetrap/compat.py b/src/mousetrap/compat.py
new file mode 100644
index 0000000..c73a62a
--- /dev/null
+++ b/src/mousetrap/compat.py
@@ -0,0 +1,12 @@
+"""
+Python 2/3 compatibility module.
+"""
+
+import sys
+
+PY3 = sys.version_info[0] == 3
+
+if PY3:
+ string_types = (str, )
+else:
+ string_types = (basestring, )
diff --git a/src/mousetrap/config.py b/src/mousetrap/config.py
index 3529f1c..0fb4515 100644
--- a/src/mousetrap/config.py
+++ b/src/mousetrap/config.py
@@ -10,13 +10,6 @@ from copy import deepcopy
from io import open
-try:
- unicode
-except NameError:
- # Python 3
- basestring = unicode = str
-
-
class Config(dict):
def load(self, paths):
@@ -48,7 +41,9 @@ class Config(dict):
x = config['classes'][self.__class__.__module__+'.'+self.__class__.__name__]['x']
'''
- if isinstance(key, basestring):
+ from mousetrap.compat import string_types
+
+ if isinstance(key, string_types):
return super(Config, self).__getitem__(key)
class_ = key.__class__
return self['classes'][class_.__module__ + '.' + class_.__name__]
diff --git a/src/mousetrap/i18n.py b/src/mousetrap/i18n.py
index 5364bfe..4190c38 100644
--- a/src/mousetrap/i18n.py
+++ b/src/mousetrap/i18n.py
@@ -12,4 +12,9 @@ LOCALE_DIR = os.path.abspath(
os.path.join(os.path.dirname(os.path.realpath(__file__)), "locale"))
LOGGER.debug("LOCALE_DIR = %s", LOCALE_DIR)
-_ = gettext.translation("mousetrap", localedir=LOCALE_DIR).ugettext
+translations = gettext.translation("mousetrap", localedir=LOCALE_DIR)
+
+try:
+ _ = translations.ugettext
+except AttributeError:
+ _ = translations.gettext
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]