-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipto.py
executable file
·98 lines (78 loc) · 2.11 KB
/
clipto.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Clipto main script and Plugin sample
"""
from __future__ import print_function
import sys
try:
import tkinter
except ImportError:
import Tkinter as tkinter
class StopProcess(Exception):
"""Exception to stop execution process
"""
class CliptoPlugin:
"""CliptoPlugin interface
"""
name = None
def process(self, content):
"""Process content of clipboard
:params any content: clipboard content
:rtype: NoneType
:return: None
:raises StopProcess: to break the process execution list
"""
raise NotImplementedError
class EchoPlugin(CliptoPlugin):
"""Echo clipboard content as an introduction plugin
"""
name = "Echo"
def process(self, content):
"""Echoes the content of clipboard
"""
if content:
print(content)
class Clipboard:
"""Clipboard main class
"""
def __init__(self):
self.tkinter = tkinter.Tk()
self.tkinter.withdraw()
self.last_content = ''
self.registry = []
def watch(self):
"""Watch clipboard content
"""
try:
content = self.tkinter.clipboard_get()
if content != self.last_content:
self.last_content = content
for plugin in self.registry:
try:
plugin().process(content)
except StopProcess:
break
except Exception:
pass
except KeyboardInterrupt:
print("\b\bBye ;-)")
sys.exit()
except tkinter.TclError:
pass
self.tkinter.after(100, self.watch)
def run(self):
"""Exexcute main loop
"""
try:
self.tkinter.after(100, self.watch)
self.tkinter.mainloop()
except KeyboardInterrupt:
print("\b\bBye ;-)")
except Exception:
pass
if __name__ == '__main__':
clipto = Clipboard()
clipto.registry = [
EchoPlugin
]
clipto.run()