From 596c49a964f4d25e3abe15339ffd687432bda12a Mon Sep 17 00:00:00 2001 From: Brendan Howell Date: Wed, 26 Feb 2020 12:49:04 +0100 Subject: [PATCH] refactor barcode generations to use kode256 --- screenless/bureau/bureau.py | 26 -------------------------- screenless/bureau/ihr/ihr.py | 23 ++++------------------- screenless/bureau/ihr/menu.html | 1 - screenless/bureau/sales/makecodes.py | 4 ++-- 4 files changed, 6 insertions(+), 48 deletions(-) diff --git a/screenless/bureau/bureau.py b/screenless/bureau/bureau.py index 8d8caf0..babb239 100644 --- a/screenless/bureau/bureau.py +++ b/screenless/bureau/bureau.py @@ -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 diff --git a/screenless/bureau/ihr/ihr.py b/screenless/bureau/ihr/ihr.py index 5e4351a..ea0d3b6 100644 --- a/screenless/bureau/ihr/ihr.py +++ b/screenless/bureau/ihr/ihr.py @@ -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 diff --git a/screenless/bureau/ihr/menu.html b/screenless/bureau/ihr/menu.html index 6df0c0e..ecbfd59 100644 --- a/screenless/bureau/ihr/menu.html +++ b/screenless/bureau/ihr/menu.html @@ -90,7 +90,6 @@

${cmd["desc"]}

-
% endif diff --git a/screenless/bureau/sales/makecodes.py b/screenless/bureau/sales/makecodes.py index 8f51046..8099738 100644 --- a/screenless/bureau/sales/makecodes.py +++ b/screenless/bureau/sales/makecodes.py @@ -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)