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:
BarcodePrintermanages the lifecycle—parsing arguments, selecting the printer class, and initiating the execution. - Data Model:
Patientencapsulates the complex delimited string into a standard object with accessible attributes for template replacement. - Executor:
PRNPrinter(andSQLitePrinter) 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 $.
| Index | Field |
|---|---|
| 0 | Mode (0: Single, 1: Bulk) |
| 1 | Patient Name |
| 2 | Age |
| 3 | Gender |
| 4 | Patient ID / MRN |
| 5 | Organization Code |
| 7 | Sample ID |
| 9 | Test 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 (inconfig.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 inutils.pythat executes CLI commands (likemove) without showing a console window, ensuring a smooth user experience.