From 24efa2b9aa61ab0c1e3b1ce9d42e46e8f5f1d18c Mon Sep 17 00:00:00 2001 From: Charles Short Date: Fri, 9 Apr 2021 09:29:30 -0400 Subject: [PATCH] python3: Fix compatibility issues - use absolute path imports to compat python3 - Replace dict.keys() with list(dict.keys()) to get a list on Python 3. On Python 3, dict.keys() now returns a view. - Fix iteritems to use items for python3. Testing: 1. Built packaging with new python3 packaging. 2. Built resulting ISO with new python-fmclient package. 3. Ran "fm" under python3. Verifified that no tracebacks happened because it could not find common_wrappers.py. 4. Ran fm alarm-list to verify that output was displayed. 5. Ran fm event-list to display the events. 6. Ran fm alarm-show to show an active alarm. Story: 2006796 Task: 42257 Signed-off-by: Charles Short Change-Id: I5081d0685c7ad200076a76b7709cbe4f39c7b456 (cherry picked from commit 5d2dfab5f99fa58683350f068c9eef2b2ada3a67) --- python-fmclient/fmclient/fmclient/common/base.py | 4 ++-- .../fmclient/fmclient/common/options.py | 2 +- python-fmclient/fmclient/fmclient/common/utils.py | 5 +++-- .../fmclient/common/wrapping_formatters.py | 15 ++++++++------- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/python-fmclient/fmclient/fmclient/common/base.py b/python-fmclient/fmclient/fmclient/common/base.py index 0d7f2bd7..90cdee5e 100644 --- a/python-fmclient/fmclient/fmclient/common/base.py +++ b/python-fmclient/fmclient/fmclient/common/base.py @@ -102,7 +102,7 @@ class Resource(object): self._loaded = loaded def _add_details(self, info): - for (k, v) in info.iteritems(): + for (k, v) in info.items(): setattr(self, k, v) def __getattr__(self, k): @@ -117,7 +117,7 @@ class Resource(object): return self.__dict__[k] def __repr__(self): - reprkeys = sorted(k for k in self.__dict__.keys() if k[0] != '_' and + reprkeys = sorted(k for k in list(self.__dict__.keys()) if k[0] != '_' and k != 'manager') info = ", ".join("%s=%s" % (k, getattr(self, k)) for k in reprkeys) return "<%s %s>" % (self.__class__.__name__, info) diff --git a/python-fmclient/fmclient/fmclient/common/options.py b/python-fmclient/fmclient/fmclient/common/options.py index 0407cefa..fad7ee87 100644 --- a/python-fmclient/fmclient/fmclient/common/options.py +++ b/python-fmclient/fmclient/fmclient/common/options.py @@ -28,7 +28,7 @@ OP_LOOKUP = {'!=': 'ne', '<': 'lt', '=': 'eq'} -OP_LOOKUP_KEYS = '|'.join(sorted(OP_LOOKUP.keys(), key=len, reverse=True)) +OP_LOOKUP_KEYS = '|'.join(sorted(list(OP_LOOKUP.keys()), key=len, reverse=True)) OP_SPLIT_RE = re.compile(r'(%s)' % OP_LOOKUP_KEYS) DATA_TYPE_RE = re.compile(r'^(string|integer|float|datetime|boolean)(::)(.+)$') diff --git a/python-fmclient/fmclient/fmclient/common/utils.py b/python-fmclient/fmclient/fmclient/common/utils.py index 2521f144..892374ce 100644 --- a/python-fmclient/fmclient/fmclient/common/utils.py +++ b/python-fmclient/fmclient/fmclient/common/utils.py @@ -39,8 +39,9 @@ from dateutil import parser from prettytable import ALL from prettytable import FRAME from prettytable import NONE +from six.moves import zip -import wrapping_formatters +from fmclient.common import wrapping_formatters SENSITIVE_HEADERS = ('X-Auth-Token', ) @@ -531,7 +532,7 @@ def print_dict(d, dict_property="Property", wrap=0): pt = prettytable.PrettyTable([dict_property, 'Value'], caching=False, print_empty=False) pt.align = 'l' - for k, v in sorted(d.iteritems()): + for k, v in sorted(d.items()): v = parse_date(v) # convert dict to str to check length if isinstance(v, dict): diff --git a/python-fmclient/fmclient/fmclient/common/wrapping_formatters.py b/python-fmclient/fmclient/fmclient/common/wrapping_formatters.py index 5f1cfabb..82f4732a 100644 --- a/python-fmclient/fmclient/fmclient/common/wrapping_formatters.py +++ b/python-fmclient/fmclient/fmclient/common/wrapping_formatters.py @@ -25,9 +25,10 @@ import re import six import textwrap -from cli_no_wrap import is_nowrap_set -from cli_no_wrap import set_no_wrap +from fmclient.common.cli_no_wrap import is_nowrap_set +from fmclient.common.cli_no_wrap import set_no_wrap from prettytable import _get_size +from six.moves import range UUID_MIN_LENGTH = 36 @@ -51,7 +52,7 @@ def get_width(value): def _get_terminal_width(): - from utils import get_terminal_size + from fmclient.common.utils import get_terminal_size result = get_terminal_size()[0] return result @@ -669,7 +670,7 @@ def build_wrapping_formatters(objs, fields, field_labels, format_spec, add_blank else: format_spec = build_best_guess_formatters_using_average_widths(objs, fields, field_labels) - for k in format_spec.keys(): + for k in list(format_spec.keys()): if k not in fields: raise Exception("Error in buildWrappingFormatters: format_spec " "specifies a field {} that is not specified " @@ -717,7 +718,7 @@ def set_no_wrap_on_formatters(no_wrap, formatters): global_orig_no_wrap = is_nowrap_set() set_no_wrap(no_wrap) - for k, f in formatters.iteritems(): + for k, f in formatters.items(): if WrapperFormatter.is_wrapper_formatter(f): formatter_no_wrap_settings[k] = (f.wrapper_formatter.no_wrap, f.wrapper_formatter) f.wrapper_formatter.no_wrap = no_wrap @@ -740,7 +741,7 @@ def unset_no_wrap_on_formatters(orig_no_wrap_settings): formatters = {} - for k, v in formatter_no_wrap_settings.iteritems(): + for k, v in formatter_no_wrap_settings.items(): formatters[k] = v[1] formatters[k].no_wrap = v[0] @@ -751,7 +752,7 @@ def unset_no_wrap_on_formatters(orig_no_wrap_settings): def _simpleTestHarness(no_wrap): - import utils + from fmclient.common import utils def testFormatter(event): return "*{}".format(event["state"])