Move escape characters to be a static class variable

It is static and constant, so it won't need to initialise this dictionary every time it reads.

Contributes to issue CURA-34.
This commit is contained in:
Ghostkeeper 2015-12-16 15:24:26 +01:00
parent 6908f2c011
commit a3936540d8
2 changed files with 22 additions and 12 deletions

View file

@ -18,6 +18,17 @@ class GCodeProfileReader(ProfileReader):
# compatibility, increment this version number!
version = 1
## Dictionary that defines how characters are escaped when embedded in
# g-code.
#
# 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.
}
## Initialises the g-code reader as a profile reader.
def __init__(self):
super().__init__()
@ -44,12 +55,6 @@ class GCodeProfileReader(ProfileReader):
return None
#Unescape the serialised profile.
escape_characters = { #Which special characters (keys) are replaced by what escape character (values).
#Note: The keys are regex strings. Values are not.
"\\\\": "\\", #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.
}
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.