test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Classes | Functions
querying Namespace Reference

Classes

class  connection
 
class  factory
 

Functions

def _get_netrc_data
 
def connect
 
def engine_from_dictionary
 
def new_connection_dictionary
 

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().

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

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

Definition at line 426 of file querying.py.

Referenced by querying.connection.setup().

427 def engine_from_dictionary(dictionary, pooling=True):
428  if dictionary["host"] != "sqlite":
429  if dictionary["host"] != "frontier":
430  # probably oracle
431  # if not frontier, we have to authenticate
432  user = dictionary["secrets"]["login"]
433  pwd = dictionary["secrets"]["password"]
434  # set max label length for oracle
435  if pooling:
436  return create_engine(connection.build_oracle_url(user, pwd, dictionary["database_name"]), label_length=6)
437  else:
438  return create_engine(connection.build_oracle_url(user, pwd, dictionary["database_name"]), label_length=6, poolclass=NullPool)
439  else:
440  # if frontier, no need to authenticate
441  # set max label length for frontier
442  if pooling:
443  return create_engine(connection.build_frontier_url(dictionary["database_name"], dictionary["schema"]), label_length=6)
444  else:
445  return create_engine(connection.build_frontier_url(dictionary["database_name"], dictionary["schema"]), label_length=6, poolclass=NullPool)
446  else:
447  # if host is sqlite, making the url is easy - no authentication
448  return create_engine("sqlite:///%s" % dictionary["database_name"])
449 
def engine_from_dictionary
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(), and split.

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