pgtoolkit/toolbase.py
changeset 62 af637235ca81
parent 56 94e091c23ebb
child 78 64c62ac8f65d
equal deleted inserted replaced
61:703bba757605 62:af637235ca81
    34         self.args = self.parser.parse_args()
    34         self.args = self.parser.parse_args()
    35         self.init_logging()
    35         self.init_logging()
    36 
    36 
    37     def init_logging(self):
    37     def init_logging(self):
    38         # logging
    38         # logging
       
    39         format = ColoredFormatter(highlight(1,7,0)+'%(asctime)s %(levelname)-5s'+highlight(0)+' %(message)s', '%H:%M:%S')
    39         handler = logging.StreamHandler()
    40         handler = logging.StreamHandler()
    40         format = ColoredFormatter(highlight(1,7,0)+'%(asctime)s %(levelname)-5s'+highlight(0)+' %(message)s', '%H:%M:%S')
       
    41         handler.setFormatter(format)
    41         handler.setFormatter(format)
    42         handler.setLevel(logging.DEBUG)
    42         handler.setLevel(logging.DEBUG)
    43         self.log = logging.getLogger('main')
    43         self.log = logging.getLogger('main')
    44         self.log.addHandler(handler)
    44         self.log.addHandler(handler)
    45         self.log.setLevel(logging.DEBUG)
    45         self.log.setLevel(logging.DEBUG)
    46 
    46 
    47         logger_notices = logging.getLogger('pgmanager_notices')
    47         log_notices = logging.getLogger('pgmanager_notices')
    48         logger_notices.addHandler(handler)
    48         log_notices.addHandler(handler)
    49         logger_notices.setLevel(logging.DEBUG)
    49         log_notices.setLevel(logging.DEBUG)
    50 
    50 
    51         if self.args.debug:
    51         if self.args.debug:
    52             logger_sql = logging.getLogger('pgmanager_sql')
    52             log_sql = logging.getLogger('pgmanager_sql')
    53             logger_sql.addHandler(handler)
    53             log_sql.addHandler(handler)
    54             logger_sql.setLevel(logging.DEBUG)
    54             log_sql.setLevel(logging.DEBUG)
    55 
    55 
    56     def prepare_conn_from_metadb(self, name, lookup_name):
    56     def prepare_conn_from_metadb(self, name, lookup_name):
    57         '''Create connection in pgmanager using meta DB.
    57         '''Create connection in pgmanager using meta DB.
    58 
    58 
    59         name -- Name for connection in pgmanager.
    59         name -- Name for connection in pgmanager.
    78                 self.pgm.create_conn(name=name,
    78                 self.pgm.create_conn(name=name,
    79                     isolation_level=self.target_isolation_level,
    79                     isolation_level=self.target_isolation_level,
    80                     dsn=dsn)
    80                     dsn=dsn)
    81                 return True
    81                 return True
    82 
    82 
    83     def prepare_conns_from_cmdline_args(self, *pgm_names):
    83     def prepare_conns(self, **kwargs):
       
    84         """Create connections in PgManager.
       
    85 
       
    86         Keyword arguments meaning:
       
    87             key: connection name for use in PgManager
       
    88             value: connection name in config or meta DB
       
    89 
       
    90         """
    84         if self.config.meta_db:
    91         if self.config.meta_db:
    85             self.pgm.create_conn(name='meta', dsn=self.config.meta_db)
    92             self.pgm.create_conn(name='meta', dsn=self.config.meta_db)
    86 
    93 
    87         for name in pgm_names:
    94         for name in kwargs:
    88             lookup_name = self.args.__dict__[name]
    95             lookup_name = kwargs[name]
    89             found = self.prepare_conn_from_config(name, lookup_name)
    96             found = self.prepare_conn_from_config(name, lookup_name)
    90             if not found and self.config.meta_db:
    97             if not found and self.config.meta_db:
    91                 found = self.prepare_conn_from_metadb(name, lookup_name)
    98                 found = self.prepare_conn_from_metadb(name, lookup_name)
    92             if not found:
    99             if not found:
    93                 raise ConnectionInfoNotFound('Connection name "%s" not found in config nor in meta DB.' % lookup_name)
   100                 raise ConnectionInfoNotFound('Connection name "%s" not found in config nor in meta DB.' % lookup_name)
   101         ToolBase.__init__(self, name, desc, **kwargs)
   108         ToolBase.__init__(self, name, desc, **kwargs)
   102         self.parser.add_argument('target', metavar='target', type=str, help='Target database')
   109         self.parser.add_argument('target', metavar='target', type=str, help='Target database')
   103 
   110 
   104     def init(self):
   111     def init(self):
   105         ToolBase.init(self)
   112         ToolBase.init(self)
   106         self.prepare_conns_from_cmdline_args('target')
   113         self.prepare_conns(target=self.args.target)
   107 
   114 
   108 
   115 
   109 class SrcDstTool(ToolBase):
   116 class SrcDstTool(ToolBase):
   110     def __init__(self, name, desc, **kwargs):
   117     def __init__(self, name, desc, **kwargs):
   111         ToolBase.__init__(self, name, desc, **kwargs)
   118         ToolBase.__init__(self, name, desc, **kwargs)
   116 
   123 
   117     def init(self):
   124     def init(self):
   118         ToolBase.init(self)
   125         ToolBase.init(self)
   119         if self.is_reversed():
   126         if self.is_reversed():
   120             self.args.src, self.args.dst = self.args.dst, self.args.src
   127             self.args.src, self.args.dst = self.args.dst, self.args.src
   121         self.prepare_conns_from_cmdline_args('src', 'dst')
   128         self.prepare_conns(src=self.args.src, dst=self.args.dst)
   122 
   129 
   123     def is_reversed(self):
   130     def is_reversed(self):
   124         return 'reverse' in self.args and self.args.reverse
   131         return 'reverse' in self.args and self.args.reverse
   125 
   132 
   126 
   133