pgtoolkit/toolbase.py
changeset 20 73f0d53fef6b
parent 17 f768a3529ee7
child 29 8636719a30f6
equal deleted inserted replaced
19:e526ca146fa9 20:73f0d53fef6b
     2 import logging
     2 import logging
     3 
     3 
     4 from pgtoolkit import pgmanager, config
     4 from pgtoolkit import pgmanager, config
     5 from pgtoolkit.coloredformatter import ColoredFormatter
     5 from pgtoolkit.coloredformatter import ColoredFormatter
     6 from pgtoolkit.highlight import highlight
     6 from pgtoolkit.highlight import highlight
       
     7 
       
     8 
       
     9 class ConnectionInfoNotFound(Exception):    
       
    10     pass
     7 
    11 
     8 
    12 
     9 class ToolBase:
    13 class ToolBase:
    10     def __init__(self, name, desc):
    14     def __init__(self, name, desc):
    11         self.parser = argparse.ArgumentParser(description=desc)
    15         self.parser = argparse.ArgumentParser(description=desc)
    17         self.config.add_argument('meta_db')
    21         self.config.add_argument('meta_db')
    18         self.config.add_argument('meta_query')
    22         self.config.add_argument('meta_query')
    19         
    23         
    20         self.pgm = pgmanager.get_instance()
    24         self.pgm = pgmanager.get_instance()
    21         
    25         
    22     def init(self):
    26     def init(self, *args):
    23         self.config.load('pgtoolkit.conf')
    27         self.config.load('pgtoolkit.conf')
    24         self.args = self.parser.parse_args()
    28         self.args = self.parser.parse_args()
    25         self.init_logging()
    29         self.init_logging()
    26         
    30         self.prepare_conns(*args)
       
    31     
    27     def init_logging(self):
    32     def init_logging(self):
    28         # logging
    33         # logging
    29         handler = logging.StreamHandler()
    34         handler = logging.StreamHandler()
    30         format = ColoredFormatter(highlight(1,7,0)+'%(asctime)s %(levelname)-5s'+highlight(0)+' %(message)s', '%H:%M:%S')
    35         format = ColoredFormatter(highlight(1,7,0)+'%(asctime)s %(levelname)-5s'+highlight(0)+' %(message)s', '%H:%M:%S')
    31         handler.setFormatter(format)
    36         handler.setFormatter(format)
    39             handler.setFormatter(format)
    44             handler.setFormatter(format)
    40             handler.setLevel(logging.DEBUG)
    45             handler.setLevel(logging.DEBUG)
    41             logger = logging.getLogger('pgmanager')
    46             logger = logging.getLogger('pgmanager')
    42             logger.addHandler(handler)
    47             logger.addHandler(handler)
    43 
    48 
    44     def buildconn(self, name, lookup_name):
    49     def prepare_conn_from_metadb(self, name, lookup_name):
       
    50         '''Create connection in pgmanager using meta DB.
       
    51         
       
    52         name -- Name for connection in pgmanager.
       
    53         lookup_name -- Name of connection in meta DB.
       
    54         
       
    55         '''
    45         with self.pgm.cursor('meta') as curs:
    56         with self.pgm.cursor('meta') as curs:
    46             curs.execute(self.config.meta_query, [lookup_name])
    57             curs.execute(self.config.meta_query, [lookup_name])
    47             row = curs.fetchone_dict()
    58             row = curs.fetchone_dict()
    48             curs.connection.commit()
    59             curs.connection.commit()
    49             
    60             if row:
    50             if not row:
    61                 self.pgm.create_conn(name=name, **row)
    51                 raise Exception('Unknown database "%s"' % lookup_name)
    62                 return True
    52             
       
    53             self.pgm.create_conn(name=name, **row)
       
    54 
    63 
    55     def prepareconns(self, *pgm_names):
    64     def prepare_conn_from_config(self, name, lookup_name):
    56         # use meta db
    65         '''Create connection in pgmanager using info in config.databases.'''
       
    66         if self.config.databases:
       
    67             if lookup_name in self.config.databases:
       
    68                 dsn = self.config.databases[lookup_name]
       
    69                 self.pgm.create_conn(name=name, dsn=dsn)
       
    70                 return True
       
    71 
       
    72     def prepare_conns(self, *pgm_names):
    57         if self.config.meta_db:
    73         if self.config.meta_db:
    58             self.pgm.create_conn(name='meta', dsn=self.config.meta_db)
    74             self.pgm.create_conn(name='meta', dsn=self.config.meta_db)
    59             for name in pgm_names:
    75             
    60                 lookup_name = self.args.__dict__[name]
    76         for name in pgm_names:
    61                 self.buildconn(name, lookup_name)
    77             lookup_name = self.args.__dict__[name]
       
    78             found = self.prepare_conn_from_config(name, lookup_name)
       
    79             if not found and self.config.meta_db:
       
    80                 found = self.prepare_conn_from_metadb(name, lookup_name)
       
    81             if not found:
       
    82                 raise ConnectionInfoNotFound('Connection name "%s" not found in config nor in meta DB.' % lookup_name)
       
    83         
       
    84         if self.config.meta_db:
    62             self.pgm.close_conn('meta')
    85             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)
       
    70 
    86 
    71 
    87 
    72 class SimpleTool(ToolBase):
    88 class SimpleTool(ToolBase):
    73     def __init__(self, name, desc):
    89     def __init__(self, name, desc):
    74         ToolBase.__init__(self, name, desc)        
    90         ToolBase.__init__(self, name, desc)        
    75         self.parser.add_argument('target', metavar='target', type=str, help='Target database')
    91         self.parser.add_argument('target', metavar='target', type=str, help='Target database')
    76       
    92       
    77     def init(self):
    93     def init(self):
    78         ToolBase.init(self)
    94         ToolBase.init(self, 'target')
    79         self.prepareconns('target')
       
    80 
    95 
    81 
    96 
    82 class SrcDstTool(ToolBase):
    97 class SrcDstTool(ToolBase):
    83     def __init__(self, name, desc):
    98     def __init__(self, name, desc):
    84         ToolBase.__init__(self, name, desc)        
    99         ToolBase.__init__(self, name, desc)        
    85         self.parser.add_argument('src', metavar='source', type=str, help='Source database')
   100         self.parser.add_argument('src', metavar='source', type=str, help='Source database')
    86         self.parser.add_argument('dst', metavar='destination', type=str, help='Destination database')
   101         self.parser.add_argument('dst', metavar='destination', type=str, help='Destination database')
    87       
   102       
    88     def init(self):
   103     def init(self):
    89         ToolBase.init(self)
   104         ToolBase.init(self, 'src', 'dst')
    90         self.prepareconns('src', 'dst')
       
    91 
   105