pgtoolkit/toolbase.py
changeset 17 f768a3529ee7
parent 14 a900bc629ecc
child 20 73f0d53fef6b
equal deleted inserted replaced
16:cb7e13711a99 17:f768a3529ee7
    39             handler.setFormatter(format)
    39             handler.setFormatter(format)
    40             handler.setLevel(logging.DEBUG)
    40             handler.setLevel(logging.DEBUG)
    41             logger = logging.getLogger('pgmanager')
    41             logger = logging.getLogger('pgmanager')
    42             logger.addHandler(handler)
    42             logger.addHandler(handler)
    43 
    43 
    44     def buildconn(self, name, targetname):
    44     def buildconn(self, name, lookup_name):
    45         with self.pgm.cursor('meta') as curs:
    45         with self.pgm.cursor('meta') as curs:
    46             curs.execute(self.config.meta_query, [targetname])
    46             curs.execute(self.config.meta_query, [lookup_name])
    47             row = curs.fetchone_dict()
    47             row = curs.fetchone_dict()
    48             curs.connection.commit()
    48             curs.connection.commit()
    49             
    49             
    50             if not row:
    50             if not row:
    51                 raise Exception('Unknown database "%s"' % targetname)
    51                 raise Exception('Unknown database "%s"' % lookup_name)
    52             
    52             
    53             self.pgm.create_conn(name=name, **row)
    53             self.pgm.create_conn(name=name, **row)
    54 
    54 
    55     def prepareconns(self, *args):        
    55     def prepareconns(self, *pgm_names):
       
    56         # use meta db
    56         if self.config.meta_db:
    57         if self.config.meta_db:
    57             self.pgm.create_conn(name='meta', dsn=self.config.meta_db)
    58             self.pgm.create_conn(name='meta', dsn=self.config.meta_db)
    58             for name in args:
    59             for name in pgm_names:
    59                 self.buildconn(name, self.args.__dict__[name])
    60                 lookup_name = self.args.__dict__[name]
       
    61                 self.buildconn(name, lookup_name)
    60             self.pgm.close_conn('meta')
    62             self.pgm.close_conn('meta')
       
    63         
       
    64         # use config dict 'databases'
       
    65         if self.config.databases:
       
    66             for name in pgm_names:
       
    67                 lookup_name = self.args.__dict__[name]
       
    68                 dsn = self.config.databases[lookup_name]
       
    69                 self.pgm.create_conn(name=name, dsn=dsn)
    61 
    70 
    62 
    71 
    63 class SimpleTool(ToolBase):
    72 class SimpleTool(ToolBase):
    64     def __init__(self, name, desc):
    73     def __init__(self, name, desc):
    65         ToolBase.__init__(self, name, desc)        
    74         ToolBase.__init__(self, name, desc)