gscrib.writers

G-code output to files and machine connections.

This module provides utilities for writing G-code to different outputs, including files, network sockets, and serial connections.

class gscrib.writers.BaseWriter

Bases: ABC

Base class for all the G-code writing implementations.

This class defines the interface for writing G-code commands to various outputs, such as files, serial connections, or network sockets.

abstractmethod connect()

Establish connection or open resource for writing

Return type:

BaseWriter

abstractmethod disconnect(wait=True)

Close connection or resource

Parameters:

wait (bool)

Return type:

None

flush()

Flush the output buffer

Return type:

None

abstractmethod write(statement)

Write G-code statement

Parameters:

statement (bytes)

Return type:

None

class gscrib.writers.ConsoleWriter(stderr=False)

Bases: FileWriter

Writer that outputs commands to the console.

This class implements a G-code writer specifically designed for console output.

Example

>>> writer = ConsoleWriter()
>>> writer.write(b"G1 X10 Y10\n")
Parameters:

stderr (bool)

connect()

Establish the connection to the console output.

Return type:

ConsoleWriter

disconnect(wait=True)

Close the file if it was opened by this writer.

Parameters:

wait (bool)

Return type:

None

flush()

Flush the output buffer

Return type:

None

write(statement)

Write a G-code statement to the file.

Parameters:

statement (bytes) – The G-code statement to write.

Raises:
  • OSError – If an error occurred while writing to the file.

  • TypeCheckError – If statement is not bytes-like

Return type:

None

class gscrib.writers.FileWriter(output)

Bases: BaseWriter

Writer that outputs commands to a file or file-like object.

This class implements a G-code writer that can write G-code commands to a file. The writer handles both text and binary output modes automatically, converting between bytes and strings as needed.

Example

>>> writer = FileWriter("output.gcode")
>>> writer.write(b"G1 X10 Y10\n")
>>> writer.disconnect()
Parameters:

output (str | TextIO | BinaryIO)

connect()

Establish the connection to the output file.

Return type:

FileWriter

disconnect(wait=True)

Close the file if it was opened by this writer.

Parameters:

wait (bool)

Return type:

None

flush()

Flush the output buffer

Return type:

None

write(statement)

Write a G-code statement to the file.

Parameters:

statement (bytes) – The G-code statement to write.

Raises:
  • OSError – If an error occurred while writing to the file.

  • TypeCheckError – If statement is not bytes-like

Return type:

None

class gscrib.writers.LogWriter

Bases: BaseWriter

Writer that outputs commands to Python’s logging system.

This class implements a G-code writer that logs G-code commands using Python’s logging framework.

Example

>>> writer = LogWriter()
>>> writer.set_level("info")
>>> writer.write(b"G1 X10 Y10\n")
connect()

Establish connection or open resource for writing

Return type:

LogWriter

disconnect(wait=True)

Close connection or resource

Parameters:

wait (bool)

Return type:

None

flush()

Flush the output buffer

Return type:

None

get_logger()

Get the logger used by this writer.

Returns:

The logger used by this writer.

Return type:

logging.Logger

set_level(level)

Set the logging level for the logger.

Parameters:

level (int | str) – The logging level to set.

Return type:

None

write(statement)

Write a G-code statement to the default logger.

Parameters:

statement (bytes) – The G-code statement to write.

Return type:

None

class gscrib.writers.PrintrunWriter(mode, host, port, baudrate)

Bases: BaseWriter

Writer that sends commands through a serial or socket connection.

This class implements a G-code writer that connects to a device using printrun core.

Parameters:
connect()

Establish the connection to the device.

Returns:

Self for method chaining

Return type:

PrintrunWriter

Raises:
disconnect(wait=True)

Close the connection if it exists.

Parameters:

wait (bool) – If True, waits for pending operations to complete

Raises:

DeviceTimeoutError – If waiting times out

Return type:

None

flush()

Flush the output buffer

