diff options
author | Ghostkeeper <ghost_keeper+github@hotmail.com> | 2015-12-16 17:59:56 +0300 |
---|---|---|
committer | Ghostkeeper <ghost_keeper+github@hotmail.com> | 2015-12-17 15:35:16 +0300 |
commit | afd63c53c05f8c540245f1d58aa581644aa99db9 (patch) | |
tree | 55484503a61b95fbc589906a3875b81838c7ee91 /plugins/GCodeProfileReader | |
parent | a3936540d8c80e67b7f6cef7a43ac8eecce50f24 (diff) |
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.
Diffstat (limited to 'plugins/GCodeProfileReader')
-rw-r--r-- | plugins/GCodeProfileReader/GCodeProfileReader.py | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/plugins/GCodeProfileReader/GCodeProfileReader.py b/plugins/GCodeProfileReader/GCodeProfileReader.py index f1111b453b..7c72d0a958 100644 --- a/plugins/GCodeProfileReader/GCodeProfileReader.py +++ b/plugins/GCodeProfileReader/GCodeProfileReader.py @@ -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,10 +55,9 @@ 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) try: |