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:
Ghostkeeper 2015-12-16 15:59:56 +01:00
parent a3936540d8
commit afd63c53c0
2 changed files with 13 additions and 15 deletions

View file

@ -24,9 +24,9 @@ class GCodeProfileReader(ProfileReader):
# Note that the keys of this dictionary are regex strings. The values are # Note that the keys of this dictionary are regex strings. The values are
# not. # not.
escape_characters = { escape_characters = {
"\\\\": "\\", #The escape character. re.escape("\\\\"): "\\", #The escape character.
"\\n": "\n", #Newlines. They break off the comment. re.escape("\\n"): "\n", #Newlines. They break off the comment.
"\\r": "\r" #Carriage return. Windows users may need this for visualisation in their editors. 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. ## 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 # specified file was no g-code or contained no parsable profile, \code
# None \endcode is returned. # None \endcode is returned.
def read(self, file_name): def read(self, file_name):
prefix = ";SETTING_" + str(version) + " " prefix = ";SETTING_" + str(GCodeProfileReader.version) + " "
prefix_length = len(prefix) 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? #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 return None
#Unescape the serialised profile. #Unescape the serialised profile.
escape_characters = dict((re.escape(key), value) for key, value in escape_characters.items()) pattern = re.compile("|".join(GCodeProfileReader.escape_characters.keys()))
pattern = re.compile("|".join(escape_characters.keys())) serialised = pattern.sub(lambda m: GCodeProfileReader.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.
#Apply the changes to the current profile. #Apply the changes to the current profile.
profile = Profile(machine_manager = Application.getInstance().getMachineManager(), read_only = False) profile = Profile(machine_manager = Application.getInstance().getMachineManager(), read_only = False)

View file

@ -22,9 +22,9 @@ class GCodeWriter(MeshWriter):
# Note that the keys of this dictionary are regex strings. The values are # Note that the keys of this dictionary are regex strings. The values are
# not. # not.
escape_characters = { escape_characters = {
"\\": "\\\\", #The escape character. re.escape("\\"): "\\\\", #The escape character.
"\n": "\\n", #Newlines. They break off the comment. re.escape("\n"): "\\n", #Newlines. They break off the comment.
"\r": "\\r" #Carriage return. Windows users may need this for visualisation in their editors. re.escape("\r"): "\\r" #Carriage return. Windows users may need this for visualisation in their editors.
} }
def __init__(self): def __init__(self):
@ -54,15 +54,14 @@ class GCodeWriter(MeshWriter):
# \param profile The profile to serialise. # \param profile The profile to serialise.
# \return A serialised string of the profile. # \return A serialised string of the profile.
def _serialiseProfile(self, 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) prefix_length = len(prefix)
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 = dict((re.escape(key), value) for key, value in escape_characters.items()) pattern = re.compile("|".join(GCodeWriter.escape_characters.keys()))
pattern = re.compile("|".join(escape_characters.keys())) serialised = pattern.sub(lambda m: GCodeWriter.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.
#Introduce line breaks so that each comment is no longer than 80 characters. Prepend each line with the prefix. #Introduce line breaks so that each comment is no longer than 80 characters. Prepend each line with the prefix.
result = "" result = ""