l10n: reformat and remove py3 incompatibilities,

removed as_unicode kwarg in l10n.formatTimestamp() and unused category
argument in l10n.setlocale(); edited unmaintained bitmessagecurses for pylint.
This commit is contained in:
Dmitri Bogomolov 2021-07-13 16:25:59 +03:00
parent d872a23ae3
commit 0c7fd5ca82
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
3 changed files with 58 additions and 48 deletions

View File

@ -274,7 +274,8 @@ def drawtab(stdscr):
stdscr.addstr(8 + i, 18, str(item).ljust(2)) stdscr.addstr(8 + i, 18, str(item).ljust(2))
# Uptime and processing data # Uptime and processing data
stdscr.addstr(6, 35, "Since startup on " + l10n.formatTimestamp(startuptime, False)) stdscr.addstr(
6, 35, "Since startup on " + l10n.formatTimestamp(startuptime))
stdscr.addstr(7, 40, "Processed " + str( stdscr.addstr(7, 40, "Processed " + str(
state.numberOfMessagesProcessed).ljust(4) + " person-to-person messages.") state.numberOfMessagesProcessed).ljust(4) + " person-to-person messages.")
stdscr.addstr(8, 40, "Processed " + str( stdscr.addstr(8, 40, "Processed " + str(
@ -1027,8 +1028,9 @@ def loadInbox():
fromlabel = shared.fixPotentiallyInvalidUTF8Data(fromlabel) fromlabel = shared.fixPotentiallyInvalidUTF8Data(fromlabel)
# Load into array # Load into array
inbox.append([msgid, tolabel, toaddr, fromlabel, fromaddr, subject, l10n.formatTimestamp( inbox.append([
received, False), read]) msgid, tolabel, toaddr, fromlabel, fromaddr, subject,
l10n.formatTimestamp(received), read])
inbox.reverse() inbox.reverse()
@ -1080,20 +1082,20 @@ def loadSent():
elif status == "msgqueued": elif status == "msgqueued":
statstr = "Message queued" statstr = "Message queued"
elif status == "msgsent": elif status == "msgsent":
t = l10n.formatTimestamp(lastactiontime, False) t = l10n.formatTimestamp(lastactiontime)
statstr = "Message sent at " + t + ".Waiting for acknowledgement." statstr = "Message sent at " + t + ".Waiting for acknowledgement."
elif status == "msgsentnoackexpected": elif status == "msgsentnoackexpected":
t = l10n.formatTimestamp(lastactiontime, False) t = l10n.formatTimestamp(lastactiontime)
statstr = "Message sent at " + t + "." statstr = "Message sent at " + t + "."
elif status == "doingmsgpow": elif status == "doingmsgpow":
statstr = "The proof of work required to send the message has been queued." statstr = "The proof of work required to send the message has been queued."
elif status == "ackreceived": elif status == "ackreceived":
t = l10n.formatTimestamp(lastactiontime, False) t = l10n.formatTimestamp(lastactiontime)
statstr = "Acknowledgment of the message received at " + t + "." statstr = "Acknowledgment of the message received at " + t + "."
elif status == "broadcastqueued": elif status == "broadcastqueued":
statstr = "Broadcast queued." statstr = "Broadcast queued."
elif status == "broadcastsent": elif status == "broadcastsent":
t = l10n.formatTimestamp(lastactiontime, False) t = l10n.formatTimestamp(lastactiontime)
statstr = "Broadcast sent at " + t + "." statstr = "Broadcast sent at " + t + "."
elif status == "forcepow": elif status == "forcepow":
statstr = "Forced difficulty override. Message will start sending soon." statstr = "Forced difficulty override. Message will start sending soon."
@ -1102,7 +1104,7 @@ def loadSent():
elif status == "toodifficult": elif status == "toodifficult":
statstr = "Error: The work demanded by the recipient is more difficult than you are willing to do." statstr = "Error: The work demanded by the recipient is more difficult than you are willing to do."
else: else:
t = l10n.formatTimestamp(lastactiontime, False) t = l10n.formatTimestamp(lastactiontime)
statstr = "Unknown status " + status + " at " + t + "." statstr = "Unknown status " + status + " at " + t + "."
# Load into array # Load into array
@ -1114,7 +1116,7 @@ def loadSent():
subject, subject,
statstr, statstr,
ackdata, ackdata,
l10n.formatTimestamp(lastactiontime, False)]) l10n.formatTimestamp(lastactiontime)])
sentbox.reverse() sentbox.reverse()

View File

