LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
lsst.daf.persistence.registries.PgsqlRegistry Class Reference
Inheritance diagram for lsst.daf.persistence.registries.PgsqlRegistry:
lsst.daf.persistence.registries.SqlRegistry lsst.daf.persistence.registries.Registry

Public Member Functions

def __init__ (self, location)
 
def lookup (self, *args, **kwargs)
 
def lookup (self, lookupProperties, reference, dataId, **kwargs)
 
def executeQuery (self, returnFields, joinClause, whereFields, range, values)
 

Static Public Member Functions

def readYaml (location)
 
def create (location)
 

Public Attributes

 root
 
 conn
 

Static Public Attributes

string placeHolder = "%s"
 

Detailed Description

A PostgreSQL-based registry

Definition at line 456 of file registries.py.

Constructor & Destructor Documentation

◆ __init__()

def lsst.daf.persistence.registries.PgsqlRegistry.__init__ (   self,
  location 
)
Constructor

Parameters
----------
location : `str`
    Path to PostgreSQL configuration file.

Reimplemented from lsst.daf.persistence.registries.SqlRegistry.

Definition at line 460 of file registries.py.

460  def __init__(self, location):
461  """Constructor
462 
463  Parameters
464  ----------
465  location : `str`
466  Path to PostgreSQL configuration file.
467  """
468  if not havePgsql:
469  raise RuntimeError("Cannot use PgsqlRegistry: could not import psycopg2")
470  config = self.readYaml(location)
471  self._config = config
472  conn = pgsql.connect(host=config["host"], port=config["port"], database=config["database"],
473  user=config["user"], password=config["password"])
474  self.root = location
475  SqlRegistry.__init__(self, conn)
476 

Member Function Documentation

◆ create()

def lsst.daf.persistence.registries.Registry.create (   location)
staticinherited
Create a registry object of an appropriate type.
@param location (string) Path or URL for registry, or None if
                         unavailable

Definition at line 71 of file registries.py.

71  def create(location):
72  """Create a registry object of an appropriate type.
73  @param location (string) Path or URL for registry, or None if
74  unavailable"""
75 
76  if location is None:
77  return
78 
79  # if re.match(r'.*\.registry', location):
80  # return FileRegistry(location)
81 
82  if location.endswith(".pgsql"):
83  return PgsqlRegistry(location)
84 
85  # look for an sqlite3 registry
86  if re.match(r'.*\.sqlite3', location):
87  if not haveSqlite3:
88  raise RuntimeError("sqlite3 registry specified (%s), but unable to import sqlite3 module" %
89  (location,))
90  registry = SqliteRegistry(location)
91  if registry.conn is None:
92  return None
93  return registry
94 
95  # if re.match(r'mysql:', location):
96  # return DbRegistry(location)
97  # return FsRegistry(location)
98 
99  # next try to create a PosixRegistry
100  if os.path.isdir(location):
101  return PosixRegistry(root=location)
102 
103  raise RuntimeError("Unable to create registry using location: " + location)
104 
105 

◆ executeQuery()

def lsst.daf.persistence.registries.SqlRegistry.executeQuery (   self,
  returnFields,
  joinClause,
  whereFields,
  range,
  values 
)
inherited
Extract metadata from the registry.
@param returnFields (list of strings) Metadata fields to be extracted.
@param joinClause   (list of strings) Tables in which metadata fields
                    are located.
@param whereFields  (list of tuples) First tuple element is metadata
                    field to query; second is the value that field
                    must have (often '?').
@param range        (tuple) Value, lower limit, and upper limit for a
                    range condition on the metadata.  Any of these can
                    be metadata fields.
@param values       (tuple) Tuple of values to be substituted for '?'
                    characters in the whereFields values or the range
                    values.
@return (list of tuples) All sets of field values that meet the
        criteria

Definition at line 401 of file registries.py.

401  def executeQuery(self, returnFields, joinClause, whereFields, range, values):
402  """Extract metadata from the registry.
403  @param returnFields (list of strings) Metadata fields to be extracted.
404  @param joinClause (list of strings) Tables in which metadata fields
405  are located.
406  @param whereFields (list of tuples) First tuple element is metadata
407  field to query; second is the value that field
408  must have (often '?').
409  @param range (tuple) Value, lower limit, and upper limit for a
410  range condition on the metadata. Any of these can
411  be metadata fields.
412  @param values (tuple) Tuple of values to be substituted for '?'
413  characters in the whereFields values or the range
414  values.
415  @return (list of tuples) All sets of field values that meet the
416  criteria"""
417  if not self.conn:
418  return None
419  cmd = "SELECT DISTINCT "
420  cmd += ", ".join(returnFields)
421  cmd += " FROM " + " NATURAL JOIN ".join(joinClause)
422  whereList = []
423  if whereFields:
424  for k, v in whereFields:
425  whereList.append("(%s = %s)" % (k, v))
426  if range is not None:
427  whereList.append("(%s BETWEEN %s AND %s)" % range)
428  if len(whereList) > 0:
429  cmd += " WHERE " + " AND ".join(whereList)
430  cursor = self.conn.cursor()
431  cursor.execute(cmd, values)
432  return [row for row in cursor.fetchall()]
433 
434 

