timing debug prints
Dieser Commit ist enthalten in:
Ursprung
6f3e268672
Commit
8e83ba1a2d
1 geänderte Dateien mit 28 neuen und 0 gelöschten Zeilen
28
pdf-sign
28
pdf-sign
|
@ -8,8 +8,14 @@
|
||||||
|
|
||||||
import argparse, os, queue, re, subprocess, sys, tempfile, traceback, time
|
import argparse, os, queue, re, subprocess, sys, tempfile, traceback, time
|
||||||
|
|
||||||
|
from inspect import currentframe
|
||||||
|
starttime=time.time()
|
||||||
|
def tdbg():
|
||||||
|
print(f"========== DBG: line {currentframe().f_back.f_lineno}, time after start={time.time()-starttime:.3f} ==========", flush=True)
|
||||||
|
|
||||||
# Inspired by https://unix.stackexchange.com/a/141496
|
# Inspired by https://unix.stackexchange.com/a/141496
|
||||||
def main(args):
|
def main(args):
|
||||||
|
tdbg()
|
||||||
if not hasQpdf and not has("pdftk"):
|
if not hasQpdf and not has("pdftk"):
|
||||||
die("Needs either qpdf or pdftk installed")
|
die("Needs either qpdf or pdftk installed")
|
||||||
if not has("gs"):
|
if not has("gs"):
|
||||||
|
@ -19,7 +25,9 @@ def main(args):
|
||||||
filePath=args.input
|
filePath=args.input
|
||||||
if not isPdfFilename(filePath):
|
if not isPdfFilename(filePath):
|
||||||
die("Input file must end with .pdf (case insensitive)")
|
die("Input file must end with .pdf (case insensitive)")
|
||||||
|
tdbg()
|
||||||
with tempfile.TemporaryDirectory() as tempdir:
|
with tempfile.TemporaryDirectory() as tempdir:
|
||||||
|
tdbg()
|
||||||
intmp=lambda fileName: Volatile(os.path.join(tempdir, fileName))
|
intmp=lambda fileName: Volatile(os.path.join(tempdir, fileName))
|
||||||
# Maybe flatten (make forms non-editable) before signing
|
# Maybe flatten (make forms non-editable) before signing
|
||||||
if args.flatten:
|
if args.flatten:
|
||||||
|
@ -36,6 +44,7 @@ def main(args):
|
||||||
])
|
])
|
||||||
else:
|
else:
|
||||||
inputPDF=filePath
|
inputPDF=filePath
|
||||||
|
tdbg()
|
||||||
# The chosen page
|
# The chosen page
|
||||||
pageCount=pdfCountPages(inputPDF)
|
pageCount=pdfCountPages(inputPDF)
|
||||||
if args.page < -pageCount or args.page==0 or pageCount < args.page:
|
if args.page < -pageCount or args.page==0 or pageCount < args.page:
|
||||||
|
@ -54,6 +63,7 @@ def main(args):
|
||||||
return outFile
|
return outFile
|
||||||
pageSize=Cell(lambda: pdfGetSize(str(pagePDF())), 'pageSize')
|
pageSize=Cell(lambda: pdfGetSize(str(pagePDF())), 'pageSize')
|
||||||
# The chosen signature
|
# The chosen signature
|
||||||
|
tdbg()
|
||||||
if not args.signature and args.batch:
|
if not args.signature and args.batch:
|
||||||
die('In batch mode, signature must be specified.')
|
die('In batch mode, signature must be specified.')
|
||||||
signatureDir=getSignatureDir()
|
signatureDir=getSignatureDir()
|
||||||
|
@ -67,6 +77,7 @@ def main(args):
|
||||||
signaturePositionX=Cell(args.x_coordinate, 'signaturePositionX')
|
signaturePositionX=Cell(args.x_coordinate, 'signaturePositionX')
|
||||||
signaturePositionY=Cell(args.y_coordinate, 'signaturePositionY')
|
signaturePositionY=Cell(args.y_coordinate, 'signaturePositionY')
|
||||||
signatureScale=Cell(0, 'signatureScale')
|
signatureScale=Cell(0, 'signatureScale')
|
||||||
|
tdbg()
|
||||||
@Cell
|
@Cell
|
||||||
def signaturePositionedPDF():
|
def signaturePositionedPDF():
|
||||||
(w, h)=pageSize()
|
(w, h)=pageSize()
|
||||||
|
@ -109,6 +120,7 @@ def main(args):
|
||||||
def displayPNG():
|
def displayPNG():
|
||||||
(w, h)=displaySize()
|
(w, h)=displaySize()
|
||||||
outFile=intmp('display.png')
|
outFile=intmp('display.png')
|
||||||
|
tdbg()
|
||||||
subprocess.run([
|
subprocess.run([
|
||||||
'gs', '-dBATCH', '-dNOPAUSE', '-dSAFER', '-dQUIET',
|
'gs', '-dBATCH', '-dNOPAUSE', '-dSAFER', '-dQUIET',
|
||||||
f'-sOutputFile={outFile}',
|
f'-sOutputFile={outFile}',
|
||||||
|
@ -117,11 +129,13 @@ def main(args):
|
||||||
f'-dDEVICEWIDTHPOINTS={w}', f'-dDEVICEHEIGHTPOINTS={h}', '-dFIXEDMEDIA', '-dPDFFitPage',
|
f'-dDEVICEWIDTHPOINTS={w}', f'-dDEVICEHEIGHTPOINTS={h}', '-dFIXEDMEDIA', '-dPDFFitPage',
|
||||||
'-f', str(signedPagePDF()),
|
'-f', str(signedPagePDF()),
|
||||||
], check=True)
|
], check=True)
|
||||||
|
tdbg()
|
||||||
return outFile
|
return outFile
|
||||||
# GUI
|
# GUI
|
||||||
doSign=True
|
doSign=True
|
||||||
gui=not args.batch
|
gui=not args.batch
|
||||||
if gui:
|
if gui:
|
||||||
|
tdbg()
|
||||||
try:
|
try:
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
|
@ -171,7 +185,9 @@ def main(args):
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
tk.Tk.report_callback_exception = tkerror
|
tk.Tk.report_callback_exception = tkerror
|
||||||
# Window and menu
|
# Window and menu
|
||||||
|
tdbg()
|
||||||
root = tk.Tk(className="pdf-sign")
|
root = tk.Tk(className="pdf-sign")
|
||||||
|
tdbg()
|
||||||
rootmenu = tk.Menu(root)
|
rootmenu = tk.Menu(root)
|
||||||
root.config(menu=rootmenu)
|
root.config(menu=rootmenu)
|
||||||
filemenu = tk.Menu(rootmenu, tearoff=0)
|
filemenu = tk.Menu(rootmenu, tearoff=0)
|
||||||
|
@ -242,9 +258,12 @@ def main(args):
|
||||||
root.bind(f'{char}', onkey)
|
root.bind(f'{char}', onkey)
|
||||||
for i, char in enumerate("123456789"): bindDigit(i, char)
|
for i, char in enumerate("123456789"): bindDigit(i, char)
|
||||||
# Canvas and click binding
|
# Canvas and click binding
|
||||||
|
tdbg()
|
||||||
root._docView=tk.Canvas(root, borderwidth=0, background='#ffffff')
|
root._docView=tk.Canvas(root, borderwidth=0, background='#ffffff')
|
||||||
|
tdbg()
|
||||||
docViewMargin=5
|
docViewMargin=5
|
||||||
docViewMinDimension=50
|
docViewMinDimension=50
|
||||||
|
tdbg()
|
||||||
def onRootResize(event):
|
def onRootResize(event):
|
||||||
rootWidth=root.winfo_width()
|
rootWidth=root.winfo_width()
|
||||||
rootHeight=root.winfo_height()
|
rootHeight=root.winfo_height()
|
||||||
|
@ -260,6 +279,7 @@ def main(args):
|
||||||
initWinSize=(min(initWinSize[0], initWinSize[1] * pageSize()[0] / pageSize()[1]),
|
initWinSize=(min(initWinSize[0], initWinSize[1] * pageSize()[0] / pageSize()[1]),
|
||||||
min(initWinSize[1], initWinSize[0] * pageSize()[1] / pageSize()[0]))
|
min(initWinSize[1], initWinSize[0] * pageSize()[1] / pageSize()[0]))
|
||||||
root.geometry(f"{int(initWinSize[0])}x{int(initWinSize[1])}")
|
root.geometry(f"{int(initWinSize[0])}x{int(initWinSize[1])}")
|
||||||
|
tdbg()
|
||||||
@Cell
|
@Cell
|
||||||
def updateTitle():
|
def updateTitle():
|
||||||
root.title(f'Signing page {pageNumber()}/{pageCount} of {filePath}')
|
root.title(f'Signing page {pageNumber()}/{pageCount} of {filePath}')
|
||||||
|
@ -278,14 +298,18 @@ def main(args):
|
||||||
updateActive = True
|
updateActive = True
|
||||||
update()
|
update()
|
||||||
root.after(100, soonAfterStart)
|
root.after(100, soonAfterStart)
|
||||||
|
tdbg()
|
||||||
@tkthrottle(100, root)
|
@tkthrottle(100, root)
|
||||||
def update():
|
def update():
|
||||||
|
tdbg()
|
||||||
if not updateActive:
|
if not updateActive:
|
||||||
return
|
return
|
||||||
(w, h) = displaySize()
|
(w, h) = displaySize()
|
||||||
filename = str(displayPNG())
|
filename = str(displayPNG())
|
||||||
before = time.time()
|
before = time.time()
|
||||||
|
tdbg()
|
||||||
root._docImg = tk.PhotoImage(file=filename)
|
root._docImg = tk.PhotoImage(file=filename)
|
||||||
|
tdbg()
|
||||||
print(f"Debug: Loading photo image took {time.time()-before:.2f} seconds", flush=True)
|
print(f"Debug: Loading photo image took {time.time()-before:.2f} seconds", flush=True)
|
||||||
root._docView.itemconfig(root._docViewIndex, image=root._docImg)
|
root._docView.itemconfig(root._docViewIndex, image=root._docImg)
|
||||||
root._docView.configure(width=w, height=h)
|
root._docView.configure(width=w, height=h)
|
||||||
|
@ -293,6 +317,7 @@ def main(args):
|
||||||
if not args.signature:
|
if not args.signature:
|
||||||
if root.signatureControlVar.get() != signatureIndex():
|
if root.signatureControlVar.get() != signatureIndex():
|
||||||
root.signatureControlVar.set(signatureIndex())
|
root.signatureControlVar.set(signatureIndex())
|
||||||
|
tdbg()
|
||||||
def dbg(event, x):
|
def dbg(event, x):
|
||||||
print(f"Debug: in dbg(event) for {x}: char={event.char}, delta={event.delta}, height={event.height}, keycode={event.keycode}, keysym={event.keysym}, keysym_num={event.keysym_num}, num={event.num}, send_event={event.send_event}, serial={event.serial}, state={event.state}, time={event.time}, type={event.type}, widget={event.widget}, width={event.width}, x={event.x}, x_root={event.x_root}, y={event.y}, y_root={event.y_root}")
|
print(f"Debug: in dbg(event) for {x}: char={event.char}, delta={event.delta}, height={event.height}, keycode={event.keycode}, keysym={event.keysym}, keysym_num={event.keysym_num}, num={event.num}, send_event={event.send_event}, serial={event.serial}, state={event.state}, time={event.time}, type={event.type}, widget={event.widget}, width={event.width}, x={event.x}, x_root={event.x_root}, y={event.y}, y_root={event.y_root}")
|
||||||
def dbgfun(x):
|
def dbgfun(x):
|
||||||
|
@ -303,6 +328,7 @@ def main(args):
|
||||||
root._docView.bind('<Button>', dbgfun("root._docView <Button>"))
|
root._docView.bind('<Button>', dbgfun("root._docView <Button>"))
|
||||||
root.bind('<KeyPress>', dbgfun("root <KeyPress>"))
|
root.bind('<KeyPress>', dbgfun("root <KeyPress>"))
|
||||||
root.bind('<Button>', dbgfun("root <Button>"))
|
root.bind('<Button>', dbgfun("root <Button>"))
|
||||||
|
tdbg()
|
||||||
def onclick(event):
|
def onclick(event):
|
||||||
print(f"Debug: in onclick(event): char={event.char}, delta={event.delta}, height={event.height}, keycode={event.keycode}, keysym={event.keysym}, keysym_num={event.keysym_num}, num={event.num}, send_event={event.send_event}, serial={event.serial}, state={event.state}, time={event.time}, type={event.type}, widget={event.widget}, width={event.width}, x={event.x}, x_root={event.x_root}, y={event.y}, y_root={event.y_root}")
|
print(f"Debug: in onclick(event): char={event.char}, delta={event.delta}, height={event.height}, keycode={event.keycode}, keysym={event.keysym}, keysym_num={event.keysym_num}, num={event.num}, send_event={event.send_event}, serial={event.serial}, state={event.state}, time={event.time}, type={event.type}, widget={event.widget}, width={event.width}, x={event.x}, x_root={event.x_root}, y={event.y}, y_root={event.y_root}")
|
||||||
x=event.x
|
x=event.x
|
||||||
|
@ -314,7 +340,9 @@ def main(args):
|
||||||
cmd_positionSignature(x/canvasWidth, y/canvasHeight)
|
cmd_positionSignature(x/canvasWidth, y/canvasHeight)
|
||||||
root._docView.bind('<Button-1>', onclick)
|
root._docView.bind('<Button-1>', onclick)
|
||||||
# Run GUI
|
# Run GUI
|
||||||
|
tdbg()
|
||||||
root.mainloop()
|
root.mainloop()
|
||||||
|
tdbg()
|
||||||
# End of GUI
|
# End of GUI
|
||||||
if doSign:
|
if doSign:
|
||||||
signedFilePath=args.output if args.output else f'{filePath[:-4]}.signed{filePath[-4:]}'
|
signedFilePath=args.output if args.output else f'{filePath[:-4]}.signed{filePath[-4:]}'
|
||||||
|
|
Laden …
Tabelle hinzufügen
In neuem Issue referenzieren