GCodeProfileReader.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 UM.i18n import i18nCatalog
  9. catalog = i18nCatalog("cura")
  10. from cura.ProfileReader import ProfileReader
  11. ## A class that reads profile data from g-code files.
  12. #
  13. # It reads the profile data from g-code files and stores it in a new profile.
  14. # This class currently does not process the rest of the g-code in any way.
  15. class GCodeProfileReader(ProfileReader):
  16. ## The file format version of the serialized g-code.
  17. #
  18. # It can only read settings with the same version as the version it was
  19. # written with. If the file format is changed in a way that breaks reverse
  20. # compatibility, increment this version number!
  21. version = 1
  22. ## Dictionary that defines how characters are escaped when embedded in
  23. # g-code.
  24. #
  25. # Note that the keys of this dictionary are regex strings. The values are
  26. # not.
  27. escape_characters = {
  28. re.escape("\\\\"): "\\", #The escape character.
  29. re.escape("\\n"): "\n", #Newlines. They break off the comment.
  30. re.escape("\\r"): "\r" #Carriage return. Windows users may need this for visualisation in their editors.
  31. }
  32. ## Initialises the g-code reader as a profile reader.
  33. def __init__(self):
  34. super().__init__()
  35. ## Reads a g-code file, loading the profile from it.
  36. #
  37. # \param file_name The name of the file to read the profile from.
  38. # \return The profile that was in the specified file, if any. If the
  39. # specified file was no g-code or contained no parsable profile, \code
  40. # None \endcode is returned.
  41. def read(self, file_name):
  42. if file_name.split(".")[-1] != "gcode":
  43. return None
  44. prefix = ";SETTING_" + str(GCodeProfileReader.version) + " "
  45. prefix_length = len(prefix)
  46. # Loading all settings from the file.
  47. # They are all at the end, but Python has no reverse seek any more since Python3.
  48. # TODO: Consider moving settings to the start?
  49. serialized = "" # Will be filled with the serialized profile.
  50. try:
  51. with open(file_name) as f:
  52. for line in f:
  53. if line.startswith(prefix):
  54. # Remove the prefix and the newline from the line and add it to the rest.
  55. serialized += line[prefix_length : -1]
  56. except IOError as e:
  57. Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e))
  58. return None
  59. # Un-escape the serialized profile.
  60. pattern = re.compile("|".join(GCodeProfileReader.escape_characters.keys()))
  61. # Perform the replacement with a regular expression.
  62. serialized = pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], serialized)
  63. Logger.log("i", "Serialized the following from %s: %s" %(file_name, repr(serialized)))
  64. # Create an empty profile - the id will be changed later
  65. profile = InstanceContainer("")
  66. profile.addMetaDataEntry("type", "quality")
  67. try:
  68. profile.deserialize(serialized)
  69. except Exception as e: # Not a valid g-code file.
  70. Logger.log("e", "Unable to serialise the profile: %s", str(e))
  71. return None
  72. #Creating a unique name using the filename of the GCode
  73. new_name = catalog.i18nc("@label", "Custom profile (%s)") %(os.path.splitext(os.path.basename(file_name))[0])
  74. profile.setName(new_name)
  75. profile._id = new_name
  76. return profile