GCodeProfileReader.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.
  44. # They are all at the end, but Python has no reverse seek any more since Python3.
  45. # TODO: Consider moving settings to the start?
  46. serialised = "" # Will be filled with the serialised profile.
  47. try:
  48. with open(file_name) as f:
  49. for line in f:
  50. if line.startswith(prefix):
  51. # Remove the prefix and the newline from the line and add it to the rest.
  52. serialised += line[prefix_length : -1]
  53. except IOError as e:
  54. Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e))
  55. return None
  56. # Un-escape the serialised profile.
  57. pattern = re.compile("|".join(GCodeProfileReader.escape_characters.keys()))
  58. # Perform the replacement with a regular expression.
  59. serialised = pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], serialised)
  60. # Apply the changes to the current profile.
  61. profile = Profile(machine_manager = Application.getInstance().getMachineManager(), read_only = False)
  62. try:
  63. profile.unserialise(serialised)
  64. profile.setType(None) # Force type to none so it's correctly added.
  65. profile.setReadOnly(False)
  66. profile.setDirty(True)
  67. except Exception as e: # Not a valid g-code file.
  68. Logger.log("e", "Unable to serialise the profile: %s", str(e))
  69. return None
  70. return profile