mirror of
				https://github.com/Klipper3d/klipper.git
				synced 2025-10-31 10:25:57 +01:00 
			
		
		
		
	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:
		| @@ -3,7 +3,7 @@ | |||||||
| # Copyright (C) 2016-2021  Kevin O'Connor <kevin@koconnor.net> | # Copyright (C) 2016-2021  Kevin O'Connor <kevin@koconnor.net> | ||||||
| # | # | ||||||
| # This file may be distributed under the terms of the GNU GPLv3 license. | # 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 | error = configparser.Error | ||||||
|  |  | ||||||
| @@ -211,7 +211,7 @@ class PrinterConfig: | |||||||
|             return |             return | ||||||
|         data = '\n'.join(buffer) |         data = '\n'.join(buffer) | ||||||
|         del buffer[:] |         del buffer[:] | ||||||
|         sbuffer = StringIO.StringIO(data) |         sbuffer = io.StringIO(data) | ||||||
|         fileconfig.readfp(sbuffer, filename) |         fileconfig.readfp(sbuffer, filename) | ||||||
|     def _resolve_include(self, source_filename, include_spec, fileconfig, |     def _resolve_include(self, source_filename, include_spec, fileconfig, | ||||||
|                          visited): |                          visited): | ||||||
| @@ -255,11 +255,11 @@ class PrinterConfig: | |||||||
|         self._parse_config_buffer(buffer, filename, fileconfig) |         self._parse_config_buffer(buffer, filename, fileconfig) | ||||||
|         visited.remove(path) |         visited.remove(path) | ||||||
|     def _build_config_wrapper(self, data, filename): |     def _build_config_wrapper(self, data, filename): | ||||||
|         fileconfig = configparser.RawConfigParser() |         fileconfig = configparser.RawConfigParser(strict=False) | ||||||
|         self._parse_config(data, filename, fileconfig, set()) |         self._parse_config(data, filename, fileconfig, set()) | ||||||
|         return ConfigWrapper(self.printer, fileconfig, {}, 'printer') |         return ConfigWrapper(self.printer, fileconfig, {}, 'printer') | ||||||
|     def _build_config_string(self, config): |     def _build_config_string(self, config): | ||||||
|         sfile = StringIO.StringIO() |         sfile = io.StringIO() | ||||||
|         config.fileconfig.write(sfile) |         config.fileconfig.write(sfile) | ||||||
|         return sfile.getvalue().strip() |         return sfile.getvalue().strip() | ||||||
|     def read_config(self, filename): |     def read_config(self, filename): | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ | |||||||
| # Copyright (C) 2016-2020  Kevin O'Connor <kevin@koconnor.net> | # Copyright (C) 2016-2020  Kevin O'Connor <kevin@koconnor.net> | ||||||
| # | # | ||||||
| # This file may be distributed under the terms of the GNU GPLv3 license. | # 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: | class SaveVariables: | ||||||
|     def __init__(self, config): |     def __init__(self, config): | ||||||
|   | |||||||
| @@ -23,7 +23,7 @@ class PrinterSysStats: | |||||||
|             self.mem_file = None |             self.mem_file = None | ||||||
|     def stats(self, eventtime): |     def stats(self, eventtime): | ||||||
|         # Get core usage stats |         # Get core usage stats | ||||||
|         ptime = time.clock() |         ptime = time.process_time() | ||||||
|         pdiff = ptime - self.last_process_time |         pdiff = ptime - self.last_process_time | ||||||
|         self.last_process_time = ptime |         self.last_process_time = ptime | ||||||
|         if pdiff > 0.: |         if pdiff > 0.: | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ | |||||||
| # Copyright (C) 2016-2019  Kevin O'Connor <kevin@koconnor.net> | # Copyright (C) 2016-2019  Kevin O'Connor <kevin@koconnor.net> | ||||||
| # | # | ||||||
| # This file may be distributed under the terms of the GNU GPLv3 license. | # 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 to forward all messages through a queue to a background thread | ||||||
| class QueueHandler(logging.Handler): | class QueueHandler(logging.Handler): | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ | |||||||
| # Copyright (C) 2016-2020  Kevin O'Connor <kevin@koconnor.net> | # Copyright (C) 2016-2020  Kevin O'Connor <kevin@koconnor.net> | ||||||
| # | # | ||||||
| # This file may be distributed under the terms of the GNU GPLv3 license. | # 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 greenlet | ||||||
| import chelper, util | import chelper, util | ||||||
|  |  | ||||||
|   | |||||||
| @@ -90,6 +90,27 @@ def dump_mcu_build(): | |||||||
|     dump_file_stats(build_dir, 'out/klipper.elf') |     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 | # General system and software information | ||||||
| ###################################################################### | ###################################################################### | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user