CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
List of all members | Public Member Functions | Static Public Member Functions | Public Attributes | Static 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__
 
def close_session
 
def commit
 
def global_tag
 
def global_tag_map
 
def hard_close
 
def iov
 
def model
 
def object
 
def payload
 
def rollback
 
def search_everything
 
def setup
 
def tag
 
def tag_authorization
 
def tear_down
 
def write
 
def write_and_commit
 

Static Public Member Functions

def build_frontier_url
 
def build_oracle_url
 

Public Attributes

 factory
 
 models
 
 radius
 
 range
 
 regexp
 
 schema
 
 sessionmaker
 

Static Public Attributes

 connection = None
 
 connection_data = None
 
 engine = None
 
 netrc_authenticators = None
 
 secrets = None
 
 session = None
 

Private Member Functions

def _oracle_match_format
 

Static Private Member Functions

def _cms_frontier_string
 
def _cms_oracle_string
 
def _get_CMS_frontier_connection_string
 

Private Attributes

 _pooling
 

Detailed Description

Definition at line 23 of file querying.py.

Constructor & Destructor Documentation

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 
36  def __init__(self, connection_data, mode=None, map_blobs=False, secrets=None, pooling=False):
37 
38  self._pooling = pooling
39 
40  # add querying utility properties
41  # these must belong to the connection since the way in which their values are handled
42  # depends on the database being connected to.
45  self.regexp = models.RegExp
46  self.regexp.connection_object = self
47 
48  if type(connection_data) in [str, str]:
49  # if we've been given a connection string, process it
50  self.connection_data = new_connection_dictionary(connection_data, secrets=secrets, mode=mode)
51  self.schema = self.connection_data.get("schema") if self.connection_data.get("schema") != None else ""
52 
53  self.range.database_type = self.connection_data["host"]
54  self.radius.database_type = self.connection_data["host"]
55  self.regexp.database_type = self.connection_data["host"]
56  else:
57  self.connection_data = connection_data
58  # assume we have an engine
59  # we need to take the string representation so we know which type of db we're aiming at
60  engine_string = str(connection_data)
61  db_type = None
62  if "oracle" in engine_string:
63  db_type = "oracle"
64  elif "frontier" in engine_string:
65  db_type = "frontier"
66  elif "sqlite" in engine_string:
67  db_type = "sqlite"
68 
69  self.range.database_type = db_type
70  self.radius.database_type = db_type
71  self.regexp.database_type = db_type
72 
73  from . import models as ms
74  self.models = ms.generate(map_blobs)
75  #self.base = self.models["Base"]
def new_connection_dictionary
Definition: querying.py:350
#define str(s)

Member Function Documentation

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

Definition at line 126 of file querying.py.

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

Definition at line 134 of file querying.py.

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

Definition at line 118 of file querying.py.

References communicate(), and digitizers_cfi.strip.

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

Definition at line 228 of file querying.py.

Referenced by querying.connection.search_everything().

229  def _oracle_match_format(self, string):
230  return "%%%s%%" % string
def querying.connection.build_frontier_url (   db_name,
  schema 
)
static

Definition at line 157 of file querying.py.

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

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

Definition at line 178 of file querying.py.

Referenced by querying.connection.tear_down().

179  def close_session(self):
180  self.session.close()
181  return True
def querying.connection.commit (   self)

Definition at line 273 of file querying.py.

Referenced by querying.connection.write_and_commit().

274  def commit(self):
275  try:
276  self.session.commit()
277  except:
278  traceback.print_exc()
279  self.session.rollback()
def querying.connection.global_tag (   self,
  pkargs 
)

Definition at line 203 of file querying.py.

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

Definition at line 206 of file querying.py.

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

Definition at line 182 of file querying.py.

183  def hard_close(self):
184  self.engine.dispose()
185  return True
def querying.connection.iov (   self,
  pkargs 
)

Definition at line 218 of file querying.py.

219  def iov(self, **pkargs):
220  return self.factory.object("iov", **pkargs)
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().

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

Definition at line 195 of file querying.py.

References querying.connection.session.

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

Definition at line 221 of file querying.py.

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

Definition at line 289 of file querying.py.

References print().

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

Definition at line 232 of file querying.py.

References querying.connection._oracle_match_format(), alcazmumu_cfi.filter, MessageLogger_cff.limit, TritonService::Module.model, and querying.connection.model().

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

Referenced by o2olib.O2OTool.execute().

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

Definition at line 212 of file querying.py.

Referenced by DTTtrigWorkflow.DTttrigWorkflow.prepare_timeboxes_correction().

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

Definition at line 215 of file querying.py.

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

Definition at line 171 of file querying.py.

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

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

Definition at line 268 of file querying.py.

References querying.connection.schema, and models.session_independent_object().

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

269  def write(self, object):
270  new_object = models.session_independent_object(object, schema=self.schema)
271  self.session.add(new_object)
272  return new_object
def session_independent_object
Definition: models.py:31
def querying.connection.write_and_commit (   self,
  object 
)

Definition at line 280 of file querying.py.

References cond::persistency::CondDBTransaction.commit(), cond::persistency::ITransaction.commit(), cond::persistency::Transaction.commit(), cond::persistency::TransactionScope.commit(), querying.connection.commit(), AlignmentCorrelationsIO.write(), AlignmentCorrelationsIORoot.write(), SurveyOutput.write(), AlignmentParametersIO.write(), popcon::PopConAnalyzer< S >.write(), AlignmentUserVariablesIO.write(), popcon::PopCon.write(), ProduceDropBoxMetadata.write, MuonAlignmentOutputXML.write(), XMLDOMBlock.write(), CSCPairResidualsConstraint.write(), presentation.SubsectionBase.write(), MuonResidualsTwoBin.write(), DTWorkflow.DTWorkflow.write(), XMLProcessor.write(), MuonResidualsFitter.write(), StraightTrackAlignment::RPSetPlots.write(), betterConfigParser.BetterConfigParser.write(), querying.connection.write(), and querying.connection.write_and_commit().

Referenced by querying.connection.write_and_commit().

281  def write_and_commit(self, object):
282  if type(object) == list:
283  for item in object:
284  self.write_and_commit(item)
285  else:
286  # should be changed to deal with errors - add them to exception handling if they appear
287  self.write(object)
288  self.commit()

Member Data Documentation

querying.connection._pooling
private

Definition at line 37 of file querying.py.

Referenced by querying.connection.setup().

querying.connection.connection = None
static

Definition at line 25 of file querying.py.

Referenced by querying_tests.querying_tests.test_check_connection(), and data_formats_tests.data_formats_tests.test_dicts_to_orm_objects().

querying.connection.connection_data = None
static

Definition at line 27 of file querying.py.

Referenced by querying.connection.setup().

querying.connection.engine = None
static

Definition at line 24 of file querying.py.

Referenced by o2o_db_manager.DbManager.connect(), conddblib.Connection.init(), querying.connection.setup(), and querying.connection.tear_down().

querying.connection.factory

Definition at line 90 of file querying.py.

querying.connection.models

Definition at line 73 of file querying.py.

Referenced by querying.connection.model().

querying.connection.netrc_authenticators = None
static

Definition at line 28 of file querying.py.

querying.connection.radius

Definition at line 43 of file querying.py.

querying.connection.range

Definition at line 42 of file querying.py.

querying.connection.regexp

Definition at line 44 of file querying.py.

querying.connection.schema

Definition at line 50 of file querying.py.

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

querying.connection.secrets = None
static

Definition at line 29 of file querying.py.

querying.connection.session = None
static

Definition at line 26 of file querying.py.

Referenced by querying.connection.object(), and o2olib.O2OJobMgr.runManager().

querying.connection.sessionmaker

Definition at line 88 of file querying.py.