-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnappo.py
executable file
·115 lines (90 loc) · 4.32 KB
/
snappo.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
import signal
import platform
from ui_abstract import *
PLATFORM_OS = platform.system()
if PLATFORM_OS == 'Darwin':
import rumps
from ui_macos import Snappo, ImageResolver, Notification
rumps.debug_mode(True)
BASH_FILE="macos/snappo.sh"
else:
from ui_gnome import Snappo, ImageResolver, Notification
BASH_FILE="linux/snappo.sh"
BASH_PATH = os.path.dirname(os.path.abspath(__file__)) + "/" + BASH_FILE
class ImageChangingNotifier:
image_changing_notifier = None
def __init__(self):
pass
def set_image_changing_notifier(self, image_changing_notifier):
self.image_changing_notifier = image_changing_notifier
def get_image_changing_notifier(self):
return self.image_changing_notifier
def _has_method(self, instance, method):
method_list = dir(instance)
if method in method_list:
return True
return False
def set_icon(self, icon):
if self.image_changing_notifier is not None:
if self._has_method(self.image_changing_notifier, 'set_image'):
self.image_changing_notifier.set_image(icon)
if self._has_method(self.image_changing_notifier, 'set_icon'):
self.image_changing_notifier.set_icon(icon)
class ClipBoardManager:
def __init__(self, notification_manager, image_resolver, image_changing_notifier):
self.notification_manager = notification_manager
self.image_resolver = image_resolver
self.image_changing_notifier = image_changing_notifier
self.BASH_PATH = BASH_PATH
def copy(self, widget):
ret = os.system(self.BASH_PATH+" copy")
if ret != 0:
self.notification_manager.new("Clipboard Error.", "The clipboard seems empty, grab an image first.").show()
def clear(self, sender):
ret = os.system(self.BASH_PATH + " clear")
self.image_changing_notifier.set_icon(self.image_resolver.get_noimg_icon())
class ScreenGrabber:
def __init__(self, notification_manager, image_resolver, image_changing_notifier):
self.notification_manager = notification_manager
self.image_resolver = image_resolver
self.image_changing_notifier = image_changing_notifier
self.BASH_PATH = BASH_PATH
def grab(self, what, delay=0, which_desktop=""):
if what in ['area', 'desktop', 'window']:
os.system(self.BASH_PATH + " " + what + " " + str(int(delay)) + " " + which_desktop)
thumb = self.image_resolver.get_lens_icon()
self.image_changing_notifier.set_icon(thumb)
if PLATFORM_OS != 'Darwin':
self.notification_manager.new("Grab complete.", "The snapshot is in the clipboard.").show()
def ocr(self, widget):
ret = os.system(self.BASH_PATH + " ocr")
if ret == 0:
self.notification_manager.new("OCR Completed.", "You can now press CTRL+V to paste text.").show()
else:
self.notification_manager.new("OCR Error.", "The clipboard seems empty, grab an image first.").show()
def display(self,widget):
ret = os.system(self.BASH_PATH + " display")
if ret != 0:
self.notification_manager.new("Clipboard Error.", "The clipboard seems empty, grab an image first.").show()
def copy_to(self,filename):
ret = os.system(self.BASH_PATH + " copyto \""+filename+"\"")
if ret >> 8 == 2:
self.notification_manager.new("Save Error.", "The file could not be saved, check permissions.").show()
if ret >> 8 == 1:
self.notification_manager.new("Clipboard Error.", "The clipboard seems empty, grab an image first.").show()
def main(version):
#signal.signal(signal.SIGINT, signal.SIG_DFL)
notification_manager = Notification()
image_resolver = ImageResolver()
image_changing_notifier = ImageChangingNotifier()
clipboard_manager = ClipBoardManager(notification_manager, image_resolver, image_changing_notifier)
screen_grabber = ScreenGrabber(notification_manager, image_resolver, image_changing_notifier)
app = Snappo(version, screen_grabber, clipboard_manager, notification_manager, image_resolver, image_changing_notifier)
app.run()
if __name__ == '__main__':
version = ""
with open(os.path.dirname(__file__)+'/VERSION', 'r') as f:
version=f.read()
f.close()
main(version)