-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCaptiOCR.py
More file actions
65 lines (57 loc) · 2.32 KB
/
Copy pathCaptiOCR.py
File metadata and controls
65 lines (57 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
"""
CaptiOCR - Main entry point
"""
import sys
import os
# Detach from the console on Windows when running from source.
# FreeConsole() only succeeds when a console is attached (python.exe); it is a no-op
# under pythonw.exe (no console). On success, redirect stdout/stderr to devnull so
# that subsequent print() calls do not raise [WinError 6] on the now-invalid handles.
if sys.platform == 'win32' and not hasattr(sys, '_MEIPASS'):
import ctypes
if ctypes.windll.kernel32.FreeConsole():
_devnull = open(os.devnull, 'w')
sys.stdout = _devnull
sys.stderr = _devnull
def main():
"""Main entry point for CaptiOCR."""
try:
# Ensure the captiocr module can be found
if hasattr(sys, '_MEIPASS'):
# Running in PyInstaller bundle
base_path = sys._MEIPASS
else:
# Running in development
base_path = os.path.dirname(os.path.abspath(__file__))
# Add base path to sys.path if not already there
if base_path not in sys.path:
sys.path.insert(0, base_path)
# Import and run the main application
from captiocr.main import main as app_main
app_main()
except Exception as e:
# Show error dialog
error_message = f"Failed to start CaptiOCR:\n\n{str(e)}"
try:
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.withdraw()
messagebox.showerror("CaptiOCR Error", error_message)
root.destroy()
except Exception as dialog_error:
# If dialog fails, create error file
try:
import traceback
with open("captiocr_error.txt", "w") as f:
f.write(f"CaptiOCR Error: {str(e)}\n\n")
f.write(f"Traceback:\n{traceback.format_exc()}\n\n")
f.write(f"Dialog error: {str(dialog_error)}\n")
print(f"Error logged to captiocr_error.txt: {error_message}", file=sys.stderr)
except Exception as file_error:
# Last resort - print to stderr
print(f"CRITICAL ERROR: {error_message}", file=sys.stderr)
print(f"Could not create error file: {file_error}", file=sys.stderr)
if __name__ == "__main__":
main()