Module: check_mk
Branch: master
Commit: 0ea735edeb6a7087da17dea073374723063495ec
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0ea735edeb6a70…
Author: Óscar Nájera <on(a)mathias-kettner.de>
Date: Mon Nov 12 11:05:27 2018 +0100
Manual fix typecheck idiom cmk/gui/
In modules availability, forms, metrics, userdb, valuespec
CMK-837
Change-Id: I6bad6f78673e7e7221d954af2287662ddde5e613
---
cmk/gui/availability.py | 2 +-
cmk/gui/forms.py | 9 ++++++++-
cmk/gui/metrics.py | 9 ++++++++-
cmk/gui/userdb.py | 9 ++++++++-
cmk/gui/valuespec.py | 29 +++++++++++++++++++----------
cmk/gui/wato/__init__.py | 2 +-
6 files changed, 45 insertions(+), 15 deletions(-)
diff --git a/cmk/gui/availability.py b/cmk/gui/availability.py
index 31bb9dd..59e4f41 100644
--- a/cmk/gui/availability.py
+++ b/cmk/gui/availability.py
@@ -508,7 +508,7 @@ def render_number_function(timeformat):
def prepare_avo_timeformats(timeformat):
this_timeformat = [("percentage_2",
render_number_function("percentage_2"))]
- if type(timeformat) in [list, tuple]:
+ if isinstance(timeformat, (list, tuple)):
if timeformat[0] == "both":
this_timeformat = [(x, render_number_function(x)) for x in timeformat[1:]]
elif timeformat[0] == "perc":
diff --git a/cmk/gui/forms.py b/cmk/gui/forms.py
index f489be3..bb3cd97 100644
--- a/cmk/gui/forms.py
+++ b/cmk/gui/forms.py
@@ -24,6 +24,13 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+try:
+ # does not exist in Py3, but is supper class of str & unicode in py2
+ basestring
+except NameError:
+ basestring = str # pylint: disable=redefined-builtin
+ unicode = str # pylint: disable=redefined-builtin
+
from cmk.gui.htmllib import HTML
from cmk.gui.i18n import _
from cmk.gui.globals import html
@@ -308,7 +315,7 @@ def section(title=None, checkbox=None, section_id=None, simple=False,
hide=False
html.close_div()
if checkbox:
html.open_div(class_="checkbox")
- if type(checkbox) in [str, unicode, HTML]:
+ if isinstance(checkbox, (basestring, HTML)):
html.write(checkbox)
else:
name, active, attrname = checkbox
diff --git a/cmk/gui/metrics.py b/cmk/gui/metrics.py
index be0dc5b..f9c09a0 100644
--- a/cmk/gui/metrics.py
+++ b/cmk/gui/metrics.py
@@ -40,6 +40,13 @@ import string
import json
import traceback
+try:
+ # does not exist in Py3, but is supper class of str & unicode in py2
+ basestring
+except NameError:
+ basestring = str # pylint: disable=redefined-builtin
+ unicode = str # pylint: disable=redefined-builtin
+
import cmk.utils
import cmk.render
import cmk.plugin_registry
@@ -207,7 +214,7 @@ def _required_trivial_metric_names(required_expressions):
allowed_chars = string.ascii_letters + string.digits + "_"
for entry in required_expressions:
- if type(entry) in [str, unicode]:
+ if isinstance(entry, basestring):
if any(char not in allowed_chars for char in entry):
# Found a non trivial metric expression. Totally skip this mechanism
return None
diff --git a/cmk/gui/userdb.py b/cmk/gui/userdb.py
index 434a5db..c23ae66 100644
--- a/cmk/gui/userdb.py
+++ b/cmk/gui/userdb.py
@@ -32,6 +32,13 @@ import traceback
import copy
import pathlib2 as pathlib
+try:
+ # does not exist in Py3, but is supper class of str & unicode in py2
+ basestring
+except NameError:
+ basestring = str # pylint: disable=redefined-builtin
+ unicode = str # pylint: disable=redefined-builtin
+
import cmk.utils
import cmk.paths
import cmk.store as store
@@ -1217,7 +1224,7 @@ def hook_login(username, password):
# False -> failed
if result not in [False, None]:
username = result
- if type(username) not in [str, unicode]:
+ if not isinstance(username, basestring):
raise MKInternalError(
_("The username returned by the %s "
"connector is not of type string (%r).") %
(connection_id, username))
diff --git a/cmk/gui/valuespec.py b/cmk/gui/valuespec.py
index d57b17d..8529162 100644
--- a/cmk/gui/valuespec.py
+++ b/cmk/gui/valuespec.py
@@ -45,7 +45,16 @@ import types
import base64
import hashlib
import socket
+import numbers
import ipaddress
+
+try:
+ # does not exist in Py3, but is supper class of str & unicode in py2
+ basestring
+except NameError:
+ basestring = str # pylint: disable=redefined-builtin
+ unicode = str # pylint: disable=redefined-builtin
+
from Cryptodome.PublicKey import RSA
from UserDict import DictMixin
from enum import Enum
@@ -93,7 +102,7 @@ class ValueSpec(object):
return self._title
def help(self):
- if type(self._help) in [types.FunctionType, types.MethodType]:
+ if isinstance(self._help, (types.FunctionType, types.MethodType)):
return self._help()
return self._help
@@ -122,7 +131,7 @@ class ValueSpec(object):
# for same cases where the default value is known.
def default_value(self):
try:
- if type(self._default_value) in [types.FunctionType, types.MethodType]:
+ if isinstance(self._default_value, (types.FunctionType, types.MethodType)):
return self._default_value()
return self._default_value
except:
@@ -354,7 +363,7 @@ class Integer(ValueSpec):
return text
def validate_datatype(self, value, varprefix):
- if type(value) not in [int, long]:
+ if isinstance(value, numbers.Integral):
raise MKUserError(
varprefix,
_("The value %r has the wrong type %s, but must be of type
int") %
@@ -522,7 +531,7 @@ class TextUnicode(TextAscii):
return html.get_unicode_input(varprefix, "").strip()
def validate_datatype(self, value, varprefix):
- if type(value) not in [str, unicode]:
+ if not isinstance(value, basestring):
raise MKUserError(
varprefix,
_("The value must be of type str or unicode, but it has type
%s") %
@@ -766,7 +775,7 @@ class TextAsciiAutocomplete(TextAscii):
# Check for correct result_data format
assert isinstance(result_data, list)
if result_data:
- assert type(result_data[0]) in [list, tuple]
+ assert isinstance(result_data[0], (list, tuple))
assert len(result_data[0]) == 2
html.write(json.dumps(result_data))
@@ -1585,7 +1594,7 @@ class Float(Integer):
if isinstance(value, float):
return
- if type(value) in [int, long] and self._allow_int:
+ if isinstance(value, numbers.Integral) and self._allow_int:
return
raise MKUserError(
@@ -1613,7 +1622,7 @@ class Percentage(Float):
def validate_datatype(self, value, varprefix):
if self._allow_int:
- if type(value) not in [int, float]:
+ if not isinstance(value, (int, float)):
raise MKUserError(
varprefix,
_("The value %r has type %s, but must be either float or
int") %
@@ -2518,7 +2527,7 @@ class RelativeDate(OptionalDropdownChoice):
return today() + reldays * seconds_per_day
def validate_datatype(self, value, varprefix):
- if type(value) not in [float, int]:
+ if not isinstance(value, (int, float)):
raise MKUserError(varprefix, _("Date must be a number value"))
def validate_value(self, value, varprefix):
@@ -2669,7 +2678,7 @@ class AbsoluteDate(ValueSpec):
def validate_datatype(self, value, varprefix):
if value == None and self._allow_empty:
return
- if type(value) not in [int, float]:
+ if not isinstance(value, (int, float)):
raise MKUserError(
varprefix,
_("The type of the timestamp must be int or float, but is %s")
% type_name(value))
@@ -3828,7 +3837,7 @@ class AutoTimestamp(FixedValue):
return time.strftime("%F %T", time.localtime(value))
def validate_datatype(self, value, varprefix):
- if type(value) not in [int, float]:
+ if not isinstance(value, (int, float)):
return MKUserError(varprefix, _("Invalid datatype of timestamp: must be
int or float."))
diff --git a/cmk/gui/wato/__init__.py b/cmk/gui/wato/__init__.py
index b545f3c..a450982 100644
--- a/cmk/gui/wato/__init__.py
+++ b/cmk/gui/wato/__init__.py
@@ -272,7 +272,7 @@ wato_root_dir = watolib.wato_root_dir
multisite_dir = watolib.multisite_dir
# TODO: Kept for old plugin compatibility. Remove this one day
-from cmk.gui.valuespec import * # pylint: disable=wildcard-import
+from cmk.gui.valuespec import * # pylint: disable=wildcard-import,redefined-builtin
syslog_facilities = cmk.gui.mkeventd.syslog_facilities
ALL_HOSTS = watolib.ALL_HOSTS
ALL_SERVICES = watolib.ALL_SERVICES