Product EngineeringFeaturesRemote Printer (lh-print)

Backend Overview

Architectural deep-dive into the lh-print logic, classes, and function mappings.

👤 Aditya Naresh📅 Updated: Apr 20, 2026📁 Remote Printer

Backend Developer Guide

The lh-print utility is built on a simple yet robust Orchestrator-Executor architecture designed to safely handle OS-level printing tasks triggered by external web browsers.

Core Concepts

  • Orchestrator: BarcodePrinter manages the lifecycle—parsing arguments, selecting the printer class, and initiating the execution.
  • Data Model: Patient encapsulates the complex delimited string into a standard object with accessible attributes for template replacement.
  • Executor: PRNPrinter (and SQLitePrinter) handles the physical communication with hardware or databases.

Class Diagram

Internal Data Flow

1. Argument Parsing

The entry point passes the sys.argv[1] to BarcodePrinter. The string is parsed using unquote from Python's urllib.parse to handle URL encoding.

# barcode_printer.py -> parse_args
args = unquote(cmd_argv).split("://")
barcode_str = args[1] if len(args) > 1 else ""
args = barcode_str.split("^")

2. Patient Data Mapping

The Patient class uses fixed-index parsing for the ^ delimited string. For bulk prints, entries are delimited by $.

IndexField
0Mode (0: Single, 1: Bulk)
1Patient Name
2Age
3Gender
4Patient ID / MRN
5Organization Code
7Sample ID
9Test Names (comma-separated)

3. Template Interpolation

PRNPrinter converts the Patient object into an uppercase dictionary (get_all()) and uses Python's .format(**args) to hydrate the .prn template.

# prn_printer.py -> generate_prn_template
args = { key.upper(): val for key, val in patient.get_all().items() }
current_template = base_template.format(**args)

Key Functions for Reference

  • init_lhprint(): Root directory and file preparation (in config.py).
  • BarcodePrinter.parse_args(): Logic for mode detection and bulk splitting.
  • PRNPrinter.prepare_and_print_barcode(): Main loop for label generation.
  • Patient.prepare_test_lines(): Utility that truncates long test strings into 3 distinct lines (30 chars each) for small label compatibility.
  • exec_cmd(): A wrapper in utils.py that executes CLI commands (like move) without showing a console window, ensuring a smooth user experience.

On this page