-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminiclip.py
executable file
·158 lines (129 loc) · 4.73 KB
/
miniclip.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2012 Hoang Minh Thang <p[at]banphim[dot]net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import pygtk, gtk, pynotify
pygtk.require('2.0')
from subprocess import Popen, PIPE
def pipeData(command, data):
sub = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = sub.communicate(data)
rc = sub.returncode
return [out, err, rc]
convertCommands = {}
convertCommands["jade"] = ['html2jade']
convertCommands["coffee"] = ['js2coffee']
convertCommands["stylus"] = ['stylus', '-C']
# Eg: out, err, rc = pipeData(convertCommands["stylus"], css_code)
display=gtk.gdk.display_get_default()
def getSelection(clipboard_type):
return gtk.Clipboard(display, clipboard_type).wait_for_text()
def setClipboard(text):
clipboard = gtk.Clipboard(display, "CLIPBOARD")
clipboard.set_text(text)
clipboard.store()
def notify(title, content=""):
pynotify.Notification(title, content).show()
def notEmptyString(text):
return isinstance(text, str) or isinstance(text, unicode) and text != ""
def run_action(clipboard_type, name):
text = getSelection(clipboard_type)
if notEmptyString(text):
out, err, rc = pipeData(convertCommands[name], text)
if rc == 0:
if text == out:
notify("Nothing happened!")
else:
if notEmptyString(out):
setClipboard(out)
notify("Converted successfully!", out[:200])
else:
notify("Got an empty output...", "Please check your input!")
else:
notify("Error converting:", "Program exited with code: " + str(rc))
notify("Error output: ", err[:120])
else:
notify("No text found on clipboard...")
# default value:
clipboard_type = "CLIPBOARD"
class SystrayIconApp:
def __init__(self):
self.tray = gtk.StatusIcon()
self.tray.set_from_stock(gtk.STOCK_COPY)
self.tray.connect('popup-menu', self.on_right_click)
self.tray.connect('activate', self.on_left_click)
self.tray.set_tooltip(('Miniclip tray app'))
def on_right_click(self, icon, event_button, event_time):
self.make_menu(event_button, event_time)
def on_left_click(self, widget):
menu = gtk.Menu()
# show jade dialog
jade = gtk.MenuItem("HTML -> Jade")
jade.show()
menu.append(jade)
jade.connect('activate', self.show_jade_dialog)
# show stylus dialog
stylus = gtk.MenuItem("CSS -> Stylus")
stylus.show()
menu.append(stylus)
stylus.connect('activate', self.show_stylus_dialog)
# show coffee dialog
coffee = gtk.MenuItem("JS -> Coffee")
coffee.show()
menu.append(coffee)
coffee.connect('activate', self.show_coffee_dialog)
menu.popup(None, None, gtk.status_icon_position_menu,
0, 0, self.tray)
def make_menu(self, event_button, event_time):
menu = gtk.Menu()
# show about dialog
about = gtk.MenuItem("About")
about.show()
menu.append(about)
about.connect('activate', self.show_about_dialog)
# add quit item
quit = gtk.MenuItem("Quit")
quit.show()
menu.append(quit)
quit.connect('activate', gtk.main_quit)
menu.popup(None, None, gtk.status_icon_position_menu,
event_button, event_time, self.tray)
def show_about_dialog(self, widget):
about_dialog = gtk.AboutDialog()
about_dialog.set_destroy_with_parent (True)
about_dialog.set_icon_name ("edit-paste")
about_dialog.set_name('Miniclip')
about_dialog.set_version('0.2')
about_dialog.set_copyright("(C) 2012 Hoang Minh Thang")
about_dialog.set_comments(("""
A Linux tray app that quickly converts HTML to Jade, JS to Coffee and CSS to Stylus from clipboard
"""))
about_dialog.set_authors(['Hoang Minh Thang <[email protected]>'])
about_dialog.run()
about_dialog.destroy()
def show_jade_dialog(self, widget):
run_action(clipboard_type, "jade")
def show_stylus_dialog(self, widget):
run_action(clipboard_type, "stylus")
def show_coffee_dialog(self, widget):
run_action(clipboard_type, "coffee")
if __name__ == "__main__":
if not pynotify.init("Basics"):
sys.exit(1)
SystrayIconApp()
gtk.main()