Ticket #1684: unified_patch.patch

File unified_patch.patch, 4.3 KB (added by sayamindu, 13 years ago)

Unified version of the previously attached patches, as discussed on IRC

  • src/sugar/activity/i18n.py

    diff --git a/src/sugar/activity/i18n.py b/src/sugar/activity/i18n.py
    index a91b912..410bc15 100644
    a b _MO_BIG_ENDIAN = 0xde120495 
    3131_MO_LITTLE_ENDIAN = 0x950412de
    3232
    3333
    34 def _readbin(handle, fmt, bytecount):
     34def _read_bin(handle, fmt, bytecount):
    3535    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]
    3939    else:
    40         return retvalue
     40        return ret_value
    4141
    4242
    4343def _extract_header(filepath):
    4444    header = ''
    4545    handle = open(filepath, 'rb')
    46     magic_number = _readbin(handle, '<I', 4)
     46    magic_number = _read_bin(handle, '<I', 4)
    4747
    4848    if magic_number == _MO_BIG_ENDIAN:
    4949        fmt = '>II'
    def _extract_header(filepath): 
    5252    else:
    5353        raise IOError('File does not seem to be valid MO file')
    5454
    55     version_, numofstrings = _readbin(handle, fmt, 8)
     55    version_, num_of_strings = _read_bin(handle, fmt, 8)
    5656
    57     msgids_hash_offset, msgstrs_hash_offset = _readbin(handle, fmt, 8)
     57    msgids_hash_offset, msgstrs_hash_offset = _read_bin(handle, fmt, 8)
    5858    handle.seek(msgids_hash_offset)
    5959
    6060    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))
    6363    handle.seek(msgstrs_hash_offset)
    6464
    6565    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))
    6868
    69     for i in range(numofstrings):
     69    for i in range(num_of_strings):
    7070        handle.seek(msgids_index[i][1])
    7171        msgid = handle.read(msgids_index[i][0])
    7272        if msgid == '':
    def _extract_modification_time(filepath): 
    8686    items = header.split('\n')
    8787    for item in items:
    8888        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())
    9292
    9393    raise ValueError('Could not find revision date')
    9494    return -1
    9595
    9696
    9797def 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
    9999        MO file is located.
    100100
    101             bundle_id -- The bundle id of the activity in question
     101        The preferred MO file is the one with the latest translation.
    102102
    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
    105107    """
    106108
    107109    # Note: We pre-assign weights to the directories so that if no translations
    def get_locale_path(bundle_id): 
    124126
    125127    for candidate_dir in candidate_dirs.keys():
    126128        if os.path.exists(candidate_dir):
    127             fullpath = os.path.join(candidate_dir, \
     129            full_path = os.path.join(candidate_dir, \
    128130                locale.getdefaultlocale()[0], 'LC_MESSAGES', \
    129131                bundle_id + '.mo')
    130             if os.path.exists(fullpath):
     132            if os.path.exists(full_path):
    131133                try:
    132134                    candidate_dirs[candidate_dir] = \
    133                         _extract_modification_time(fullpath)
     135                        _extract_modification_time(full_path)
    134136                except (IOError, ValueError):
    135137                    # The mo file is damaged or has not been initialized
    136138                    # Set lowest priority
    137139                    candidate_dirs[candidate_dir] = -1
    138140
    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]