fix: rewrite filter as bash+awk to avoid Python/xcode-select dependency
This commit is contained in:
parent
5bd9a0277a
commit
118b0faca0
101
filter/kyofilter
101
filter/kyofilter
@ -1,65 +1,48 @@
|
||||
#!/usr/bin/python3
|
||||
"""CUPS filter for TA 3505ci - injects department code into PostScript stream."""
|
||||
import sys
|
||||
#!/bin/bash
|
||||
# CUPS filter for TA 3505ci - injects department code into PostScript stream.
|
||||
# Uses bash+awk only, no Python dependency.
|
||||
|
||||
if [ $# -lt 5 ]; then
|
||||
echo "Usage: kyofilter job-id user title copies options [file]" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
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
|
||||
OPTIONS="${5:-}"
|
||||
INFILE="${6:-}"
|
||||
|
||||
# Extract department code: KmManagment=MGxxxxx -> xxxxx
|
||||
CODE=""
|
||||
for OPT in $OPTIONS; do
|
||||
if [[ "$OPT" =~ ^KmManagment=MG([0-9]+)$ ]]; then
|
||||
CODE="${BASH_REMATCH[1]}"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
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 # raw digits, no zero-padding — matches kyofilter_E behaviour
|
||||
# Pure passthrough when no code selected
|
||||
if [ -z "$CODE" ]; then
|
||||
if [ -n "$INFILE" ]; then
|
||||
cat "$INFILE"
|
||||
else
|
||||
cat
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Inject PostScript command before %%EndSetup (fallback: before %%Page:)
|
||||
INJECT="($CODE) statusdict /setmanagementnumber get exec"
|
||||
INPUT="${INFILE:--}"
|
||||
|
||||
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.rstrip() == 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")
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 6:
|
||||
sys.stderr.write("Usage: kyofilter job-id user title copies options [file]\n")
|
||||
sys.exit(1)
|
||||
|
||||
options = parse_options(sys.argv[5])
|
||||
account_code = get_account_code(options)
|
||||
|
||||
infile = open(sys.argv[6], 'rb') if len(sys.argv) > 6 else sys.stdin.buffer
|
||||
try:
|
||||
for chunk in process_stream(infile, account_code):
|
||||
sys.stdout.buffer.write(chunk)
|
||||
finally:
|
||||
if len(sys.argv) > 6:
|
||||
infile.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
awk -v inject="$INJECT" '
|
||||
BEGIN { injected = 0 }
|
||||
!injected && /^%%EndSetup/ {
|
||||
print inject
|
||||
injected = 1
|
||||
}
|
||||
!injected && /^%%Page:/ {
|
||||
print inject
|
||||
injected = 1
|
||||
}
|
||||
{ print }
|
||||
END { if (!injected) print "kyofilter: WARNING: no injection point found" > "/dev/stderr" }
|
||||
' "$INPUT"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user