CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 337 of file querying.py.

References ComparisonHelper.zip().

Referenced by new_connection_dictionary().

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

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

Definition at line 429 of file querying.py.

Referenced by querying.connection.setup().

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

Definition at line 350 of file querying.py.

References _get_netrc_data(), input, submitPVValidationJobs.split(), and str.

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