2015-08-29 23:46:34 +00:00
|
|
|
import json
|
2015-08-29 22:46:51 +00:00
|
|
|
import math
|
|
|
|
import os
|
|
|
|
import StringIO
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
|
|
|
import time
|
|
|
|
|
|
|
|
import cairo
|
|
|
|
import flask
|
|
|
|
import pango
|
|
|
|
import pangocairo
|
|
|
|
|
|
|
|
class App(flask.Flask):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(App, self).__init__(*args, **kwargs)
|
|
|
|
self.last = 0
|
2015-08-29 23:46:34 +00:00
|
|
|
self.health = (0, False, "unknown")
|
2015-08-29 22:46:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
app = App(__name__)
|
|
|
|
|
|
|
|
class Renderer(object):
|
|
|
|
INCH_PER_MM = 0.039
|
|
|
|
DPI = 300
|
2016-12-16 00:13:25 +00:00
|
|
|
def __init__(self, size=(36,89)):
|
2015-08-29 22:46:51 +00:00
|
|
|
width, height = [int(s * self.INCH_PER_MM * self.DPI) for s in size]
|
|
|
|
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
|
|
|
|
context = cairo.Context(surface)
|
2016-10-14 00:16:37 +00:00
|
|
|
|
2015-08-29 22:46:51 +00:00
|
|
|
# fill it white, while we're at it
|
|
|
|
context.rectangle(0, 0, width, height)
|
|
|
|
context.set_source_rgb(1, 1, 1)
|
|
|
|
context.fill()
|
2016-10-14 00:16:37 +00:00
|
|
|
|
2015-08-29 22:46:51 +00:00
|
|
|
context.translate(width, 0)
|
|
|
|
context.rotate(math.pi/2)
|
|
|
|
# yolo
|
|
|
|
self.width, self.height = height, width
|
|
|
|
|
|
|
|
self.context = context
|
|
|
|
self.surface = surface
|
|
|
|
|
|
|
|
def export_png(self, name):
|
|
|
|
with open(name, 'w') as f:
|
|
|
|
self.surface.write_to_png(f)
|
|
|
|
|
2016-10-14 00:16:37 +00:00
|
|
|
def render_text(self, text, fontname, x, y, html=False):
|
2015-08-29 22:46:51 +00:00
|
|
|
self.context.save()
|
|
|
|
if y != -1:
|
|
|
|
self.context.translate(x, y)
|
|
|
|
|
|
|
|
pangocairo_context = pangocairo.CairoContext(self.context)
|
|
|
|
pangocairo_context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
|
|
|
|
layout = pangocairo_context.create_layout()
|
|
|
|
layout.set_width(self.width*pango.SCALE)
|
|
|
|
layout.set_alignment(pango.ALIGN_CENTER)
|
|
|
|
|
2016-10-14 00:16:37 +00:00
|
|
|
if html:
|
|
|
|
# Absolutely horrifying hack to fix broken text wrapping
|
|
|
|
layout.set_markup('<span font_desc="%s">%s</span>' % (fontname, text))
|
|
|
|
else:
|
|
|
|
font = pango.FontDescription(fontname)
|
|
|
|
layout.set_font_description(font)
|
|
|
|
layout.set_text(text)
|
|
|
|
|
2015-08-29 22:46:51 +00:00
|
|
|
if y == -1:
|
|
|
|
self.context.translate(0, (self.height - layout.get_size()[1]/pango.SCALE)/2)
|
|
|
|
|
|
|
|
self.context.set_source_rgb(0, 0, 0)
|
|
|
|
pangocairo_context.update_layout(layout)
|
|
|
|
pangocairo_context.show_layout(layout)
|
|
|
|
|
|
|
|
self.context.restore()
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def index():
|
|
|
|
return flask.render_template('index.html')
|
|
|
|
|
2015-08-29 23:46:34 +00:00
|
|
|
def healthcheck():
|
|
|
|
last_checked, last_status, last_details = app.health
|
|
|
|
if time.time() - last_checked < 1:
|
|
|
|
return last_status, last_details
|
|
|
|
output = subprocess.check_output(['lpstat', '-p', '-d'])
|
|
|
|
mark = False
|
|
|
|
for line in output.split('\n'):
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith('printer DYMO_LabelWriter_450'):
|
2016-12-16 00:13:25 +00:00
|
|
|
if 'is idle.' in line:
|
|
|
|
return True, 'Idle'
|
2015-08-29 23:46:34 +00:00
|
|
|
mark = True
|
|
|
|
continue
|
|
|
|
if mark:
|
|
|
|
if line.startswith('Ready to print'):
|
|
|
|
app.health = (time.time(), True, line)
|
|
|
|
return True, line
|
|
|
|
else:
|
|
|
|
app.health = (time.time(), False, line)
|
|
|
|
return False, line
|
|
|
|
mark = False
|
|
|
|
|
|
|
|
@app.route('/health')
|
|
|
|
def health():
|
|
|
|
ok, details = healthcheck()
|
|
|
|
return json.dumps({'ok': ok, 'details': details})
|
|
|
|
|
2015-08-29 22:46:51 +00:00
|
|
|
@app.route('/stuff/preview/<int:size>/')
|
|
|
|
def stuff_preview(size):
|
|
|
|
text = flask.request.args.get('text')
|
2016-10-14 00:16:37 +00:00
|
|
|
html = flask.request.args.get('html') == '1'
|
2015-08-29 22:46:51 +00:00
|
|
|
r = Renderer()
|
2016-10-14 00:16:37 +00:00
|
|
|
r.render_text(text, 'Sans {}'.format(size), 0, -1, html)
|
2015-08-29 22:46:51 +00:00
|
|
|
|
|
|
|
sio = StringIO.StringIO()
|
|
|
|
r.surface.write_to_png(sio)
|
|
|
|
sio.seek(0)
|
|
|
|
return flask.send_file(sio, mimetype='image/png')
|
|
|
|
|
|
|
|
DELAY = 5
|
|
|
|
@app.route('/stuff/print/<int:size>/', methods=['POST'])
|
|
|
|
def stuff_print(size):
|
2015-08-29 23:46:34 +00:00
|
|
|
if not healthcheck()[0]:
|
|
|
|
return 'Printer is down.'
|
2015-08-29 22:46:51 +00:00
|
|
|
last = app.last
|
|
|
|
print last, time.time() - last
|
|
|
|
if time.time() - last < DELAY:
|
|
|
|
return 'Please wait {} more seconds before next print.'.format(int(DELAY - (time.time() - last)))
|
|
|
|
text = flask.request.args.get('text')
|
2016-10-14 00:16:37 +00:00
|
|
|
html = flask.request.args.get('html') == '1'
|
2015-08-29 22:46:51 +00:00
|
|
|
r = Renderer()
|
2016-10-14 00:16:37 +00:00
|
|
|
r.render_text(text, 'Sans {}'.format(size), 0, -1, html)
|
2015-08-29 22:46:51 +00:00
|
|
|
path = '/tmp/hslabel'
|
|
|
|
f = open(path, 'w')
|
|
|
|
|
|
|
|
r.surface.write_to_png(f)
|
|
|
|
f.flush()
|
|
|
|
f.close()
|
|
|
|
time.sleep(1)
|
|
|
|
ex = 'lpr -P DYMO_LabelWriter_450 {}'.format(path)
|
|
|
|
os.system(ex)
|
|
|
|
|
|
|
|
f.close()
|
|
|
|
app.last = time.time()
|
|
|
|
return 'ok'
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(debug=True)
|