More cleanups for i18n.py

master
Sayamindu Dasgupta 15 years ago
parent 1b600b147d
commit d00d9b6431

@ -31,40 +31,41 @@ _MO_BIG_ENDIAN = 0xde120495
_MO_LITTLE_ENDIAN = 0x950412de _MO_LITTLE_ENDIAN = 0x950412de
def _read_bin(handle, fmt, bytecount): def _read_bin(handle, format_string, byte_count):
read_bytes = handle.read(bytecount) read_bytes = handle.read(byte_count)
ret_value = struct.unpack(fmt, read_bytes) return_value = struct.unpack(format_string, read_bytes)
if len(ret_value) == 1: if len(return_value) == 1:
return ret_value[0] return return_value[0]
else: else:
return ret_value return return_value
def _extract_header(filepath): def _extract_header(file_path):
header = '' header = ''
handle = open(filepath, 'rb') handle = open(file_path, 'rb')
magic_number = _read_bin(handle, '<I', 4) magic_number = _read_bin(handle, '<I', 4)
if magic_number == _MO_BIG_ENDIAN: if magic_number == _MO_BIG_ENDIAN:
fmt = '>II' format_string = '>II'
elif magic_number == _MO_LITTLE_ENDIAN: elif magic_number == _MO_LITTLE_ENDIAN:
fmt = '<II' format_string = '<II'
else: else:
raise IOError('File does not seem to be valid MO file') raise IOError('File does not seem to be a valid MO file')
version_, num_of_strings = _read_bin(handle, fmt, 8) version_, num_of_strings = _read_bin(handle, format_string, 8)
msgids_hash_offset, msgstrs_hash_offset = _read_bin(handle, fmt, 8) msgids_hash_offset, msgstrs_hash_offset = _read_bin(handle, \
format_string, 8)
handle.seek(msgids_hash_offset) handle.seek(msgids_hash_offset)
msgids_index = [] msgids_index = []
for i in range(num_of_strings): for i in range(num_of_strings):
msgids_index.append(_read_bin(handle, fmt, 8)) msgids_index.append(_read_bin(handle, format_string, 8))
handle.seek(msgstrs_hash_offset) handle.seek(msgstrs_hash_offset)
msgstrs_index = [] msgstrs_index = []
for i in range(num_of_strings): for i in range(num_of_strings):
msgstrs_index.append(_read_bin(handle, fmt, 8)) msgstrs_index.append(_read_bin(handle, format_string, 8))
for i in range(num_of_strings): for i in range(num_of_strings):
handle.seek(msgids_index[i][1]) handle.seek(msgids_index[i][1])
@ -81,8 +82,8 @@ def _extract_header(filepath):
return header return header
def _extract_modification_time(filepath): def _extract_modification_time(file_path):
header = _extract_header(filepath) header = _extract_header(file_path)
items = header.split('\n') items = header.split('\n')
for item in items: for item in items:
if item.startswith('PO-Revision-Date:'): if item.startswith('PO-Revision-Date:'):
@ -90,8 +91,7 @@ def _extract_modification_time(filepath):
parsed_time = dateutil.parser.parse(time_str) parsed_time = dateutil.parser.parse(time_str)
return time.mktime(parsed_time.timetuple()) return time.mktime(parsed_time.timetuple())
raise ValueError('Could not find revision date') raise ValueError('Could not find a revision date')
return -1
def get_locale_path(bundle_id): def get_locale_path(bundle_id):
@ -138,7 +138,7 @@ def get_locale_path(bundle_id):
# Set lowest priority # Set lowest priority
candidate_dirs[candidate_dir] = -1 candidate_dirs[candidate_dir] = -1
sorted_dict = sorted(candidate_dirs.iteritems(), key=lambda (k, v): \ available_paths = sorted(candidate_dirs.iteritems(), key=lambda (k, v): \
(v, k), reverse=True) (v, k), reverse=True)
preferred_path = available_paths[0][0]
return sorted_dict[0][0] return preferred_path

Loading…
Cancel
Save