test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Classes | Functions | Variables
das Namespace Reference

Classes

class  DASOptionParser
 

Functions

def get_data
 
def get_value
 
def main
 
def query
 

Variables

string __author__ = "Valentin Kuznetsov"
 

Function Documentation

def das.get_data (   host,
  query,
  idx,
  limit,
  debug 
)
Contact DAS server and retrieve data for given DAS query

Definition at line 70 of file das.py.

Referenced by main(), and query().

70 
71 def get_data(host, query, idx, limit, debug):
72  """Contact DAS server and retrieve data for given DAS query"""
73  params = {'input':query, 'idx':idx, 'limit':limit}
74  path = '/das/cache'
75  pat = re.compile('http[s]{0,1}://')
76  if not pat.match(host):
77  msg = 'Invalid hostname: %s' % host
78  raise Exception(msg)
79  url = host + path
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)
84  if debug:
85  hdlr = urllib2.HTTPHandler(debuglevel=1)
86  opener = urllib2.build_opener(hdlr)
87  else:
88  opener = urllib2.build_opener()
89  fdesc = opener.open(req)
90  data = fdesc.read()
91  fdesc.close()
92 
93  pat = re.compile(r'^[a-z0-9]{32}')
94  if data and isinstance(data, str) and pat.match(data) and len(data) == 32:
95  pid = data
96  else:
97  pid = None
98  count = 5 # initial waiting time in seconds
99  timeout = 30 # final waiting time in seconds
100  while pid:
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)
105  try:
106  fdesc = opener.open(req)
107  data = fdesc.read()
108  fdesc.close()
109  except urllib2.HTTPError as err:
110  print err
111  return ""
112  if data and isinstance(data, str) and pat.match(data) and len(data) == 32:
113  pid = data
114  else:
115  pid = None
116  time.sleep(count)
117  if count < timeout:
118  count *= 2
119  else:
120  count = timeout
121  return data
def get_data
Definition: das.py:70
def das.get_value (   data,
  filters 
)
Filter data from a row for given list of filters

Definition at line 54 of file das.py.

References cmsPerfStripChart.dict.

Referenced by main().

54 
55 def get_value(data, filters):
56  """Filter data from a row for given list of filters"""
57  for ftr in filters:
58  if ftr.find('>') != -1 or ftr.find('<') != -1 or ftr.find('=') != -1:
59  continue
60  row = dict(data)
61  for key in ftr.split('.'):
62  if isinstance(row, dict) and key in row:
63  row = row[key]
64  if isinstance(row, list):
65  for item in row:
66  if isinstance(item, dict) and key in item:
67  row = item[key]
68  break
69  yield str(row)
def get_value
Definition: das.py:54
def das.main ( )
Main function

Definition at line 122 of file das.py.

References get_data(), get_value(), and join().

123 def main():
124  """Main function"""
125  optmgr = DASOptionParser()
126  opts, _ = optmgr.get_opt()
127  host = opts.host
128  debug = opts.verbose
129  query = opts.query
130  idx = opts.idx
131  limit = opts.limit
132  if not query:
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)]
143  print ' '.join(rows)
144  elif isinstance(data, list):
145  for row in data:
146  rows = [r for r in get_value(row, filters)]
147  print ' '.join(rows)
148  else:
149  print jsondict
150  else:
151  print data
152 
def get_data
Definition: das.py:70
def main
Definition: das.py:122
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def get_value
Definition: das.py:54
def das.query (   query_str,
  verbose = False 
)

Definition at line 5 of file das.py.

References get_data().

Referenced by shallowTree_test_template.add_rawRelVals().

5 
6 def query(query_str, verbose=False):
7  'simple query function to interface with DAS, better than using Popen as everything is handled by python'
8  if verbose:
9  print 'querying DAS with: "%s"' % query_str
10  data = get_data(
11  'https://cmsweb.cern.ch',
12  query_str,
13  0, 0, False)
14 
15  to_get = query_str.split()[0].strip(',')
16  if data['status'] != 'ok':
17  raise RuntimeError('Das query crashed')
18 
19  #-1 works both when getting dataset from files and files from datasets,
20  #not checked on everything
21  return [i[to_get][-1]['name'] for i in data['data']]
def get_data
Definition: das.py:70
def query
Definition: das.py:5

Variable Documentation

string das.__author__ = "Valentin Kuznetsov"

Definition at line 7 of file das.py.