GCodeProfileReader.py 3.5 KB

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