CMS 3D CMS Logo

Classes | Functions | Variables
das Namespace Reference

Classes

class  DASOptionParser
 

Functions

def get_data (host, query, idx, limit, debug)
 
def get_value (data, filters)
 
def main ()
 
def query (query_str, verbose=False)
 

Variables

 __author__
 

Function Documentation

◆ get_data()

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.

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
122 

References print().

Referenced by main(), and query().

◆ get_value()

def das.get_value (   data,
  filters 
)
Filter data from a row for given list of filters

Definition at line 55 of file das.py.

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)
70 

References str.

Referenced by main().

◆ main()

def das.main ( )
Main function

Definition at line 123 of file das.py.

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 

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

◆ query()

def das.query (   query_str,
  verbose = False 
)

Definition at line 6 of file das.py.

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']]

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

Variable Documentation

◆ __author__

das.__author__
private

Definition at line 8 of file das.py.

digitizers_cfi.strip
strip
Definition: digitizers_cfi.py:19
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
str
#define str(s)
Definition: TestProcessor.cc:51
das.get_value
def get_value(data, filters)
Definition: das.py:55
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
Exception
das.query
def query(query_str, verbose=False)
Definition: das.py:6
das.get_data
def get_data(host, query, idx, limit, debug)
Definition: das.py:71
das.main
def main()
Definition: das.py:123