Module: check_mk
Branch: master
Commit: 2fba5a191f51aeb2fadb689ff22d571c9bc4c0ac
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=2fba5a191f51ae…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Jul 12 09:10:23 2018 +0200
Add type checking for some cmk.gui modules
* These checks are not executed yet because mypy
is not able to deal with __builtin__.html.
This will be cleaned up soon.
Change-Id: I1f661e99ee15ce48bb4889ff1dc7c8c5e2920206
---
cmk/gui/exceptions.py | 28 ++++++++++++++++++++++------
cmk/gui/http_status.py | 1 +
cmk/gui/i18n.py | 13 ++++++++++---
cmk/paths.py | 4 ++--
tests/static/Makefile | 3 ++-
5 files changed, 37 insertions(+), 12 deletions(-)
diff --git a/cmk/gui/exceptions.py b/cmk/gui/exceptions.py
index d1bf6b1..005d285 100644
--- a/cmk/gui/exceptions.py
+++ b/cmk/gui/exceptions.py
@@ -24,8 +24,10 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+from typing import Optional # pylint: disable=unused-import
import cmk.gui.http_status
+from cmk.gui.i18n import _
from cmk.exceptions import MKGeneralException, MKException
@@ -41,8 +43,9 @@ class FinalizeRequest(Exception):
"""Is used to end the HTTP request processing from deeper code
levels"""
# TODO: Drop this default and make exit code explicit for all call sites
def __init__(self, code = None):
- super(FinalizeRequest,
self).__init__(cmk.gui.http_status.status_with_reason(code))
- self.status = code or cmk.gui.http_status.HTTP_OK
+ # type: (Optional[int]) -> None
+ self.status = code or cmk.gui.http_status.HTTP_OK # type: int
+ super(FinalizeRequest,
self).__init__(cmk.gui.http_status.status_with_reason(self.status))
@@ -50,69 +53,82 @@ class HTTPRedirect(FinalizeRequest):
"""Is used to end the HTTP request processing from deeper code levels
and making the client request another page after receiving the
response."""
def __init__(self, url, code=cmk.gui.http_status.HTTP_MOVED_TEMPORARILY):
+ # type: (str, int) -> None
super(HTTPRedirect, self).__init__(code)
if code not in [ cmk.gui.http_status.HTTP_MOVED_PERMANENTLY,
cmk.gui.http_status.HTTP_MOVED_TEMPORARILY ]:
raise Exception("Invalid status code: %d" % code)
- self.url = url
+ self.url = url #type: str
class MKAuthException(MKException):
def __init__(self, reason):
- self.reason = reason
+ # type: (str) -> None
+ self.reason = reason # type: str
super(MKAuthException, self).__init__(reason)
def __str__(self):
+ # type: () -> str
return self.reason
def title(self):
+ # type: () -> unicode
return _("Permission denied")
def plain_title(self):
+ # type: () -> unicode
return _("Authentication error")
class MKUnauthenticatedException(MKGeneralException):
def title(self):
+ # type: () -> unicode
return _("Not authenticated")
def plain_title(self):
+ # type: () -> unicode
return _("Missing authentication credentials")
class MKConfigError(MKException):
def title(self):
+ # type: () -> unicode
return _("Configuration error")
def plain_title(self):
+ # type: () -> unicode
return self.title()
class MKUserError(MKException):
def __init__(self, varname, message):
- self.varname = varname
- self.message = message
+ # type: (str, str) -> None
+ self.varname = varname # type: str
+ self.message = message # type: str
super(MKUserError, self).__init__(varname, message)
def __str__(self):
+ # type: () -> str
return self.message
def title(self):
+ # type: () -> unicode
return _("Invalid User Input")
def plain_title(self):
+ # type: () -> unicode
return _("User error")
diff --git a/cmk/gui/http_status.py b/cmk/gui/http_status.py
index 4a5dfac..2061f3c 100644
--- a/cmk/gui/http_status.py
+++ b/cmk/gui/http_status.py
@@ -77,6 +77,7 @@ HTTP_NOT_EXTENDED = 510
def status_with_reason(code):
+ # type: (int) -> str
if code == HTTP_OK:
return '200 OK'
elif code == HTTP_MOVED_TEMPORARILY:
diff --git a/cmk/gui/i18n.py b/cmk/gui/i18n.py
index 704c4a5..3a95c32 100644
--- a/cmk/gui/i18n.py
+++ b/cmk/gui/i18n.py
@@ -27,7 +27,7 @@
import __builtin__
import os
import gettext as gettext_module
-from typing import NamedTuple, Optional, List
+from typing import NamedTuple, Optional, List, Tuple
import cmk.paths
@@ -77,7 +77,7 @@ def get_language_alias(lang):
alias = lang
for lang_dir in _get_language_dirs():
try:
- alias = file('%s/%s/alias' % (lang_dir, lang),
'r').read().strip()
+ alias = open('%s/%s/alias' % (lang_dir, lang),
'r').read().strip()
except (OSError, IOError):
pass
return alias
@@ -103,16 +103,19 @@ def get_languages():
def unlocalize():
+ # type: () -> None
global _translation
_translation = None
def localize(lang):
+ # type: (str) -> None
set_language_cookie(lang)
_do_localize(lang)
def _do_localize(lang):
+ # type: (str) -> None
global _translation
if lang is None:
unlocalize()
@@ -127,9 +130,10 @@ def _do_localize(lang):
def _init_language(lang):
+ # type: (str) -> Optional[gettext_module.NullTranslations]
try:
translation = gettext_module.translation("multisite",
_get_cmk_locale_path(lang),
- languages = [ lang ], codeset =
'UTF-8')
+ languages = [ lang ], codeset = 'UTF-8') # type:
Optional[gettext_module.NullTranslations]
except IOError, e:
translation = None
@@ -137,6 +141,7 @@ def _init_language(lang):
def _get_cmk_locale_path(lang):
+ # type: (str) -> str
po_path = '/%s/LC_MESSAGES/multisite.mo' % lang
if os.path.exists(cmk.paths.local_locale_dir + po_path):
return cmk.paths.local_locale_dir
@@ -145,6 +150,7 @@ def _get_cmk_locale_path(lang):
def initialize():
+ # type: () -> None
unlocalize()
@@ -176,6 +182,7 @@ def set_language_cookie(lang):
# Localization of user supplied texts
def _u(text):
+ # type: (unicode) -> unicode
# TODO: Reimplement this once config is available in "cmk.gui"!
#import config
#ldict = config.user_localizations.get(text)
diff --git a/cmk/paths.py b/cmk/paths.py
index f6fd948..bc7af77 100644
--- a/cmk/paths.py
+++ b/cmk/paths.py
@@ -89,7 +89,7 @@ mibs_dir = None
web_dir = None
pnp_templates_dir = None
doc_dir = None
-locale_dir = None
+locale_dir = None # type: str
bin_dir = None
lib_dir = None
mib_dir = None
@@ -105,7 +105,7 @@ local_mibs_dir = None
local_web_dir = None
local_pnp_templates_dir = None
local_doc_dir = None
-local_locale_dir = None
+local_locale_dir = None # type: str
local_bin_dir = None
local_lib_dir = None
local_mib_dir = None
diff --git a/tests/static/Makefile b/tests/static/Makefile
index f8c6b28..5399e16 100644
--- a/tests/static/Makefile
+++ b/tests/static/Makefile
@@ -1,4 +1,5 @@
-FILES_TO_CHECK := $(realpath $(addprefix ../../cmk/ec/,defaults.py export.py
settings.py))
+FILES_TO_CHECK := $(realpath $(addprefix ../../cmk/ec/,defaults.py export.py
settings.py)) \
+ $(realpath $(addprefix ../../cmk/gui/,http_status.py exceptions.py i18n.py))
MYPYPATH := $(realpath typeshed)
PIPENV := PIPENV_NO_INHERIT=true PIPENV_VENV_IN_PROJECT=true pipenv