gcode_reader.emulate.parsers

Classes

GcodeParser

Parses G-code text into a list of high-level Command objects.

Module Contents

class gcode_reader.emulate.parsers.GcodeParser(syntax_name='default')[source]

Parses G-code text into a list of high-level Command objects.

This class acts as a bridge between the low-level dictionary representation of a G-code line (from the read module) and the structured Command objects used for analysis and machine simulation. It uses a series of helper methods to identify the specific type of command for each line.

syntax

The syntax definition for the chosen G-code flavor.

Type:

dict

helpers

An ordered list of handler methods used to parse a dictionary of G-code words into a specific Command object.

Type:

list

Initializes the GcodeParser with a specific syntax flavor.

Parameters:

syntax_name (str, optional) – The name of the syntax flavor to use, which corresponds to a definition in gcode_reader.syntax.flavors. Defaults to “default”.

dataframe_to_commands(df: pandas.DataFrame)[source]

Reads a gcode dataframe and converts it to a list of Command objects.

Ignores nan data in the input dataframe.

Parameters:

df (DataFrame) – The dataframe of gcode information to read.

Returns:

A list of parsed Command Objects, one for each row of the dataframe.

Return type:

List[Command]

gcode_file_to_commands(filepath: str) List[gcode_reader.emulate.commands.Command][source]

Reads a G-code file and converts it into a list of Command objects.

This is a high-level method that handles opening, reading, and parsing an entire G-code file from start to finish.

Parameters:

filepath (str) – The path to the G-code file.

Returns:

A list of parsed Command objects, one for each line in the file.

Return type:

List[Command]

gcode_line_to_command(line: str, **named_parameters)[source]

Reads a single G-code line string and converts it to a Command object.

Parameters:
  • line (str) – The G-code line to parse.

  • **named_parameters – Additional parameters to inject into the parsed words, used for context not present in the line itself.

Returns:

A parsed Command object.

Return type:

Command

gcode_words_to_command(**words) gcode_reader.emulate.commands.Command[source]

Translates a dictionary of G-code words into a specific Command object.

This method orchestrates the parsing process. It takes a dictionary of words from a single G-code line and passes it sequentially to each function in the self.helpers list. The first helper that successfully identifies the command pattern and returns a Command object wins.

If no helper can identify the command, it logs a warning and returns a generic Command object containing the original data.

Parameters:

**words – A dictionary where keys are G-code words (e.g., ‘G’, ‘X’) and values are their numeric or string values.

Returns:

The specific Command subclass instance that best matches

the G-code words.

Return type:

Command

property arc_move_codes
property code_map
property dwell_code
helpers
property linear_move_codes
property syntax
property word_map