Browse Source

Set encoding correctly when opening files everywhere

Otherwise the encoding is interpreted differently on Windows and Mac.
Ghostkeeper 6 years ago
parent
commit
54a03723ab

+ 1 - 1
plugins/ChangeLogPlugin/ChangeLog.py

@@ -55,7 +55,7 @@ class ChangeLog(Extension, QObject,):
 
     def loadChangeLogs(self):
         self._change_logs = collections.OrderedDict()
-        with open(os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), "ChangeLog.txt"), "r",-1, "utf-8") as f:
+        with open(os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), "ChangeLog.txt"), "r", encoding = "utf-8") as f:
             open_version = None
             open_header = "" # Initialise to an empty header in case there is no "*" in the first line of the changelog
             for line in f:

+ 1 - 1
plugins/GCodeProfileReader/GCodeProfileReader.py

@@ -57,7 +57,7 @@ class GCodeProfileReader(ProfileReader):
         # TODO: Consider moving settings to the start?
         serialized = ""  # Will be filled with the serialized profile.
         try:
-            with open(file_name, "r") as f:
+            with open(file_name, "r", encoding = "utf-8") as f:
                 for line in f:
                     if line.startswith(prefix):
                         # Remove the prefix and the newline from the line and add it to the rest.

+ 1 - 1
plugins/LegacyProfileReader/LegacyProfileReader.py

@@ -100,7 +100,7 @@ class LegacyProfileReader(ProfileReader):
             return None
 
         try:
-            with open(os.path.join(PluginRegistry.getInstance().getPluginPath("LegacyProfileReader"), "DictionaryOfDoom.json"), "r", -1, "utf-8") as f:
+            with open(os.path.join(PluginRegistry.getInstance().getPluginPath("LegacyProfileReader"), "DictionaryOfDoom.json"), "r", encoding = "utf-8") as f:
                 dict_of_doom = json.load(f)  # Parse the Dictionary of Doom.
         except IOError as e:
             Logger.log("e", "Could not open DictionaryOfDoom.json for reading: %s", str(e))

+ 1 - 1
plugins/USBPrinting/avr_isp/intelHex.py

@@ -13,7 +13,7 @@ def readHex(filename):
     """
     data = []
     extra_addr = 0
-    f = io.open(filename, "r")
+    f = io.open(filename, "r", encoding = "utf-8")
     for line in f:
         line = line.strip()
         if len(line) < 1:

+ 3 - 3
plugins/VersionUpgrade/VersionUpgrade22to24/VersionUpgrade.py

@@ -94,7 +94,7 @@ class VersionUpgrade22to24(VersionUpgrade):
         if variant_path.endswith("_variant.inst.cfg"):
             variant_path = variant_path[:-len("_variant.inst.cfg")] + "_settings.inst.cfg"
 
-        with open(os.path.join(machine_instances_dir, os.path.basename(variant_path)), "w") as fp:
+        with open(os.path.join(machine_instances_dir, os.path.basename(variant_path)), "w", encoding = "utf-8") as fp:
             variant_config.write(fp)
 
         return config_name
@@ -105,9 +105,9 @@ class VersionUpgrade22to24(VersionUpgrade):
 
         result = []
         for entry in os.scandir(variants_dir):
-            if entry.name.endswith('.inst.cfg') and entry.is_file():
+            if entry.name.endswith(".inst.cfg") and entry.is_file():
                 config = configparser.ConfigParser(interpolation = None)
-                with open(entry.path, "r") as fhandle:
+                with open(entry.path, "r", encoding = "utf-8") as fhandle:
                     config.read_file(fhandle)
                 if config.has_section("general") and config.has_option("general", "name"):
                     result.append( { "path": entry.path, "name": config.get("general", "name") } )

+ 3 - 3
plugins/VersionUpgrade/VersionUpgrade25to26/VersionUpgrade25to26.py

@@ -249,11 +249,11 @@ class VersionUpgrade25to26(VersionUpgrade):
         definition_changes_dir = Resources.getPath(CuraApplication.ResourceTypes.DefinitionChangesContainer)
         user_settings_dir = Resources.getPath(CuraApplication.ResourceTypes.UserInstanceContainer)
 
-        with open(os.path.join(definition_changes_dir, definition_changes_filename), "w") as f:
+        with open(os.path.join(definition_changes_dir, definition_changes_filename), "w", encoding = "utf-8") as f:
             f.write(definition_changes_output.getvalue())
-        with open(os.path.join(user_settings_dir, user_settings_filename), "w") as f:
+        with open(os.path.join(user_settings_dir, user_settings_filename), "w", encoding = "utf-8") as f:
             f.write(user_settings_output.getvalue())
-        with open(os.path.join(extruder_stack_dir, extruder_filename), "w") as f:
+        with open(os.path.join(extruder_stack_dir, extruder_filename), "w", encoding = "utf-8") as f:
             f.write(extruder_output.getvalue())
 
     ##  Creates a definition changes container which doesn't contain anything for the Custom FDM Printers.

+ 1 - 1
plugins/XmlMaterialProfile/XmlMaterialProfile.py

@@ -1018,7 +1018,7 @@ class XmlMaterialProfile(InstanceContainer):
     @classmethod
     def getProductIdMap(cls) -> Dict[str, List[str]]:
         product_to_id_file = os.path.join(os.path.dirname(sys.modules[cls.__module__].__file__), "product_to_id.json")
-        with open(product_to_id_file) as f:
+        with open(product_to_id_file, encoding = "utf-8") as f:
             product_to_id_map = json.load(f)
         product_to_id_map = {key: [value] for key, value in product_to_id_map.items()}
         return product_to_id_map

+ 2 - 2
tests/TestProfileRequirements.py

@@ -19,7 +19,7 @@ import pytest
 def test_ultimaker3extended_variants(um3_file, um3e_file):
     directory = os.path.join(os.path.dirname(__file__), "..", "resources", "variants") #TODO: Hardcoded path relative to this test file.
     um3 = configparser.ConfigParser()
-    um3.read_file(open(os.path.join(directory, um3_file)))
+    um3.read_file(open(os.path.join(directory, um3_file), encoding = "utf-8"))
     um3e = configparser.ConfigParser()
-    um3e.read_file(open(os.path.join(directory, um3e_file)))
+    um3e.read_file(open(os.path.join(directory, um3e_file), encoding = "utf-8"))
     assert um3["values"] == um3e["values"]