Style and PEP8 fixes

This commit is contained in:
Sayamindu Dasgupta 2010-02-08 23:45:39 +05:30
parent 415f3a3066
commit 85840b269f

View File

@ -27,9 +27,10 @@ import sys
import dateutil.parser import dateutil.parser
import time import time
_MO_BIG_ENDIAN = 0xde120495 _MO_BIG_ENDIAN = 0xde120495
_MO_LITTLE_ENDIAN = 0x950412de _MO_LITTLE_ENDIAN = 0x950412de
def _readbin(handle, fmt, bytecount): def _readbin(handle, fmt, bytecount):
read_bytes = handle.read(bytecount) read_bytes = handle.read(bytecount)
retvalue = struct.unpack(fmt, read_bytes) retvalue = struct.unpack(fmt, read_bytes)
@ -38,6 +39,7 @@ def _readbin(handle, fmt, bytecount):
else: else:
return retvalue return retvalue
def _extract_header(filepath): def _extract_header(filepath):
header = '' header = ''
handle = open(filepath, 'rb') handle = open(filepath, 'rb')
@ -48,9 +50,9 @@ def _extract_header(filepath):
elif magic_number == _MO_LITTLE_ENDIAN: elif magic_number == _MO_LITTLE_ENDIAN:
fmt = '<II' fmt = '<II'
else: else:
raise IOError ('File does not seem to be valid MO file') raise IOError('File does not seem to be valid MO file')
version, numofstrings = _readbin(handle, fmt, 8) version_, numofstrings = _readbin(handle, fmt, 8)
msgids_hash_offset, msgstrs_hash_offset = _readbin(handle, fmt, 8) msgids_hash_offset, msgstrs_hash_offset = _readbin(handle, fmt, 8)
handle.seek(msgids_hash_offset) handle.seek(msgids_hash_offset)
@ -78,6 +80,7 @@ def _extract_header(filepath):
handle.close() handle.close()
return header return header
def _extract_modification_time(filepath): def _extract_modification_time(filepath):
header = _extract_header(filepath) header = _extract_header(filepath)
items = header.split('\n') items = header.split('\n')
@ -87,36 +90,36 @@ def _extract_modification_time(filepath):
parsedtime = dateutil.parser.parse(timestr) parsedtime = dateutil.parser.parse(timestr)
return time.mktime(parsedtime.timetuple()) return time.mktime(parsedtime.timetuple())
raise ValueError ('Could not find revision date') raise ValueError('Could not find revision date')
return -1 return -1
def get_locale_path(bundle_id): def get_locale_path(bundle_id):
""" Gets the locale path, the directory where the preferred """ Gets the locale path, the directory where the preferred
MO file is located. MO file is located.
bundle_id -- The bundle id of the activity in question bundle_id -- The bundle id of the activity in question
The preferred MO file is the one with the latest The preferred MO file is the one with the latest
translation. translation.
""" """
# Note: We pre-assign weights to the directories so that if no translations # Note: We pre-assign weights to the directories so that if no translations
# exist, the appropriate fallbacks (eg: bn for bn_BD) can be loaded # exist, the appropriate fallbacks (eg: bn for bn_BD) can be loaded
# The directory with the highest weight is returned, and if a MO file is # The directory with the highest weight is returned, and if a MO file is
# found, the weight of the directory is set to the MO's modification time # found, the weight of the directory is set to the MO's modification time
# (as described in the MO header, and _not_ the filesystem mtime) # (as described in the MO header, and _not_ the filesystem mtime)
candidate_dirs = {} candidate_dirs = {}
if 'SUGAR_LOCALEDIR' in os.environ: if 'SUGAR_LOCALEDIR' in os.environ:
candidate_dirs[os.environ['SUGAR_LOCALEDIR']] = 2 candidate_dirs[os.environ['SUGAR_LOCALEDIR']] = 2
gconf_client = gconf.client_get_default() gconf_client = gconf.client_get_default()
packdir = gconf_client.get_string("/desktop/sugar/i18n/langpackdir") package_dir = gconf_client.get_string("/desktop/sugar/i18n/langpackdir")
if packdir is not None or packdir is not '': if package_dir is not None or package_dir is not '':
candidate_dirs[packdir] = 1 candidate_dirs[package_dir] = 1
candidate_dirs[os.path.join(sys.prefix, 'share', 'locale')] = 0 candidate_dirs[os.path.join(sys.prefix, 'share', 'locale')] = 0
for candidate_dir in candidate_dirs.keys(): for candidate_dir in candidate_dirs.keys():
@ -133,5 +136,6 @@ def get_locale_path(bundle_id):
# Set lowest priority # Set lowest priority
candidate_dirs[candidate_dir] = -1 candidate_dirs[candidate_dir] = -1
# Fancy way to sort the dictionary by value
return sorted(candidate_dirs.iteritems(), key=lambda (k, v): (v, k), \ return sorted(candidate_dirs.iteritems(), key=lambda (k, v): (v, k), \
reverse = True)[0][0] reverse=True)[0][0]