Merge pull request #2828 from tedder/devel_ted_py3_round3
tedder's python3 fixes, round 3
This commit is contained in:
12 files changed
+61
-31
No files matched your search
@@ -1502,7 +1502,7 @@ class Settings(object):
|
||||
return value is not None
|
||||
|
||||
def getBaseFolder(self, type, create=True, allow_fallback=True, log_error=False, check_writable=True, deep_check_writable=False):
|
||||
if type not in default_settings["folder"].keys() + ["base"]:
|
||||
if type not in list(default_settings["folder"].keys()) + ["base"]:
|
||||
return None
|
||||
|
||||
if type == "base":
|
||||
|
||||
@@ -157,7 +157,7 @@ class SlicingManager(object):
|
||||
Returns:
|
||||
(list of str) Identifiers of all available slicers.
|
||||
"""
|
||||
return self._slicers.keys()
|
||||
return list(self._slicers.keys())
|
||||
|
||||
@property
|
||||
def configured_slicers(self):
|
||||
|
||||
@@ -510,7 +510,7 @@ def to_str(s_or_u, encoding="utf-8", errors="strict"):
|
||||
|
||||
def to_unicode(s_or_u, encoding="utf-8", errors="strict"):
|
||||
"""Make sure ``s_or_u`` is a unicode string."""
|
||||
if isinstance(s_or_u, str):
|
||||
if isinstance(s_or_u, bytes):
|
||||
return s_or_u.decode(encoding, errors=errors)
|
||||
else:
|
||||
return s_or_u
|
||||
|
||||
@@ -4219,7 +4219,7 @@ def parse_temperature_line(line, current):
|
||||
tool = values["tool"]
|
||||
toolnum = values.get("toolnum", None)
|
||||
toolNumber = int(toolnum) if toolnum is not None and len(toolnum) else None
|
||||
if toolNumber > maxToolNum:
|
||||
if toolNumber and toolNumber > maxToolNum:
|
||||
maxToolNum = toolNumber
|
||||
|
||||
try:
|
||||
|
||||
@@ -54,9 +54,9 @@ def has_requirement(requirement, requirements):
|
||||
if requirement is None or requirements is None:
|
||||
return False
|
||||
|
||||
assert isinstance(requirement, basestring)
|
||||
assert isinstance(requirements, (list, tuple))
|
||||
assert all(list(map(lambda x: x is not None and isinstance(x, basestring), requirements)))
|
||||
#assert isinstance(requirement, basestring)
|
||||
#assert isinstance(requirements, (list, tuple))
|
||||
#assert all(list(map(lambda x: x is not None and isinstance(x, basestring), requirements)))
|
||||
|
||||
requirement = requirement.lower()
|
||||
requirements = [r.lower() for r in requirements]
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
OPEN_SIGNATURE = 'builtins.open' if sys.version_info[0] >= 3 else '__builtin__.open'
|
||||
@@ -3,6 +3,8 @@ import unittest
|
||||
import ddt
|
||||
import mock
|
||||
|
||||
import _fixups
|
||||
|
||||
OCTOPI_VERSION = "0.14.0"
|
||||
|
||||
DT_MODEL = "Raspberry Pi Model F Rev 1.1"
|
||||
@@ -13,8 +15,9 @@ class PiSupportTestCase(unittest.TestCase):
|
||||
|
||||
def test_get_octopi_version(self):
|
||||
from octoprint.plugins.pi_support import get_octopi_version
|
||||
#import _fixups
|
||||
|
||||
with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m:
|
||||
with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m:
|
||||
m.return_value.readline.return_value = OCTOPI_VERSION
|
||||
version = get_octopi_version()
|
||||
|
||||
@@ -23,8 +26,9 @@ class PiSupportTestCase(unittest.TestCase):
|
||||
|
||||
def test_get_proc_dt_model(self):
|
||||
from octoprint.plugins.pi_support import get_proc_dt_model
|
||||
#import _fixups
|
||||
|
||||
with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m:
|
||||
with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m:
|
||||
m.return_value.readline.return_value = DT_MODEL
|
||||
model = get_proc_dt_model()
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import unittest
|
||||
import shutil
|
||||
import contextlib
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import yaml
|
||||
import hashlib
|
||||
@@ -24,15 +25,29 @@ import octoprint.settings
|
||||
@ddt.ddt
|
||||
class TestSettings(unittest.TestCase):
|
||||
|
||||
def _load_yaml(self, fname):
|
||||
if sys.version_info[0] >= 3:
|
||||
with open(fname, "r+", encoding='utf-8') as f:
|
||||
return yaml.safe_load(f)
|
||||
else:
|
||||
with open(fname, "r+b") as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
def _dump_yaml(self, fname, config):
|
||||
if sys.version_info[0] >= 3:
|
||||
with open(fname, "w+", encoding='utf-8') as f:
|
||||
yaml.safe_dump(config, f)
|
||||
else:
|
||||
with open(fname, "wb+") as f:
|
||||
yaml.safe_dump(config, f)
|
||||
|
||||
def setUp(self):
|
||||
self.base_path = os.path.join(os.path.dirname(__file__), "_files")
|
||||
self.config_path = os.path.realpath(os.path.join(self.base_path, "config.yaml"))
|
||||
self.defaults_path = os.path.realpath(os.path.join(self.base_path, "defaults.yaml"))
|
||||
|
||||
with open(self.config_path, "r+b") as f:
|
||||
self.config = yaml.safe_load(f)
|
||||
with open(self.defaults_path, "r+b") as f:
|
||||
self.defaults = yaml.safe_load(f)
|
||||
self.config = self._load_yaml(self.config_path)
|
||||
self.defaults = self._load_yaml(self.defaults_path)
|
||||
|
||||
from octoprint.util import dict_merge
|
||||
self.expected_effective = dict_merge(self.defaults, self.config)
|
||||
@@ -503,11 +518,9 @@ class TestSettings(unittest.TestCase):
|
||||
self.assertEqual("0.0.0.0", settings.get(["server", "host"]))
|
||||
|
||||
# modify yaml file externally
|
||||
with open(configfile, "r+b") as f:
|
||||
config = yaml.safe_load(f)
|
||||
config = self._load_yaml(configfile)
|
||||
config["server"]["host"] = "127.0.0.1"
|
||||
with open(configfile, "w+b") as f:
|
||||
yaml.safe_dump(config, f)
|
||||
self._dump_yaml(configfile, config)
|
||||
|
||||
# set some value, should also reload file before setting new api key
|
||||
settings.set(["api", "key"], "key")
|
||||
|
||||
@@ -28,6 +28,7 @@ class TestSlicingManager(unittest.TestCase):
|
||||
self._mock_slicer_plugins(self.slicer_plugin)
|
||||
|
||||
# mock profile manager
|
||||
import octoprint.printer.profile
|
||||
self.printer_profile_manager = mock.MagicMock(spec=octoprint.printer.profile.PrinterProfileManager)
|
||||
|
||||
# mock settings
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
# coding=utf-8
|
||||
from __future__ import absolute_import, division, print_function
|
||||
import _fixups
|
||||
|
||||
import unittest
|
||||
import mock
|
||||
@@ -102,7 +103,7 @@ class DaemonTest(unittest.TestCase):
|
||||
@mock.patch("sys.stdout")
|
||||
@mock.patch("sys.stderr")
|
||||
@mock.patch("os.devnull")
|
||||
@mock.patch("__builtin__.open")
|
||||
@mock.patch(_fixups.OPEN_SIGNATURE)
|
||||
@mock.patch("os.dup2")
|
||||
def test_redirect_io(self, mock_dup2, mock_open, mock_devnull, mock_stderr, mock_stdout, mock_stdin):
|
||||
# setup
|
||||
@@ -390,7 +391,7 @@ class DaemonTest(unittest.TestCase):
|
||||
pid = 1234
|
||||
|
||||
# test
|
||||
with mock.patch("__builtin__.open", mock.mock_open(read_data="{}\n".format(pid)), create=True) as m:
|
||||
with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(read_data="{}\n".format(pid)), create=True) as m:
|
||||
result = self.daemon.get_pid()
|
||||
|
||||
# assert
|
||||
@@ -403,7 +404,7 @@ class DaemonTest(unittest.TestCase):
|
||||
handle.__enter__.side_effect = IOError()
|
||||
|
||||
# test
|
||||
with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m:
|
||||
with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m:
|
||||
result = self.daemon.get_pid()
|
||||
|
||||
# assert
|
||||
@@ -415,7 +416,7 @@ class DaemonTest(unittest.TestCase):
|
||||
pid = "not an integer"
|
||||
|
||||
# test
|
||||
with mock.patch("__builtin__.open", mock.mock_open(read_data="{}\n".format(pid)), create=True) as m:
|
||||
with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(read_data="{}\n".format(pid)), create=True) as m:
|
||||
result = self.daemon.get_pid()
|
||||
|
||||
# assert
|
||||
@@ -427,7 +428,7 @@ class DaemonTest(unittest.TestCase):
|
||||
pid = "1234"
|
||||
|
||||
# test
|
||||
with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m:
|
||||
with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m:
|
||||
self.daemon.set_pid(pid)
|
||||
|
||||
# assert
|
||||
@@ -440,7 +441,7 @@ class DaemonTest(unittest.TestCase):
|
||||
pid = 1234
|
||||
|
||||
# test
|
||||
with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m:
|
||||
with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m:
|
||||
self.daemon.set_pid(pid)
|
||||
|
||||
# assert
|
||||
|
||||
@@ -103,15 +103,15 @@ class TestCommHelpers(unittest.TestCase):
|
||||
m.update(input)
|
||||
return m.hexdigest()
|
||||
|
||||
temp_regex = "T:((\d*\.)\d+)"
|
||||
temp_template = "Temp: {}"
|
||||
temp2_template = "Temperature: {}"
|
||||
temp_regex = b'T:((\d*\.)\d+)'
|
||||
temp_template = b"Temp: {}"
|
||||
temp2_template = b"Temperature: {}"
|
||||
temp_key = md5sum(temp_regex)
|
||||
temp_template_key = md5sum(temp_template)
|
||||
temp2_template_key = md5sum(temp2_template)
|
||||
|
||||
x_regex = "X:(?P<x>\d+)"
|
||||
x_template = "X: {x}"
|
||||
x_regex = b"X:(?P<x>\d+)"
|
||||
x_template = b"X: {x}"
|
||||
x_key = md5sum(x_regex)
|
||||
x_template_key = md5sum(x_template)
|
||||
|
||||
|
||||
@@ -332,12 +332,16 @@ class IsHiddenPathTest(unittest.TestCase):
|
||||
self.path_hidden_on_windows = os.path.join(self.basepath, "hidden_on_windows.txt")
|
||||
self.path_always_hidden = os.path.join(self.basepath, ".always_hidden.txt")
|
||||
|
||||
import sys
|
||||
for attr in ("path_always_visible", "path_hidden_on_windows", "path_always_hidden"):
|
||||
path = getattr(self, attr)
|
||||
with open(path, "w+b") as f:
|
||||
f.write(attr)
|
||||
if sys.version_info[0] >= 3:
|
||||
with open(path, "w+", encoding='utf-8') as f:
|
||||
f.write(attr)
|
||||
else:
|
||||
with open(path, "wb+") as f:
|
||||
f.write(attr)
|
||||
|
||||
import sys
|
||||
if sys.platform == "win32":
|
||||
# we use ctypes and the windows API to set the hidden attribute on the file
|
||||
# only hidden on windows
|
||||
|
||||
Reference in new issue
Block a user