pgtoolkit/toolbase.py
changeset 13 16dc5dec9c36
parent 11 bc69eca59041
child 14 a900bc629ecc
equal deleted inserted replaced
12:203be9022b46 13:16dc5dec9c36
     1 import argparse
     1 import argparse
       
     2 import logging
     2 
     3 
     3 from pgtoolkit import pgmanager, config
     4 from pgtoolkit import pgmanager, config
       
     5 from pgtoolkit.coloredformatter import ColoredFormatter
       
     6 from pgtoolkit.highlight import highlight
     4 
     7 
     5 
     8 
     6 class ToolBase:
     9 class ToolBase:
     7     def __init__(self, name, desc):
    10     def __init__(self, name, desc):
     8         self.parser = argparse.ArgumentParser(description=desc)
    11         self.parser = argparse.ArgumentParser(description=desc)
       
    12         self.parser.add_argument('-d', dest='debug', action='store_true',
       
    13             help='Debug mode - print database queries.')
     9         
    14         
    10         self.config = config.ConfigParser()
    15         self.config = config.ConfigParser()
    11         self.config.add_argument('databases', type=dict)
    16         self.config.add_argument('databases', type=dict)
    12         self.config.add_argument('meta_db')
    17         self.config.add_argument('meta_db')
    13         self.config.add_argument('meta_query')
    18         self.config.add_argument('meta_query')
    15         self.pgm = pgmanager.get_instance()
    20         self.pgm = pgmanager.get_instance()
    16         
    21         
    17     def init(self):
    22     def init(self):
    18         self.config.load('pgtoolkit.conf')
    23         self.config.load('pgtoolkit.conf')
    19         self.args = self.parser.parse_args()
    24         self.args = self.parser.parse_args()
       
    25         if self.args.debug:
       
    26             handler = logging.StreamHandler()
       
    27             format = ColoredFormatter(
       
    28                 highlight(1,7,0)+'%(asctime)s %(levelname)-5s'+highlight(0)+' %(message)s', '%H:%M:%S')
       
    29             handler.setFormatter(format)
       
    30             handler.setLevel(logging.DEBUG)
       
    31             logger = logging.getLogger('pgmanager')
       
    32             logger.addHandler(handler)
    20 
    33 
    21     def buildconn(self, name, targetname):
    34     def buildconn(self, name, targetname):
    22         with self.pgm.cursor('meta') as curs:
    35         with self.pgm.cursor('meta') as curs:
    23             curs.execute(self.config.meta_query, [targetname])
    36             curs.execute(self.config.meta_query, [targetname])
    24             row = curs.fetchone_dict()
    37             row = curs.fetchone_dict()