diff options
Diffstat (limited to 'scripts/misc.py')
-rw-r--r-- | scripts/misc.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/scripts/misc.py b/scripts/misc.py index 94e378e..92d4b2f 100644 --- a/scripts/misc.py +++ b/scripts/misc.py @@ -1,7 +1,10 @@ from dbaccess import execQuery from statlib import stats from string import hexdigits +import sys +import os import re +import urllib # ### 2 B DOCUMENTED! def idToText(table, id_): @@ -661,3 +664,46 @@ def printJSONHeader(): def printErrorAsJSON(error): printJSONHeader() print "{\"error\": \"" + error + "\"}\n" + +# Returns a 2-tuple consisting of: +# 1: an option dictionary, and +# 2: a flag that is true iff the QUERY_STRING environment variable is +# present (i.e. that the script is invoked as a CGI-script for a +# HTTP GET request). +# +# The option dictionary is extracted from either the QUERY_STRING environment +# variable (first priority) or command-line arguments (second priority). +# In the latter case, the options must be of the form +# ... --<opt1> <val1> ... --<optN> <valN> ... +def getOptions(): + + def getOptDictFromQueryString(qs): + options = {} + for sq in qs.split("&"): + keyval = sq.split("=") + options[keyval[0]] = urllib.unquote(keyval[1]) + return options + + def getOptDictFromCommandLine(): + options = {} + p = re.compile("^--(.+)$") + key = None + for arg in sys.argv[1:]: + if key != None: + options[key] = arg + m = p.match(arg) + if m: + key = m.group(1) + # Support "--help" as the only value-less option: + if key == "help": + options[key] = 1 + key = None + else: + key = None + return options + + qs = "QUERY_STRING" + if qs in os.environ: + return (getOptDictFromQueryString(os.environ[qs]), True) + else: + return (getOptDictFromCommandLine(), False) |