From d7bede17a291dcd24da986dd96a1ead7e0ac8a1f Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Wed, 25 Oct 2023 12:38:07 -0700 Subject: [PATCH] Add query parse script --- .chezmoiignore | 1 + .gitignore | 2 +- dot_local/bin/executable_query-parse | 31 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100755 dot_local/bin/executable_query-parse diff --git a/.chezmoiignore b/.chezmoiignore index 01d80b4..6212704 100644 --- a/.chezmoiignore +++ b/.chezmoiignore @@ -1,3 +1,4 @@ +/.idea /README.md /scripts/ {{- if not .include_legacy }} diff --git a/.gitignore b/.gitignore index 2c66cd4..a09c56d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -vim/plugged +/.idea diff --git a/dot_local/bin/executable_query-parse b/dot_local/bin/executable_query-parse new file mode 100755 index 0000000..e578380 --- /dev/null +++ b/dot_local/bin/executable_query-parse @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +import json +import select +import sys +import urllib.parse + + +def main(args=(), stdin=()): + for arg in args: + print(parse(arg)) + + for line in stdin: + print(parse(line)) + + +def parse(params): + qs = urllib.parse.parse_qs(params.strip(), keep_blank_values=True) + obj = {} + for key, value in qs.items(): + obj[key] = value[0] if len(value) == 1 else value + return json.dumps(obj) + + +def get_stdin(): + found = select.select((sys.stdin,), (), (), 0.0)[0] + return found[0] if found else () + + +if __name__ == "__main__": + main(sys.argv[1:], get_stdin())