layerview.gcode.parser module

Raw G-code text parsing logic.

class layerview.gcode.parser.GcodeParser[source]

Bases: object

Gcode parser.

_LINE_LENGTH_MAX: int = 256
static command_generator(data: Iterable, skip_unknown: bool | None = False) Generator[source]
Parameters:
  • data (Iterable) – G-code text data.

  • skip_unknown (Optional[bool]) – If True, unknown commands are skipped.

Yields:

Command – Parsed Gcode command.

static parse(path: str | Path, skip_unknown: bool | None = False) Gcode[source]

Parses Gcode file at specified path.

Parameters:
  • path (str or Path) – Path to Gcode file.

  • skip_unknown (Optional[bool]) – If True, unknown commands are skipped.

Return type:

Gcode

class layerview.gcode.parser.GcodeScanner[source]

Bases: object

_PATTERN_GCODE_TOKEN = re.compile('([a-z])([-+]?\\d*\\.?\\d*)')
_PATTERN_INSIGNIFICANT = re.compile('\\(.*\\)|;.*|\\s+')
static _sanitize(line: str) str[source]

Strips comments, whitespace and converts text to lowercase.

Parameters:

line (str) – Gcode line to sanitize.

Returns:

Sanitized Gcode line.

Return type:

str

static analyze(line: str) List[Tuple[str, str]][source]

Performs lexical analysis on single line of G-code.

Parameters:

line (str) – Raw G-code line string.

Raises:

GcodeSyntaxError – If G-code syntax is invalid.

Returns:

List of G-code tokens in form [(command_or_param_letter, number_literal)], e.g. [(‘G’, ‘1’), (‘X’, ‘20’)].

Return type:

List[GcodeToken]

exception layerview.gcode.parser.GcodeSyntaxError(line_num: int | None = None, line_raw: str | None = None, error: Any | None = None)[source]

Bases: Exception, MixinMarkdown

_abc_impl = <_abc_data object>
args
as_markdown() str[source]

Return Markdown string representation for this object.

Returns:

Markdown string representation for this object.

Return type:

str

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.