CMS 3D CMS Logo

Classes | Functions
querying Namespace Reference

Classes

class  connection
 
class  factory
 

Functions

def _get_netrc_data (netrc_file, key)
 
def connect (connection_data, mode="r", map_blobs=False, secrets=None, pooling=True)
 
def engine_from_dictionary (dictionary, pooling=True)
 
def new_connection_dictionary (connection_data, secrets=None, mode="r")
 

Detailed Description

connection class translates either a connection string for sqlite, oracle of frontier into a connection object.
Also sets up ORM with SQLAlchemy.

connection class can also take a pre-constructed engine - useful for web services.

Function Documentation

def querying._get_netrc_data (   netrc_file,
  key 
)
private
Returns a dictionary {login : ..., account : ..., password : ...}

Definition at line 334 of file querying.py.

References cmsPerfStripChart.dict, and ComparisonHelper.zip().

Referenced by new_connection_dictionary().

334 def _get_netrc_data(netrc_file, key):
335  """
336  Returns a dictionary {login : ..., account : ..., password : ...}
337  """
338  try:
339  headers = ["login", "account", "password"]
340  authenticator_tuple = netrc.netrc(netrc_file).authenticators(key)
341  if authenticator_tuple == None:
342  raise Exception("netrc file must contain key '%s'." % key)
343  except:
344  raise Exception("Couldn't get credentials from netrc file.")
345  return dict(zip(headers, authenticator_tuple))
346 
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def _get_netrc_data(netrc_file, key)
Definition: querying.py:334
def querying.connect (   connection_data,
  mode = "r",
  map_blobs = False,
  secrets = None,
  pooling = True 
)
Utility method for user - set up a connection object.

Definition at line 450 of file querying.py.

Referenced by shell.connect(), command_line.copy_global_tag(), command_line.copy_tag(), command_line.diff_of_gts(), command_line.diff_of_tags(), command_line.list_object(), command_line.search(), uploads.uploader.send_payloads(), and payload_tests.payload_tests.test_write_blob_to_sqlite().

450 def connect(connection_data, mode="r", map_blobs=False, secrets=None, pooling=True):
451  """
452  Utility method for user - set up a connection object.
453  """
454  con = connection(connection_data=connection_data, mode=mode, map_blobs=map_blobs, secrets=secrets, pooling=pooling)
455  con = con.setup()
456  return con
def connect(connection_data, mode="r", map_blobs=False, secrets=None, pooling=True)
Definition: querying.py:450
def querying.engine_from_dictionary (   dictionary,
  pooling = True 
)

Definition at line 426 of file querying.py.

426 def engine_from_dictionary(dictionary, pooling=True):
427  if dictionary["host"] != "sqlite":
428  if dictionary["host"] != "frontier":
429  # probably oracle
430  # if not frontier, we have to authenticate
431  user = dictionary["secrets"]["login"]
432  pwd = dictionary["secrets"]["password"]
433  # set max label length for oracle
434  if pooling:
435  return create_engine(connection.build_oracle_url(user, pwd, dictionary["database_name"]), label_length=6)
436  else:
437  return create_engine(connection.build_oracle_url(user, pwd, dictionary["database_name"]), label_length=6, poolclass=NullPool)
438  else:
439  # if frontier, no need to authenticate
440  # set max label length for frontier
441  if pooling:
442  return create_engine(connection.build_frontier_url(dictionary["database_name"], dictionary["schema"]), label_length=6)
443  else:
444  return create_engine(connection.build_frontier_url(dictionary["database_name"], dictionary["schema"]), label_length=6, poolclass=NullPool)
445  else:
446  # if host is sqlite, making the url is easy - no authentication
447  return create_engine("sqlite:///%s" % dictionary["database_name"])
448 
449 
def engine_from_dictionary(dictionary, pooling=True)
Definition: querying.py:426
def querying.new_connection_dictionary (   connection_data,
  secrets = None,
  mode = "r" 
)
Function used to construct connection data dictionaries - internal to framework.

