#!/usr/bin/env python3
# encoding:utf-8
# This code is released under CC0.
# https://creativecommons.org/publicdomain/zero/1.0/
import argparse, re, os, subprocess, hashlib
parser = argparse.ArgumentParser(description='Convert ASCII data to printable A5 HTML page.')
parser.add_argument('input', metavar='INPUT', type=argparse.FileType('r'),
help='input tarsnap.key file')
parser.add_argument('output', metavar='OUTPUT', type=str,
help='output directory with HTML and DataMatrix images')
parser.add_argument('title', metavar='TITLE', type=str,
help='title for the output page')
args = parser.parse_args()
key = args.input.read()
os.mkdir(args.output, 0o700)
chunks = re.findall(re.compile('.{1,850}', re.DOTALL), key)
for index, chunk in enumerate(chunks):
subprocess.call([
"iec16022", "--ecc=200", "--format=PNG",
"--barcode={}".format(chunk),
"--outfile={}/chunk{}.png".format(args.output, index)
])
with open("{}/index.html".format(args.output), "w") as html:
images = ""
for index, chunk in enumerate(chunks):
images += """
""".format(index)
digest = hashlib.sha256(key.encode()).hexdigest()
html.write("""
This page contains {}.
This data is encoded using multiple ISO/IEC 16022:2006 (Data Matrix) ECC 200 barcodes. To reproduce the data, scan every barcode from left to right and from top to bottom, and concatenate their contents without anything in between.
The SHA-256 digest of the original data is {}.
{}
""".format(args.title, digest, images))