48 lines
1.2 KiB
Python
Executable File
48 lines
1.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""CUPS filter for TA 3505ci - injects department account code into PostScript stream."""
|
|
import sys
|
|
|
|
|
|
def parse_options(options_str):
|
|
options = {}
|
|
for token in options_str.split():
|
|
if '=' in token:
|
|
k, v = token.split('=', 1)
|
|
options[k] = v
|
|
else:
|
|
options[token] = 'true'
|
|
return options
|
|
|
|
|
|
def get_account_code(options):
|
|
km = options.get('KmManagment', 'Default')
|
|
if km == 'Default' or not km.startswith('MG'):
|
|
return None
|
|
code = km[2:]
|
|
if not code.isdigit():
|
|
return None
|
|
return code.zfill(8)
|
|
|
|
|
|
def process_stream(lines, account_code):
|
|
if not account_code:
|
|
yield from lines
|
|
return
|
|
|
|
inject = f"({account_code}) statusdict /setmanagementnumber get exec\n".encode()
|
|
injected = False
|
|
|
|
for line in lines:
|
|
stripped = line.rstrip(b'\r\n')
|
|
if not injected and (stripped == b'%%EndSetup' or stripped.startswith(b'%%Page:')):
|
|
yield inject
|
|
injected = True
|
|
yield line
|
|
|
|
if not injected:
|
|
sys.stderr.write("kyofilter: WARNING: no injection point found in PostScript stream\n")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
pass
|