CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 71 of file das.py.

References print().

Referenced by main(), and query().

71 
72 def get_data(host, query, idx, limit, debug):
73  """Contact DAS server and retrieve data for given DAS query"""
74  params = {'input':query, 'idx':idx, 'limit':limit}
75  path = '/das/cache'
76  pat = re.compile('http[s]{0,1}://')
77  if not pat.match(host):
78  msg = 'Invalid hostname: %s' % host
79  raise Exception(msg)
80  url = host + path
81  headers = {"Accept": "application/json"}
82  encoded_data = urllib.urlencode(params, doseq=True)
83  url += '?%s' % encoded_data
84  req = urllib2.Request(url=url, headers=headers)
85  if debug:
86  hdlr = urllib2.HTTPHandler(debuglevel=1)
87  opener = urllib2.build_opener(hdlr)
88  else:
89  opener = urllib2.build_opener()
90  fdesc = opener.open(req)
91  data = fdesc.read()
92  fdesc.close()
93 
94  pat = re.compile(r'^[a-z0-9]{32}')
95  if data and isinstance(data, str) and pat.match(data) and len(data) == 32:
96  pid = data
97  else:
98  pid = None
99  count = 5 # initial waiting time in seconds
100  timeout = 30 # final waiting time in seconds
101  while pid:
102  params.update({'pid':data})
103  encoded_data = urllib.urlencode(params, doseq=True)
104  url = host + path + '?%s' % encoded_data
105  req = urllib2.Request(url=url, headers=headers)
106  try:
107  fdesc = opener.open(req)
108  data = fdesc.read()
109  fdesc.close()
110  except urllib2.HTTPError as err:
111  print(err)
112  return ""
113  if data and isinstance(data, str) and pat.match(data) and len(data) == 32:
114  pid = data
115  else:
116  pid = None
117  time.sleep(count)
118  if count < timeout:
119  count *= 2
120  else:
121  count = timeout
122  return data
def get_data
Definition: das.py:71
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def das.get_value (   data,
  filters 
)
Filter data from a row for given list of filters

Definition at line 55 of file das.py.

References str.

Referenced by main().

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

Definition at line 123 of file das.py.

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

124 def main():
125  """Main function"""
126  optmgr = DASOptionParser()
127  opts, _ = optmgr.get_opt()
128  host = opts.host
129  debug = opts.verbose
130  query = opts.query
131  idx = opts.idx
132  limit = opts.limit
133  if not query:
134  raise Exception('You must provide input query')
135  data = get_data(host, query, idx, limit, debug)
136  if opts.format == 'plain':
137  jsondict = json.loads(data)
138  mongo_query = jsondict['mongo_query']
139  if 'filters' in mongo_query:
140  filters = mongo_query['filters']
141  data = jsondict['data']
142  if isinstance(data, dict):
143  rows = [r for r in get_value(data, filters)]
144  print(' '.join(rows))
145  elif isinstance(data, list):
146  for row in data:
147  rows = [r for r in get_value(row, filters)]
148  print(' '.join(rows))
149  else:
150  print(jsondict)
151  else:
152  print(data)
153 
def get_data
Definition: das.py:71
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def main
Definition: das.py:123
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
def get_value
Definition: das.py:55
def das.query (   query_str,
  verbose = False 
)

Definition at line 6 of file das.py.

References get_data(), print(), and digitizers_cfi.strip.

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

Variable Documentation

string das.__author__ = "Valentin Kuznetsov"

Definition at line 8 of file das.py.