#!/usr/bin/env python3
import os, sys, io
BASE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(BASE, 'py_vendor'))
try:
    import qrcode
    import qrcode.image.svg
except Exception as e:
    sys.stderr.write('import error: %s\n' % e)
    sys.exit(2)

data = sys.stdin.read()
if not data:
    sys.exit(3)
try:
    qr = qrcode.QRCode(
        version=None,
        error_correction=qrcode.constants.ERROR_CORRECT_M,
        box_size=8,
        border=2,
        image_factory=qrcode.image.svg.SvgPathImage,
    )
    qr.add_data(data)
    qr.make(fit=True)
    img = qr.make_image(attrib={'width':'246', 'height':'246'})
    out = io.BytesIO()
    img.save(out)
    sys.stdout.buffer.write(out.getvalue())
except Exception as e:
    sys.stderr.write('qr error: %s\n' % e)
    sys.exit(4)
