From 0f128a91b2fa0748ff132a270917003f94de2201 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Tue, 30 Apr 2019 22:24:56 +0200 Subject: [PATCH] added lib/standalone.py for queries in standalone mode --- lib/standalone.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 lib/standalone.py diff --git a/lib/standalone.py b/lib/standalone.py new file mode 100644 index 0000000..91e6de8 --- /dev/null +++ b/lib/standalone.py @@ -0,0 +1,51 @@ +""" +Standalone wrapper for the cheat.sh server. +""" + +import sys +import textwrap + +import cheat_wrapper + +def show_usage(): + """ + Show how to use the program in the standalone mode + """ + + print textwrap.dedent(""" + Usage: + + lib/standalone.py [OPTIONS] QUERY + + For OPTIONS see :help + """)[1:-1] + +def parse_cmdline(args): + """ + Parses command line arguments and returns + query and request_options + """ + + if not args: + show_usage() + sys.exit(0) + + request_options = {} + query = args[0] + query = query.lstrip("/") + if not query: + query = ":firstpage" + return query, request_options + + +def main(args): + """ + standalone wrapper for cheat_wrapper() + """ + + query, request_options = parse_cmdline(args) + answer, _ = cheat_wrapper.cheat_wrapper(query, request_options=request_options) + sys.stdout.write(answer) + +if __name__ == '__main__': + main(sys.argv[1:])