5 HCE project, Python bindings, Distributed Crawler application. 6 Application level constants and enumerations. 9 @author bgv bgv.hce@gmail.com 10 @link: http://hierarchical-cluster-engine.com/ 11 @copyright: Copyright © 2013-2014 IOIX Ukraine 12 @license: http://hierarchical-cluster-engine.com/license/ 21 from optparse
import OptionParser
25 parser = OptionParser()
26 parser.add_option(
"-f",
"--field", type=
"string",
27 help=
"json field pathname semicolon delimited, for example response:error_code", dest=
"field")
28 parser.add_option(
"-b",
"--base64", type=
"string",
29 help=
"base64 decode value", dest=
"base64")
32 if __name__ ==
"__main__":
33 options, arguments = parser.parse_args()
35 if options.__dict__[
"field"]:
36 fieldNameStr = options.__dict__[
"field"]
37 if options.__dict__[
"base64"]
is not None:
38 base64Decode = int(options.__dict__[
"base64"])
42 if fieldNameStr
is not None and fieldNameStr !=
"":
43 fieldNamesList = fieldNameStr.split(
":")
44 jsonString = sys.stdin.read()
46 if jsonString
is not None and jsonString !=
"":
49 jsonDic = json.loads(str(jsonString))
52 for fieldName
in fieldNamesList:
53 if isinstance(retVal, dict):
54 for jsonName
in retVal:
55 if fieldName == jsonName:
56 retVal = retVal[fieldName]
59 if isinstance(retVal, list):
60 if fieldName ==
"__LEN__":
61 retVal = str(len(retVal))
64 index = int(fieldName)
65 if len(retVal) > index:
66 retVal = retVal[index]
70 if fieldName ==
"__LEN__":
73 if base64Decode
and fieldName !=
"__LEN__":
74 retVal = base64.b64decode(str(retVal))
78 if isinstance(retVal, dict):
79 retVal = json.dumps(retVal)
81 if isinstance(retVal, unicode):
82 sys.stdout.write(retVal)
84 sys.stdout.write(str(retVal))
86 sys.stdout.write(
"Json parsing or item select error : " + str(e) +
"\n")
88 sys.stdout.write(
"Input json is empty.\n")
90 sys.stdout.write(
"Json field name [" + str(fieldNameStr) +
"] error!\n")
92 sys.stdout.write(
"Mandatory argument(s) not specified, try -h to get detailed descriptions.\n")