CURA-5330 Fix code style and typing in GCodeReader plugin

This commit is contained in:
Diego Prado Gesto 2018-06-13 17:36:04 +02:00
parent 51888d86fe
commit 0a21ce44b0

View file

@ -23,9 +23,9 @@ import numpy
import math import math
import re import re
from typing import Dict, List, NamedTuple, Optional, Union from typing import Dict, List, NamedTuple, Optional, Union
from collections import namedtuple
Position = NamedTuple("Position", [("x", float), ("y", float), ("z", float), ("f", float), ("e", float)]) PositionOptional = NamedTuple("Position", [("x", Optional[float]), ("y", Optional[float]), ("z", Optional[float]), ("f", Optional[float]), ("e", Optional[float])])
Position = NamedTuple("Position", [("x", float), ("y", float), ("z", float), ("f", float), ("e", List[float])])
## This parser is intended to interpret the common firmware codes among all the ## This parser is intended to interpret the common firmware codes among all the
# different flavors # different flavors
@ -40,9 +40,9 @@ class FlavorParser:
self._clearValues() self._clearValues()
self._scene_node = None self._scene_node = None
# X, Y, Z position, F feedrate and E extruder values are stored # X, Y, Z position, F feedrate and E extruder values are stored
self._position = namedtuple('Position', ['x', 'y', 'z', 'f', 'e']) self._position = Position
self._is_layers_in_file = False # Does the Gcode have the layers comment? self._is_layers_in_file = False # Does the Gcode have the layers comment?
self._extruder_offsets = {} # Offsets for multi extruders. key is index, value is [x-offset, y-offset] self._extruder_offsets = {} # type: Dict[int, List[float]] # Offsets for multi extruders. key is index, value is [x-offset, y-offset]
self._current_layer_thickness = 0.2 # default self._current_layer_thickness = 0.2 # default
self._filament_diameter = 2.85 # default self._filament_diameter = 2.85 # default
@ -50,10 +50,10 @@ class FlavorParser:
def _clearValues(self) -> None: def _clearValues(self) -> None:
self._extruder_number = 0 self._extruder_number = 0
self._extrusion_length_offset = [0] self._extrusion_length_offset = [0] # type: List[float]
self._layer_type = LayerPolygon.Inset0Type self._layer_type = LayerPolygon.Inset0Type
self._layer_number = 0 self._layer_number = 0
self._previous_z = 0 self._previous_z = 0 # type: float
self._layer_data_builder = LayerDataBuilder.LayerDataBuilder() self._layer_data_builder = LayerDataBuilder.LayerDataBuilder()
self._is_absolute_positioning = True # It can be absolute (G90) or relative (G91) self._is_absolute_positioning = True # It can be absolute (G90) or relative (G91)
self._is_absolute_extrusion = True # It can become absolute (M82, default) or relative (M83) self._is_absolute_extrusion = True # It can become absolute (M82, default) or relative (M83)
@ -77,14 +77,14 @@ class FlavorParser:
def _getInt(self, line: str, code: str) -> Optional[int]: def _getInt(self, line: str, code: str) -> Optional[int]:
value = self._getValue(line, code) value = self._getValue(line, code)
try: try:
return int(value) return int(value) # type: ignore
except: except:
return None return None
def _getFloat(self, line: str, code: str) -> Optional[float]: def _getFloat(self, line: str, code: str) -> Optional[float]:
value = self._getValue(line, code) value = self._getValue(line, code)
try: try:
return float(value) return float(value) # type: ignore
except: except:
return None return None
@ -169,7 +169,7 @@ class FlavorParser:
return 0.35 return 0.35
return line_width return line_width
def _gCode0(self, position: Position, params: Position, path: List[List[Union[float, int]]]) -> Position: def _gCode0(self, position: Position, params: PositionOptional, path: List[List[Union[float, int]]]) -> Position:
x, y, z, f, e = position x, y, z, f, e = position
if self._is_absolute_positioning: if self._is_absolute_positioning:
@ -205,7 +205,7 @@ class FlavorParser:
_gCode1 = _gCode0 _gCode1 = _gCode0
## Home the head. ## Home the head.
def _gCode28(self, position: Position, params: Position, path: List[List[Union[float, int]]]) -> Position: def _gCode28(self, position: Position, params: PositionOptional, path: List[List[Union[float, int]]]) -> Position:
return self._position( return self._position(
params.x if params.x is not None else position.x, params.x if params.x is not None else position.x,
params.y if params.y is not None else position.y, params.y if params.y is not None else position.y,
@ -214,20 +214,20 @@ class FlavorParser:
position.e) position.e)
## Set the absolute positioning ## Set the absolute positioning
def _gCode90(self, position: Position, params: Position, path: List[List[Union[float, int]]]) -> Position: def _gCode90(self, position: Position, params: PositionOptional, path: List[List[Union[float, int]]]) -> Position:
self._is_absolute_positioning = True self._is_absolute_positioning = True
self._is_absolute_extrusion = True self._is_absolute_extrusion = True
return position return position
## Set the relative positioning ## Set the relative positioning
def _gCode91(self, position: Position, params: Position, path: List[List[Union[float, int]]]) -> Position: def _gCode91(self, position: Position, params: PositionOptional, path: List[List[Union[float, int]]]) -> Position:
self._is_absolute_positioning = False self._is_absolute_positioning = False
self._is_absolute_extrusion = False self._is_absolute_extrusion = False
return position return position
## Reset the current position to the values specified. ## Reset the current position to the values specified.
# For example: G92 X10 will set the X to 10 without any physical motion. # For example: G92 X10 will set the X to 10 without any physical motion.
def _gCode92(self, position: Position, params: Position, path: List[List[Union[float, int]]]) -> Position: def _gCode92(self, position: Position, params: PositionOptional, path: List[List[Union[float, int]]]) -> Position:
if params.e is not None: if params.e is not None:
# Sometimes a G92 E0 is introduced in the middle of the GCode so we need to keep those offsets for calculate the line_width # Sometimes a G92 E0 is introduced in the middle of the GCode so we need to keep those offsets for calculate the line_width
self._extrusion_length_offset[self._extruder_number] += position.e[self._extruder_number] - params.e self._extrusion_length_offset[self._extruder_number] += position.e[self._extruder_number] - params.e
@ -260,7 +260,7 @@ class FlavorParser:
f = float(item[1:]) / 60 f = float(item[1:]) / 60
if item[0] == "E": if item[0] == "E":
e = float(item[1:]) e = float(item[1:])
params = self._position(x, y, z, f, e) params = PositionOptional(x, y, z, f, e)
return func(position, params, path) return func(position, params, path)
return position return position
@ -322,12 +322,13 @@ class FlavorParser:
lifetime=0, lifetime=0,
title = catalog.i18nc("@info:title", "G-code Details")) title = catalog.i18nc("@info:title", "G-code Details"))
assert(self._message is not None) # use for typing purposes
self._message.setProgress(0) self._message.setProgress(0)
self._message.show() self._message.show()
Logger.log("d", "Parsing Gcode...") Logger.log("d", "Parsing Gcode...")
current_position = self._position(0, 0, 0, 0, [0]) current_position = Position(0, 0, 0, 0, [0])
current_path = [] current_path = []
min_layer_number = 0 min_layer_number = 0
negative_layers = 0 negative_layers = 0