GCodeProfileReader.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.Application import Application #To get the machine manager to create the new profile in.
  4. from UM.Settings.Profile import Profile
  5. from UM.Settings.ProfileReader import ProfileReader
  6. from UM.Logger import Logger
  7. import re #Regular expressions for parsing escape characters in the settings.
  8. ## A class that reads profile data from g-code files.
  9. #
  10. # It reads the profile data from g-code files and stores it in a new profile.
  11. # This class currently does not process the rest of the g-code in any way.
  12. class GCodeProfileReader(ProfileReader):
  13. ## The file format version of the serialised g-code.
  14. #
  15. # It can only read settings with the same version as the version it was
  16. # written with. If the file format is changed in a way that breaks reverse
  17. # compatibility, increment this version number!
  18. version = 1
  19. ## Dictionary that defines how characters are escaped when embedded in
  20. # g-code.
  21. #
  22. # Note that the keys of this dictionary are regex strings. The values are
  23. # not.
  24. escape_characters = {
  25. re.escape("\\\\"): "\\", #The escape character.
  26. re.escape("\\n"): "\n", #Newlines. They break off the comment.
  27. re.escape("\\r"): "\r" #Carriage return. Windows users may need this for visualisation in their editors.
  28. }
  29. ## Initialises the g-code reader as a profile reader.
  30. def __init__(self):
  31. super().__init__()
  32. ## Reads a g-code file, loading the profile from it.
  33. #
  34. # \param file_name The name of the file to read the profile from.
  35. # \return The profile that was in the specified file, if any. If the
  36. # specified file was no g-code or contained no parsable profile, \code
  37. # None \endcode is returned.
  38. def read(self, file_name):
  39. if file_name.split(".")[-1] != "gcode":
  40. return None
  41. prefix = ";SETTING_" + str(GCodeProfileReader.version) + " "
  42. prefix_length = len(prefix)
  43. #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?
  44. serialised = "" #Will be filled with the serialised profile.
  45. try:
  46. with open(file_name) as f:
  47. for line in f:
  48. if line.startswith(prefix):
  49. serialised += line[prefix_length : -1] #Remove the prefix and the newline from the line, and add it to the rest.
  50. except IOError as e:
  51. Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e))
  52. return None
  53. #Unescape the serialised profile.
  54. pattern = re.compile("|".join(GCodeProfileReader.escape_characters.keys()))
  55. serialised = pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], serialised) #Perform the replacement with a regular expression.
  56. #Apply the changes to the current profile.
  57. profile = Profile(machine_manager = Application.getInstance().getMachineManager(), read_only = False)
  58. try:
  59. profile.unserialise(serialised)
  60. profile.setType(None) #Force type to none so it's correctly added.
  61. profile.setReadOnly(False)
  62. profile.setDirty(True)
  63. except Exception as e: #Not a valid g-comachine_instance_profilede file.
  64. Logger.log("e", "Unable to serialise the profile: %s", str(e))
  65. return None
  66. return profile