Codestyle & documentation

CURA-537
This commit is contained in:
Jaime van Kessel 2016-04-28 16:01:54 +02:00
parent efd14421cc
commit 9009fb9d3d
4 changed files with 36 additions and 23 deletions

View file

@ -22,9 +22,9 @@ class GCodeWriter(MeshWriter):
# Note that the keys of this dictionary are regex strings. The values are
# not.
escape_characters = {
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.
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):
@ -40,7 +40,8 @@ class GCodeWriter(MeshWriter):
if gcode_list:
for gcode in gcode_list:
stream.write(gcode)
profile = self._serialiseProfile(Application.getInstance().getMachineManager().getWorkingProfile()) #Serialise the profile and put them at the end of the file.
# Serialise the profile and put them at the end of the file.
profile = self._serialiseProfile(Application.getInstance().getMachineManager().getWorkingProfile())
stream.write(profile)
return True
@ -54,19 +55,22 @@ class GCodeWriter(MeshWriter):
# \param profile The profile to serialise.
# \return A serialised string of the profile.
def _serialiseProfile(self, profile):
prefix = ";SETTING_" + str(GCodeWriter.version) + " " #The prefix to put before each line.
prefix = ";SETTING_" + str(GCodeWriter.version) + " " # The prefix to put before each line.
prefix_length = len(prefix)
#Serialise a deepcopy to remove the defaults from the profile
# Serialise a deepcopy to remove the defaults from the profile
serialised = copy.deepcopy(profile).serialise()
#Escape characters that have a special meaning in g-code comments.
# Escape characters that have a special meaning in g-code comments.
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.
# Perform the replacement with a regular expression.
serialised = pattern.sub(lambda m: GCodeWriter.escape_characters[re.escape(m.group(0))], serialised)
#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 = ""
for pos in range(0, len(serialised), 80 - prefix_length): #Lines have 80 characters, so the payload of each line is 80 - prefix.
# Lines have 80 characters, so the payload of each line is 80 - prefix.
for pos in range(0, len(serialised), 80 - prefix_length):
result += prefix + serialised[pos : pos + 80 - prefix_length] + "\n"
serialised = result