7 from __future__
import print_function
8 __author__ =
"Valentin Kuznetsov" 11 if sys.version_info < (2, 6):
12 raise Exception(
"DAS requires python 2.6 or greater")
19 from optparse
import OptionParser, OptionGroup
23 DAS cache client option parser 27 usage =
'usage: %prog [options] --query "dataset=/HT/Run2011A-*/AOD"' 28 self.
parser = OptionParser(usage=usage)
29 group = OptionGroup(self.
parser,
"Das options",
"These options relate to the Das client interface.")
30 group.add_option(
"-v",
"--verbose", action=
"store",
31 type=
"int", default=0, dest=
"verbose",
32 help=
"verbose output")
33 group.add_option(
"--query", action=
"store", type=
"string",
34 default=
False, dest=
"query",
35 help=
"specify query for your request")
36 group.add_option(
"--host", action=
"store", type=
"string",
37 default=
'https://cmsweb.cern.ch', dest=
"host",
38 help=
"specify host name of DAS cache server, default https://cmsweb.cern.ch")
39 group.add_option(
"--idx", action=
"store", type=
"int",
40 default=0, dest=
"idx",
41 help=
"start index for returned result set, aka pagination, use w/ limit")
42 group.add_option(
"--limit", action=
"store", type=
"int",
43 default=10, dest=
"limit",
44 help=
"number of returned results (results per page)")
45 group.add_option(
"--format", action=
"store", type=
"string",
46 default=
"json", dest=
"format",
47 help=
"specify return data format (json or plain), default json")
48 self.parser.add_option_group(group)
51 Returns parse list of options 53 return self.parser.parse_args()
56 """Filter data from a row for given list of filters""" 58 if ftr.find(
'>') != -1
or ftr.find(
'<') != -1
or ftr.find(
'=') != -1:
61 for key
in ftr.split(
'.'):
62 if isinstance(row, dict)
and key
in row:
64 if isinstance(row, list):
66 if isinstance(item, dict)
and key
in item:
72 """Contact DAS server and retrieve data for given DAS query""" 73 params = {
'input':query,
'idx':idx,
'limit':limit}
75 pat = re.compile(
'http[s]{0,1}://')
76 if not pat.match(host):
77 msg =
'Invalid hostname: %s' % host
80 headers = {
"Accept":
"application/json"}
81 encoded_data = urllib.urlencode(params, doseq=
True)
82 url +=
'?%s' % encoded_data
83 req = urllib2.Request(url=url, headers=headers)
85 hdlr = urllib2.HTTPHandler(debuglevel=1)
86 opener = urllib2.build_opener(hdlr)
88 opener = urllib2.build_opener()
89 fdesc = opener.open(req)
93 pat = re.compile(
r'^[a-z0-9]{32}')
94 if data
and isinstance(data, str)
and pat.match(data)
and len(data) == 32:
101 params.update({
'pid':data})
102 encoded_data = urllib.urlencode(params, doseq=
True)
103 url = host + path +
'?%s' % encoded_data
104 req = urllib2.Request(url=url, headers=headers)
106 fdesc = opener.open(req)
109 except urllib2.HTTPError
as err:
112 if data
and isinstance(data, str)
and pat.match(data)
and len(data) == 32:
126 opts, _ = optmgr.get_opt()
133 raise Exception(
'You must provide input query')
134 data =
get_data(host, query, idx, limit, debug)
135 if opts.format ==
'plain':
136 jsondict = json.loads(data)
137 mongo_query = jsondict[
'mongo_query']
138 if 'filters' in mongo_query:
139 filters = mongo_query[
'filters']
140 data = jsondict[
'data']
141 if isinstance(data, dict):
142 rows = [r
for r
in get_value(data, filters)]
144 elif isinstance(data, list):
146 rows = [r
for r
in get_value(row, filters)]
def __init__(self, usage=None)
S & print(S &os, JobReport::InputFile const &f)
def get_data(host, query, idx, limit, debug)
def get_value(data, filters)
static std::string join(char **cmd)