From e2890100960ad5ca927e060a8ef7309ebc7fddd6 Mon Sep 17 00:00:00 2001 From: Marcin Usielski Date: Tue, 23 Jan 2024 13:27:42 +0100 Subject: [PATCH 1/8] Removed usused code - for Pythons <3.7 --- moler/helpers.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/moler/helpers.py b/moler/helpers.py index 3c4aa845d..11fd4331f 100644 --- a/moler/helpers.py +++ b/moler/helpers.py @@ -4,24 +4,19 @@ """ __author__ = 'Grzegorz Latuszek, Michal Ernst, Marcin Usielski' -__copyright__ = 'Copyright (C) 2018-2023, Nokia' +__copyright__ = 'Copyright (C) 2018-2024, Nokia' __email__ = 'grzegorz.latuszek@nokia.com, michal.ernst@nokia.com, marcin.usielski@nokia.com' +from functools import wraps +from types import FunctionType, MethodType +from six import string_types +from math import isclose +import collections.abc as collections import copy import importlib import logging import re import sys -from functools import wraps -from types import FunctionType, MethodType -from six import string_types -if sys.version_info > (3, 5): - from math import isclose - -try: - import collections.abc as collections -except ImportError: - import collections class ClassProperty(property): From d60fd43eb89cf1cda115146a6ef7eda606f6f161 Mon Sep 17 00:00:00 2001 From: Marcin Usielski Date: Tue, 23 Jan 2024 13:40:25 +0100 Subject: [PATCH 2/8] import --- moler/config/loggers.py | 7 +------ moler/device/textualdevice.py | 10 ++-------- moler/helpers.py | 12 ++++++------ moler/observer_thread_wrapper.py | 12 +++--------- moler/runner_single_thread.py | 17 +++++------------ moler/util/devices_SM.py | 10 ++-------- 6 files changed, 19 insertions(+), 49 deletions(-) diff --git a/moler/config/loggers.py b/moler/config/loggers.py index d01fc8224..516c58ef4 100644 --- a/moler/config/loggers.py +++ b/moler/config/loggers.py @@ -4,7 +4,7 @@ """ __author__ = 'Grzegorz Latuszek, Marcin Usielski, Michal Ernst' -__copyright__ = 'Copyright (C) 2018-2023, Nokia' +__copyright__ = 'Copyright (C) 2018-2024, Nokia' __email__ = 'grzegorz.latuszek@nokia.com, marcin.usielski@nokia.com, michal.ernst@nokia.com' import codecs @@ -514,11 +514,6 @@ def configure_moler_main_logger(): configure_moler_threads_logger() logger.info("More logs in: {}".format(_logging_path)) _list_libraries(logger=logger) - if sys.version_info < (3, 7): - logger.warning("****************************************************\n" - "* Support for Python < 3.7 will be removed soon. *\n" - "* Please update your Python to 3.7 at least. *\n" - "****************************************************") global _main_logger _main_logger = logger diff --git a/moler/device/textualdevice.py b/moler/device/textualdevice.py index 427c9026d..e115b3184 100644 --- a/moler/device/textualdevice.py +++ b/moler/device/textualdevice.py @@ -5,7 +5,7 @@ - be the state machine that controls which commands may run in given state """ __author__ = 'Grzegorz Latuszek, Marcin Usielski, Michal Ernst' -__copyright__ = 'Copyright (C) 2018-2023, Nokia' +__copyright__ = 'Copyright (C) 2018-2024, Nokia' __email__ = 'grzegorz.latuszek@nokia.com, marcin.usielski@nokia.com, michal.ernst@nokia.com' import abc @@ -18,7 +18,7 @@ import time import traceback import threading - +import queue from moler.cmd.commandtextualgeneric import CommandTextualGeneric from moler.connection_observer import ConnectionObserver from moler.config.loggers import configure_device_logger @@ -31,12 +31,6 @@ from moler.device.abstract_device import AbstractDevice from moler.config.loggers import change_logging_suffix -try: - import queue -except ImportError: - # noinspection PyUnresolvedReferences - import Queue as queue # For python 2 - # TODO: name, logger/logger_name as param class TextualDevice(AbstractDevice): diff --git a/moler/helpers.py b/moler/helpers.py index 11fd4331f..f6cc865f5 100644 --- a/moler/helpers.py +++ b/moler/helpers.py @@ -7,16 +7,16 @@ __copyright__ = 'Copyright (C) 2018-2024, Nokia' __email__ = 'grzegorz.latuszek@nokia.com, michal.ernst@nokia.com, marcin.usielski@nokia.com' -from functools import wraps -from types import FunctionType, MethodType -from six import string_types -from math import isclose -import collections.abc as collections import copy import importlib import logging import re import sys +import collections.abc +from functools import wraps +from types import FunctionType, MethodType +from six import string_types +from math import isclose class ClassProperty(property): @@ -203,7 +203,7 @@ def create_object_from_name(full_class_name, constructor_params): def update_dict(target_dict, expand_dict): for key, value in expand_dict.items(): if (key in target_dict and isinstance(target_dict[key], dict) and isinstance(expand_dict[key], - collections.Mapping)): + collections.abc.Mapping)): update_dict(target_dict[key], expand_dict[key]) else: target_dict[key] = expand_dict[key] diff --git a/moler/observer_thread_wrapper.py b/moler/observer_thread_wrapper.py index 5b616dbc0..8f3cdd3ca 100644 --- a/moler/observer_thread_wrapper.py +++ b/moler/observer_thread_wrapper.py @@ -3,23 +3,17 @@ """Wrapper for observer registered in ThreadedMolerConnection (old name: ObservableConnection).""" __author__ = 'Marcin Usielski' -__copyright__ = 'Copyright (C) 2020-2023, Nokia' +__copyright__ = 'Copyright (C) 2020-2024, Nokia' __email__ = 'marcin.usielski@nokia.com' import logging import traceback import threading -from threading import Thread - +import queue from moler.util import tracked_thread from moler.config.loggers import TRACE from moler.exceptions import CommandFailure, MolerException - -try: - import queue -except ImportError: - # noinspection PyUnresolvedReferences - import Queue as queue # For python 2 +from threading import Thread class ObserverThreadWrapper(object): diff --git a/moler/runner_single_thread.py b/moler/runner_single_thread.py index a5209b1cf..193305f59 100644 --- a/moler/runner_single_thread.py +++ b/moler/runner_single_thread.py @@ -5,25 +5,18 @@ """ __author__ = 'Marcin Usielski' -__copyright__ = 'Copyright (C) 2021-2023, Nokia' +__copyright__ = 'Copyright (C) 2021-2024, Nokia' __email__ = 'marcin.usielski@nokia.com' -from threading import Lock +import threading +import logging +import time from moler.runner import ConnectionObserverRunner from moler.exceptions import CommandTimeout from moler.exceptions import ConnectionObserverTimeout from moler.util.loghelper import log_into_logger from moler.helpers import copy_list -import threading -import logging -import time - -try: - import queue -except ImportError: - # noinspection PyUnresolvedReferences - import Queue as queue # For python 2 class RunnerSingleThread(ConnectionObserverRunner): @@ -48,7 +41,7 @@ def __init__(self): name="RunnerSingle-{}".format(RunnerSingleThread._th_nr) ) RunnerSingleThread._th_nr += 1 - self._connection_observer_lock = Lock() + self._connection_observer_lock = threading.Lock() self._loop_thread.daemon = True self._loop_thread.start() diff --git a/moler/util/devices_SM.py b/moler/util/devices_SM.py index 57ba29112..579faa761 100644 --- a/moler/util/devices_SM.py +++ b/moler/util/devices_SM.py @@ -4,7 +4,7 @@ """ __author__ = 'Michal Ernst, Marcin Usielski' -__copyright__ = 'Copyright (C) 2019-2023, Nokia' +__copyright__ = 'Copyright (C) 2019-2024, Nokia' __email__ = 'michal.ernst@nokia.com, marcin.usielski@nokia.com' import os @@ -12,7 +12,7 @@ import time import math import threading - +import queue from moler.io.raw.memory import ThreadedFifoBuffer from moler.device import DeviceFactory from moler.device.textualdevice import TextualDevice @@ -21,12 +21,6 @@ from moler.helpers import copy_list from moler.util.moler_test import MolerTest -try: - import queue -except ImportError: - # noinspection PyUnresolvedReferences - import Queue as queue # Python 2 and 3. - def iterate_over_device_states(device, max_time=None, tests_per_device=300, max_no_of_threads=11, rerun=0, timeout_multiply=3.0): From 6ae5f693f933bb6b44f8e5195eadd6e6efdfffb1 Mon Sep 17 00:00:00 2001 From: Marcin Usielski Date: Tue, 23 Jan 2024 13:50:30 +0100 Subject: [PATCH 3/8] sys.version --- moler/config/connections.py | 14 ++++---------- moler/config/runners.py | 7 +++---- moler/events/textualevent.py | 5 +---- moler/helpers.py | 12 +++--------- moler/util/cmds_events_doc.py | 10 ++-------- 5 files changed, 13 insertions(+), 35 deletions(-) diff --git a/moler/config/connections.py b/moler/config/connections.py index 8dd0248b6..c99f8e0ff 100644 --- a/moler/config/connections.py +++ b/moler/config/connections.py @@ -4,8 +4,8 @@ """ __author__ = 'Grzegorz Latuszek, Michal Ernst, Marcin Usielski' -__copyright__ = 'Copyright (C) 2018-2021, Nokia' -__email__ = 'grzegorz.latuszek@nokia.com, michal.ernst@nokia.com, marcin' +__copyright__ = 'Copyright (C) 2018-2024, Nokia' +__email__ = 'grzegorz.latuszek@nokia.com, michal.ernst@nokia.com, marcin.usielski@nokia.com' import sys import platform @@ -54,10 +54,6 @@ def set_defaults(): set_default_variant(io_type="sshshell", variant="threaded") -def _running_python_3_5_or_above(): - return (sys.version_info[0] >= 3) and (sys.version_info[1] >= 5) - - supported_unix_systems = ['Linux', "FreeBSD", "Darwin", "SunOS"] supported_windows_systems = ['Windows'] supported_systems = supported_unix_systems + supported_windows_systems @@ -73,13 +69,11 @@ def _running_on_supported_windows(): def register_builtin_connections(connection_factory, moler_conn_class): _register_builtin_connections(connection_factory, moler_conn_class) # unix & windows connections - if _running_python_3_5_or_above(): - _register_python3_builtin_connections(connection_factory, moler_conn_class) + _register_python3_builtin_connections(connection_factory, moler_conn_class) if _running_on_supported_unix(): _register_builtin_unix_connections(connection_factory, moler_conn_class) # unix-only connections - if _running_python_3_5_or_above(): - _register_builtin_py3_unix_connections(connection_factory, moler_conn_class) + _register_builtin_py3_unix_connections(connection_factory, moler_conn_class) elif _running_on_supported_windows(): pass # placeholder for windows-only connections else: diff --git a/moler/config/runners.py b/moler/config/runners.py index a8951b296..eaaad833e 100644 --- a/moler/config/runners.py +++ b/moler/config/runners.py @@ -4,8 +4,8 @@ """ __author__ = 'Grzegorz Latuszek' -__copyright__ = 'Copyright (C) 2018, Nokia' -__email__ = 'grzegorz.latuszek@nokia.com' +__copyright__ = 'Copyright (C) 2018-2024, Nokia' +__email__ = 'grzegorz.latuszek@nokia.com, marcin.usielski@nokia.com' import sys @@ -26,8 +26,7 @@ def clear(): def register_builtin_runners(runner_factory): _register_builtin_runners(runner_factory) - if (sys.version_info[0] >= 3) and (sys.version_info[1] >= 5): - _register_python3_builtin_runners(runner_factory) + _register_python3_builtin_runners(runner_factory) def _register_builtin_runners(runner_factory): diff --git a/moler/events/textualevent.py b/moler/events/textualevent.py index cb50e63ee..1a0ed53a0 100644 --- a/moler/events/textualevent.py +++ b/moler/events/textualevent.py @@ -1,12 +1,11 @@ # -*- coding: utf-8 -*- __author__ = 'Marcin Usielski, Michal Ernst' -__copyright__ = 'Copyright (C) 2018-2021, Nokia' +__copyright__ = 'Copyright (C) 2018-2024, Nokia' __email__ = 'marcin.usielski@nokia.com, michal.ernst@nokia.com' import abc import six -import sys import logging from moler.event import Event from moler.cmd import RegexHelper @@ -51,8 +50,6 @@ def data_received(self, data, recv_time): if not self._paused: self._last_recv_time_data_read_from_connection = recv_time try: - # Workaround for some terminals and python 2.7 - data = u"".join(str(data.encode("utf-8", errors="ignore"))) if sys.version_info < (3, 0) else data lines = data.splitlines(True) for current_chunk in lines: if not self.done(): diff --git a/moler/helpers.py b/moler/helpers.py index f6cc865f5..22ff4083b 100644 --- a/moler/helpers.py +++ b/moler/helpers.py @@ -270,15 +270,9 @@ def diff_data(first_object, second_object, significant_digits=None, abs_tol = 0.0001 if significant_digits: abs_tol = 1.0 / 10 ** significant_digits - if sys.version_info > (3, 5): - if not isclose(first_object, second_object, abs_tol=abs_tol): - return "{} the first value {} is different from the second value" \ - " {}.".format(msg, first_object, second_object) - else: - # Remove when no support for Python 2.7 - if abs(first_object - second_object) > abs_tol: - return "{} the first value {} is different from the second value" \ - " {}.".format(msg, first_object, second_object) + if not isclose(first_object, second_object, abs_tol=abs_tol): + return "{} the first value {} is different from the second value" \ + " {}.".format(msg, first_object, second_object) else: if first_object != second_object: return "{} First value {} is different from the second {}.".format( diff --git a/moler/util/cmds_events_doc.py b/moler/util/cmds_events_doc.py index 95a3701f2..08ba99b05 100755 --- a/moler/util/cmds_events_doc.py +++ b/moler/util/cmds_events_doc.py @@ -6,19 +6,18 @@ from __future__ import print_function __author__ = 'Grzegorz Latuszek', 'Michal Ernst', 'Michal Plichta, Marcin Usielski' -__copyright__ = 'Copyright (C) 2018-2023, Nokia' +__copyright__ = 'Copyright (C) 2018-2024, Nokia' __email__ = 'grzegorz.latuszek@nokia.com', 'michal.ernst@nokia.com', 'michal.plichta@nokia.com,' \ ' marcin.usielski@nokia.com' import collections -import sys +import time from argparse import ArgumentParser from datetime import datetime from importlib import import_module from os import walk, sep from os.path import abspath, join, relpath, exists, split from pprint import pformat -import time from moler.command import Command from moler.event import Event @@ -226,11 +225,6 @@ def _run_command_parsing_test(moler_cmd, creation_str, buffer_io, cmd_output, cm buffer_io.remote_inject_response([cmd_output]) result = moler_cmd() - if sys.version_info < (3, 0): - # workaround for python2.7 - convert str to unicode - cmd_result = _convert_str_to_unicode(cmd_result) - result = _convert_str_to_unicode(result) - try: diff = compare_objects(cmd_result, result, significant_digits=6, exclude_types=exclude_types) except TypeError: From 0ba445d9c7833dec09e451c5b19a4c4c37e67f44 Mon Sep 17 00:00:00 2001 From: Marcin Usielski Date: Tue, 23 Jan 2024 13:52:14 +0100 Subject: [PATCH 4/8] style --- moler/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moler/helpers.py b/moler/helpers.py index 22ff4083b..98ffeaac8 100644 --- a/moler/helpers.py +++ b/moler/helpers.py @@ -272,7 +272,7 @@ def diff_data(first_object, second_object, significant_digits=None, abs_tol = 1.0 / 10 ** significant_digits if not isclose(first_object, second_object, abs_tol=abs_tol): return "{} the first value {} is different from the second value" \ - " {}.".format(msg, first_object, second_object) + " {}.".format(msg, first_object, second_object) else: if first_object != second_object: return "{} First value {} is different from the second {}.".format( From 492c49e09266f99544041285ecdef546b3bbb2d8 Mon Sep 17 00:00:00 2001 From: Marcin Usielski Date: Tue, 23 Jan 2024 14:16:55 +0100 Subject: [PATCH 5/8] cmds_events_doc.py --- moler/util/cmds_events_doc.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/moler/util/cmds_events_doc.py b/moler/util/cmds_events_doc.py index 08ba99b05..2042670cd 100755 --- a/moler/util/cmds_events_doc.py +++ b/moler/util/cmds_events_doc.py @@ -199,18 +199,6 @@ def _reformat_str_to_unicode(cmd_result): return cmd_result -def _convert_str_to_unicode(input): - if isinstance(input, dict): - return {_convert_str_to_unicode(key): _convert_str_to_unicode(value) for key, value in input.items()} - elif isinstance(input, list): - return [_convert_str_to_unicode(element) for element in input] - elif isinstance(input, str): - # noinspection PyUnresolvedReferences - return input.decode('utf-8') # Python 2.7 - else: - return input - - def _run_command_parsing_test(moler_cmd, creation_str, buffer_io, cmd_output, cmd_result, variant, base_class, observer_type): with buffer_io: # open it (autoclose by context-mngr) From 0cf2e6373cc5bd2527e44f438ed7186fe8d3b3ee Mon Sep 17 00:00:00 2001 From: Marcin Usielski Date: Tue, 23 Jan 2024 15:10:02 +0100 Subject: [PATCH 6/8] ConnectionObserver - __init__ type hint --- moler/connection_observer.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/moler/connection_observer.py b/moler/connection_observer.py index 90d92f0a7..7c5bdf7fc 100644 --- a/moler/connection_observer.py +++ b/moler/connection_observer.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- __author__ = 'Grzegorz Latuszek, Marcin Usielski, Michal Ernst' -__copyright__ = 'Copyright (C) 2018-2023 Nokia' +__copyright__ = 'Copyright (C) 2018-2024 Nokia' __email__ = 'grzegorz.latuszek@nokia.com, marcin.usielski@nokia.com, michal.ernst@nokia.com' import logging @@ -29,6 +29,8 @@ from moler.runner_factory import get_runner from moler.command_scheduler import CommandScheduler from moler.util.connection_observer_life_status import ConnectionObserverLifeStatus +from moler.abstract_moler_connection import AbstractMolerConnection +from moler.runner import ConnectionObserverRunner @add_metaclass(ABCMeta) @@ -36,11 +38,13 @@ class ConnectionObserver(object): _not_raised_exceptions = list() # list of dict: "exception" and "time" _exceptions_lock = threading.Lock() - def __init__(self, connection=None, runner=None): + def __init__(self, connection:AbstractMolerConnection=None, runner:ConnectionObserverRunner=None): """ Create instance of ConnectionObserver class :param connection: connection used to receive data awaited for """ + print(type(connection)) + print(type(runner)) super(ConnectionObserver, self).__init__() self.life_status = ConnectionObserverLifeStatus() self.connection = connection @@ -324,11 +328,8 @@ def _set_exception_without_done(self, exception): :param exception: exception to set :return: None """ - if sys.version_info >= (3, 6): - mg = traceback.format_list(traceback.extract_stack()[:-3] + traceback.extract_tb(exception.__traceback__)) - stack_msg = ''.join(mg) + '\n {} {}'.format(exception.__class__, exception) - else: - stack_msg = traceback.format_exc() + mg = traceback.format_list(traceback.extract_stack()[:-3] + traceback.extract_tb(exception.__traceback__)) + stack_msg = ''.join(mg) + '\n {} {}'.format(exception.__class__, exception) if self._is_done: self._log(logging.WARNING, From 23250c709ad6444def89e1f3541edb04fdc5aba9 Mon Sep 17 00:00:00 2001 From: Marcin Usielski Date: Tue, 23 Jan 2024 15:11:55 +0100 Subject: [PATCH 7/8] -print --- moler/connection_observer.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/moler/connection_observer.py b/moler/connection_observer.py index 7c5bdf7fc..cdd84476b 100644 --- a/moler/connection_observer.py +++ b/moler/connection_observer.py @@ -43,8 +43,6 @@ def __init__(self, connection:AbstractMolerConnection=None, runner:ConnectionObs Create instance of ConnectionObserver class :param connection: connection used to receive data awaited for """ - print(type(connection)) - print(type(runner)) super(ConnectionObserver, self).__init__() self.life_status = ConnectionObserverLifeStatus() self.connection = connection From 54dd3abff04e0b19dc5ae4582cb7282a774a574c Mon Sep 17 00:00:00 2001 From: Marcin Usielski Date: Tue, 23 Jan 2024 15:31:45 +0100 Subject: [PATCH 8/8] style --- moler/connection_observer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moler/connection_observer.py b/moler/connection_observer.py index cdd84476b..820528ede 100644 --- a/moler/connection_observer.py +++ b/moler/connection_observer.py @@ -38,7 +38,7 @@ class ConnectionObserver(object): _not_raised_exceptions = list() # list of dict: "exception" and "time" _exceptions_lock = threading.Lock() - def __init__(self, connection:AbstractMolerConnection=None, runner:ConnectionObserverRunner=None): + def __init__(self, connection: AbstractMolerConnection = None, runner: ConnectionObserverRunner = None): """ Create instance of ConnectionObserver class :param connection: connection used to receive data awaited for