Skip to content

Commit

Permalink
Python 3: Removed unicode
Browse files Browse the repository at this point in the history
Change-Id: Iad22af67ff49a9eef8788be69696d5542ef240c2
  • Loading branch information
si-23 committed Oct 1, 2019
1 parent 19aee48 commit df50b52
Show file tree
Hide file tree
Showing 21 changed files with 49 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs=0
# TODO: Why do we need persistence?
persistent=yes
extension-pkg-whitelist=rrdtool,_ldap,netifaces,pymssql
bad-functions=file,cmp,apply,execfile,reduce,reload
bad-functions=unicode,file,cmp,apply,execfile,reduce,reload

[MESSAGES CONTROL]
disable=
Expand Down
8 changes: 4 additions & 4 deletions agents/plugins/mk_filestats.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ class LazyFileStats(object):
def __init__(self, path):
super(LazyFileStats, self).__init__()
LOGGER.debug("Creating LazyFileStats(%r)", path)
if not isinstance(path, unicode):
path = unicode(path, 'utf8')
if not isinstance(path, unicode): # pylint: disable=bad-builtin
path = path.decode('utf8')
self.path = path
self.stat_status = None
self._size = None
Expand Down Expand Up @@ -343,8 +343,8 @@ class RegexFilter(AbstractFilter):
def __init__(self, regex_pattern):
super(RegexFilter, self).__init__()
LOGGER.debug("initializing with pattern: %r", regex_pattern)
if not isinstance(regex_pattern, unicode):
regex_pattern = unicode(regex_pattern, 'utf8')
if not isinstance(regex_pattern, unicode): # pylint: disable=bad-builtin
regex_pattern = regex_pattern.decode('utf8')
self._regex = re.compile(regex_pattern, re.UNICODE)

def matches(self, lazy_file):
Expand Down
4 changes: 2 additions & 2 deletions agents/plugins/mtr
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ _punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.:]+')
def host_to_filename(host, delim=u'-'):
# Get rid of gibberish chars, stolen from Django
"""Generates an slightly worse ASCII-only slug."""
host = unicode(host, 'UTF-8')
host = host.decode("utf8")
result = []
for word in _punct_re.split(host.lower()):
word = normalize('NFKD', word).encode('ascii', 'ignore')
if word:
result.append(word)
return unicode(delim.join(result))
return delim.join(result).decode("utf8")


def check_mtr_pid(pid):
Expand Down
3 changes: 2 additions & 1 deletion agents/windows/it/test_section_logfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import platform
import re
import sys
import six

