forked from stevenleeg/boo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboom.py
190 lines (162 loc) · 4.54 KB
/
boom.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import sys, os, pickle, md5
def to_clipboard(value):
if sys.platform.startswith('darwin'):
from AppKit import NSPasteboard
pb = NSPasteboard.generalPasteboard()
pb.clearContents()
pb.writeObjects_([value])
elif sys.platform.startswith('win32'):
import win32clipboard
win32clipboard.OpenClipboard()
win32clipboard.SetClipboardText(value)
win32clipboard.CloseClipboard()
class Boom:
def __init__(self):
self.boomfile = os.path.join(os.environ['HOME'], '.boom')
try:
f = open(self.boomfile, 'rb')
self.data = pickle.load(f)
f.close()
except (IOError, EOFError):
self.data = {}
self.save()
print("\033[94m [MEH] \033[0m .boom not found. I'll create it for you...")
def save(self):
f = open(self.boomfile, 'wb')
pickle.dump(self.data, f)
return True
def get(self, key):
try:
return self.data[key]
except KeyError:
return None
def set(self, key, value):
self.data[key] = value
self.save()
def list(self):
for key in self.data:
print(key)
def rm(self, key):
del(self.data[key])
self.save()
def mv(self, key, to):
self.data[to] = self.data[key]
del(self.data[key])
self.save()
def concat(self, key, value):
if not key in self.data:
return
self.data[key] = self.data[key] + value
self.save()
def main():
b = Boom()
if len(sys.argv) == 1:
print("Usage:\n Copy a key to clipboard: boo [key name] \n Setting a key: boo [key name] [value]")
return
# See if they specified a command as the first argument
if sys.argv[1] == '-a':
b.list()
return True
elif sys.argv[1] == '-m':
try:
b.mv(sys.argv[2], sys.argv[3])
print("\033[92m [OK!] \033[0m Key %s is now known as %s." % (sys.argv[2], sys.argv[3]))
except KeyError:
print("\033[91m [NAH] \033[0m Key '%s' does not exist" % sys.argv[2])
return True
# Delete?
elif sys.argv[1] == '-r':
try:
b.rm(sys.argv[2])
print("\033[92m [OK!] \033[0m Key %s has been removed." % (sys.argv[2]))
except KeyError:
print("\033[91m [NAH] \033[0m Key '%s' does not exist" % sys.argv[2])
return True
elif sys.argv[1] == "-p":
value = b.get(sys.argv[2])
if value is None:
print("\033[91m [NAH] \033[0m Key '%s' does not exist" % sys.argv[2])
return True
print value
return True
elif sys.argv[1] == "-e":
# edit value using EDITOR or vi.
try:
key = sys.argv[2]
filename = "/tmp/" + md5.md5(key).hexdigest()
value = b.get(key)
if value is None:
value = ""
tmpFile = open(filename, "w")
tmpFile.write(value)
tmpFile.close()
editor = os.environ["EDITOR"]
if not editor:
editor = "vi"
os.system(editor + " " + filename)
tmpFile = open(filename, "r")
value = tmpFile.read()
if value[-1:] == "\n" or value[-1:] == "\r":
value = value[:-1]
tmpFile.close()
os.remove(filename)
b.set(key, value)
print("\033[92m [OK!] \033[0m Key '%s' has been set to the contents of the edited file." % key)
except Exception as e:
print(e)
return True
elif sys.argv[1] == "-c": # concat
try:
key = sys.argv[2]
if sys.argv[3] == "-":
value = " ".join(sys.argv[4:])
value = value + "\n" + sys.stdin.read()
if value[-1:] == "\n" or value[-1:] == "\r":
value = value[:-1]
else:
value = " ".join(sys.argv[3:])
if not value:
# there's nothing to concat.
return True
if b.get(key) is None:
print("\033[92m [OK!] \033[0m Key '%s' does not exist... creating!" % key)
b.set(key, value)
else:
b.concat(key, value)
value = b.get(key)
if '\n' in value:
print("\033[92m [OK!] \033[0m Key '%s' now has value:\n%s" %(key, value))
else:
print("\033[92m [OK!] \033[0m Key '%s' now has value: %s" % (key, value))
except:
pass
return True
# Set?
try:
if sys.argv[2] == "-":
value = " ".join(sys.argv[3:])
value = value + "\n" + sys.stdin.read()
if value[-1:] == "\n" or value[-1:] == "\r":
value = value[:-1]
else:
value = " ".join(sys.argv[2:])
if len(value) == 0:
raise IndexError
b.set(sys.argv[1], value)
if '\n' in value:
print("\033[92m [OK!] \033[0m Key '%s' now has value:\n%s" % (sys.argv[1], value))
else:
print("\033[92m [OK!] \033[0m Key '%s' now has value: %s" % (sys.argv[1], value))
return
except IndexError:
pass
# Otherwise, let's just get the key and send it to the clipboard
value = b.get(sys.argv[1])
if value is None:
print("\033[91m [NAH] \033[0m Key '%s' does not exist" % sys.argv[1])
return
to_clipboard(value)
print("\033[92m [OK!] \033[0m Key '%s' copied to clipboard." % sys.argv[1])
return True
if __name__ == "__main__":
main()