tools.qr_generator module
QR Code Generator Tools.
Generates QR codes for URLs, text, contacts (vCard), WiFi credentials, email, and batch generation. Supports PNG, SVG, and ASCII output.
- class tools.qr_generator.QRCodeGenerator[source]
Bases:
objectSynchronous engine that encodes payloads into QR codes.
Wraps the third-party
qrcodelibrary to turn arbitrary strings into PNG files, SVG markup, or ASCII art, and builds the structured payload strings (vCard, WiFi, mailto) that the contact, WiFi, and email tools encode. All methods here are blocking CPU/IO work, so the async tool handlers run them off the event loop viaasyncio.to_thread.A single module-level instance
_qr_genis created at import time whenqrcodeis available, and every_generate_*handler in this file calls through it. Touches the local filesystem only for PNG output, writing into a fresh temp directory.- __init__()[source]
Verify the optional
qrcodedependency is importable.Guards construction so a
QRCodeGeneratoronly exists when the backing library is present; the module-level_qr_gensingleton is otherwise left asNoneand the tool handlers return a library-missing error instead.- Raises:
ImportError – If the
qrcodelibrary is not installed.
- generate_qr_code(data, size=10, border=4, error_correction='M', fill_color='black', back_color='white', format_type='PNG')[source]
Encode a string into a QR code in the requested output format.
The single rendering primitive behind every QR tool: it builds a
qrcode.QRCodewith an auto-fitted version and the chosen error-correction level, then emits one of three formats. ASCII returns a block-character grid inline, SVG returns vector markup inline, and PNG is written to a freshly created temp directory and returned as a file path so the caller can attach it.Runs synchronously and is invoked off the event loop via
asyncio.to_threadby the module-level_generate_*handlers (for example_generate_qr_codeand_batch_generate_qr). For PNG output it touches the filesystem, creating a temp directory and writingqrcode.pnginto it; ASCII and SVG stay in memory.- Parameters:
data – The text, URL, or structured payload to encode.
size – Pixel size of each QR box (
box_size).border – Quiet-zone width in boxes around the code.
error_correction – One of
L,M,Q, orH; case-insensitive, defaulting toMwhen unrecognized.fill_color – Foreground color for PNG output.
back_color – Background color for PNG output.
format_type – Output format –
PNG,SVG, orASCII(case-insensitive).
- Returns:
A result dict with
successtrue plus the encoded output (one ofqr_code_ascii,qr_code_svg, or afile_path), the module count, error-correction level, format, andmime_type; on failure a dict withsuccessfalse, theerrortext, and the originaldata.
- create_vcard_qr(contact_info)[source]
Build a vCard 3.0 payload string from contact fields.
Assembles the multi-line
BEGIN:VCARD/END:VCARDblock that a phone’s camera recognizes as a saveable contact, emitting only the lines for fields that are present. Phone and email accept either a single value or a list, producing oneTELorEMAILline each.Pure string construction with no IO. Called by
_generate_contact_qr, which feeds the returned vCard intogenerate_qr_code.- Parameters:
contact_info – Dict of optional contact fields –
first_name,last_name,display_name,phone,email,organization,title,website.- Returns:
The assembled vCard text as a single newline-joined string.
- create_wifi_qr(wifi_info)[source]
Build a
WIFI:join-credential payload string.Produces the standard
WIFI:T:...;S:...;P:...;;string that lets a phone join a network by scanning the resulting code, filling in the security type, SSID, and password from the supplied dict. The SSID is mandatory; everything else has sensible defaults.Pure string construction with no IO. Called by
_generate_wifi_qr, which encodes the returned string viagenerate_qr_code.- Parameters:
wifi_info – Dict with
ssid(required), and optionalpasswordandsecurity(for exampleWPA,WEP,nopass).- Returns:
The WIFI credential string, or an
Error: ...message when the SSID is missing.
- create_email_qr(email_info)[source]
Build a
mailto:URL with optional subject and body.Produces the
mailto:link that opens a pre-addressed draft when the resulting code is scanned, appending URL-encodedsubjectandbodyquery parameters when provided (spaces are escaped to%20). The recipient is mandatory.Pure string construction with no IO. Called by
_generate_email_qr, which encodes the returned URL viagenerate_qr_code.- Parameters:
email_info – Dict with
to(required) and optionalsubjectandbody.- Returns:
The assembled
mailto:URL, or anError: ...message when the recipient is missing.