Return type:

None

get_parameter(name)

Get the last reading for a parameter by name.

This method retrieves the last reported value for a device parameter. These parameters are stored and updated each time the device reports a new value for them.

Parameters:

name (str) – Name of the parameter (case-insensitive)

Returns:

Last value read for the parameter or None

Return type:

float

set_timeout(timeout)

Set the timeout for waiting for device operations.

Parameters:

timeout (float) – Timeout in seconds.

Return type:

None

write(statement)

Send a G-code statement through the device connection.

Parameters:

statement (bytes) – The G-code statement to send

Raises:
Return type:

None

property has_pending_operations: bool

Check if there are pending operations.

property is_connected: bool

Check if device is currently connected.

property is_printing: bool

Check if the device is currently printing.

class gscrib.writers.SerialWriter(port, baudrate)

Bases: BaseWriter

Writer that sends commands through a serial connection.

This class implements a G-code writer that connects to a device through a serial port and sends G-code commands to it.

Example

>>> writer = SerialWriter("/dev/ttyUSB0", 115200)
>>> writer.write(b"G1 X10 Y10\n")
>>> writer.disconnect()
Parameters:
connect()

Establish the serial connection to the device.

Creates a printcore object with the configured port and baudrate, and waits for the connection to be established.

Returns:

Self for method chaining

Return type:

SerialWriter

Raises:
disconnect(wait=True)

Close the serial connection if it exists.

Parameters:

wait (bool) – If True, waits for pending operations to complete

Raises:

DeviceTimeoutError – If waiting times out

Return type:

None

flush()

Flush the output buffer

Return type:

None

get_parameter(name)

Get the last reading for a parameter by name.

This method retrieves the last reported value for a device parameter. These parameters are stored and updated each time the device reports a new value for them.

Parameters:

name (str) – Name of the parameter (case-insensitive)

Returns:

Last value read for the parameter or None

Return type:

float

set_timeout(timeout)

Set the timeout for waiting for device operations.

Parameters:

timeout (float) – Timeout in seconds.

Return type:

None

write(statement)

Send a G-code statement through the serial connection.

Parameters:

statement (bytes) – The G-code statement to send

Raises:

DeviceConnectionError – If connection cannot be established

Return type:

None

property is_connected: bool

Check if device is currently connected.

property is_printing: bool

Check if the device is currently printing.

class gscrib.writers.SocketWriter(host, port)

Bases: BaseWriter

Writer that sends commands through a network socket connection.

This class implements a G-code writer that connects to a device through a TCP/IP socket and sends G-code commands to it.

Example

>>> writer = SocketWriter("localhost", 8000)
>>> writer.write(b"G1 X10 Y10\n")
>>> writer.disconnect()
Parameters:
connect()

Establish the socket connection to the device.

Creates a printcore object with the configured host and port, and waits for the connection to be established.

Returns:

Self for method chaining

Return type:

SocketWriter

Raises:
disconnect(wait=True)

Close the socket connection if it exists.

Parameters:

wait (bool) – If True, waits for pending operations to complete

Raises:

DeviceTimeoutError – If waiting times out

Return type:

None

flush()

Flush the output buffer

Return type:

None

get_parameter(name)

Get the last reading for a parameter by name.

This method retrieves the last reported value for a device parameter. These parameters are stored and updated each time the device reports a new value for them.

Parameters:

name (str) – Name of the parameter (case-insensitive)

Returns:

Last value read for the parameter or None

Return type:

float

set_timeout(timeout)

Set the timeout for waiting for device operations.

Parameters:

timeout (float) – Timeout in seconds.

Return type:

None

write(statement)

Send a G-code statement through the socket connection.

Parameters:

statement (bytes) – The G-code statement to send

Raises:

DeviceConnectionError – If connection cannot be established

Return type:

None

property is_connected: bool

Check if device is currently connected.

property is_printing: bool

Check if the device is currently printing.