def read(self, file_name): if file_name.split(".")[-1] != "gcode": return None prefix = ";SETTING_" + str(GCodeProfileReader.version) + " " prefix_length = len(prefix) #Loading all settings from the file. They are all at the end, but Python has no reverse seek any more since Python3. TODO: Consider moving settings to the start? serialised = "" #Will be filled with the serialised profile. try: with open(file_name) as f: for line in f: if line.startswith(prefix): serialised += line[prefix_length : -1] #Remove the prefix and the newline from the line, and add it to the rest. except IOError as e: Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e)) return None #Unescape the serialised profile. pattern = re.compile("|".join(GCodeProfileReader.escape_characters.keys())) serialised = pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], serialised) #Perform the replacement with a regular expression. #Apply the changes to the current profile. profile = Profile(machine_manager = Application.getInstance().getMachineManager(), read_only = False) try: profile.unserialise(serialised) profile.setType(None) #Force type to none so it's correctly added. profile.setReadOnly(False) profile.setDirty(True) except Exception as e: #Not a valid g-code file. Logger.log("e", "Unable to serialise the profile: %s", str(e)) return None return profile
def read(self, file_name): profile = Profile(machine_manager = Application.getInstance().getMachineManager(), read_only = False) #Create an empty profile. serialised = "" try: with open(file_name) as f: #Open file for reading. serialised = f.read() except IOError as e: Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e)) return None try: profile.unserialise(serialised) except Exception as e: #Parsing error. This is not a (valid) Cura profile then. return None return profile
def read(self, file_name): if file_name.split(".")[-1] != "gcode": return None prefix = ";SETTING_" + str(GCodeProfileReader.version) + " " prefix_length = len(prefix) # Loading all settings from the file. # They are all at the end, but Python has no reverse seek any more since Python3. # TODO: Consider moving settings to the start? serialised = "" # Will be filled with the serialised profile. try: with open(file_name) 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. serialised += 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 # Un-escape the serialised profile. pattern = re.compile("|".join(GCodeProfileReader.escape_characters.keys())) # Perform the replacement with a regular expression. serialised = pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], serialised) # Apply the changes to the current profile. profile = Profile(machine_manager = Application.getInstance().getMachineManager(), read_only = False) try: profile.unserialise(serialised) profile.setType(None) # Force type to none so it's correctly added. profile.setReadOnly(False) profile.setDirty(True) except Exception as e: # Not a valid g-code file. Logger.log("e", "Unable to serialise the profile: %s", str(e)) return None return profile