refactor barcode generations to use kode256

workspace
Brendan Howell 4 years ago
parent 9c8e26b426
commit 596c49a964

@ -15,7 +15,6 @@ import textwrap
#import traceback
import threading
import barcode
import lmdb
import PIL
import weasyprint
@ -233,31 +232,6 @@ class Bureau(object):
db = KeyValStore(self.dbenv, name)
return db
def bc_svg(self, code, tofile=False, outfile=None, width=0.2, height=15.0,
notext=False):
"""
generate a CODE128 barcode as an SVG
returns a string or can write to a tmp file or specific
file given arguments
"""
fp = io.BytesIO()
# TODO: support notext option
writer = barcode.writer.SVGWriter(module_width=width,
module_height=height)
barcode.generate("CODE128", code, writer=writer, output=fp)
fp.seek(0)
# TODO: add file output
return fp.read().decode()
def bc_png(self, code, outfile=None):
"""
generate a code128 barcode as a png
returns the full path to the file
if file is not specified, uses a tmp file
"""
# TODO: implement this (its like 5 lines!)
raise NotImplemented("png generation not implemented!")
def load_config(self):
"""
load (or reload) config data from file

@ -4,27 +4,12 @@ import signal
import socket
import subprocess
import barcode
import kode256
from lxml import etree
from bureau import Bureau, add_command, add_api
def clean_svg(svg):
"""
This is a little hack that removes the height and width attributes from
SVG strings as they confuse Firefox. Sigh... hopefully this can go away
some day.
"""
utf8_parser = etree.XMLParser(encoding='utf-8')
svg = svg.encode("utf-8")
otree = etree.fromstring(svg, utf8_parser)
del otree.attrib["height"]
del otree.attrib["width"]
clean_svg = etree.tostring(otree)
return clean_svg.decode("utf-8")
class InhumanResources(Bureau):
"""
This is a core functional bureau of the Screenless office it keeps track
@ -72,9 +57,9 @@ class InhumanResources(Bureau):
cmdname = data["cmdname"]
desc = data["desc"]
cmd_code = prefix + cmd + "."
# barcode = clean_svg(code128.svg(prefix + cmd + "."))
barcode_png = os.path.join(self.datadir, cmd_code)
bc = barcode.generate("code128", prefix + cmd + ".", writer=barcode.writer.ImageWriter(), output=barcode_png)
barcode_png = os.path.join(self.datadir, cmd_code + "png")
#bc = barcode.generate("code128", prefix + cmd + ".", writer=barcode.writer.ImageWriter(), output=barcode_png)
bc = kode256.image(cmd_code).save(barcode_png)
except KeyError as e:
print("cannot add invalid command:", str(e))
return

@ -90,7 +90,6 @@
<div class="cmdcol">
<p class="cmddesc">${cmd["desc"]}</p>
</div>
<!--div class="cmdcol">${cmd["barcode"]}</div-->
<div class="cmdcol"><img src="${cmd["barcode_png"]}" /></div>
</div>
% endif

@ -1,11 +1,11 @@
import random
import string
import barcode
import kode256
chars = string.ascii_letters + string.digits
for code in range(30):
code = ''.join(random.choice(chars) for _ in range(5))
code = "SAp." + code
out_file = "/tmp/" + code + ".svg"
barcode.generate("code128", code, output=out_file)
kode256.svg(code).save(out_file)

Loading…
Cancel
Save