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 336 of file querying.py.

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

Referenced by new_connection_dictionary().

336 def _get_netrc_data(netrc_file, key):
337  """
338  Returns a dictionary {login : ..., account : ..., password : ...}
339  """
340  try:
341  headers = ["login", "account", "password"]
342  authenticator_tuple = netrc.netrc(netrc_file).authenticators(key)
343  if authenticator_tuple == None:
344  raise Exception("netrc file must contain key '%s'." % key)
345  except:
346  raise Exception("Couldn't get credentials from netrc file.")
347  return dict(zip(headers, authenticator_tuple))
348 
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def _get_netrc_data(netrc_file, key)
Definition: querying.py:336
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 452 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().

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

Definition at line 428 of file querying.py.

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

Definition at line 349 of file querying.py.

References _get_netrc_data(), split, and str.

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