mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-07 06:57:28 -06:00
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:
parent
6908f2c011
commit
a3936540d8
2 changed files with 22 additions and 12 deletions
|
@ -18,6 +18,17 @@ class GCodeProfileReader(ProfileReader):
|
||||||
# compatibility, increment this version number!
|
# compatibility, increment this version number!
|
||||||
version = 1
|
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.
|
## Initialises the g-code reader as a profile reader.
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
@ -44,12 +55,6 @@ class GCodeProfileReader(ProfileReader):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
#Unescape the serialised profile.
|
#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())
|
escape_characters = dict((re.escape(key), value) for key, value in escape_characters.items())
|
||||||
pattern = re.compile("|".join(escape_characters.keys()))
|
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.
|
serialised = pattern.sub(lambda m: escape_characters[re.escape(m.group(0))], serialised) #Perform the replacement with a regular expression.
|
||||||
|
|
|
@ -16,6 +16,17 @@ class GCodeWriter(MeshWriter):
|
||||||
# compatibility, increment this version number!
|
# compatibility, increment this version number!
|
||||||
version = 1
|
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.
|
||||||
|
}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
@ -49,12 +60,6 @@ class GCodeWriter(MeshWriter):
|
||||||
serialised = profile.serialise()
|
serialised = profile.serialise()
|
||||||
|
|
||||||
#Escape characters that have a special meaning in g-code comments.
|
#Escape characters that have a special meaning in g-code comments.
|
||||||
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())
|
escape_characters = dict((re.escape(key), value) for key, value in escape_characters.items())
|
||||||
pattern = re.compile("|".join(escape_characters.keys()))
|
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.
|
serialised = pattern.sub(lambda m: escape_characters[re.escape(m.group(0))], serialised) #Perform the replacement with a regular expression.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue