CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes
querying.connection Class Reference
Inheritance diagram for querying.connection:

Public Member Functions

def __init__ (self, connection_data, mode=None, map_blobs=False, secrets=None, pooling=False)
 
def close_session (self)
 
def commit (self)
 
def global_tag (self, pkargs)
 
def global_tag_map (self, pkargs)
 
def hard_close (self)
 
def iov (self, pkargs)
 
def model (self, model_name)
 
def object (self, model, pk_to_value)
 
def payload (self, pkargs)
 
def rollback (self)
 
def search_everything (self, string, amount=10)
 
def setup (self)
 
def tag (self, pkargs)
 
def tag_authorization (self, pkargs)
 
def tear_down (self)
 
def write (self, object)
 
def write_and_commit (self, object)
 

Static Public Member Functions

def build_frontier_url (db_name, schema)
 
def build_oracle_url (user, pwd, db_name)
 

Public Attributes

 connection_data
 
 engine
 
 factory
 
 models
 
 radius
 
 range
 
 regexp
 
 schema
 
 session
 
 sessionmaker
 

Private Member Functions

def _oracle_match_format (self, string)
 

Static Private Member Functions

def _cms_frontier_string (database, schema="cms_conditions")
 
def _cms_oracle_string (user, pwd, db_name)
 
def _get_CMS_frontier_connection_string (database)
 

Private Attributes

 _pooling
 

Detailed Description

Definition at line 23 of file querying.py.

Constructor & Destructor Documentation

◆ __init__()

def querying.connection.__init__ (   self,
  connection_data,
  mode = None,
  map_blobs = False,
  secrets = None,
  pooling = False 
)

Definition at line 35 of file querying.py.

35  def __init__(self, connection_data, mode=None, map_blobs=False, secrets=None, pooling=False):
36 
37  self._pooling = pooling
38 
39  # add querying utility properties
40  # these must belong to the connection since the way in which their values are handled
41  # depends on the database being connected to.
42  self.range = models.Range
43  self.radius = models.Radius
44  self.regexp = models.RegExp
45  self.regexp.connection_object = self
46 
47  if type(connection_data) in [str, str]:
48  # if we've been given a connection string, process it
49  self.connection_data = new_connection_dictionary(connection_data, secrets=secrets, mode=mode)
50  self.schema = self.connection_data.get("schema") if self.connection_data.get("schema") != None else ""
51 
52  self.range.database_type = self.connection_data["host"]
53  self.radius.database_type = self.connection_data["host"]
54  self.regexp.database_type = self.connection_data["host"]
55  else:
56  self.connection_data = connection_data
57  # assume we have an engine
58  # we need to take the string representation so we know which type of db we're aiming at
59  engine_string = str(connection_data)
60  db_type = None
61  if "oracle" in engine_string:
62  db_type = "oracle"
63  elif "frontier" in engine_string:
64  db_type = "frontier"
65  elif "sqlite" in engine_string:
66  db_type = "sqlite"
67 
68  self.range.database_type = db_type
69  self.radius.database_type = db_type
70  self.regexp.database_type = db_type
71 
72  from . import models as ms
73  self.models = ms.generate(map_blobs)
74  #self.base = self.models["Base"]
75 
def __init__(self, dataset, job_number, job_id, job_name, isDA, isMC, applyBOWS, applyEXTRACOND, extraconditions, runboundary, lumilist, intlumi, maxevents, gt, allFromGT, alignmentDB, alignmentTAG, apeDB, apeTAG, bowDB, bowTAG, vertextype, tracktype, refittertype, ttrhtype, applyruncontrol, ptcut, CMSSW_dir, the_dir)
def new_connection_dictionary(connection_data, secrets=None, mode="r")
Definition: querying.py:350
#define str(s)

Member Function Documentation

◆ _cms_frontier_string()

def querying.connection._cms_frontier_string (   database,
  schema = "cms_conditions" 
)
staticprivate
Get database string for frontier.

Definition at line 126 of file querying.py.

126  def _cms_frontier_string(database, schema="cms_conditions"):
127  """
128  Get database string for frontier.
129  """
130  import urllib.request, urllib.parse, urllib.error
131  return 'oracle+frontier://@%s/%s' % (urllib.parse.quote_plus(connection._get_CMS_frontier_connection_string(database)), schema)
132 

◆ _cms_oracle_string()

def querying.connection._cms_oracle_string (   user,
  pwd,
  db_name 
)
staticprivate
Get database string for oracle.

Definition at line 134 of file querying.py.

134  def _cms_oracle_string(user, pwd, db_name):
135  """
136  Get database string for oracle.
137  """
138  return 'oracle://%s:%s@%s' % (user, pwd, db_name)
139 

◆ _get_CMS_frontier_connection_string()

def querying.connection._get_CMS_frontier_connection_string (   database)
staticprivate

Definition at line 118 of file querying.py.

References communicate(), and digitizers_cfi.strip.

118  def _get_CMS_frontier_connection_string(database):
119  try:
120  import subprocess
121  return subprocess.Popen(['cmsGetFnConnect', 'frontier://%s' % database], stdout = subprocess.PIPE).communicate()[0].strip()
122  except:
123  raise Exception("Frontier connections can only be constructed when inside a CMSSW environment.")
124 
static void * communicate(void *obj)
Definition: DQMNet.cc:1057

◆ _oracle_match_format()

def querying.connection._oracle_match_format (   self,
  string 
)
private

Definition at line 228 of file querying.py.

Referenced by querying.connection.search_everything().

228  def _oracle_match_format(self, string):
229  return "%%%s%%" % string
230 

◆ build_frontier_url()

def querying.connection.build_frontier_url (   db_name,
  schema 
)
static

Definition at line 157 of file querying.py.

157  def build_frontier_url(db_name, schema):
158  database_url = connection._cms_frontier_string(db_name, schema)
159 
160  try:
161  url = sqlalchemy.engine.url.make_url(database_url)
162  except sqlalchemy.exc.ArgumentError:
163  """
164  Is this needed for a use case?
165  """
166  url = sqlalchemy.engine.url.make_url('sqlite:///%s' % db_name)
167  return url
168 

◆ build_oracle_url()

def querying.connection.build_oracle_url (   user,
  pwd,
  db_name 
)
static
Build the connection url, and get credentials from self.secrets dictionary.

Definition at line 141 of file querying.py.

141  def build_oracle_url(user, pwd, db_name):
142  """
143  Build the connection url, and get credentials from self.secrets dictionary.
144  """
145 
146  database_url = connection._cms_oracle_string(user, pwd, db_name)
147 
148  try:
149  url = sqlalchemy.engine.url.make_url(database_url)
150  if url.password is None:
151  url.password = pwd
152  except sqlalchemy.exc.ArgumentError:
153  url = sqlalchemy.engine.url.make_url('sqlite:///%s' % db_name)
154  return url
155 

◆ close_session()

def querying.connection.close_session (   self)

Definition at line 178 of file querying.py.

References querying.connection.session.

Referenced by querying.connection.tear_down().

178  def close_session(self):
179  self.session.close()
180  return True
181 

◆ commit()

def querying.connection.commit (   self)

Definition at line 273 of file querying.py.

References querying.connection.rollback(), and querying.connection.session.

Referenced by querying.connection.tear_down(), and querying.connection.write_and_commit().

273  def commit(self):
274  try:
275  self.session.commit()
276  except:
277  traceback.print_exc()
278  self.session.rollback()
279 

◆ global_tag()

def querying.connection.global_tag (   self,
  pkargs 
)

Definition at line 203 of file querying.py.

References querying.connection.factory, and querying.connection.object().

203  def global_tag(self, **pkargs):
204  return self.factory.object("globaltag", **pkargs)
205 

◆ global_tag_map()

def querying.connection.global_tag_map (   self,
  pkargs 
)

Definition at line 206 of file querying.py.

References querying.connection.factory, and querying.connection.object().

206  def global_tag_map(self, **pkargs):
207  return self.factory.object("globaltagmap", **pkargs)
208 

◆ hard_close()

def querying.connection.hard_close (   self)

Definition at line 182 of file querying.py.

References cms::xerces.dispose(), and querying.connection.engine.

182  def hard_close(self):
183  self.engine.dispose()
184  return True
185 
void dispose(XMLCh *ptr)

◆ iov()

def querying.connection.iov (   self,
  pkargs 
)

Definition at line 218 of file querying.py.

References querying.connection.factory, and querying.connection.object().

218  def iov(self, **pkargs):
219  return self.factory.object("iov", **pkargs)
220 

◆ model()

def querying.connection.model (   self,
  model_name 
)

Definition at line 187 of file querying.py.

References querying.connection.models.

Referenced by querying.connection.search_everything().

187  def model(self, model_name):
188  if model_name.__class__ == sqlalchemy.ext.declarative.api.DeclarativeMeta:
189  model_name = model_name.__name__
190  model_name = model_name.replace("_", "")
191  return self.models[model_name]
192 

◆ object()

def querying.connection.object (   self,
  model,
  pk_to_value 
)

Definition at line 195 of file querying.py.

References querying.connection.session.

Referenced by querying.connection.global_tag(), querying.connection.global_tag_map(), querying.connection.iov(), querying.connection.payload(), querying.connection.tag(), and querying.connection.tag_authorization().

195  def object(self, model, pk_to_value):
196  if self.session == None:
197  return None
198  model_data = self.session.query(model)
199  for pk in pk_to_value:
200  model_data = model_data.filter(model.__dict__[pk] == pk_to_value[pk])
201  return model_data.first()
202 
Definition: query.py:1

◆ payload()

def querying.connection.payload (   self,
  pkargs 
)

Definition at line 221 of file querying.py.

References querying.connection.factory, and querying.connection.object().

221  def payload(self, **pkargs):
222  return self.factory.object("payload", **pkargs)
223 

◆ rollback()

def querying.connection.rollback (   self)

Definition at line 289 of file querying.py.

References print(), and querying.connection.session.

Referenced by querying.connection.commit().

289  def rollback(self):
290  try:
291  self.session.rollback()
292  except:
293  traceback.print_exc()
294  print("Session couldn't be rolled back.")
295 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47

◆ search_everything()

def querying.connection.search_everything (   self,
  string,
  amount = 10 
)

Definition at line 232 of file querying.py.

References querying.connection._oracle_match_format(), ALCARECOTkAlBeamHalo_cff.filter, remoteMonitoring_LASER_era2018_cfg.limit, TritonService::Module.model, L1TCaloSummary.model, querying.connection.model(), and querying.connection.session.

232  def search_everything(self, string, amount=10):
233  string = self._oracle_match_format(string)
234 
235  gt = self.model("globaltag")
236  global_tags = self.session.query(gt).filter(or_(
237  gt.name.ilike(string),
238  gt.description.ilike(string),
239  gt.release.ilike(string)
240  )).limit(amount)
241  tag = self.model("tag")
242  tags = self.session.query(tag).filter(or_(
243  tag.name.ilike(string),
244  tag.object_type.ilike(string),
245  tag.description.ilike(string))
246  ).limit(amount)
247  iov = self.model("iov")
248  iovs = self.session.query(iov).filter(or_(
249  iov.tag_name.ilike(string),
250  iov.since.ilike(string),
251  iov.payload_hash.ilike(string),
252  iov.insertion_time.ilike(string)
253  )).limit(amount)
254  payload = self.model("payload")
255  payloads = self.session.query(payload).filter(or_(
256  payload.hash.ilike(string),
257  payload.object_type.ilike(string),
258  payload.insertion_time.ilike(string)
259  )).limit(amount)
260 
261  return json_data_node.make({
262  "global_tags" : global_tags.all(),
263  "tags" : tags.all(),
264  "iovs" : iovs.all(),
265  "payloads" : payloads.all()
266  })
267 
Definition: query.py:1

◆ setup()

def querying.connection.setup (   self)
Setup engine with given credentials from netrc file, and make a session maker.

Definition at line 76 of file querying.py.

References querying.connection.connection_data.

Referenced by o2olib.O2OTool.execute().

76  def setup(self):
77  """
78  Setup engine with given credentials from netrc file, and make a session maker.
79  """
80 
81  if type(self.connection_data) == dict:
82  self.engine = engine_from_dictionary(self.connection_data, pooling=self._pooling)
83  else:
84  # we've been given an engine by the user
85  # use it as the engine
86  self.engine = self.connection_data
87 
88  self.sessionmaker = sessionmaker(bind=self.engine)
89  self.session = self.sessionmaker()
90  self.factory = factory(self)
91 
92  # assign correct schema for database name to each model
93  tmp_models_dict = {}
94  for key in self.models:
95  if self.models[key].__class__ == sqlalchemy.ext.declarative.api.DeclarativeMeta\
96  and str(self.models[key].__name__) != "Base":
97 
98  if type(self.connection_data) == dict:
99  # we can only extract the secrets and schema individuall
100  # if we were given a dictionary... if we were given an engine
101  # we can't do this without parsing the connection string from the engine
102  # - a wide range of which it will be difficult to support!
103  self.models[key].__table__.schema = self.connection_data["schema"]
104  self.models[key].secrets = self.connection_data["secrets"]
105 
106  self.models[key].session = self.session
107  # isn't used anywhere - comment it out for now
108  #self.models[key].authentication = self.netrc_authenticators
109  self.models[key].connection = self
110  tmp_models_dict[key.lower()] = self.models[key]
111  tmp_models_dict[key.lower()].empty = False
112 
113  self.models = tmp_models_dict
114 
115  return self
116 
def engine_from_dictionary(dictionary, pooling=True)
Definition: querying.py:429
#define str(s)

◆ tag()

def querying.connection.tag (   self,
  pkargs 
)

Definition at line 212 of file querying.py.

References querying.connection.factory, and querying.connection.object().

212  def tag(self, **pkargs):
213  return self.factory.object("tag", **pkargs)
214 

◆ tag_authorization()

def querying.connection.tag_authorization (   self,
  pkargs 
)

Definition at line 215 of file querying.py.

References querying.connection.factory, and querying.connection.object().

215  def tag_authorization(self, **pkargs):
216  return self.factory.object("tagauthorization", **pkargs)
217 

◆ tear_down()

def querying.connection.tear_down (   self)

Definition at line 171 of file querying.py.

References querying.connection.close_session(), querying.connection.commit(), querying.connection.engine, querying.connection.session, and str.

171  def tear_down(self):
172  try:
173  self.session.commit()
174  self.close_session()
175  except:
176  return "Couldn't tear down connection on engine %s." % str(self.engine)
177 
#define str(s)

◆ write()

def querying.connection.write (   self,
  object 
)

Definition at line 268 of file querying.py.

References PVValHelper.add(), querying.connection.schema, querying.connection.session, and models.session_independent_object().

Referenced by pkg.AbstractPkg.generate(), and querying.connection.write_and_commit().

268  def write(self, object):
269  new_object = models.session_independent_object(object, schema=self.schema)
270  self.session.add(new_object)
271  return new_object
272 
def session_independent_object(object, schema=None)
Definition: models.py:31
void add(std::map< std::string, TH1 *> &h, TH1 *hist)

◆ write_and_commit()

def querying.connection.write_and_commit (   self,
  object 
)

Member Data Documentation

◆ _pooling

querying.connection._pooling
private

Definition at line 37 of file querying.py.

◆ connection_data

querying.connection.connection_data

Definition at line 49 of file querying.py.

Referenced by querying.connection.setup().

◆ engine

querying.connection.engine

◆ factory

querying.connection.factory

◆ models

querying.connection.models

Definition at line 73 of file querying.py.

Referenced by querying.connection.model().

◆ radius

querying.connection.radius

Definition at line 43 of file querying.py.

◆ range

querying.connection.range

Definition at line 42 of file querying.py.

◆ regexp

querying.connection.regexp

Definition at line 44 of file querying.py.

◆ schema

querying.connection.schema

Definition at line 50 of file querying.py.

Referenced by o2o_db_manager.DbManager.get_url(), and querying.connection.write().

◆ session

querying.connection.session

◆ sessionmaker

querying.connection.sessionmaker

Definition at line 88 of file querying.py.