diff --git a/src/sugar/activity/i18n.py b/src/sugar/activity/i18n.py
index a91b912..410bc15 100644
a
|
b
|
_MO_BIG_ENDIAN = 0xde120495 |
31 | 31 | _MO_LITTLE_ENDIAN = 0x950412de |
32 | 32 | |
33 | 33 | |
34 | | def _readbin(handle, fmt, bytecount): |
| 34 | def _read_bin(handle, fmt, bytecount): |
35 | 35 | read_bytes = handle.read(bytecount) |
36 | | retvalue = struct.unpack(fmt, read_bytes) |
37 | | if len(retvalue) == 1: |
38 | | return retvalue[0] |
| 36 | ret_value = struct.unpack(fmt, read_bytes) |
| 37 | if len(ret_value) == 1: |
| 38 | return ret_value[0] |
39 | 39 | else: |
40 | | return retvalue |
| 40 | return ret_value |
41 | 41 | |
42 | 42 | |
43 | 43 | def _extract_header(filepath): |
44 | 44 | header = '' |
45 | 45 | handle = open(filepath, 'rb') |
46 | | magic_number = _readbin(handle, '<I', 4) |
| 46 | magic_number = _read_bin(handle, '<I', 4) |
47 | 47 | |
48 | 48 | if magic_number == _MO_BIG_ENDIAN: |
49 | 49 | fmt = '>II' |
… |
… |
def _extract_header(filepath): |
52 | 52 | else: |
53 | 53 | raise IOError('File does not seem to be valid MO file') |
54 | 54 | |
55 | | version_, numofstrings = _readbin(handle, fmt, 8) |
| 55 | version_, num_of_strings = _read_bin(handle, fmt, 8) |
56 | 56 | |
57 | | msgids_hash_offset, msgstrs_hash_offset = _readbin(handle, fmt, 8) |
| 57 | msgids_hash_offset, msgstrs_hash_offset = _read_bin(handle, fmt, 8) |
58 | 58 | handle.seek(msgids_hash_offset) |
59 | 59 | |
60 | 60 | msgids_index = [] |
61 | | for i in range(numofstrings): |
62 | | msgids_index.append(_readbin(handle, fmt, 8)) |
| 61 | for i in range(num_of_strings): |
| 62 | msgids_index.append(_read_bin(handle, fmt, 8)) |
63 | 63 | handle.seek(msgstrs_hash_offset) |
64 | 64 | |
65 | 65 | msgstrs_index = [] |
66 | | for i in range(numofstrings): |
67 | | msgstrs_index.append(_readbin(handle, fmt, 8)) |
| 66 | for i in range(num_of_strings): |
| 67 | msgstrs_index.append(_read_bin(handle, fmt, 8)) |
68 | 68 | |
69 | | for i in range(numofstrings): |
| 69 | for i in range(num_of_strings): |
70 | 70 | handle.seek(msgids_index[i][1]) |
71 | 71 | msgid = handle.read(msgids_index[i][0]) |
72 | 72 | if msgid == '': |
… |
… |
def _extract_modification_time(filepath): |
86 | 86 | items = header.split('\n') |
87 | 87 | for item in items: |
88 | 88 | if item.startswith('PO-Revision-Date:'): |
89 | | timestr = item.split(': ')[1] |
90 | | parsedtime = dateutil.parser.parse(timestr) |
91 | | return time.mktime(parsedtime.timetuple()) |
| 89 | time_str = item.split(': ')[1] |
| 90 | parsed_time = dateutil.parser.parse(time_str) |
| 91 | return time.mktime(parsed_time.timetuple()) |
92 | 92 | |
93 | 93 | raise ValueError('Could not find revision date') |
94 | 94 | return -1 |
95 | 95 | |
96 | 96 | |
97 | 97 | def get_locale_path(bundle_id): |
98 | | """ Gets the locale path, the directory where the preferred |
| 98 | """ Returns the locale path, which is the directory where the preferred |
99 | 99 | MO file is located. |
100 | 100 | |
101 | | bundle_id -- The bundle id of the activity in question |
| 101 | The preferred MO file is the one with the latest translation. |
102 | 102 | |
103 | | The preferred MO file is the one with the latest |
104 | | translation. |
| 103 | @type bundle_id: string |
| 104 | @param bundle_id: The bundle id of the activity in question |
| 105 | @rtype: string |
| 106 | @return: the preferred locale path |
105 | 107 | """ |
106 | 108 | |
107 | 109 | # Note: We pre-assign weights to the directories so that if no translations |
… |
… |
def get_locale_path(bundle_id): |
124 | 126 | |
125 | 127 | for candidate_dir in candidate_dirs.keys(): |
126 | 128 | if os.path.exists(candidate_dir): |
127 | | fullpath = os.path.join(candidate_dir, \ |
| 129 | full_path = os.path.join(candidate_dir, \ |
128 | 130 | locale.getdefaultlocale()[0], 'LC_MESSAGES', \ |
129 | 131 | bundle_id + '.mo') |
130 | | if os.path.exists(fullpath): |
| 132 | if os.path.exists(full_path): |
131 | 133 | try: |
132 | 134 | candidate_dirs[candidate_dir] = \ |
133 | | _extract_modification_time(fullpath) |
| 135 | _extract_modification_time(full_path) |
134 | 136 | except (IOError, ValueError): |
135 | 137 | # The mo file is damaged or has not been initialized |
136 | 138 | # Set lowest priority |
137 | 139 | candidate_dirs[candidate_dir] = -1 |
138 | 140 | |
139 | | # Fancy way to sort the dictionary by value |
140 | | return sorted(candidate_dirs.iteritems(), key=lambda (k, v): (v, k), \ |
141 | | reverse=True)[0][0] |
| 141 | sorted_dict = sorted(candidate_dirs.iteritems(), key=lambda (k, v): \ |
| 142 | (v, k), reverse=True) |
| 143 | |
| 144 | return sorted_dict[0][0] |