-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcb.py
executable file
·367 lines (287 loc) · 9.75 KB
/
mcb.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#! /usr/bin/python3
# mcb.py - Saves and loads pieces of text to the clipboard
#
# Usage: python3 mcb.py [options]
#
# Options:
# -a, --add <content> <key> - Store <content> as <key>
# -c, --copy <key> - Copy <key>'s contents to clipboard for
# usage
# -d, --delete [<keys>] - Delete <keys> and associated content
# -i, --interactive - Use stdin to interactively add a key
# and content
# -l, --list - List all keys and their contents
# -p, --purge - Delete *ALL* keys and contents
import argparse
import logging
import os
import pyperclip
import shelve
import sys
import textwrap
logging.basicConfig(level=logging.DEBUG,
format='[%(levelname)s] %(filename)s:%(lineno)d\n%(message)s')
def initialize_shelf(filename: str) -> int:
"""
Create a shelf file.
Create an empty shelf with the specified filename.
Parameters:
filename: the string to use as the shelf's name
Return:
0 -- shelf created successfully
1 -- shelf with name 'filename' already exists
2 -- other -- see stack trace
"""
rc: int = 0
try:
if (not os.path.exists(filename)):
shelf = shelve.open(filename)
shelf.close()
elif (os.path.exists(filename)):
logging.error("Shelf '%s' already exists.", filename)
rc = 1
except:
logging.error("Unkown error. Stacktrace: %s", sys.exc_info())
rc = 2
finally:
return rc
def add(filename: "shelf", content: str, key: str) -> bool:
"""
Add the specified contents to the shelf.
Add the content to the specified shelf, saved as the specified key.
Parameters:
filename: The filename of the shelf to save to
key: The string to use as the key
content: The actual content to save
Return:
True if successful, False otherwise
"""
rc: bool = False
if (key in filename.keys()):
print(f"Key {key} already exists.")
print(f"It contains: {filename[key]}")
print("If you want to overwrite the contents, press ENTER.")
print("Otherwise, press CTRL-C to abort.")
try:
input()
filename[key] = content
rc = True
except KeyboardInterrupt:
print("\nAborted.")
except:
logging.error("%s", sys.exc_info())
rc = False
finally:
return rc
def copy(filename: "shelf", key: str) -> int:
"""
Copy the contents of the specified key.
Parameters:
filename: the filename of the shelf to retrieve fromt
key: The key to retrieve and copy from
Return:
0 -- key found, contents copied successfully
1 -- key not found
2 -- Operation aborted with CTRL-C
"""
rc: int = 0
print(f"Key {key} contains the following:")
print(f"{filename[key]}\n")
print("Is this what you want?")
print("Press ENTER to proceed.")
try:
input("Otherwise, press CTRL-C to abort.\n")
pyperclip.copy(filename[key])
except KeyboardInterrupt:
print("\nAborted.")
rc = 2
except KeyError:
logging.error("'%s' not found in %s. :( Did you mean another key?", key, filename)
rc = 1
finally:
return rc
def confirm_delete(filename: "shelf", key: str) -> bool:
"""
Display message to confirm key deletion.
Parameters:
filename: the shelf where the key exists
key: the key pending deletion
Return:
True if the key is to be deleted, False otherwise
"""
rc: bool = False
confirm: str = input(f"Delete {key} from shelf? (y/n) ")
if (confirm[0].lower() == 'y'):
rc = True
return rc
def delete_key(filename: "shelf", key: str) -> int:
"""
Delete a key from the shelf.
Delete the specified key from the shelf.
Parameters:
filename: the filename of the shelf where the key exists
key: the key to delete
Return:
0 -- Key found and successfully deleted
1 -- Key not found
"""
rc: int = 0
try:
filename.pop(key)
except KeyError:
logging.error("'%s' not found in '%s' :(. Did you mean a different key?",
key, filename)
rc = 1
finally:
return rc
def get_interactive(filename: "shelf") -> None:
"""
Interactively prompt user for a key and content.
Use stdin to interactively get a key and content from the user. If the user
specifies a key that already exists, will ask for confirmation before
overwriting the old contents.
Parameters:
filename: the shelf to check in, and save to
Return:
None. Because the all information is coming from stdin, copying is not
involved. Additionally, the user is prompted if the key they want
already exists -- they overwrite at their own discretion.
"""
content: str = input("Enter the text you want to save: ")
key: str = input("Enter the key you want to save it as: ")
overwrite: str = "y"
if (key in filename.keys()):
print(f"'{key}' already exists in the shelf")
print(f"content: '{filename[key]}'")
overwrite = input("Overwrite its contents? (y/n) ")
if (overwrite[0].lower() == 'n'):
print("Exiting...")
return
else:
filename[key] = content
else:
print()
print(f"Your key is {key} .")
print(f"Your content is {content} .")
print()
try:
print("If this is wrong, press CTRL-C to abort.")
input("Else, press ENTER\n")
filename[key] = content
except KeyboardInterrupt:
print("\nAborting...")
return
def list_keys(filename: "shelf") -> None:
"""
List all keys in the specified file
Parameters:
filename: the shelf to search and list
Return:
None
"""
all_keys = filename.keys()
if (len(all_keys) == 0):
print("There are no keys in the specified file.")
else:
for key in all_keys:
print(f"Key: {key}")
print(f"Contents: {filename[key]}")
print()
def purge(filename: "shelf") -> int:
"""
Delete all keys in the shelf.
Delete all keys in shelf specified by filename. Only prompts for
confirmation before the first delete operation.
Parameters:
filename: the shelf to purge
Return:
The number of keys deleted
"""
rc: int = 0
confirm: str = "n"
print("This will delete *ALL* keys and content from the shelf.")
print("Are you sure you want to do this? (y/n) ")
confirm = input("Blank is equivalent to yes.\n")
if (len(confirm) == 0 or confirm[0].lower() == "y"):
for key in filename.keys():
filename.pop(key)
rc += 1
else:
print("Purging aborted.")
return rc
def setup_parser() -> "ArgumentParser":
"""
Set up the option parser.
Return:
A reference to the created parser
"""
parser = argparse.ArgumentParser(usage="python3 %(prog)s [options]",
formatter_class=argparse.RawTextHelpFormatter,
description="Clipboard persistence -- save/load text from/to the clipboard"
)
parser.add_argument("-a", "--add", nargs=2,
metavar=('<content>', '<key>'),
help="Save <content> to the shelf as <key>\n \n"
)
parser.add_argument("-c", "--copy", metavar='<key>',
help="Copy <key>'s contents from the shelf to the clipboard\n \n"
)
parser.add_argument("-d", "--delete", nargs="+", metavar="<keys>",
help=textwrap.dedent("""\
Delete the selected keys. This should be a list
of keys separated by spaces \n \n
""")
)
parser.add_argument("-i", "--interactive",
action="store_true",
help=textwrap.dedent("""\
Interactively enter a key and content. This
differs from -a by allowing you to confirm
that you've entered things correctly, or abort
if not.\n \n""")
)
parser.add_argument("-l", "--list", action="store_true",
help="List all keys and content\n \n")
parser.add_argument("-p", "--purge", action="store_true",
help="Delete *ALL* keys and contents\n \n"
)
return parser
def main():
filename: str = "mcb"
if (not os.path.exists(filename)):
initialize_shelf(filename)
parser = setup_parser()
args = parser.parse_args()
if (len(sys.argv) == 1):
parser.parse_args(['-h'])
with shelve.open(filename) as shelf:
if (args.add):
content: str = args.add[0]
key: str = args.add[1]
rc: bool = add(shelf, content, key)
elif (args.copy):
key: str = args.copy
rc: int = copy(shelf, key)
if (rc == 1 or rc == 2):
list_keys(shelf)
elif (args.delete):
for key in args.delete:
go_ahead: bool = confirm_delete(shelf, key)
if (go_ahead):
rc: int = delete_key(shelf, key)
if (rc == 1):
list_keys(shelf)
elif (args.interactive):
get_interactive(shelf)
elif (args.list):
list_keys(shelf)
elif (args.purge):
rc: int = purge(shelf)
if (rc == 0):
print("It looks like you didn't need to do that...")
print("The shelf was already empty...")
else:
print("Purging successful.")
print(f"Deleted {rc} keys.")
if __name__ == "__main__":
main()