1
0
Fork 0

Add decorator ctrlc_commit to take care of this try-except.

This commit is contained in:
voussoir 2020-11-06 23:29:54 -08:00
parent 955e1e3e0a
commit 1c3fa864e9

View file

@ -52,6 +52,14 @@ sql.executescript(DB_INIT)
# HELPERS ########################################################################################## # HELPERS ##########################################################################################
def ctrlc_commit(function):
def wrapped(*args, **kwargs):
try:
function(*args, **kwargs)
except KeyboardInterrupt:
commit()
return wrapped
def int_or_none(x): def int_or_none(x):
if x is None: if x is None:
return x return x
@ -335,6 +343,7 @@ update_items:
DOCSTRING = betterhelp.add_previews(DOCSTRING, SUB_DOCSTRINGS) DOCSTRING = betterhelp.add_previews(DOCSTRING, SUB_DOCSTRINGS)
@ctrlc_commit
def get_argparse(args): def get_argparse(args):
lower = args.lower or 1 lower = args.lower or 1
upper = args.upper or get_latest_id() upper = args.upper or get_latest_id()
@ -342,32 +351,26 @@ def get_argparse(args):
ids = range(lower, upper+1) ids = range(lower, upper+1)
items = get_items(ids, threads=args.threads) items = get_items(ids, threads=args.threads)
try: insert_items(items, commit_period=args.commit_period)
insert_items(items, commit_period=args.commit_period)
except KeyboardInterrupt:
commit()
@ctrlc_commit
def livestream_argparse(args): def livestream_argparse(args):
try: insert_items(livestream(), commit_period=args.commit_period)
insert_items(livestream(), commit_period=args.commit_period)
except KeyboardInterrupt:
commit()
@ctrlc_commit
def update_argparse(args): def update_argparse(args):
try: while True:
while True: lower = select_latest_id() or 1
lower = select_latest_id() or 1 upper = get_latest_id()
upper = get_latest_id() if lower == upper:
if lower == upper: break
break
ids = range(lower, upper+1) ids = range(lower, upper+1)
items = get_items(ids, threads=args.threads) items = get_items(ids, threads=args.threads)
insert_items(items, commit_period=args.commit_period) insert_items(items, commit_period=args.commit_period)
except KeyboardInterrupt:
commit()
@ctrlc_commit
def update_items_argparse(args): def update_items_argparse(args):
seconds = args.days * 86400 seconds = args.days * 86400
if args.only_mature: if args.only_mature:
@ -384,10 +387,7 @@ def update_items_argparse(args):
ids = [id for (id,) in ids] ids = [id for (id,) in ids]
items = get_items(ids, threads=args.threads) items = get_items(ids, threads=args.threads)
try: insert_items(items, commit_period=args.commit_period)
insert_items(items, commit_period=args.commit_period)
except KeyboardInterrupt:
commit()
def main(argv): def main(argv):
argv = vlogging.set_level_by_argv(log, argv) argv = vlogging.set_level_by_argv(log, argv)