Commit a170ead2 authored by Maxime Bombar's avatar Maxime Bombar

[re2o-printer] Last commit before prod

parent b61b3779
......@@ -4,18 +4,20 @@
from configparser import ConfigParser
import socket
import sys
import sys, os
from re2oapi import Re2oAPIClient
import subprocess
DEBUG = True
DEBUG = False
LOCATION = '/re2o/media/'
# LOCATION = '/re2o/media/'
LOCATION = '/var/impressions/'
path = os.path.dirname(os.path.abspath(__file__))
config = ConfigParser()
config.read('config.ini')
config.read(path+'/config.ini')
api_hostname = config.get('Re2o', 'hostname')
api_password = config.get('Re2o', 'password')
......@@ -26,6 +28,13 @@ api_client = Re2oAPIClient(api_hostname, api_username, api_password)
client_hostname = socket.gethostname().split('.', 1)[0]
class InvalidPDF(Exception):
"""
"""
def __init__(self, message):
super().__init__(message)
def get_path(filefield):
"""
"""
......@@ -40,7 +49,7 @@ def HP_name(name):
return '2Staples%s' % name
def send(filepath, filename, number, settings):
def send(filepath, filename, number, pages, settings, debug):
"""
"""
......@@ -97,11 +106,16 @@ def send(filepath, filename, number, settings):
if not(perforation == 'None'):
cmd += ['-o', 'HPStaplerOptions=%s' % perforation]
# Format booklet
if booklet:
filepath = pdfbook(filepath, pages)
# The End
cmd.append('--')
cmd.append(filepath)
if DEBUG:
if debug:
toprint = ''
for k in cmd:
toprint += k
......@@ -129,15 +143,87 @@ def extract(job):
filepath = get_path(job['file'])
filename = job['filename']
number = job['count']
return(filepath, filename, number, options)
pages = job['pages']
return(filepath, filename, number, pages, options)
import math
import itertools
def booklet_order(n):
page = (lambda k: None if k > n else k)
recto = 2*int(math.ceil(n/4.))
verso = recto + 2
while recto > 1:
yield page(verso)
yield page(recto-1)
yield page(recto)
yield page(verso-1)
recto -= 2
verso += 2
def pdfjam_order(n):
return ",".join(map(lambda k: str(k or '{}'), booklet_order(n)))
def pdfbook(filepath, pages):
"""
Creates a booklet from a pdf
requires texlive-extra-utils
"""
_dir = os.path.dirname(filepath)
_fname = os.path.basename(filepath)
newfile = os.path.join(_dir, "pdfbook_%s" % (_fname,))
subprocess.check_output(
['/usr/bin/pdfjam',
'--no-landscape',
filepath,
pdfjam_order(pages),
'-o', newfile,
])
return newfile
def check(filepath):
"""
Check if the pdf is valid according to PDF spec.
Requires qpdf
"""
proc = subprocess.Popen(['qpdf', '--check', filepath],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
res = proc.communicate()
if proc.returncode:
raise InvalidPDF(res[0].decode('utf-8').split('\n')[-2])
if '--debug' is sys.argv:
DEBUG = True
if '--history' in sys.argv:
for job in api_client.list("printer/history-jobs"):
filepath, filename, number, options = extract(job)
send(filepath, filename, number, options)
try:
check(filepath)
send(filepath, filename, number, options)
print('\n')
except InvalidPDF as e:
print('Invalid PDF. Please resend a well formatted PDF')
print(e)
print('\n')
else:
for job in api_client.list("printer/printable-jobs"):
filepath, filename, number, options = extract(job)
send(filepath, filename, number, options)
api_client.patch(job['api_url'], data={'status': 'Running'})
filepath, filename, number, pages, options = extract(job)
try:
check(filepath)
send(filepath, filename, number, pages, options, debug=False)
print('\n')
api_client.patch(job['api_url'], data={'status': 'Running'})
except InvalidPDF as e:
print('Invalid PDF. Please resend a well formatted PDF')
print(e)
print('\n')
api_client.patch(job['api_url'], data={'status': 'Cancelled'})
print('Printing of %s Cancelled' % (filename,))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment