qrr-py/qrr.cairo.py

62 lines
1.6 KiB
Python
Executable file

#!/usr/bin/env python3
from cairosvg import (svg2pdf, svg2png)
from qrr import qr_rechnung
def help(args):
return '\n'.join([
'{0}: Ungültige Eingabe',
'Aufruf: {0} TEXTDATEI [SVG-ZIELDATEI]',
'Alternativ kann die ZIELDATEI auch mit .pdf oder .png enden.'
]).format(args[0])
def main(args):
try:
textfile = args[1]
except IndexError:
print(help(args))
return 1
try:
output_format = 'svg'
svgfile = args[2]
if svgfile[-4:] == '.svg':
pass
elif svgfile[-4:] == '.png':
svgfile = svgfile[:-4]+'.svg'
output_format = 'png'
elif svgfile[-4:] == '.pdf':
svgfile = svgfile[:-4]+'.svg'
output_format = 'pdf'
else:
svgfile += '.svg'
except IndexError:
try:
assert textfile[-4:] == '.txt'
svgfile = textfile[:-4]+'.svg'
except AssertionError:
print(help(args))
return 1
try:
x = qr_rechnung(textfile=textfile)
x.write_file(svgfile, force=False)
except (PermissionError, FileNotFoundError) as e:
print(e)
return 1
if output_format == 'svg':
return 0
elif output_format == 'png':
svg2png(url=svgfile, write_to=svgfile[:-4]+'.png', dpi=600)
return 0
elif output_format == 'pdf':
svg2pdf(url=svgfile, write_to=svgfile[:-4]+'.pdf')
return 0
else:
print(f"ERROR: Unbekanntes Output_format: {output_format}")
return 1
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))