Definition at line 347 of file querying.py.

References _get_netrc_data(), split, and str.

347 def new_connection_dictionary(connection_data, secrets=None, mode="r"):
348  """
349  Function used to construct connection data dictionaries - internal to framework.
350  """
351  frontier_str_length = len("frontier://")
352  sqlite_str_length = len("sqlite://")
353  #sqlite_file_str_length = len("sqlite_file://")
354  oracle_str_length = len("oracle://")
355 
356  if type(connection_data) in [str, unicode] and connection_data[0:frontier_str_length] == "frontier://":
357  """
358  frontier://database_name/schema
359  """
360  db_name = connection_data[frontier_str_length:].split("/")[0]
361  schema = connection_data[frontier_str_length:].split("/")[1]
362  connection_data = {}
363  connection_data["database_name"] = db_name
364  connection_data["schema"] = schema
365  connection_data["host"] = "frontier"
366  connection_data["secrets"] = None
367  elif type(connection_data) in [str, unicode] and connection_data[0:sqlite_str_length] == "sqlite://":
368  """
369  sqlite://database_file_name
370  """
371  # for now, just support "sqlite://" format for sqlite connection strings
372  db_name = connection_data[sqlite_str_length:]
373  schema = ""
374  connection_data = {}
375  connection_data["database_name"] = os.path.abspath(db_name)
376  connection_data["schema"] = schema
377  connection_data["host"] = "sqlite"
378  connection_data["secrets"] = None
379  elif type(connection_data) in [str, unicode] and connection_data[0:oracle_str_length] == "oracle://":
380  """
381  oracle://account:password@database_name
382  or
383  oracle://database_name/schema (requires a separate method of authentication - either dictionary or netrc)
384  """
385  new_connection_string = connection_data[oracle_str_length:]
386 
387  if ":" in new_connection_string:
388  # the user has given a password - usually in the case of the db upload service
389  database_name = new_connection_string[new_connection_string.index("@")+1:]
390  schema_name = new_connection_string[0:new_connection_string.index(":")]
391  # set username based on connection string
392  username = new_connection_string[0:new_connection_string.index(":")]
393  password = new_connection_string[new_connection_string.index(":")+1:new_connection_string.index("@")]
394  else:
395  mode_to_netrc_key_suffix = {"r" : "read", "w" : "write"}
396  database_name = new_connection_string[0:new_connection_string.index("/")]
397  schema_name = new_connection_string[new_connection_string.index("/")+1:]
398  if secrets == None:
399  username = str(raw_input("Enter the username you want to connect to the schema '%s' with: " % (schema_name)))
400  password = str(raw_input("Enter the password for the user '%s' in database '%s': " % (username, database_name)))
401  else:
402  if isinstance(secrets, str):
403  netrc_key = "%s/%s/%s" % (database_name, schema_name, mode_to_netrc_key_suffix[mode])
404  netrc_data = _get_netrc_data(secrets, key=netrc_key)
405  # take the username from the netrc entry corresponding to the mode the database is opened in
406  # eg, if the user has given mode="read", the database_name/schema_name/read entry will be taken
407  username = netrc_data["login"]
408  password = netrc_data["password"]
409  elif isinstance(secrets, dict):
410  username = secrets["user"]
411  password = secrets["password"]
412  else:
413  raise Exception("Invalid type given for secrets. Either an str or a dict must be given.")
414 
415  #print("Connected to database %s, schema %s, with username %s." % (database_name, schema_name, username))
416 
417  connection_data = {}
418  connection_data["database_name"] = database_name
419  connection_data["schema"] = schema_name
420  connection_data["password"] = password
421  connection_data["host"] = "oracle"
422  connection_data["secrets"] = {"login" : username, "password" : password}
423 
424  return connection_data
425 
def new_connection_dictionary(connection_data, secrets=None, mode="r")
Definition: querying.py:347
#define str(s)
def _get_netrc_data(netrc_file, key)
Definition: querying.py:334
double split
Definition: MVATrainer.cc:139