mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-06 22:47:29 -06:00
Escape characters of escape_characters dict at initialisation
Instead of escaping it each time you read a function with that ugly inline for loop, escape the characters when initialising the dict itself. Contributes to issue CURA-34.
This commit is contained in:
parent
a3936540d8
commit
afd63c53c0
2 changed files with 13 additions and 15 deletions
|
@ -24,9 +24,9 @@ class GCodeProfileReader(ProfileReader):
|
|||
# Note that the keys of this dictionary are regex strings. The values are
|
||||
# not.
|
||||
escape_characters = {
|
||||
"\\\\": "\\", #The escape character.
|
||||
"\\n": "\n", #Newlines. They break off the comment.
|
||||
"\\r": "\r" #Carriage return. Windows users may need this for visualisation in their editors.
|
||||
re.escape("\\\\"): "\\", #The escape character.
|
||||
re.escape("\\n"): "\n", #Newlines. They break off the comment.
|
||||
re.escape("\\r"): "\r" #Carriage return. Windows users may need this for visualisation in their editors.
|
||||
}
|
||||
|
||||
## Initialises the g-code reader as a profile reader.
|
||||
|
@ -40,7 +40,7 @@ class GCodeProfileReader(ProfileReader):
|
|||
# specified file was no g-code or contained no parsable profile, \code
|
||||
# None \endcode is returned.
|
||||
def read(self, file_name):
|
||||
prefix = ";SETTING_" + str(version) + " "
|
||||
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?
|
||||
|
@ -55,9 +55,8 @@ class GCodeProfileReader(ProfileReader):
|
|||
return None
|
||||
|
||||
#Unescape the serialised profile.
|
||||
escape_characters = dict((re.escape(key), value) for key, value in escape_characters.items())
|
||||
pattern = re.compile("|".join(escape_characters.keys()))
|
||||
serialised = pattern.sub(lambda m: escape_characters[re.escape(m.group(0))], serialised) #Perform the replacement with a regular expression.
|
||||
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)
|
||||
|
|
|
@ -22,9 +22,9 @@ class GCodeWriter(MeshWriter):
|
|||
# Note that the keys of this dictionary are regex strings. The values are
|
||||
# not.
|
||||
escape_characters = {
|
||||
"\\": "\\\\", #The escape character.
|
||||
"\n": "\\n", #Newlines. They break off the comment.
|
||||
"\r": "\\r" #Carriage return. Windows users may need this for visualisation in their editors.
|
||||
re.escape("\\"): "\\\\", #The escape character.
|
||||
re.escape("\n"): "\\n", #Newlines. They break off the comment.
|
||||
re.escape("\r"): "\\r" #Carriage return. Windows users may need this for visualisation in their editors.
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
|
@ -54,15 +54,14 @@ class GCodeWriter(MeshWriter):
|
|||
# \param profile The profile to serialise.
|
||||
# \return A serialised string of the profile.
|
||||
def _serialiseProfile(self, profile):
|
||||
prefix = ";SETTING_" + str(version) + " " #The prefix to put before each line.
|
||||
prefix = ";SETTING_" + str(GCodeWriter.version) + " " #The prefix to put before each line.
|
||||
prefix_length = len(prefix)
|
||||
|
||||
serialised = profile.serialise()
|
||||
|
||||
#Escape characters that have a special meaning in g-code comments.
|
||||
escape_characters = dict((re.escape(key), value) for key, value in escape_characters.items())
|
||||
pattern = re.compile("|".join(escape_characters.keys()))
|
||||
serialised = pattern.sub(lambda m: escape_characters[re.escape(m.group(0))], serialised) #Perform the replacement with a regular expression.
|
||||
pattern = re.compile("|".join(GCodeWriter.escape_characters.keys()))
|
||||
serialised = pattern.sub(lambda m: GCodeWriter.escape_characters[re.escape(m.group(0))], serialised) #Perform the replacement with a regular expression.
|
||||
|
||||
#Introduce line breaks so that each comment is no longer than 80 characters. Prepend each line with the prefix.
|
||||
result = ""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue