From 7dfc1a4aa717c00819bf0f11e714c1f93116b352 Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Mon, 29 Jan 2018 12:29:20 +0100 Subject: [PATCH 1/2] Add encoding='utf-8' for text file reading CURA-4875 When encoding is not provided, the behaviour is system dependent and it can break on OS X. --- cura/CrashHandler.py | 6 +++--- cura/Settings/ContainerManager.py | 2 +- cura/Settings/CuraContainerRegistry.py | 2 +- cura_app.py | 4 ++-- plugins/CuraProfileReader/CuraProfileReader.py | 2 +- plugins/GCodeReader/GCodeReader.py | 2 +- .../VersionUpgrade/VersionUpgrade22to24/VersionUpgrade.py | 2 +- .../VersionUpgrade30to31/VersionUpgrade30to31.py | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cura/CrashHandler.py b/cura/CrashHandler.py index b5d4001fe2..0c6740f740 100644 --- a/cura/CrashHandler.py +++ b/cura/CrashHandler.py @@ -189,7 +189,7 @@ class CrashHandler: json_metadata_file = os.path.join(directory, "plugin.json") try: - with open(json_metadata_file, "r") as f: + with open(json_metadata_file, "r", encoding = "utf-8") as f: try: metadata = json.loads(f.read()) module_version = metadata["version"] @@ -217,9 +217,9 @@ class CrashHandler: text_area = QTextEdit() tmp_file_fd, tmp_file_path = tempfile.mkstemp(prefix = "cura-crash", text = True) os.close(tmp_file_fd) - with open(tmp_file_path, "w") as f: + with open(tmp_file_path, "w", encoding = "utf-8") as f: faulthandler.dump_traceback(f, all_threads=True) - with open(tmp_file_path, "r") as f: + with open(tmp_file_path, "r", encoding = "utf-8") as f: logdata = f.read() text_area.setText(logdata) diff --git a/cura/Settings/ContainerManager.py b/cura/Settings/ContainerManager.py index 21fc3f43c0..007b0917a0 100644 --- a/cura/Settings/ContainerManager.py +++ b/cura/Settings/ContainerManager.py @@ -486,7 +486,7 @@ class ContainerManager(QObject): container = container_type(container_id) try: - with open(file_url, "rt") as f: + with open(file_url, "rt", encoding = "utf-8") as f: container.deserialize(f.read()) except PermissionError: return { "status": "error", "message": "Permission denied when trying to read the file"} diff --git a/cura/Settings/CuraContainerRegistry.py b/cura/Settings/CuraContainerRegistry.py index 41b2c492f0..589948216d 100644 --- a/cura/Settings/CuraContainerRegistry.py +++ b/cura/Settings/CuraContainerRegistry.py @@ -693,7 +693,7 @@ class CuraContainerRegistry(ContainerRegistry): continue instance_container = InstanceContainer(container_id) - with open(file_path, "r") as f: + with open(file_path, "r", encoding = "utf-8") as f: serialized = f.read() instance_container.deserialize(serialized, file_path) self.addContainer(instance_container) diff --git a/cura_app.py b/cura_app.py index 6d1ff6ab6b..b9afb9bbcc 100755 --- a/cura_app.py +++ b/cura_app.py @@ -30,8 +30,8 @@ if not known_args["debug"]: if hasattr(sys, "frozen"): dirpath = get_cura_dir_path() os.makedirs(dirpath, exist_ok = True) - sys.stdout = open(os.path.join(dirpath, "stdout.log"), "w") - sys.stderr = open(os.path.join(dirpath, "stderr.log"), "w") + sys.stdout = open(os.path.join(dirpath, "stdout.log"), "w", encoding = "utf-8") + sys.stderr = open(os.path.join(dirpath, "stderr.log"), "w", encoding = "utf-8") import platform import faulthandler diff --git a/plugins/CuraProfileReader/CuraProfileReader.py b/plugins/CuraProfileReader/CuraProfileReader.py index 5631d138aa..3a0f0ee1aa 100644 --- a/plugins/CuraProfileReader/CuraProfileReader.py +++ b/plugins/CuraProfileReader/CuraProfileReader.py @@ -39,7 +39,7 @@ class CuraProfileReader(ProfileReader): except zipfile.BadZipFile: # It must be an older profile from Cura 2.1. - with open(file_name, encoding="utf-8") as fhandle: + with open(file_name, encoding = "utf-8") as fhandle: serialized = fhandle.read() return [self._loadProfile(serialized, profile_id) for serialized, profile_id in self._upgradeProfile(serialized, file_name)] diff --git a/plugins/GCodeReader/GCodeReader.py b/plugins/GCodeReader/GCodeReader.py index cb2f5d99e0..050f243f9b 100755 --- a/plugins/GCodeReader/GCodeReader.py +++ b/plugins/GCodeReader/GCodeReader.py @@ -26,7 +26,7 @@ class GCodeReader(MeshReader): # PreRead is used to get the correct flavor. If not, Marlin is set by default def preRead(self, file_name, *args, **kwargs): - with open(file_name, "r") as file: + with open(file_name, "r", encoding = "utf-8") as file: for line in file: if line[:len(self._flavor_keyword)] == self._flavor_keyword: try: diff --git a/plugins/VersionUpgrade/VersionUpgrade22to24/VersionUpgrade.py b/plugins/VersionUpgrade/VersionUpgrade22to24/VersionUpgrade.py index 19f0563f10..7505911049 100644 --- a/plugins/VersionUpgrade/VersionUpgrade22to24/VersionUpgrade.py +++ b/plugins/VersionUpgrade/VersionUpgrade22to24/VersionUpgrade.py @@ -74,7 +74,7 @@ class VersionUpgrade22to24(VersionUpgrade): def __convertVariant(self, variant_path): # Copy the variant to the machine_instances/*_settings.inst.cfg variant_config = configparser.ConfigParser(interpolation=None) - with open(variant_path, "r") as fhandle: + with open(variant_path, "r", encoding = "utf-8") as fhandle: variant_config.read_file(fhandle) config_name = "Unknown Variant" diff --git a/plugins/VersionUpgrade/VersionUpgrade30to31/VersionUpgrade30to31.py b/plugins/VersionUpgrade/VersionUpgrade30to31/VersionUpgrade30to31.py index 8c5a160ff4..517b5659b2 100644 --- a/plugins/VersionUpgrade/VersionUpgrade30to31/VersionUpgrade30to31.py +++ b/plugins/VersionUpgrade/VersionUpgrade30to31/VersionUpgrade30to31.py @@ -231,5 +231,5 @@ class VersionUpgrade30to31(VersionUpgrade): quality_changes_dir = Resources.getPath(CuraApplication.ResourceTypes.QualityInstanceContainer) - with open(os.path.join(quality_changes_dir, extruder_quality_changes_filename), "w") as f: + with open(os.path.join(quality_changes_dir, extruder_quality_changes_filename), "w", encoding = "utf-8") as f: f.write(extruder_quality_changes_output.getvalue()) From b9fc3997e570bdf46882198f1ac137a5b372ee33 Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Mon, 29 Jan 2018 13:00:45 +0100 Subject: [PATCH 2/2] Fix jellybox -> imade3d_jellybox renaming CURA-4863 --- .../VersionUpgrade30to31.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/plugins/VersionUpgrade/VersionUpgrade30to31/VersionUpgrade30to31.py b/plugins/VersionUpgrade/VersionUpgrade30to31/VersionUpgrade30to31.py index 517b5659b2..c1f32b424f 100644 --- a/plugins/VersionUpgrade/VersionUpgrade30to31/VersionUpgrade30to31.py +++ b/plugins/VersionUpgrade/VersionUpgrade30to31/VersionUpgrade30to31.py @@ -59,6 +59,12 @@ _EMPTY_CONTAINER_DICT = { } +# Renamed definition files +_RENAMED_DEFINITION_DICT = { + "jellybox": "imade3d_jellybox", +} + + class VersionUpgrade30to31(VersionUpgrade): ## Gets the version number from a CFG file in Uranium's 3.0 format. # @@ -122,6 +128,10 @@ class VersionUpgrade30to31(VersionUpgrade): if len(all_quality_changes) <= 1 and not parser.has_option("metadata", "extruder"): self._createExtruderQualityChangesForSingleExtrusionMachine(filename, parser) + # Check renamed definitions + if "definition" in parser["general"] and parser["general"]["definition"] in _RENAMED_DEFINITION_DICT: + parser["general"]["definition"] = _RENAMED_DEFINITION_DICT[parser["general"]["definition"]] + # Update version numbers parser["general"]["version"] = "2" parser["metadata"]["setting_version"] = "4" @@ -156,6 +166,10 @@ class VersionUpgrade30to31(VersionUpgrade): if parser.has_option("containers", key) and parser["containers"][key] == "empty": parser["containers"][key] = specific_empty_container + # check renamed definition + if parser.has_option("containers", "6") and parser["containers"]["6"] in _RENAMED_DEFINITION_DICT: + parser["containers"]["6"] = _RENAMED_DEFINITION_DICT[parser["containers"]["6"]] + # Update version numbers if "general" not in parser: parser["general"] = {} @@ -219,6 +233,10 @@ class VersionUpgrade30to31(VersionUpgrade): extruder_quality_changes_parser["general"]["name"] = global_quality_changes["general"]["name"] extruder_quality_changes_parser["general"]["definition"] = global_quality_changes["general"]["definition"] + # check renamed definition + if extruder_quality_changes_parser["general"]["definition"] in _RENAMED_DEFINITION_DICT: + extruder_quality_changes_parser["general"]["definition"] = _RENAMED_DEFINITION_DICT[extruder_quality_changes_parser["general"]["definition"]] + extruder_quality_changes_parser.add_section("metadata") extruder_quality_changes_parser["metadata"]["quality_type"] = global_quality_changes["metadata"]["quality_type"] extruder_quality_changes_parser["metadata"]["type"] = global_quality_changes["metadata"]["type"]