From a48241d45eae0c898f8399398dda1756d828763b Mon Sep 17 00:00:00 2001 From: MirceaDan Date: Fri, 15 Jul 2016 01:43:31 -0700 Subject: [PATCH] python3: iterators, bytes and next iteritems got removed items is to be used for python 2 and 3 compatibility , it returns a view in python 3 and must be casted to a list if the list is needed. bytes form builtins using builtin function next is the compatible way between python 2 and 3 --- src/octoprint/server/__init__.py | 3 ++- src/octoprint/server/api/printer.py | 4 ++-- src/octoprint/server/util/tornado.py | 8 ++++---- src/octoprint/settings.py | 3 ++- src/octoprint/users.py | 3 ++- src/octoprint/util/jinja.py | 2 +- 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index a653f409..2d175263 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -17,6 +17,7 @@ from babel import Locale from watchdog.observers import Observer from watchdog.observers.polling import PollingObserver from collections import defaultdict +from builtins import bytes import os import logging @@ -69,7 +70,7 @@ from octoprint.server.util.flask import PreemptiveCache from . import util -UI_API_KEY = ''.join('%02X' % ord(z) for z in uuid.uuid4().bytes) +UI_API_KEY = ''.join('%02X' % z for z in bytes(uuid.uuid4().bytes)) VERSION = __version__ BRANCH = __branch__ diff --git a/src/octoprint/server/api/printer.py b/src/octoprint/server/api/printer.py index 50e74fcc..ccf7a031 100644 --- a/src/octoprint/server/api/printer.py +++ b/src/octoprint/server/api/printer.py @@ -91,7 +91,7 @@ def printerToolCommand(): # make sure the targets are valid and the values are numbers validated_values = {} - for tool, value in targets.iteritems(): + for tool, value in targets.items(): if re.match(validation_regex, tool) is None: return make_response("Invalid target for setting temperature: %s" % tool, 400) if not isinstance(value, (int, long, float)): @@ -108,7 +108,7 @@ def printerToolCommand(): # make sure the targets are valid, the values are numbers and in the range [-50, 50] validated_values = {} - for tool, value in offsets.iteritems(): + for tool, value in offsets.items(): if re.match(validation_regex, tool) is None: return make_response("Invalid target for setting temperature: %s" % tool, 400) if not isinstance(value, (int, long, float)): diff --git a/src/octoprint/server/util/tornado.py b/src/octoprint/server/util/tornado.py index 3ef205dd..d099ff1c 100644 --- a/src/octoprint/server/util/tornado.py +++ b/src/octoprint/server/util/tornado.py @@ -137,7 +137,7 @@ class UploadStorageFallbackHandler(tornado.web.RequestHandler): self._path = path self._suffixes = dict((key, key) for key in ("name", "path", "content_type", "size")) - for suffix_type, suffix in suffixes.iteritems(): + for suffix_type, suffix in suffixes.items(): if suffix_type in self._suffixes and suffix is not None: self._suffixes[suffix_type] = suffix @@ -357,7 +357,7 @@ class UploadStorageFallbackHandler(tornado.web.RequestHandler): """ self._new_body = b"" - for name, part in self._parts.iteritems(): + for name, part in self._parts.items(): if "filename" in part: # add form fields for filename, path, size and content_type for all files contained in the request if not "path" in part: @@ -371,8 +371,8 @@ class UploadStorageFallbackHandler(tornado.web.RequestHandler): if "content_type" in part: parameters["content_type"] = part["content_type"] - fields = dict((self._suffixes[key], value) for (key, value) in parameters.iteritems()) - for n, p in fields.iteritems(): + fields = dict((self._suffixes[key], value) for (key, value) in parameters.items()) + for n, p in fields.items(): key = name + "." + n self._new_body += b"--%s\r\n" % self._multipart_boundary self._new_body += b"Content-Disposition: form-data; name=\"%s\"\r\n" % key diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index fed93526..2137943b 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -30,6 +30,7 @@ import logging import re import uuid import copy +from builtins import bytes try: from collections import ChainMap @@ -516,7 +517,7 @@ class Settings(object): self.load(migrate=True) if self.get(["api", "key"]) is None: - self.set(["api", "key"], ''.join('%02X' % ord(z) for z in uuid.uuid4().bytes)) + self.set(["api", "key"], ''.join('%02X' % z for z in bytes(uuid.uuid4().bytes))) self.save(force=True) self._script_env = self._init_script_templating() diff --git a/src/octoprint/users.py b/src/octoprint/users.py index 065aaff2..889baaef 100644 --- a/src/octoprint/users.py +++ b/src/octoprint/users.py @@ -14,6 +14,7 @@ import yaml import uuid import logging +from builtins import bytes from octoprint.settings import settings @@ -346,7 +347,7 @@ class FilebasedUserManager(UserManager): raise UnknownUser(username) user = self._users[username] - user._apikey = ''.join('%02X' % ord(z) for z in uuid.uuid4().bytes) + user._apikey = ''.join('%02X' % z for z in bytes(uuid.uuid4().bytes)) self._dirty = True self._save() return user._apikey diff --git a/src/octoprint/util/jinja.py b/src/octoprint/util/jinja.py index 34954b88..220ff036 100644 --- a/src/octoprint/util/jinja.py +++ b/src/octoprint/util/jinja.py @@ -95,7 +95,7 @@ class ExceptionHandlerExtension(Extension): self._logger = logging.getLogger(__name__) def parse(self, parser): - token = parser.stream.next() + token = next(parser.stream) lineno = token.lineno filename = parser.name error = parser.parse_expression()