From f1747b51182cbe4d77c991492b55fd2df12b4c18 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 1 Oct 2021 19:30:48 -0400 Subject: [PATCH] klippy: Add Python2 module wrappers and use Python3 module naming Add wrappers for some common Python modules so that the code can run on both Python2 and Python3. Signed-off-by: Kevin O'Connor --- klippy/configfile.py | 8 ++++---- klippy/extras/save_variables.py | 2 +- klippy/extras/statistics.py | 2 +- klippy/queuelogger.py | 2 +- klippy/reactor.py | 2 +- klippy/util.py | 21 +++++++++++++++++++++ 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/klippy/configfile.py b/klippy/configfile.py index 0c4f8ba7..71f5ca60 100644 --- a/klippy/configfile.py +++ b/klippy/configfile.py @@ -3,7 +3,7 @@ # Copyright (C) 2016-2021 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. -import os, glob, re, time, logging, ConfigParser as configparser, StringIO +import os, glob, re, time, logging, configparser, io error = configparser.Error @@ -211,7 +211,7 @@ class PrinterConfig: return data = '\n'.join(buffer) del buffer[:] - sbuffer = StringIO.StringIO(data) + sbuffer = io.StringIO(data) fileconfig.readfp(sbuffer, filename) def _resolve_include(self, source_filename, include_spec, fileconfig, visited): @@ -255,11 +255,11 @@ class PrinterConfig: self._parse_config_buffer(buffer, filename, fileconfig) visited.remove(path) def _build_config_wrapper(self, data, filename): - fileconfig = configparser.RawConfigParser() + fileconfig = configparser.RawConfigParser(strict=False) self._parse_config(data, filename, fileconfig, set()) return ConfigWrapper(self.printer, fileconfig, {}, 'printer') def _build_config_string(self, config): - sfile = StringIO.StringIO() + sfile = io.StringIO() config.fileconfig.write(sfile) return sfile.getvalue().strip() def read_config(self, filename): diff --git a/klippy/extras/save_variables.py b/klippy/extras/save_variables.py index f15dbc11..3765a1ae 100644 --- a/klippy/extras/save_variables.py +++ b/klippy/extras/save_variables.py @@ -4,7 +4,7 @@ # Copyright (C) 2016-2020 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. -import os, logging, ast, ConfigParser as configparser +import os, logging, ast, configparser class SaveVariables: def __init__(self, config): diff --git a/klippy/extras/statistics.py b/klippy/extras/statistics.py index 641396a3..d9c0a367 100644 --- a/klippy/extras/statistics.py +++ b/klippy/extras/statistics.py @@ -23,7 +23,7 @@ class PrinterSysStats: self.mem_file = None def stats(self, eventtime): # Get core usage stats - ptime = time.clock() + ptime = time.process_time() pdiff = ptime - self.last_process_time self.last_process_time = ptime if pdiff > 0.: diff --git a/klippy/queuelogger.py b/klippy/queuelogger.py index 8fa98a6b..c6447f8e 100644 --- a/klippy/queuelogger.py +++ b/klippy/queuelogger.py @@ -3,7 +3,7 @@ # Copyright (C) 2016-2019 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. -import logging, logging.handlers, threading, Queue as queue, time +import logging, logging.handlers, threading, queue, time # Class to forward all messages through a queue to a background thread class QueueHandler(logging.Handler): diff --git a/klippy/reactor.py b/klippy/reactor.py index dd083a07..69eedcbd 100644 --- a/klippy/reactor.py +++ b/klippy/reactor.py @@ -3,7 +3,7 @@ # Copyright (C) 2016-2020 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. -import os, gc, select, math, time, logging, Queue as queue +import os, gc, select, math, time, logging, queue import greenlet import chelper, util diff --git a/klippy/util.py b/klippy/util.py index 0321014e..6d110e47 100644 --- a/klippy/util.py +++ b/klippy/util.py @@ -90,6 +90,27 @@ def dump_mcu_build(): dump_file_stats(build_dir, 'out/klipper.elf') +###################################################################### +# Python2 wrapper hacks +###################################################################### + +def setup_python2_wrappers(): + if sys.version_info.major >= 3: + return + # Add module hacks so that common Python3 module imports work in Python2 + import Queue, io, StringIO, ConfigParser, time + sys.modules["queue"] = Queue + io.StringIO = StringIO.StringIO + time.process_time = time.clock + sys.modules["configparser"] = ConfigParser + OrigRawConfigParser = ConfigParser.RawConfigParser + def RCP(strict=False, *args, **kwargs): + return OrigRawConfigParser(*args, **kwargs) + RCP.SECTCRE = OrigRawConfigParser.SECTCRE + ConfigParser.RawConfigParser = RCP +setup_python2_wrappers() + + ###################################################################### # General system and software information ######################################################################