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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2021-10-01 19:30:48 -04:00
parent b8c91914b7
commit f1747b5118
6 changed files with 29 additions and 8 deletions

View File

@ -3,7 +3,7 @@
# Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net>
#
# 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):

View File

@ -4,7 +4,7 @@
# Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net>
#
# 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):

View File

@ -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.:

View File

@ -3,7 +3,7 @@
# Copyright (C) 2016-2019 Kevin O'Connor <kevin@koconnor.net>
#
# 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):

View File

@ -3,7 +3,7 @@
# Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net>
#
# 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

View File

@ -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
######################################################################