/
workclipmini.py
86 lines (70 loc) · 2.42 KB
/
workclipmini.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
# Author: Kevin Darlington
# License: MIT
# Version: 0.1
import threading
import os
import time
import re
import webbrowser
import louie
from twisted.internet import reactor
from twisted.internet import task
from network import Network
if os.name == 'posix':
try:
import linux as anyos
except:
import mac as anyos
if os.name == 'nt':
import windows as anyos
#-------------------------------
class WorkclipMini(object):
#===============================
def __init__(self):
self.clipboard = anyos.Clipboard()
self.hotkey = anyos.Hotkey()
louie.connect(self.on_hotkey, self.hotkey.event)
self.network = Network('192.168.0.255', 45644)
louie.connect(self.on_command, self.network.command)
self.hotkey.add_bind('CLIPBOARD', '<control><alt>C')
self.hotkey.add_bind('URL', '<control><alt>B')
chrome = os.path.join(os.environ.get('LOCALAPPDATA') or '', 'Google\\Chrome\\Application\\chrome.exe')
extra_browsers = [chrome, 'chrome.exe']
for browser in extra_browsers:
if webbrowser._iscommand(browser):
webbrowser.register(browser, None, webbrowser.BackgroundBrowser(browser), -1)
#===============================
def run(self):
thread = threading.Thread(target=self.loop)
thread.start()
self.hotkey.loop()
#===============================
def on_command(self, command, **kwargs):
command = command.split(' ', 1)
if command[0] == 'CLIPBOARD':
self.clipboard.text = command[1]
elif command[0] == 'URL':
webbrowser.open_new_tab(command[1])
#===============================
def on_hotkey(self, name, **kwargs):
self.clipboard.copy_selected_text()
text = self.clipboard.text
if name == 'CLIPBOARD':
for address in self.network.addresses:
self.network.send(address, name, text)
elif name == 'URL':
urls = re.findall(r'(([a-zA-Z]+://)?(www.)?[^ ]+\.[^ \n\r]{2,})', text, re.IGNORECASE)
if not urls:
urls = [[text,],]
for url in urls:
for address in self.network.addresses:
self.network.send(address, name, url[0])
#===============================
def loop(self):
reactor.listenUDP(45644, self.network)
refresh = task.LoopingCall(self.network.refresh_list)
refresh.start(30) # Refresh list at rate defined in config.py
reactor.run(False)
#===============================
if __name__ == '__main__':
WorkclipMini().run()