123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import re
- import json
- from UM.Settings.InstanceContainer import InstanceContainer
- from UM.Logger import Logger
- from UM.i18n import i18nCatalog
- catalog = i18nCatalog("cura")
- from cura.ProfileReader import ProfileReader
- class GCodeProfileReader(ProfileReader):
-
-
-
-
-
- version = 3
-
-
-
-
-
- escape_characters = {
- re.escape("\\\\"): "\\",
- re.escape("\\n"): "\n",
- re.escape("\\r"): "\r"
- }
-
- def __init__(self):
- super().__init__()
-
-
-
-
-
-
- def read(self, file_name):
- if file_name.split(".")[-1] != "gcode":
- return None
- prefix = ";SETTING_" + str(GCodeProfileReader.version) + " "
- prefix_length = len(prefix)
-
-
-
- serialized = ""
- try:
- with open(file_name) as f:
- for line in f:
- if line.startswith(prefix):
-
- serialized += line[prefix_length : -1]
- except IOError as e:
- Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e))
- return None
- serialized = unescapeGcodeComment(serialized)
- Logger.log("i", "Serialized the following from %s: %s" %(file_name, repr(serialized)))
- json_data = json.loads(serialized)
- profile_strings = [json_data["global_quality"]]
- profile_strings.extend(json_data.get("extruder_quality", []))
- return [readQualityProfileFromString(profile_string) for profile_string in profile_strings]
- def unescapeGcodeComment(string):
-
- pattern = re.compile("|".join(GCodeProfileReader.escape_characters.keys()))
-
- return pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], string)
- def readQualityProfileFromString(profile_string):
-
- profile = InstanceContainer("")
- try:
- profile.deserialize(profile_string)
- except Exception as e:
- Logger.log("e", "Unable to serialise the profile: %s", str(e))
- return None
- return profile
|