From 2f5c6f502d55c9201cf015d10db52acee65a3f05 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 12 Oct 2021 21:42:55 -0700 Subject: [PATCH] Add wait_for_internet.py. --- wait_for_internet.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 wait_for_internet.py diff --git a/wait_for_internet.py b/wait_for_internet.py new file mode 100644 index 0000000..340f57a --- /dev/null +++ b/wait_for_internet.py @@ -0,0 +1,38 @@ +''' +wait_for_internet +================= + +This program will block until internet access is available. It can be useful to +run this program before running another program that expects an internet +connection. + +> wait_for_internet timeout + +timeout: + An integer number of seconds, after which to give up and return 1. +''' +import argparse +import sys + +from voussoirkit import betterhelp +from voussoirkit import networktools +from voussoirkit import vlogging + +def wait_for_internet_argparse(args): + try: + networktools.wait_for_internet(timeout=args.timeout) + return 0 + except networktools.NoInternet: + return 1 + +@vlogging.main_decorator +def main(argv): + parser = argparse.ArgumentParser(description=__doc__) + + parser.add_argument('timeout', type=int) + parser.set_defaults(func=wait_for_internet_argparse) + + return betterhelp.single_main(argv, parser, __doc__) + +if __name__ == '__main__': + raise SystemExit(main(sys.argv[1:]))