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: object

Synchronous engine that encodes payloads into QR codes.

Wraps the third-party qrcode library 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 via asyncio.to_thread.

A single module-level instance _qr_gen is created at import time when qrcode is 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 qrcode dependency is importable.

Guards construction so a QRCodeGenerator only exists when the backing library is present; the module-level _qr_gen singleton is otherwise left as None and the tool handlers return a library-missing error instead.

Raises:

ImportError – If the qrcode library 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.QRCode with 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_thread by the module-level _generate_* handlers (for example _generate_qr_code and _batch_generate_qr). For PNG output it touches the filesystem, creating a temp directory and writing qrcode.png into 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, or H; case-insensitive, defaulting to M when unrecognized.

  • fill_color – Foreground color for PNG output.

  • back_color – Background color for PNG output.

  • format_type – Output format – PNG, SVG, or ASCII (case-insensitive).

Returns:

A result dict with success true plus the encoded output (one of qr_code_ascii, qr_code_svg, or a file_path), the module count, error-correction level, format, and mime_type; on failure a dict with success false, the error text, and the original data.

create_vcard_qr(contact_info)[source]

Build a vCard 3.0 payload string from contact fields.

Assembles the multi-line BEGIN:VCARD / END:VCARD block 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 one TEL or EMAIL line each.

Pure string construction with no IO. Called by _generate_contact_qr, which feeds the returned vCard into generate_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 via generate_qr_code.

Parameters:

wifi_info – Dict with ssid (required), and optional password and security (for example WPA, 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-encoded subject and body query 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 via generate_qr_code.

Parameters:

email_info – Dict with to (required) and optional subject and body.

Returns:

The assembled mailto: URL, or an Error: ... message when the recipient is missing.