@ -125,6 +125,8 @@ class MyForm(settingsmixin.SMainWindow):
self.qsystranslator.load(translationpath) self.qsystranslator.load(translationpath)
QtGui.QApplication.installTranslator(self.qsystranslator) QtGui.QApplication.installTranslator(self.qsystranslator)
# TODO: move this block into l10n
# FIXME: shouldn't newlocale be used here?
lang = locale.normalize(l10n.getTranslationLanguage()) lang = locale.normalize(l10n.getTranslationLanguage())
langs = [ langs = [
lang.split(".")[0] + "." + l10n.encoding, lang.split(".")[0] + "." + l10n.encoding,
@ -135,7 +137,7 @@ class MyForm(settingsmixin.SMainWindow):
langs = [l10n.getWindowsLocale(lang)] langs = [l10n.getWindowsLocale(lang)]
for lang in langs: for lang in langs:
try: try:
l10n.setlocale(locale.LC_ALL, lang) l10n.setlocale(lang)
if 'win32' not in sys.platform and 'win64' not in sys.platform: if 'win32' not in sys.platform and 'win64' not in sys.platform:
l10n.encoding = locale.nl_langinfo(locale.CODESET) l10n.encoding = locale.nl_langinfo(locale.CODESET)
else: else:

View File

@ -1,22 +1,32 @@
""" """Localization helpers"""
Localization
"""
import logging import logging
import os import os
import re
import sys
import time import time
from bmconfigparser import BMConfigParser from bmconfigparser import BMConfigParser
logger = logging.getLogger('default') logger = logging.getLogger('default')
DEFAULT_ENCODING = 'ISO8859-1' DEFAULT_ENCODING = 'ISO8859-1'
DEFAULT_LANGUAGE = 'en_US' DEFAULT_LANGUAGE = 'en_US'
DEFAULT_TIME_FORMAT = '%Y-%m-%d %H:%M:%S' DEFAULT_TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
try:
import locale
encoding = locale.getpreferredencoding(True) or DEFAULT_ENCODING
language = (
locale.getlocale()[0] or locale.getdefaultlocale()[0]
or DEFAULT_LANGUAGE)
except (ImportError, AttributeError): # FIXME: it never happens
logger.exception('Could not determine language or encoding')
locale = None
encoding = DEFAULT_ENCODING encoding = DEFAULT_ENCODING
language = DEFAULT_LANGUAGE language = DEFAULT_LANGUAGE
windowsLanguageMap = { windowsLanguageMap = {
"ar": "arabic", "ar": "arabic",
"cs": "czech", "cs": "czech",
@ -40,55 +50,51 @@ windowsLanguageMap = {
"zh_TW": "chinese-traditional" "zh_TW": "chinese-traditional"
} }
try:
import locale
encoding = locale.getpreferredencoding(True) or DEFAULT_ENCODING
language = locale.getlocale()[0] or locale.getdefaultlocale()[0] or DEFAULT_LANGUAGE
except:
logger.exception('Could not determine language or encoding')
time_format = BMConfigParser().safeGet(
'bitmessagesettings', 'timeformat', DEFAULT_TIME_FORMAT)
if BMConfigParser().has_option('bitmessagesettings', 'timeformat'): if not re.search(r'\d', time.strftime(time_format)):
time_format = BMConfigParser().get('bitmessagesettings', 'timeformat')
# Test the format string
try:
time.strftime(time_format)
except:
logger.exception('Could not format timestamp')
time_format = DEFAULT_TIME_FORMAT
else:
time_format = DEFAULT_TIME_FORMAT time_format = DEFAULT_TIME_FORMAT
# It seems some systems lie about the encoding they use so we perform # It seems some systems lie about the encoding they use
# comprehensive decoding tests # so we perform comprehensive decoding tests
if time_format != DEFAULT_TIME_FORMAT: elif sys.version_info[0] == 2:
try: try:
# Check day names # Check day names
for i in xrange(7): for i in range(7):
unicode(time.strftime(time_format, (0, 0, 0, 0, 0, 0, i, 0, 0)), encoding) time.strftime(
time_format, (0, 0, 0, 0, 0, 0, i, 0, 0)).decode(encoding)
# Check month names # Check month names
for i in xrange(1, 13): for i in range(1, 13):
unicode(time.strftime(time_format, (0, i, 0, 0, 0, 0, 0, 0, 0)), encoding) time.strftime(
time_format, (0, i, 0, 0, 0, 0, 0, 0, 0)).decode(encoding)
# Check AM/PM # Check AM/PM
unicode(time.strftime(time_format, (0, 0, 0, 11, 0, 0, 0, 0, 0)), encoding) time.strftime(
unicode(time.strftime(time_format, (0, 0, 0, 13, 0, 0, 0, 0, 0)), encoding) time_format, (0, 0, 0, 11, 0, 0, 0, 0, 0)).decode(encoding)
time.strftime(
time_format, (0, 0, 0, 13, 0, 0, 0, 0, 0)).decode(encoding)
# Check DST # Check DST
unicode(time.strftime(time_format, (0, 0, 0, 0, 0, 0, 0, 0, 1)), encoding) time.strftime(
except: time_format, (0, 0, 0, 0, 0, 0, 0, 0, 1)).decode(encoding)
except Exception: # TODO: write tests and determine exception types
logger.exception('Could not decode locale formatted timestamp') logger.exception('Could not decode locale formatted timestamp')
time_format = DEFAULT_TIME_FORMAT # time_format = DEFAULT_TIME_FORMAT
encoding = DEFAULT_ENCODING encoding = DEFAULT_ENCODING
def setlocale(category, newlocale): def setlocale(newlocale):
"""Set the locale""" """Set the locale"""
locale.setlocale(category, newlocale) try:
locale.setlocale(locale.LC_ALL, newlocale)
except AttributeError: # locale is None
pass
# it looks like some stuff isn't initialised yet when this is called the # it looks like some stuff isn't initialised yet when this is called the
# first time and its init gets the locale settings from the environment # first time and its init gets the locale settings from the environment
os.environ["LC_ALL"] = newlocale os.environ["LC_ALL"] = newlocale
def formatTimestamp(timestamp=None, as_unicode=True): def formatTimestamp(timestamp=None):
"""Return a formatted timestamp""" """Return a formatted timestamp"""
# For some reason some timestamps are strings so we need to sanitize. # For some reason some timestamps are strings so we need to sanitize.
if timestamp is not None and not isinstance(timestamp, int): if timestamp is not None and not isinstance(timestamp, int):
@ -110,8 +116,8 @@ def formatTimestamp(timestamp=None, as_unicode=True):
except ValueError: except ValueError:
timestring = time.strftime(time_format) timestring = time.strftime(time_format)
if as_unicode: if sys.version_info[0] == 2:
return unicode(timestring, encoding) return timestring.decode(encoding)
return timestring return timestring