From: Michael Tremer Date: Mon, 28 Sep 2020 17:04:09 +0000 (+0000) Subject: locales: Drop our custom module X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=72fc5ca569b36eecfb9cf47f0c822671bf260a7e;p=oddments%2Fcollecty.git locales: Drop our custom module Gettext can handle this for us Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 83110ac..d4e355b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -82,7 +82,6 @@ collecty_PYTHON = \ src/collecty/daemon.py \ src/collecty/errors.py \ src/collecty/i18n.py \ - src/collecty/locales.py \ src/collecty/logger.py \ src/collecty/util.py diff --git a/src/collecty/locales.py b/src/collecty/locales.py deleted file mode 100644 index c6051db..0000000 --- a/src/collecty/locales.py +++ /dev/null @@ -1,125 +0,0 @@ -#!/usr/bin/python3 -############################################################################### -# # -# collecty - A system statistics collection daemon for IPFire # -# Copyright (C) 2015 IPFire development team # -# # -# This program is free software: you can redistribute it and/or modify # -# it under the terms of the GNU General Public License as published by # -# the Free Software Foundation, either version 3 of the License, or # -# (at your option) any later version. # -# # -# This program is distributed in the hope that it will be useful, # -# but WITHOUT ANY WARRANTY; without even the implied warranty of # -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # -# GNU General Public License for more details. # -# # -# You should have received a copy of the GNU General Public License # -# along with this program. If not, see . # -# # -############################################################################### - -import gettext -import logging -import os - -from .constants import * -from .i18n import TEXTDOMAIN - -log = logging.getLogger("collecty.locale") - -DEFAULT_LOCALE = "en_US.utf-8" - -class Locale(object): - def __init__(self, lang): - self.lang = lang - - def translate(self, message, plural_message=None, count=None): - if plural_message is not None: - assert count is not None - - # Replace the message by the plural message if - # we are using plural. - if count > 1: - message = plural_message - - return message - - -class GettextLocale(Locale): - def __init__(self, lang, translation): - Locale.__init__(self, lang) - - self.translation = translation - - # Bind gettext functions - self.gettext = self.translation.gettext - self.ngettext = self.translation.ngettext - - def translate(self, message, plural_message=None, count=None): - if plural_message is not None: - assert count is not None - return self.ngettext(message, plural_message, count) - - return self.gettext(message) - - -def _find_all_locales(domain, directory): - locales = { - DEFAULT_LOCALE : Locale(DEFAULT_LOCALE), - } - - for lang in os.listdir(directory): - if lang.startswith("."): - continue - - filename = os.path.join(directory, lang, "LC_MESSAGES", - "%s.mo" % domain) - - # Check if a translation file exists and go on if not - if not os.path.exists(filename): - continue - - try: - translation = gettext.translation(domain, - directory, languages=[lang]) - except Exception as e: - log.error("Cound not load translation for %s: %s" \ - % (lang, e)) - continue - - locales[lang] = GettextLocale(lang, translation) - - log.debug("Loaded translations: %s" % ", ".join(locales.keys())) - - return locales - -_locales = _find_all_locales(TEXTDOMAIN, "/usr/share/locale") - -def get_supported_locales(): - return list(_locales.keys()) - -def get_closest(*langs): - for lang in langs: - if not lang: - continue - - lang = lang.replace("-", "_") - parts = lang.split("_") - - if len(parts) > 2: - continue - - elif len(parts) == 2: - parts[0] = parts[0].lower() - parts[1] = parts[1].upper() - lang = "_".join(parts) - - for l in (lang, parts[0]): - try: - return _locales[l] - except KeyError: - pass - -def get(*langs): - return get_closest(*langs) or _locales.get(DEFAULT_LOCALE, None) diff --git a/src/collecty/plugins/base.py b/src/collecty/plugins/base.py index 7c97aef..8f12255 100644 --- a/src/collecty/plugins/base.py +++ b/src/collecty/plugins/base.py @@ -26,7 +26,6 @@ import rrdtool import time import unicodedata -from .. import locales from .. import util from ..constants import * from ..i18n import _ @@ -38,15 +37,14 @@ class Environment(object): Sets the correct environment for rrdtool to create localised graphs and graphs in the correct timezone. """ - def __init__(self, timezone, locale): + def __init__(self, timezone="UTC", locale="en_US.utf-8"): # Build the new environment self.new_environment = { - "TZ" : timezone or "UTC", + "LANG" : locale, + "LC_ALL" : locale, + "TZ" : timezone, } - for k in ("LANG", "LC_ALL"): - self.new_environment[k] = locale or "en_US.utf-8" - def __enter__(self): # Save the current environment self.old_environment = {} @@ -199,7 +197,8 @@ class Plugin(object, metaclass=PluginRegistration): time_start = time.time() - graph = template.generate_graph(**kwargs) + with Environment(timezone=timezone, locale=locale): + graph = template.generate_graph(**kwargs) duration = time.time() - time_start self.log.debug(_("Generated graph %s in %.1fms") \ @@ -533,7 +532,7 @@ class GraphTemplate(object): self.plugin = plugin # Save localisation parameters - self.locale = locales.get(locale) + self.locale = locale self.timezone = timezone # Get all required RRD objects @@ -692,8 +691,7 @@ class GraphTemplate(object): for arg in args: self.log.debug(" %s" % arg) - with Environment(self.timezone, self.locale.lang): - graph = rrdtool.graphv("-", *args) + graph = rrdtool.graphv("-", *args) return { "image" : graph.get("image"), diff --git a/src/collecty/plugins/conntrack.py b/src/collecty/plugins/conntrack.py index 443c53f..3191b55 100644 --- a/src/collecty/plugins/conntrack.py +++ b/src/collecty/plugins/conntrack.py @@ -23,6 +23,7 @@ from . import base from ..colours import * from ..constants import * +from ..i18n import _ class ConntrackGraphTemplate(base.GraphTemplate): name = "conntrack" @@ -31,8 +32,6 @@ class ConntrackGraphTemplate(base.GraphTemplate): @property def rrd_graph(self): - _ = self.locale.translate - return [ "COMMENT:%s" % EMPTY_LABEL, "COMMENT:%s" % (COLUMN % _("Current")), @@ -58,14 +57,10 @@ class ConntrackGraphTemplate(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate - return _("Connection Tracking Table") @property def graph_vertical_label(self): - _ = self.locale.translate - return _("Entries") diff --git a/src/collecty/plugins/contextswitches.py b/src/collecty/plugins/contextswitches.py index 519c3e0..3bc84e2 100644 --- a/src/collecty/plugins/contextswitches.py +++ b/src/collecty/plugins/contextswitches.py @@ -25,14 +25,13 @@ from . import base from ..colours import * from ..constants import * +from ..i18n import _ class GraphTemplateContextSwitches(base.GraphTemplate): name = "context-switches" @property def rrd_graph(self): - _ = self.locale.translate - return [ "COMMENT:%s" % EMPTY_LABEL, "COMMENT:%s" % (COLUMN % _("Current")), @@ -56,12 +55,10 @@ class GraphTemplateContextSwitches(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate return _("Context Switches") @property def graph_vertical_label(self): - _ = self.locale.translate return _("Context Switches/s") diff --git a/src/collecty/plugins/cpufreq.py b/src/collecty/plugins/cpufreq.py index 638b6aa..266a57f 100644 --- a/src/collecty/plugins/cpufreq.py +++ b/src/collecty/plugins/cpufreq.py @@ -34,12 +34,10 @@ class GraphTemplateCPUFreq(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate return _("Processor Frequencies") @property def graph_vertical_label(self): - _ = self.locale.translate return "%s [%s]" % (_("Frequency"), _("Hz")) processor_colours = [ diff --git a/src/collecty/plugins/df.py b/src/collecty/plugins/df.py index 7fef88a..0c083bb 100644 --- a/src/collecty/plugins/df.py +++ b/src/collecty/plugins/df.py @@ -26,6 +26,7 @@ from . import base from ..constants import * from ..colours import * +from ..i18n import _ class GraphTemplateDiskUsage(base.GraphTemplate): name = "disk-usage" @@ -33,8 +34,6 @@ class GraphTemplateDiskUsage(base.GraphTemplate): @property def rrd_graph(self): - _ = self.locale.translate - return [ "COMMENT:%s" % EMPTY_LABEL, "COMMENT:%s" % (COLUMN % _("Current")), @@ -69,12 +68,10 @@ class GraphTemplateDiskUsage(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate return _("Disk Usage of %s") % self.object.mountpoint @property def graph_vertical_label(self): - _ = self.locale.translate return _("Bytes") @@ -84,8 +81,6 @@ class GraphTemplateInodeUsage(base.GraphTemplate): @property def rrd_graph(self): - _ = self.locale.translate - rrd_graph = [ "COMMENT:%s" % EMPTY_LABEL, "COMMENT:%s" % (COLUMN % _("Current")), @@ -126,12 +121,10 @@ class GraphTemplateInodeUsage(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate return _("Inode Usage of %s") % self.object.mountpoint @property def graph_vertical_label(self): - _ = self.locale.translate return _("Inodes") diff --git a/src/collecty/plugins/disk.py b/src/collecty/plugins/disk.py index 71ac02d..192598d 100644 --- a/src/collecty/plugins/disk.py +++ b/src/collecty/plugins/disk.py @@ -27,14 +27,13 @@ from . import base from ..colours import * from ..constants import * +from ..i18n import _ class GraphTemplateDiskBadSectors(base.GraphTemplate): name = "disk-bad-sectors" @property def rrd_graph(self): - _ = self.locale.translate - return [ "COMMENT:%s" % EMPTY_LABEL, "COMMENT:%s" % (COLUMN % _("Current")), @@ -53,14 +52,10 @@ class GraphTemplateDiskBadSectors(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate - return _("Bad Sectors of %s") % self.object.device_string @property def graph_vertical_label(self): - _ = self.locale.translate - return _("Pending/Relocated Sectors") @@ -69,8 +64,6 @@ class GraphTemplateDiskBytes(base.GraphTemplate): @property def rrd_graph(self): - _ = self.locale.translate - rrd_graph = [ "CDEF:read_bytes=read_sectors,512,*", "CDEF:write_bytes=write_sectors,512,*", @@ -94,12 +87,10 @@ class GraphTemplateDiskBytes(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate return _("Disk Utilisation of %s") % self.object.device_string @property def graph_vertical_label(self): - _ = self.locale.translate return _("Byte per Second") @@ -108,8 +99,6 @@ class GraphTemplateDiskIoOps(base.GraphTemplate): @property def rrd_graph(self): - _ = self.locale.translate - rrd_graph = [ "LINE1:read_ios%s:%-15s" % (COLOUR_READ, _("Read")), "GPRINT:read_ios_cur:%12s\:" % _("Current") + " %6.2lf", @@ -130,12 +119,10 @@ class GraphTemplateDiskIoOps(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate return _("Disk IO Operations of %s") % self.object.device_string @property def graph_vertical_label(self): - _ = self.locale.translate return _("Operations per Second") @@ -144,8 +131,6 @@ class GraphTemplateDiskTemperature(base.GraphTemplate): @property def rrd_graph(self): - _ = self.locale.translate - rrd_graph = [ "CDEF:celsius=temperature,273.15,-", "VDEF:temp_cur=celsius,LAST", @@ -164,12 +149,10 @@ class GraphTemplateDiskTemperature(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate return _("Disk Temperature of %s") % self.object.device_string @property def graph_vertical_label(self): - _ = self.locale.translate return _("° Celsius") @property diff --git a/src/collecty/plugins/interface.py b/src/collecty/plugins/interface.py index 6500fb0..5cf9975 100644 --- a/src/collecty/plugins/interface.py +++ b/src/collecty/plugins/interface.py @@ -26,6 +26,7 @@ from . import base from ..colours import * from ..constants import * +from ..i18n import _ class GraphTemplateInterfaceBase(base.GraphTemplate): @property @@ -38,8 +39,6 @@ class GraphTemplateInterfaceBits(GraphTemplateInterfaceBase): @property def rrd_graph(self): - _ = self.locale.translate - return [ # Headline "COMMENT:%s" % EMPTY_LABEL, @@ -92,14 +91,10 @@ class GraphTemplateInterfaceBits(GraphTemplateInterfaceBase): @property def graph_title(self): - _ = self.locale.translate - return _("Bandwidth Usage on %s") % self.interface @property def graph_vertical_label(self): - _ = self.locale.translate - return _("Bit/s") @@ -108,8 +103,6 @@ class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase): @property def rrd_graph(self): - _ = self.locale.translate - return [ # Headline "COMMENT:%s" % EMPTY_LABEL, @@ -145,13 +138,10 @@ class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase): @property def graph_title(self): - _ = self.locale.translate - return _("Transferred Packets on %s") % self.interface @property def graph_vertical_label(self): - _ = self.locale.translate return _("Packets/s") @@ -160,8 +150,6 @@ class GraphTemplateInterfaceErrors(GraphTemplateInterfaceBase): @property def rrd_graph(self): - _ = self.locale.translate - return [ # Headline "COMMENT:%s" % EMPTY_LABEL, @@ -233,14 +221,10 @@ class GraphTemplateInterfaceErrors(GraphTemplateInterfaceBase): @property def graph_title(self): - _ = self.locale.translate - return _("Errors/Dropped Packets on %s") % self.interface @property def graph_vertical_label(self): - _ = self.locale.translate - return _("Packets/s") diff --git a/src/collecty/plugins/interrupts.py b/src/collecty/plugins/interrupts.py index e616356..e58a328 100644 --- a/src/collecty/plugins/interrupts.py +++ b/src/collecty/plugins/interrupts.py @@ -26,14 +26,13 @@ from . import base from ..colours import * from ..constants import * +from ..i18n import _ class GraphTemplateInterrupts(base.GraphTemplate): name = "interrupts" @property def rrd_graph(self): - _ = self.locale.translate - return [ # Headline "COMMENT:%s" % EMPTY_LABEL, @@ -57,8 +56,6 @@ class GraphTemplateInterrupts(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate - if self.object.irq is None: return _("Interrupts") @@ -66,8 +63,6 @@ class GraphTemplateInterrupts(base.GraphTemplate): @property def graph_vertical_label(self): - _ = self.locale.translate - return _("Interrupts/s") diff --git a/src/collecty/plugins/ipfrag.py b/src/collecty/plugins/ipfrag.py index fb27b05..0d6f935 100644 --- a/src/collecty/plugins/ipfrag.py +++ b/src/collecty/plugins/ipfrag.py @@ -26,14 +26,13 @@ from . import base from ..colours import * from ..constants import * +from ..i18n import _ class GraphTemplateIPv6Fragmentation(base.GraphTemplate): name = "ipv6-fragmentation" @property def rrd_graph(self): - _ = self.locale.translate - return [ "CDEF:ip6_reasm_real_fails=ip6_reasm_fails,ip6_reasm_timeout,-", @@ -96,8 +95,6 @@ class GraphTemplateIPv6Fragmentation(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate - if self.object.interface: return _("IPv6 Fragmentation on %s") % self.object.interface @@ -105,8 +102,6 @@ class GraphTemplateIPv6Fragmentation(base.GraphTemplate): @property def graph_vertical_label(self): - _ = self.locale.translate - return _("Packets/s") @property @@ -122,8 +117,6 @@ class GraphTemplateIPv4Fragmentation(base.GraphTemplate): @property def rrd_graph(self): - _ = self.locale.translate - return [ "CDEF:ip4_reasm_real_fails=ip4_reasm_fails,ip4_reasm_timeout,-", @@ -186,8 +179,6 @@ class GraphTemplateIPv4Fragmentation(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate - if self.object.interface: return _("IPv4 Fragmentation on %s") % self.object.interface @@ -195,7 +186,6 @@ class GraphTemplateIPv4Fragmentation(base.GraphTemplate): @property def graph_vertical_label(self): - _ = self.locale.translate return _("Packets/s") @property diff --git a/src/collecty/plugins/latency.py b/src/collecty/plugins/latency.py index 3334782..27cdc66 100644 --- a/src/collecty/plugins/latency.py +++ b/src/collecty/plugins/latency.py @@ -22,11 +22,11 @@ import socket from .. import _collecty -from ..i18n import _ from . import base from ..colours import * from ..constants import * +from ..i18n import _ PING_HOSTS = [ # gateway is a special name that is automatically @@ -44,8 +44,6 @@ class GraphTemplateLatency(base.GraphTemplate): @property def rrd_graph(self): - _ = self.locale.translate - return [ # Compute the biggest loss and convert into percentage "CDEF:ploss=loss6,loss4,MAX,100,*", @@ -121,8 +119,6 @@ class GraphTemplateLatency(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate - if self.object.hostname == "gateway": hostname = _("Default Gateway") else: @@ -132,7 +128,6 @@ class GraphTemplateLatency(base.GraphTemplate): @property def graph_vertical_label(self): - _ = self.locale.translate return _("Milliseconds") @property diff --git a/src/collecty/plugins/loadavg.py b/src/collecty/plugins/loadavg.py index bcdbf62..3a786a6 100644 --- a/src/collecty/plugins/loadavg.py +++ b/src/collecty/plugins/loadavg.py @@ -25,14 +25,13 @@ from . import base from ..colours import * from ..constants import * +from ..i18n import _ class GraphTemplateLoadAvg(base.GraphTemplate): name = "loadavg" @property def rrd_graph(self): - _ = self.locale.translate - rrd_graph = [ "LINE2:load15%s:%s" % ( YELLOW, LABEL % _("15 Minutes"), @@ -72,14 +71,10 @@ class GraphTemplateLoadAvg(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate - return _("Load Average") @property def graph_vertical_label(self): - _ = self.locale.translate - return _("Load") @property diff --git a/src/collecty/plugins/memory.py b/src/collecty/plugins/memory.py index e16205c..786acc4 100644 --- a/src/collecty/plugins/memory.py +++ b/src/collecty/plugins/memory.py @@ -23,6 +23,7 @@ from . import base from ..colours import * from ..constants import * +from ..i18n import _ class GraphTemplateMemory(base.GraphTemplate): name = "memory" @@ -31,8 +32,6 @@ class GraphTemplateMemory(base.GraphTemplate): @property def rrd_graph(self): - _ = self.locale.translate - return [ # Headline "COMMENT:%s" % EMPTY_LABEL, @@ -104,14 +103,10 @@ class GraphTemplateMemory(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate - return _("Memory Usage") @property def graph_vertical_label(self): - _ = self.locale.translate - return _("Bytes") diff --git a/src/collecty/plugins/processor.py b/src/collecty/plugins/processor.py index 97d71e1..8b7f7e9 100644 --- a/src/collecty/plugins/processor.py +++ b/src/collecty/plugins/processor.py @@ -25,14 +25,13 @@ from . import base from ..colours import * from ..constants import * +from ..i18n import _ class GraphTemplateProcessor(base.GraphTemplate): name = "processor" @property def rrd_graph(self): - _ = self.locale.translate - return [ # Add all used CPU cycles "CDEF:usage=user,nice,+,sys,+,wait,+,irq,+,sirq,+,steal,+,guest,+,guest_nice,+", @@ -166,12 +165,10 @@ class GraphTemplateProcessor(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate return _("Processor Usage") @property def graph_vertical_label(self): - _ = self.locale.translate return _("Percent") diff --git a/src/collecty/plugins/sensors.py b/src/collecty/plugins/sensors.py index 684a03f..da244d2 100644 --- a/src/collecty/plugins/sensors.py +++ b/src/collecty/plugins/sensors.py @@ -25,14 +25,13 @@ import re from .. import _collecty from . import base +from ..i18n import _ class GraphTemplateSensorsTemperature(base.GraphTemplate): name = "sensors-temperature" @property def rrd_graph(self): - _ = self.locale.translate - return [ # Convert everything to Celsius "CDEF:value_c=value,273.15,-", @@ -71,12 +70,10 @@ class GraphTemplateSensorsTemperature(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate return _("Temperature (%s)") % self.object.sensor.name @property def graph_vertical_label(self): - _ = self.locale.translate return _("° Celsius") @@ -107,7 +104,6 @@ class GraphTemplateSensorsProcessorTemperature(base.GraphTemplate): @property def rrd_graph(self): - _ = self.locale.translate rrd_graph = [] counter = 0 @@ -170,12 +166,10 @@ class GraphTemplateSensorsProcessorTemperature(base.GraphTemplate): @property def graph_title(self): - _ = self.locale.translate return _("Processor") @property def graph_vertical_label(self): - _ = self.locale.translate return _("Temperature")