Commit 34aa5c76 authored by Maxime Bombar's avatar Maxime Bombar

[re2o-printer] refactor printer service

parent eedf9944
......@@ -10,7 +10,10 @@ from re2oapi import Re2oAPIClient
import subprocess
import argparse
DEBUG = False
VERBOSE = False
# LOCATION = '/re2o/media/'
LOCATION = '/var/impressions/'
......@@ -65,16 +68,18 @@ def HP_name_punch(name):
return "4HolePunchRight"
def send(filepath, filename, number, pages, settings, debug):
def send(filepath, filename, number, pages, settings, debug, verbose):
"""
"""
if debug:
verbose = True
filename = filename.replace(' ', '_')
booklet = (settings['disposition'] == 'Booklet')
cmd = ['lp', '-d', 'MFPM880-1', '-t', filename, '-n', str(number)]
cmd = ['lp', '-d', 'MFPM880-2', '-t', filename, '-n', str(number)]
cmd += ['-o', 'Collate=True']
......@@ -132,13 +137,16 @@ def send(filepath, filename, number, pages, settings, debug):
cmd.append('--')
cmd.append(filepath)
toprint = ''
for k in cmd:
toprint += k
toprint += ' '
if debug:
toprint = ''
for k in cmd:
toprint += k
toprint += ' '
print(toprint[:-1])
else:
if verbose:
print(toprint[:-1])
try:
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
......@@ -211,36 +219,49 @@ def 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 proc.returncode:
# raise InvalidPDF(res[0].decode('utf-8').split('\n')[-2])
if '--debug' is sys.argv:
DEBUG = True
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Gestion des jobs à envoyer à l'imprimante")
parser.add_argument('-d', '--debug', help="N'envoie pas les jobs à l'imprimante. Implique verbose.", action='store_true')
parser.add_argument('-v', '--verbose', help="Affiche les actions sur stdout", action="store_true")
parser.add_argument('-H', '--history', help="Récupère les anciens jobs", action='store_true')
if '--history' in sys.argv:
for job in api_client.list("printer/history-jobs"):
filepath, filename, number, options = extract(job)
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, 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,))
args = parser.parse_args()
if args.debug:
DEBUG = True
VERBOSE = True
if args.verbose:
VERBOSE = True
if args.history:
for job in api_client.list("printer/history-jobs"):
filepath, filename, number, pages, options = extract(job)
try:
check(filepath)
send(filepath, filename, number, pages, options, debug=DEBUG, verbose=VERBOSE)
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, pages, options = extract(job)
try:
check(filepath)
send(filepath, filename, number, pages, options, debug=DEBUG, verbose=VERBOSE)
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