188 lines
5.0 KiB
Python
Executable File
188 lines
5.0 KiB
Python
Executable File
"""
|
|
Linux BenchTools - QR Code Generator
|
|
Generate QR codes for locations
|
|
"""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
import qrcode
|
|
from qrcode.image.styledpil import StyledPilImage
|
|
from qrcode.image.styles.moduledrawers import RoundedModuleDrawer
|
|
|
|
|
|
class QRCodeGenerator:
|
|
"""QR Code generation utilities"""
|
|
|
|
@staticmethod
|
|
def generate_location_qr(
|
|
location_id: int,
|
|
location_name: str,
|
|
base_url: str,
|
|
output_dir: str,
|
|
size: int = 300
|
|
) -> str:
|
|
"""
|
|
Generate QR code for a location
|
|
|
|
Args:
|
|
location_id: Location ID
|
|
location_name: Location name (for filename)
|
|
base_url: Base URL of the application
|
|
output_dir: Directory for output
|
|
size: QR code size in pixels
|
|
|
|
Returns:
|
|
Path to generated QR code image
|
|
"""
|
|
# Create URL pointing to location page
|
|
url = f"{base_url}/peripherals?location={location_id}"
|
|
|
|
# Create QR code
|
|
qr = qrcode.QRCode(
|
|
version=1, # Auto-adjust
|
|
error_correction=qrcode.constants.ERROR_CORRECT_H, # High error correction
|
|
box_size=10,
|
|
border=4,
|
|
)
|
|
|
|
qr.add_data(url)
|
|
qr.make(fit=True)
|
|
|
|
# Generate image with rounded style
|
|
img = qr.make_image(
|
|
image_factory=StyledPilImage,
|
|
module_drawer=RoundedModuleDrawer()
|
|
)
|
|
|
|
# Resize to specified size
|
|
img = img.resize((size, size))
|
|
|
|
# Generate filename
|
|
safe_name = "".join(c for c in location_name if c.isalnum() or c in (' ', '-', '_')).strip()
|
|
safe_name = safe_name.replace(' ', '_')
|
|
output_filename = f"qr_location_{location_id}_{safe_name}.png"
|
|
output_path = os.path.join(output_dir, output_filename)
|
|
|
|
# Ensure output directory exists
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
# Save
|
|
img.save(output_path)
|
|
|
|
return output_path
|
|
|
|
@staticmethod
|
|
def generate_peripheral_qr(
|
|
peripheral_id: int,
|
|
peripheral_name: str,
|
|
base_url: str,
|
|
output_dir: str,
|
|
size: int = 200
|
|
) -> str:
|
|
"""
|
|
Generate QR code for a peripheral
|
|
|
|
Args:
|
|
peripheral_id: Peripheral ID
|
|
peripheral_name: Peripheral name (for filename)
|
|
base_url: Base URL of the application
|
|
output_dir: Directory for output
|
|
size: QR code size in pixels
|
|
|
|
Returns:
|
|
Path to generated QR code image
|
|
"""
|
|
# Create URL pointing to peripheral detail page
|
|
url = f"{base_url}/peripheral/{peripheral_id}"
|
|
|
|
# Create QR code
|
|
qr = qrcode.QRCode(
|
|
version=1,
|
|
error_correction=qrcode.constants.ERROR_CORRECT_H,
|
|
box_size=10,
|
|
border=4,
|
|
)
|
|
|
|
qr.add_data(url)
|
|
qr.make(fit=True)
|
|
|
|
# Generate image
|
|
img = qr.make_image(
|
|
image_factory=StyledPilImage,
|
|
module_drawer=RoundedModuleDrawer()
|
|
)
|
|
|
|
# Resize
|
|
img = img.resize((size, size))
|
|
|
|
# Generate filename
|
|
safe_name = "".join(c for c in peripheral_name if c.isalnum() or c in (' ', '-', '_')).strip()
|
|
safe_name = safe_name.replace(' ', '_')
|
|
output_filename = f"qr_peripheral_{peripheral_id}_{safe_name}.png"
|
|
output_path = os.path.join(output_dir, output_filename)
|
|
|
|
# Ensure output directory exists
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
# Save
|
|
img.save(output_path)
|
|
|
|
return output_path
|
|
|
|
@staticmethod
|
|
def generate_custom_qr(
|
|
data: str,
|
|
output_path: str,
|
|
size: int = 300,
|
|
error_correction: str = "H"
|
|
) -> str:
|
|
"""
|
|
Generate a custom QR code
|
|
|
|
Args:
|
|
data: Data to encode
|
|
output_path: Full output path
|
|
size: QR code size in pixels
|
|
error_correction: Error correction level (L, M, Q, H)
|
|
|
|
Returns:
|
|
Path to generated QR code image
|
|
"""
|
|
# Map error correction
|
|
ec_map = {
|
|
"L": qrcode.constants.ERROR_CORRECT_L,
|
|
"M": qrcode.constants.ERROR_CORRECT_M,
|
|
"Q": qrcode.constants.ERROR_CORRECT_Q,
|
|
"H": qrcode.constants.ERROR_CORRECT_H
|
|
}
|
|
ec = ec_map.get(error_correction.upper(), qrcode.constants.ERROR_CORRECT_H)
|
|
|
|
# Create QR code
|
|
qr = qrcode.QRCode(
|
|
version=1,
|
|
error_correction=ec,
|
|
box_size=10,
|
|
border=4,
|
|
)
|
|
|
|
qr.add_data(data)
|
|
qr.make(fit=True)
|
|
|
|
# Generate image
|
|
img = qr.make_image(
|
|
image_factory=StyledPilImage,
|
|
module_drawer=RoundedModuleDrawer()
|
|
)
|
|
|
|
# Resize
|
|
img = img.resize((size, size))
|
|
|
|
# Ensure output directory exists
|
|
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
|
|
|
# Save
|
|
img.save(output_path)
|
|
|
|
return output_path
|