Let watchforlinks take custom extension, use argparse.

This commit is contained in:
voussoir 2020-08-20 10:57:32 -07:00
parent ef3a4a830e
commit 95d2f10f79

View file

@ -1,38 +1,49 @@
import argparse
import pyperclip import pyperclip
import sys import sys
import time import time
from voussoirkit import passwordy from voussoirkit import passwordy
from voussoirkit import pathclass
def loop_once(): def loop_once(extension):
try: try:
text = pyperclip.paste() text = pyperclip.paste()
except Exception: except Exception:
return return
if '\n' in text: text = text.strip()
if len(text.split(sep=None, maxsplit=1)) > 1:
return return
if 'http://' in text or 'https://' in text: if 'http://' in text or 'https://' in text:
fn = passwordy.urandom_hex(12) + '.generic' path = pathclass.Path(passwordy.urandom_hex(12)).add_extension(extension)
pyperclip.copy('') pyperclip.copy('')
print(fn, text) print(path.basename, text)
h = open(fn, 'w', encoding='utf-8') h = open(path.absolute_path, 'w', encoding='utf-8')
h.write(text) h.write(text)
h.close() h.close()
def loop_forever(): def loop_forever(extension):
while True: while True:
loop_once() loop_once(extension=extension)
time.sleep(1) time.sleep(1)
def main(argv): def watchforlinks_argparse(args):
try: try:
loop_forever() loop_forever(extension=args.extension)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
return 0 def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('extension', nargs='?', default='generic')
parser.set_defaults(func=watchforlinks_argparse)
args = parser.parse_args(argv)
return args.func(args)
if __name__ == '__main__': if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:])) raise SystemExit(main(sys.argv[1:]))