import pytest
from remote import (actual_output, config, remotedir, remotetest, run_subprocess, wait_agent,
Expand Down Expand Up @@ -183,7 +184,7 @@ def manage_logfiles(request):
if platform.system() == 'Windows':
for log in [Globals.testlog1, Globals.testlog2]:
with io.open(log, 'w', encoding=request.param) as logfile:
for entry in [unicode(Globals.testentry1), unicode(Globals.testentry2)]:
for entry in [six.text_type(Globals.testentry1), six.text_type(Globals.testentry2)]:
logfile.write('%s\r\n' % entry)
yield
if platform.system() == 'Windows':
Expand Down
3 changes: 2 additions & 1 deletion agents/wnx/integration/_test_section_logfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import platform
import re
import sys
import six

import pytest
from local import (actual_output, make_yaml_config, src_exec_dir, local_test, run_subprocess,
Expand Down Expand Up @@ -183,7 +184,7 @@ def manage_logfiles(request):
if platform.system() == 'Windows':
for log in [Globals.testlog1, Globals.testlog2]:
with io.open(log, 'w', encoding=request.param) as logfile:
for entry in [unicode(Globals.testentry1), unicode(Globals.testentry2)]:
for entry in [six.text_type(Globals.testentry1), six.text_type(Globals.testentry2)]:
logfile.write('%s\r\n' % entry)
yield
if platform.system() == 'Windows':
Expand Down
2 changes: 1 addition & 1 deletion checks/sshd_config
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def check_sshd_config(_no_item, params, parsed):

for option, val in parsed.iteritems():
state = 0
infotext = "%s: %s" % (option, convert(val, unicode))
infotext = "%s: %s" % (option, convert(val, six.text_type))

expected = params.get(option)
if expected and expected != val:
Expand Down
3 changes: 2 additions & 1 deletion cmk/gui/crash_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import cStringIO
import sys
import json
import six
import livestatus

import cmk.gui.pages
Expand Down Expand Up @@ -106,7 +107,7 @@ def page_crashed(what):
# Unify different string types from exception messages to a unicode string
exc_value = sys.exc_info()[1]
try:
exc_txt = unicode(exc_value)
exc_txt = six.text_type(exc_value)
except UnicodeDecodeError:
exc_txt = str(exc_value).decode("utf-8")

Expand Down
3 changes: 2 additions & 1 deletion cmk/gui/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from typing import ( # pylint: disable=unused-import
Any, Dict, Optional,
)
import six

import cmk.gui.pages
import cmk.gui.notify as notify
Expand Down Expand Up @@ -792,7 +793,7 @@ def render_dashlet_exception_content(dashlet_instance, nr, e):

# Unify different string types from exception messages to a unicode string
try:
exc_txt = unicode(e)
exc_txt = six.text_type(e)
except UnicodeDecodeError:
exc_txt = str(e).decode("utf-8")

Expand Down
7 changes: 2 additions & 5 deletions cmk/gui/htmllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def escape_attribute(self, value):
return str(value)
elif isinstance(value, HTML):
return "%s" % value # This is HTML code which must not be escaped
elif attr_type not in [str, unicode]: # also possible: type Exception!
elif not isinstance(attr_type, six.string_types): # also possible: type Exception!
value = "%s" % value # Note: this allows Unicode. value might not have type str now
return html_escape(value, quote=True)

Expand Down Expand Up @@ -249,12 +249,9 @@ def __init__(self, value=u''):
super(HTML, self).__init__()
self.value = self._ensure_unicode(value)

def __unicode__(self):
return self.value

def _ensure_unicode(self, thing, encoding_index=0):
try:
return unicode(thing)
return six.text_type(thing)
except UnicodeDecodeError:
return thing.decode("utf-8")

Expand Down
3 changes: 2 additions & 1 deletion cmk/gui/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Dict, NamedTuple, Optional, List, Tuple, Text,
)
from pathlib2 import Path # pylint: disable=unused-import
import six

import cmk.utils.paths

Expand Down Expand Up @@ -57,7 +58,7 @@
def _(message):
# type: (str) -> Text
if not _translation:
return unicode(message)
return six.text_type(message)
return _translation.translation.ugettext(message)


Expand Down
3 changes: 2 additions & 1 deletion cmk/gui/plugins/views/painters.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import abc
import os
import time
import io
import six

import cmk.utils.paths
Expand Down Expand Up @@ -1607,7 +1608,7 @@ def replace_tags(text):
.replace('$SERVICEDESC$', row.get("service_description", ""))

for f in files:
contents.append(replace_tags(unicode(open(f).read(), "utf-8").strip()))
contents.append(replace_tags(io.open(f, encoding="utf8").read().strip()))
return "", "<hr>".join(contents)


Expand Down
6 changes: 3 additions & 3 deletions cmk/gui/valuespec.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
from typing import Type, Union, Callable, Text, Any # pylint: disable=unused-import
from PIL import Image # type: ignore

from Cryptodome.PublicKey import RSA
import six
from Cryptodome.PublicKey import RSA

import cmk.utils.log
import cmk.utils.paths
Expand Down Expand Up @@ -508,8 +508,8 @@ def validate_datatype(self, value, varprefix):

def _validate_value(self, value, varprefix):
try:
unicode(value)
except:
assert isinstance(value, six.text_type)
except AssertionError:
raise MKUserError(varprefix, _("Non-ASCII characters are not allowed here."))
if self._forbidden_chars:
for c in self._forbidden_chars:
Expand Down
3 changes: 2 additions & 1 deletion cmk/gui/view_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import re
import json
import six

from cmk.gui.i18n import _
from cmk.gui.globals import html
Expand Down Expand Up @@ -94,7 +95,7 @@ def get_host_list_links(site, hosts):
args.append(("display_options", html.request.var("display_options")))

url = html.makeuri_contextless(args, filename="view.py")
link = unicode(html.render_a(host, href=url))
link = six.text_type(html.render_a(host, href=url))
entries.append(link)
return entries

Expand Down
5 changes: 3 additions & 2 deletions cmk/gui/wato/pages/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import csv
import time
from difflib import SequenceMatcher
import six

import cmk.utils.store as store

Expand Down Expand Up @@ -235,8 +236,8 @@ def _get_host_info_from_row(self, row):
attributes[attribute] = value.decode("utf-8")
else:
try:
unicode(value)
except:
assert isinstance(value, six.text_type)
except AssertionError:
raise MKUserError(
None,
_("Non-ASCII characters are not allowed in the "
Expand Down
2 changes: 1 addition & 1 deletion cmk/utils/crash_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def create_crash_info(crash_type, details=None, version=None):

# Unify different string types from exception messages to a unicode string
try:
exc_txt = unicode(exc_value)
exc_txt = six.text_type(exc_value)
except UnicodeDecodeError:
exc_txt = str(exc_value).decode("utf-8")

Expand Down
4 changes: 2 additions & 2 deletions cmk_base/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ def prepare_check_command(command_spec, hostname, description):
if arg_type in [int, float]:
formated.append("%s" % arg)

elif arg_type in [str, unicode]:
elif isinstance(arg, six.string_types):
formated.append(cmk.utils.quote_shell_string(arg))

elif arg_type == tuple and len(arg) == 3:
Expand Down Expand Up @@ -1903,7 +1903,7 @@ def _get_checkgroup_parameters(config_cache, host, checktype, item, descr):
# in the following code.
# TODO: This should be strictly validated by the check API in 1.7.
if item is not None and not isinstance(item, six.string_types):
item = unicode(item)
item = six.text_type(item)

# checks with an item need service-specific rules
match_object = config_cache.ruleset_match_object_for_checkgroup_parameters(
Expand Down
2 changes: 1 addition & 1 deletion cmk_base/core_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def get_host_attributes(hostname, config_cache):


def _get_tag_attributes(collection, prefix):
return {u"__%s_%s" % (prefix, k): unicode(v) for k, v in collection.iteritems()}
return {u"__%s_%s" % (prefix, k): six.text_type(v) for k, v in collection.iteritems()}


def get_cluster_attributes(config_cache, host_config, nodes):
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/livestatus/test_livestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import print_function
import collections
import pytest # type: ignore
import six

from testlib import web, create_linux_test_host # pylint: disable=unused-import

Expand Down Expand Up @@ -69,7 +70,7 @@ def test_host_custom_variables(default_cfg, site):
u'ip-v4': u'ip-v4',
u'networking': u'lan',
u'piggyback': u'auto-piggyback',
u'site': unicode(site.id),
u'site': six.text_type(site.id),
u'snmp_ds': u'no-snmp',
u'tcp': u'tcp',
}
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/cmk/gui/test_gui_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import six
import pytest # type: ignore
from pathlib2 import Path
import six

import cmk.utils.paths
import cmk.gui.modules as modules
Expand Down Expand Up @@ -795,7 +796,7 @@ def my_theme(theme_dirs):
my_dir = theme_path / "my_theme"
my_dir.mkdir()
my_dir.joinpath("theme.json").open(mode="w", encoding="utf-8").write(
unicode(json.dumps({"title": "Määh Theme :-)"})))
six.text_type(json.dumps({"title": "Määh Theme :-)"})))
return my_dir


Expand All @@ -813,7 +814,7 @@ def test_theme_choices_local_theme(theme_dirs, my_theme):
my_dir = local_theme_path / "my_improved_theme"
my_dir.mkdir()
my_dir.joinpath("theme.json").open(mode="w", encoding="utf-8").write(
unicode(json.dumps({"title": "Määh Bettr Theme :-D"})))
six.text_type(json.dumps({"title": "Määh Bettr Theme :-D"})))

assert config.theme_choices() == sorted([
("my_theme", u"Määh Theme :-)"),
Expand All @@ -827,7 +828,7 @@ def test_theme_choices_override(theme_dirs, my_theme):
my_dir = local_theme_path / "my_theme"
my_dir.mkdir()
my_dir.joinpath("theme.json").open(mode="w", encoding="utf-8").write(
unicode(json.dumps({"title": "Fixed theme"})))
six.text_type(json.dumps({"title": "Fixed theme"})))

assert config.theme_choices() == sorted([
("my_theme", u"Fixed theme"),
Expand All @@ -836,7 +837,7 @@ def test_theme_choices_override(theme_dirs, my_theme):

def test_theme_broken_meta(my_theme):
my_theme.joinpath("theme.json").open(mode="w", encoding="utf-8").write(
unicode("{\"titlewrong\": xyz\"bla\"}"))
six.text_type("{\"titlewrong\": xyz\"bla\"}"))

assert config.theme_choices() == sorted([
("my_theme", u"my_theme"),
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/cmk/gui/test_htmllib_HTML.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_class_HTML():
a = "Oneüლ,ᔑ•ﺪ͟͠•ᔐ.ლ"
b = "two"
c = "Three"
d = unicode('u')
d = six.text_type('u')

A = HTML(a)
B = HTML(b)
Expand All @@ -30,12 +30,12 @@ def test_class_HTML():
assert HTML(None) == HTML(u"%s" % None)
assert HTML() == HTML('')
# One day we will fix this!
assert unicode(A) == a.decode("utf-8"), unicode(A)
assert six.text_type(A) == a.decode("utf-8"), six.text_type(A)
assert "%s" % A == a.decode("utf-8"), "%s" % A
assert json.loads(json.dumps(A)) == A
assert repr(A) == 'HTML(\"%s\")' % A.value.encode("utf-8")
assert len(B) == len(b)
assert unicode(B) == unicode(b)
assert six.text_type(B) == six.text_type(b)

assert "1" + B + "2" + C == "1" + b + "2" + c

Expand All @@ -44,7 +44,7 @@ def test_class_HTML():
assert HTML().join([a, b]) == a + b
assert HTML("jo").join([A, B]) == A + "jo" + B
assert HTML("jo").join([a, b]) == a + "jo" + b
assert ''.join(map(unicode, [A, B])) == A + B
assert ''.join(map(six.text_type, [A, B])) == A + B

assert isinstance(A, HTML), type(A)
# assert isinstance(A, six.text_type), type(A)
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/cmk/gui/test_htmllib_HTMLGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import print_function
import traceback
import six

from cmk.gui.globals import html
from tools import compare_html
Expand Down Expand Up @@ -46,9 +47,9 @@ def test_ABCHTMLGenerator(register_builtin_html):
html.render_a(u"test", href=u"www.test.case")
try:
assert html.render_a(u"test",
href=unicode("www.test.case"),
id_=unicode("something"),
class_=unicode("test_%s") % a)
href=six.text_type("www.test.case"),
id_=six.text_type("something"),
class_=six.text_type("test_%s") % a)
except Exception as e:
print(traceback.print_exc())
print(e)
Expand Down

0 comments on commit df50b52

Please sign in to comment.