From 173a4027019e0adff04534176167ac24a0b303cd Mon Sep 17 00:00:00 2001 From: Sayamindu Dasgupta Date: Sun, 24 Jan 2010 20:28:21 +0530 Subject: [PATCH 01/10] Code for language pack support --- src/sugar/activity/i18n.py | 134 +++++++++++++++++++++++++++++++++++++ src/sugar/activity/main.py | 6 +- 2 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 src/sugar/activity/i18n.py diff --git a/src/sugar/activity/i18n.py b/src/sugar/activity/i18n.py new file mode 100644 index 00000000..0963ffad --- /dev/null +++ b/src/sugar/activity/i18n.py @@ -0,0 +1,134 @@ +# Copyright (C) 2010 One Laptop Per Child +# +# Author: Sayamindu Dasgupta +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +import gconf + +import locale +import os +import struct +import sys + +import dateutil.parser +import time + +_MO_BIG_ENDIAN = 0xde120495 +_MO_LITTLE_ENDIAN = 0x950412de + +def _readbin(handle, fmt, bytecount): + read_bytes = handle.read(bytecount) + retvalue = struct.unpack(fmt, read_bytes) + if len(retvalue) == 1: + return retvalue[0] + else: + return retvalue + +def _extract_header(filepath): + header = '' + handle = open(filepath, 'rb') + magic_number = _readbin(handle, ' Date: Sun, 24 Jan 2010 20:29:10 +0530 Subject: [PATCH 02/10] Update Makefile.am --- src/sugar/activity/Makefile.am | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sugar/activity/Makefile.am b/src/sugar/activity/Makefile.am index e2e6fdce..2c2eff18 100644 --- a/src/sugar/activity/Makefile.am +++ b/src/sugar/activity/Makefile.am @@ -6,6 +6,7 @@ sugar_PYTHON = \ activityhandle.py \ activityservice.py \ bundlebuilder.py \ + i18n.py \ main.py \ namingalert.py \ widgets.py From 259fca2caa941234da79ad325f513e5ed784e211 Mon Sep 17 00:00:00 2001 From: Sayamindu Dasgupta Date: Mon, 1 Feb 2010 16:24:58 +0530 Subject: [PATCH 03/10] Import in two lines instead of one --- src/sugar/activity/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sugar/activity/main.py b/src/sugar/activity/main.py index b03272b3..0647e810 100644 --- a/src/sugar/activity/main.py +++ b/src/sugar/activity/main.py @@ -26,7 +26,8 @@ import dbus.service import dbus.glib import sugar -from sugar.activity import activityhandle, i18n +from sugar.activity import activityhandle +from sugar.activity import i18n from sugar.bundle.activitybundle import ActivityBundle from sugar.graphics import style from sugar import logger From 415f3a30660377703929f2c8f6ec0cf60c11bacc Mon Sep 17 00:00:00 2001 From: Sayamindu Dasgupta Date: Mon, 1 Feb 2010 16:28:19 +0530 Subject: [PATCH 04/10] Describe the args of the i18n methods --- src/sugar/activity/i18n.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/sugar/activity/i18n.py b/src/sugar/activity/i18n.py index 0963ffad..b9732216 100644 --- a/src/sugar/activity/i18n.py +++ b/src/sugar/activity/i18n.py @@ -94,6 +94,9 @@ def _extract_modification_time(filepath): def get_locale_path(bundle_id): """ Gets the locale path, the directory where the preferred MO file is located. + + bundle_id -- The bundle id of the activity in question + The preferred MO file is the one with the latest translation. """ From 85840b269f9b28f9d53f5159d23eeedf744135a6 Mon Sep 17 00:00:00 2001 From: Sayamindu Dasgupta Date: Mon, 8 Feb 2010 23:45:39 +0530 Subject: [PATCH 05/10] Style and PEP8 fixes --- src/sugar/activity/i18n.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/sugar/activity/i18n.py b/src/sugar/activity/i18n.py index b9732216..e45fc19a 100644 --- a/src/sugar/activity/i18n.py +++ b/src/sugar/activity/i18n.py @@ -27,9 +27,10 @@ import sys import dateutil.parser import time -_MO_BIG_ENDIAN = 0xde120495 +_MO_BIG_ENDIAN = 0xde120495 _MO_LITTLE_ENDIAN = 0x950412de + def _readbin(handle, fmt, bytecount): read_bytes = handle.read(bytecount) retvalue = struct.unpack(fmt, read_bytes) @@ -38,6 +39,7 @@ def _readbin(handle, fmt, bytecount): else: return retvalue + def _extract_header(filepath): header = '' handle = open(filepath, 'rb') @@ -48,9 +50,9 @@ def _extract_header(filepath): elif magic_number == _MO_LITTLE_ENDIAN: fmt = ' Date: Mon, 8 Feb 2010 23:46:22 +0530 Subject: [PATCH 06/10] Should be and, and not or --- src/sugar/activity/i18n.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sugar/activity/i18n.py b/src/sugar/activity/i18n.py index e45fc19a..a91b9126 100644 --- a/src/sugar/activity/i18n.py +++ b/src/sugar/activity/i18n.py @@ -117,7 +117,7 @@ def get_locale_path(bundle_id): gconf_client = gconf.client_get_default() package_dir = gconf_client.get_string("/desktop/sugar/i18n/langpackdir") - if package_dir is not None or package_dir is not '': + if package_dir is not None and package_dir is not '': candidate_dirs[package_dir] = 1 candidate_dirs[os.path.join(sys.prefix, 'share', 'locale')] = 0 From 1817caf30f16712c59475db2af98030f9456cebf Mon Sep 17 00:00:00 2001 From: Sayamindu Dasgupta Date: Tue, 9 Feb 2010 17:13:04 +0530 Subject: [PATCH 07/10] Consistent variable names --- src/sugar/activity/i18n.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/sugar/activity/i18n.py b/src/sugar/activity/i18n.py index a91b9126..d235a554 100644 --- a/src/sugar/activity/i18n.py +++ b/src/sugar/activity/i18n.py @@ -31,19 +31,19 @@ _MO_BIG_ENDIAN = 0xde120495 _MO_LITTLE_ENDIAN = 0x950412de -def _readbin(handle, fmt, bytecount): +def _read_bin(handle, fmt, bytecount): read_bytes = handle.read(bytecount) - retvalue = struct.unpack(fmt, read_bytes) - if len(retvalue) == 1: - return retvalue[0] + ret_value = struct.unpack(fmt, read_bytes) + if len(ret_value) == 1: + return ret_value[0] else: - return retvalue + return ret_value def _extract_header(filepath): header = '' handle = open(filepath, 'rb') - magic_number = _readbin(handle, ' Date: Tue, 9 Feb 2010 17:41:10 +0530 Subject: [PATCH 08/10] Clean up docstring --- src/sugar/activity/i18n.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/sugar/activity/i18n.py b/src/sugar/activity/i18n.py index d235a554..865928a6 100644 --- a/src/sugar/activity/i18n.py +++ b/src/sugar/activity/i18n.py @@ -95,13 +95,15 @@ def _extract_modification_time(filepath): def get_locale_path(bundle_id): - """ Gets the locale path, the directory where the preferred + """ Returns the locale path, which is the directory where the preferred MO file is located. - bundle_id -- The bundle id of the activity in question + The preferred MO file is the one with the latest translation. - The preferred MO file is the one with the latest - translation. + @type bundle_id: string + @param bundle_id: The bundle id of the activity in question + @rtype: string + @return: the preferred locale path """ # Note: We pre-assign weights to the directories so that if no translations From 1b600b147d76b84322376c910738b74d38b4a5ca Mon Sep 17 00:00:00 2001 From: Sayamindu Dasgupta Date: Tue, 9 Feb 2010 17:43:18 +0530 Subject: [PATCH 09/10] Make the dictionary sorting a bit more sensible --- src/sugar/activity/i18n.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/sugar/activity/i18n.py b/src/sugar/activity/i18n.py index 865928a6..410bc159 100644 --- a/src/sugar/activity/i18n.py +++ b/src/sugar/activity/i18n.py @@ -138,6 +138,7 @@ def get_locale_path(bundle_id): # Set lowest priority candidate_dirs[candidate_dir] = -1 - # Fancy way to sort the dictionary by value - return sorted(candidate_dirs.iteritems(), key=lambda (k, v): (v, k), \ - reverse=True)[0][0] + sorted_dict = sorted(candidate_dirs.iteritems(), key=lambda (k, v): \ + (v, k), reverse=True) + + return sorted_dict[0][0] From d00d9b64316eb0ed647621286a708ffc68bc8656 Mon Sep 17 00:00:00 2001 From: Sayamindu Dasgupta Date: Wed, 10 Feb 2010 20:53:09 +0530 Subject: [PATCH 10/10] More cleanups for i18n.py --- src/sugar/activity/i18n.py | 44 +++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/sugar/activity/i18n.py b/src/sugar/activity/i18n.py index 410bc159..1c3c8936 100644 --- a/src/sugar/activity/i18n.py +++ b/src/sugar/activity/i18n.py @@ -31,40 +31,41 @@ _MO_BIG_ENDIAN = 0xde120495 _MO_LITTLE_ENDIAN = 0x950412de -def _read_bin(handle, fmt, bytecount): - read_bytes = handle.read(bytecount) - ret_value = struct.unpack(fmt, read_bytes) - if len(ret_value) == 1: - return ret_value[0] +def _read_bin(handle, format_string, byte_count): + read_bytes = handle.read(byte_count) + return_value = struct.unpack(format_string, read_bytes) + if len(return_value) == 1: + return return_value[0] else: - return ret_value + return return_value -def _extract_header(filepath): +def _extract_header(file_path): header = '' - handle = open(filepath, 'rb') + handle = open(file_path, 'rb') magic_number = _read_bin(handle, '