Module: check_mk
Branch: master
Commit: e86db4f1b117d1b00e8e1171ebfbcac615914bdf
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e86db4f1b117d1…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Jul 13 21:09:07 2018 +0200
Centralized cmk.i18n _() stub and added type infos
Change-Id: I2fac819c2002179172c5dcd4946f17799ade8fa8
---
cmk/__init__.py | 9 +--------
cmk/cpu_tracking.py | 5 -----
cmk/defines.py | 9 +--------
cmk/ec/defaults.py | 11 +----------
cmk/exceptions.py | 10 +---------
cmk/i18n.py | 37 +++++++++++++++++++++++++++++++++++++
cmk/man_pages.py | 12 ++----------
cmk/paths.py | 13 -------------
cmk/regex.py | 13 ++++---------
cmk/render.py | 8 +-------
cmk/store.py | 12 ++----------
cmk/werks.py | 11 ++---------
12 files changed, 52 insertions(+), 98 deletions(-)
diff --git a/cmk/__init__.py b/cmk/__init__.py
index f650ace..902a3ca 100644
--- a/cmk/__init__.py
+++ b/cmk/__init__.py
@@ -35,14 +35,7 @@ import os
import cmk.paths
from cmk.exceptions import MKGeneralException
-
-# TODO: Clean this up one day by using the way recommended by gettext.
-# (See
https://docs.python.org/2/library/gettext.html). For this we
-# need the path to the locale files here.
-try:
- _
-except NameError:
- _ = lambda x: x # Fake i18n when not available
+from cmk.i18n import _
def omd_version():
return os.path.basename(os.readlink(cmk.paths.omd_root + "/version"))
diff --git a/cmk/cpu_tracking.py b/cmk/cpu_tracking.py
index 76fad2a..f4ad90f 100644
--- a/cmk/cpu_tracking.py
+++ b/cmk/cpu_tracking.py
@@ -29,11 +29,6 @@ import time
import cmk_base.console as console
-try:
- _
-except NameError:
- _ = lambda x: x # Fake i18n when not available
-
# TODO: Move state out of module scope
# TODO: This should be rewritten to a context manager object. See cmk.profile for
# an example how it could look like.
diff --git a/cmk/defines.py b/cmk/defines.py
index fb53b0e..dcd995a 100644
--- a/cmk/defines.py
+++ b/cmk/defines.py
@@ -27,14 +27,7 @@
"""This module serves constants which are needed in several components
of Check_MK."""
-# TODO: Clean this up one day by using the way recommended by gettext.
-# (See
https://docs.python.org/2/library/gettext.html). For this we
-# need the path to the locale files here.
-try:
- _
-except NameError:
- _ = lambda x: x # Fake i18n when not available
-
+from cmk.i18n import _
# TODO: Investigate Check_MK code for more defines and other places
# where similar strucures are defined and use the things from
diff --git a/cmk/ec/defaults.py b/cmk/ec/defaults.py
index 07485d8..7e5c4fe 100644
--- a/cmk/ec/defaults.py
+++ b/cmk/ec/defaults.py
@@ -28,16 +28,7 @@
from typing import Any, AnyStr, Dict, List # pylint: disable=unused-import
import cmk.log
-# Clean this up one day by using the way recommended by gettext.
-# (See
https://docs.python.org/2/library/gettext.html). For this we
-# need the path to the locale files here.
-try:
- _
-except NameError:
- # Fake i18n when not available
- def _(string):
- # type: (AnyStr) -> AnyStr
- return string
+from cmk.i18n import _
def default_rule_pack(rules):
diff --git a/cmk/exceptions.py b/cmk/exceptions.py
index 1ac4350..9b50e66 100644
--- a/cmk/exceptions.py
+++ b/cmk/exceptions.py
@@ -24,15 +24,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
-
-# TODO: Clean this up one day by using the way recommended by gettext.
-# (See
https://docs.python.org/2/library/gettext.html). For this we
-# need the path to the locale files here.
-try:
- _
-except NameError:
- _ = lambda x: x # Fake i18n when not available
-
+from cmk.i18n import _
# never used directly in the code. Just some wrapper to make all of our
# exceptions handleable with one call
diff --git a/cmk/i18n.py b/cmk/i18n.py
new file mode 100644
index 0000000..96f3bd5
--- /dev/null
+++ b/cmk/i18n.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2016 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at
http://mathias-kettner.de/check_mk.
+#
+# check_mk 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 in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# tails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+
+"""Stub for future i18n code on cmk main module level"""
+
+from typing import AnyStr, Text # pylint:disable=unused-import
+
+# TODO: Clean this up one day by using the way recommended by gettext.
+# (See
https://docs.python.org/2/library/gettext.html). For this we
+# need the path to the locale files here.
+# Fake i18n when not available
+def _(string):
+ # type: (AnyStr) -> Text
+ return string
diff --git a/cmk/man_pages.py b/cmk/man_pages.py
index 2dc6bef..7afb3f0 100644
--- a/cmk/man_pages.py
+++ b/cmk/man_pages.py
@@ -41,16 +41,8 @@ import cmk.debug
import cmk.paths
import cmk.tty as tty
-from .exceptions import MKGeneralException
-
-# TODO: Clean this up one day by using the way recommended by gettext.
-# (See
https://docs.python.org/2/library/gettext.html). For this we
-# need the path to the locale files here.
-try:
- _
-except NameError:
- _ = lambda x: x # Fake i18n when not available
-
+from cmk.exceptions import MKGeneralException
+from cmk.i18n import _
catalog_titles = {
"hw" : "Appliances, other dedicated Hardware",
diff --git a/cmk/paths.py b/cmk/paths.py
index 5398b2f..7986296 100644
--- a/cmk/paths.py
+++ b/cmk/paths.py
@@ -29,15 +29,6 @@ to all components of Check_MK."""
import os
-# TODO: Clean this up one day by using the way recommended by gettext.
-# (See
https://docs.python.org/2/library/gettext.html). For this we
-# need the path to the locale files here.
-try:
- _
-except NameError:
- _ = lambda x: x # Fake i18n when not available
-
-
#
# First declare the possible paths for the linters. Then set it within _set_paths()
#
@@ -155,10 +146,6 @@ def _set_paths():
def _omd_root():
return os.environ.get("OMD_ROOT", "")
- #try:
- #except KeyError:
- # raise MKGeneralException(_("OMD_ROOT environment variable not set. You can
"
- # "only execute this in an OMD site."))
def _set_core_specific_paths():
diff --git a/cmk/regex.py b/cmk/regex.py
index 374c5db..f4be0db 100644
--- a/cmk/regex.py
+++ b/cmk/regex.py
@@ -27,17 +27,12 @@
"""This module wraps some regex handling functions used by
Check_MK"""
import re
-from .exceptions import MKGeneralException
+from typing import Dict, Pattern # pylint:disable=unused-import
-# TODO: Clean this up one day by using the way recommended by gettext.
-# (See
https://docs.python.org/2/library/gettext.html). For this we
-# need the path to the locale files here.
-try:
- _
-except NameError:
- _ = lambda x: x # Fake i18n when not available
+from cmk.exceptions import MKGeneralException
+from cmk.i18n import _
-g_compiled_regexes = {}
+g_compiled_regexes = {} # type: Dict[str, Pattern]
def regex(pattern):
"""Compile regex or look it up in already compiled regexes.
diff --git a/cmk/render.py b/cmk/render.py
index 85642a6..b964dfe 100644
--- a/cmk/render.py
+++ b/cmk/render.py
@@ -33,13 +33,7 @@ are just for optical output purposes."""
import time
import math
-# TODO: Clean this up one day by using the way recommended by gettext.
-# (See
https://docs.python.org/2/library/gettext.html). For this we
-# need the path to the locale files here.
-try:
- _
-except NameError:
- _ = lambda x: x # Fake i18n when not available
+from cmk.i18n import _
#.
# .--Date/Time-----------------------------------------------------------.
diff --git a/cmk/store.py b/cmk/store.py
index fbc3fad..d7a68c2 100644
--- a/cmk/store.py
+++ b/cmk/store.py
@@ -36,20 +36,12 @@ import pprint
import tempfile
import time
-from .exceptions import MKGeneralException
+from cmk.exceptions import MKGeneralException
+from cmk.i18n import _
# TODO: Make all methods handle paths the same way. e.g. mkdir() and makedirs()
# care about encoding a path to UTF-8. The others don't to that.
-# TODO: Clean this up one day by using the way recommended by gettext.
-# (See
https://docs.python.org/2/library/gettext.html). For this we
-# need the path to the locale files here.
-try:
- _
-except NameError:
- _ = lambda x: x # Fake i18n when not available
-
-
#.
#.
# .--Directories---------------------------------------------------------.
diff --git a/cmk/werks.py b/cmk/werks.py
index ab1c730..f46a20d 100644
--- a/cmk/werks.py
+++ b/cmk/werks.py
@@ -32,15 +32,8 @@ import json
import cmk.paths
-from .exceptions import MKGeneralException
-
-# TODO: Clean this up one day by using the way recommended by gettext.
-# (See
https://docs.python.org/2/library/gettext.html). For this we
-# need the path to the locale files here.
-try:
- _
-except NameError:
- _ = lambda x: x # Fake i18n when not available
+from cmk.exceptions import MKGeneralException
+from cmk.i18n import _
def werk_classes():