Package Gnumed :: Package pycommon :: Module gmLoginInfo |
[frames] | no frames]

# Source Code for Module Gnumed.pycommon.gmLoginInfo

      1  ############################################################################
      2  # gmLoginInfo - a class to encapsulate Postgres login information
      3  ############################################################################
      4  __author__ = "H. Herb <hherb@gnumed.net>, I. Haywood <i.haywood@ugrad.unimelb.edu.au>"
      5  __license__ = 'GPL v2 or later (details at http://www.gnu.org)'
      6
      7  import logging
      8
      9  _log = logging.getLogger('gm.db')
     10  #====================================================================
     11 -class LoginInfo:
     12          """a class to encapsulate Postgres login information to default database"""
     13
     14          # private variables
     15  #       user = ''
     16  #       password = ''
     17  #       host = ''
     18  #       port = 5432
     19  #       database = ''
     20          #------------------------------------------
     21 -        def __init__(self, user=None, password=None, host=None, port=5432, database=None):
     22                  self.user = user
     23                  self.password = password
     24                  self.host = host
     25                  self.port = port
     26                  self.database = database
     27          #------------------------------------------
     28 -        def _get_port(self):
     29                  return self.__port
     30
     31 -        def _set_port(self, value):
     32                  self.__port = int(value)
     33
     34          port = property(_get_port, _set_port)
     35          #------------------------------------------
     36 -        def GetInfo(self):
     37                  return (
     38                          self.GetUser(),
     39                          self.GetPassword(),
     40                          self.GetHost(),
     41                          self.GetPort(),
     42                          self.GetDatabase(),
     43                          self.GetProfile()
     44                  )
     45          #------------------------------------------
     46 -        def GetInfoStr(self):
     47                  # don't hand out passwords just like that
     48                  info = "host:port=%s:%s, db=%s, user=%s, pw=??" % (
     49                                          self.GetHost(),
     50                                          str(self.GetPort()),
     51                                          self.GetDatabase(),
     52                                          self.GetUser()
     53                                  )
     54                  return info
     55          #------------------------------------------
     56 -        def GetPGDB_DSN(self):
     57                  host = self.GetHost()
     58                  port = str(self.GetPort())
     59                  # for local UNIX domain sockets connections: leave host/port empty
     60                  # IH: *PLEASE* option of local TCP/IP connection must be available
     61  #               if host in ['', 'localhost']:
     62  #                       host = ""
     63                  if host == '':
     64                          port = ''
     65                  dsn = "%s:%s:%s:%s" % (
     66                          host,
     67                          self.GetDatabase(),
     68                          self.GetUser(),
     69                          self.GetPassword()
     70                  )
     71                  host_port = "%s:%s" % (host, port)
     72                  return dsn, host_port
     73          #------------------------------------------
     74 -        def get_psycopg2_dsn(self):
     75                  dsn_parts = []
     76
     77                  if self.database.strip() != '':
     78                          dsn_parts.append('dbname=%s' % self.database)
     79
     80                  if self.host.strip() != '':
     81                          dsn_parts.append('host=%s' % self.host)
     82
     83                  dsn_parts.append('port=%s' % self.port)
     84
     85                  if self.user.strip() != '':
     86                          dsn_parts.append('user=%s' % self.user)
     87
     88                  if self.password.strip() != '':
     89                          dsn_parts.append('password=%s' % self.password)
     90
     91                  return ' '.join(dsn_parts)
     92          #------------------------------------------
     93 -        def GetDBAPI_DSN(self):
     94                  host = self.GetHost()
     95                  port = str(self.GetPort())
     96                  # for local UNIX domain sockets connections: leave host/port empty
     97  #               if host in ['', 'localhost']:
     98  #                       host = ''
     99                  if host == '':
    100                          port = ''
    101                  dsn = "%s:%s:%s:%s:%s" % (
    102                          host,
    103                          port,
    104                          self.GetDatabase(),
    105                          self.GetUser(),
    106                          self.GetPassword()
    107                  )
    108                  return dsn
    109          #------------------------------------------
    110 -        def SetUser(self, user):
    111                  self.user = user
    112          #------------------------------------------
    113 -        def GetUser(self):
    114                  return self.user
    115          #------------------------------------------
    116 -        def SetDatabase(self, dbname):
    117                  self.database = dbname
    118          #------------------------------------------
    119 -        def GetDatabase(self):
    120                  return self.database
    121          #------------------------------------------
    122 -        def SetHost(self, host):
    123                  self.host = host
    124          #------------------------------------------
    125 -        def GetHost(self):
    126                  return self.host
    127          #------------------------------------------
    128 -        def SetPort(self, port):
    129                  try:
    130                          port = int (port)
    131                  except ValueError:
    132                          _log.warning("tried to set port to '%s', set to -1" % port)
    133                          port = -1
    134                  self.port = port
    135          #------------------------------------------
    136 -        def GetPort(self):
    137                  return self.port
    138          #------------------------------------------
    139 -        def SetProfile(self, profile):
    140                  self.__profile = profile
    141          #------------------------------------------
    142 -        def GetProfile(self):
    143                  return self.__profile
    144          #------------------------------------------
    145 -        def Clear(self):
    146                  "clears all connection information regarding user, password etc."
    147
    148                  self.user = "guest"
    149                  self.password = ""
    150                  self.host = ''
    151                  self.port = 5432
    152                  self.database = "gnumed_v9"
    153                  self.__profile = 'default'
    154
    155  #====================================================================
    156  if __name__ == "__main__" :
    157          print "Please somebody write a module test function here!"
    158