◆ lookup() [1/2]

def lsst.daf.persistence.registries.PgsqlRegistry.lookup (   self,
args,
**  kwargs 
)

Definition at line 525 of file registries.py.

525  def lookup(self, *args, **kwargs):
526  try:
527  return SqlRegistry.lookup(self, *args, **kwargs)
528  except Exception:
529  self.conn.rollback()
530  raise

◆ lookup() [2/2]

def lsst.daf.persistence.registries.SqlRegistry.lookup (   self,
  lookupProperties,
  reference,
  dataId,
**  kwargs 
)
inherited
Perform a lookup in the registry.

Return values are refined by the values in dataId.
Returns a list of values that match keys in lookupProperties.
e.g. if the template is 'raw/raw_v%(visit)d_f%(filter)s.fits.gz', and
dataId={'visit':1}, and lookupProperties is ['filter'], and the
filesystem under self.root has exactly one file 'raw/raw_v1_fg.fits.gz'
then the return value will be [('g',)]

:param lookupProperties:
:param dataId: must be a key/value iterable. Keys must be string.
If value is a string then will look for elements in the repository that match value for value.
If value is a 2-item iterable then will look for elements in the repository where the value is between
the values of value[0] and value[1].
:param reference: other data types that may be used to search for values.
:param **kwargs: nothing needed for sqlite lookup
:return: a list of values that match keys in lookupProperties.

Definition at line 370 of file registries.py.

370  def lookup(self, lookupProperties, reference, dataId, **kwargs):
371  """Perform a lookup in the registry.
372 
373  Return values are refined by the values in dataId.
374  Returns a list of values that match keys in lookupProperties.
375  e.g. if the template is 'raw/raw_v%(visit)d_f%(filter)s.fits.gz', and
376  dataId={'visit':1}, and lookupProperties is ['filter'], and the
377  filesystem under self.root has exactly one file 'raw/raw_v1_fg.fits.gz'
378  then the return value will be [('g',)]
379 
380  :param lookupProperties:
381  :param dataId: must be a key/value iterable. Keys must be string.
382  If value is a string then will look for elements in the repository that match value for value.
383  If value is a 2-item iterable then will look for elements in the repository where the value is between
384  the values of value[0] and value[1].
385  :param reference: other data types that may be used to search for values.
386  :param **kwargs: nothing needed for sqlite lookup
387  :return: a list of values that match keys in lookupProperties.
388  """
389  if not self.conn:
390  return None
391 
392  # input variable sanitization:
393  reference = sequencify(reference)
394  lookupProperties = sequencify(lookupProperties)
395 
396  try:
397  return self._lookup(lookupProperties, dataId, reference)
398  except sqlite3.OperationalError: # try again, with extra checking of the dataId keys
399  return self._lookup(lookupProperties, dataId, reference, checkColumns=True)
400 

◆ readYaml()

def lsst.daf.persistence.registries.PgsqlRegistry.readYaml (   location)
static
Read YAML configuration file

The YAML configuration file should contain:
* host : host name for database connection
* port : port for database connection
* user : user name for database connection
* database : database name

It may also contain:
* password : password for database connection

The optional entries are set to `None` in the output configuration.

Parameters
----------
location : `str`
    Path to PostgreSQL YAML config file.

Returns
-------
config : `dict`
    Configuration

Definition at line 478 of file registries.py.

478  def readYaml(location):
479  """Read YAML configuration file
480 
481  The YAML configuration file should contain:
482  * host : host name for database connection
483  * port : port for database connection
484  * user : user name for database connection
485  * database : database name
486 
487  It may also contain:
488  * password : password for database connection
489 
490  The optional entries are set to `None` in the output configuration.
491 
492  Parameters
493  ----------
494  location : `str`
495  Path to PostgreSQL YAML config file.
496 
497  Returns
498  -------
499  config : `dict`
500  Configuration
501  """
502  try:
503  # PyYAML >=5.1 prefers a different loader
504  loader = yaml.UnsafeLoader
505  except AttributeError:
506  loader = yaml.Loader
507  with open(location) as ff:
508  data = yaml.load(ff, Loader=loader)
509  requireKeys = set(["host", "port", "database", "user"])
510  optionalKeys = set(["password"])
511  haveKeys = set(data.keys())
512  if haveKeys - optionalKeys != requireKeys:
513  raise RuntimeError(
514  "PostgreSQL YAML configuration (%s) should contain only %s, and may contain 'password', "
515  "but this contains: %s" %
516  (location, ",".join("'%s'" % key for key in requireKeys),
517  ",".join("'%s'" % key for key in data.keys()))
518  )
519  for key in optionalKeys:
520  if key not in data:
521  data[key] = None
522 
523  return data
524 
daf::base::PropertySet * set
Definition: fits.cc:912

Member Data Documentation

◆ conn

lsst.daf.persistence.registries.SqlRegistry.conn
inherited

Definition at line 313 of file registries.py.

◆ placeHolder

string lsst.daf.persistence.registries.PgsqlRegistry.placeHolder = "%s"
static

Definition at line 458 of file registries.py.

◆ root

lsst.daf.persistence.registries.PgsqlRegistry.root

Definition at line 474 of file registries.py.


The documentation for this class was generated from the following file: