pgtoolkit/toolbase.py
changeset 56 94e091c23ebb
parent 51 bdc44f96cb0b
child 62 af637235ca81
equal deleted inserted replaced
55:adc1615d8fc5 56:94e091c23ebb
    14 class BadArgsError(Exception):
    14 class BadArgsError(Exception):
    15     pass
    15     pass
    16 
    16 
    17 
    17 
    18 class ToolBase:
    18 class ToolBase:
    19     def __init__(self, name, desc):
    19     def __init__(self, name, desc, **kwargs):
    20         self.parser = argparse.ArgumentParser(description=desc)
    20         self.parser = argparse.ArgumentParser(description=desc)
    21         self.parser.add_argument('-d', dest='debug', action='store_true',
    21         self.parser.add_argument('-d', dest='debug', action='store_true',
    22             help='Debug mode - print database queries.')
    22             help='Debug mode - print database queries.')
    23 
    23 
    24         self.config = config.ConfigParser()
    24         self.config = config.ConfigParser()
    95         if self.config.meta_db:
    95         if self.config.meta_db:
    96             self.pgm.close_conn('meta')
    96             self.pgm.close_conn('meta')
    97 
    97 
    98 
    98 
    99 class SimpleTool(ToolBase):
    99 class SimpleTool(ToolBase):
   100     def __init__(self, name, desc):
   100     def __init__(self, name, desc, **kwargs):
   101         ToolBase.__init__(self, name, desc)
   101         ToolBase.__init__(self, name, desc, **kwargs)
   102         self.parser.add_argument('target', metavar='target', type=str, help='Target database')
   102         self.parser.add_argument('target', metavar='target', type=str, help='Target database')
   103 
   103 
   104     def init(self):
   104     def init(self):
   105         ToolBase.init(self)
   105         ToolBase.init(self)
   106         self.prepare_conns_from_cmdline_args('target')
   106         self.prepare_conns_from_cmdline_args('target')
   107 
   107 
   108 
   108 
   109 class SrcDstTool(ToolBase):
   109 class SrcDstTool(ToolBase):
   110     def __init__(self, name, desc):
   110     def __init__(self, name, desc, **kwargs):
   111         ToolBase.__init__(self, name, desc)
   111         ToolBase.__init__(self, name, desc, **kwargs)
   112         self.parser.add_argument('src', metavar='source', type=str, help='Source database')
   112         self.parser.add_argument('src', metavar='source', type=str, help='Source database')
   113         self.parser.add_argument('dst', metavar='destination', type=str, help='Destination database')
   113         self.parser.add_argument('dst', metavar='destination', type=str, help='Destination database')
       
   114         if 'allow_reverse' in kwargs and kwargs['allow_reverse']:
       
   115             self.parser.add_argument('-r', '--reverse', action='store_true', help='Reverse operation. Swap source and destination.')
   114 
   116 
   115     def init(self):
   117     def init(self):
   116         ToolBase.init(self)
   118         ToolBase.init(self)
       
   119         if self.is_reversed():
       
   120             self.args.src, self.args.dst = self.args.dst, self.args.src
   117         self.prepare_conns_from_cmdline_args('src', 'dst')
   121         self.prepare_conns_from_cmdline_args('src', 'dst')
   118 
   122 
       
   123     def is_reversed(self):
       
   124         return 'reverse' in self.args and self.args.reverse
       
   125 
   119 
   126 
   120 class SrcDstTablesTool(SrcDstTool):
   127 class SrcDstTablesTool(SrcDstTool):
   121     def __init__(self, name, desc):
   128     def __init__(self, name, desc, **kwargs):
   122         SrcDstTool.__init__(self, name, desc)
   129         SrcDstTool.__init__(self, name, desc, **kwargs)
   123         self.parser.add_argument('-t', '--src-table', metavar='source_table',
   130         self.parser.add_argument('-t', '--src-table', metavar='source_table',
   124             dest='srctable', type=str, default='', help='Source table name.')
   131             dest='srctable', type=str, default='', help='Source table name.')
   125         self.parser.add_argument('-s', '--src-schema', metavar='source_schema',
   132         self.parser.add_argument('-s', '--src-schema', metavar='source_schema',
   126             dest='srcschema', type=str, default='', help='Source schema name (default=public).')
   133             dest='srcschema', type=str, default='', help='Source schema name (default=public).')
   127         self.parser.add_argument('--dst-table', metavar='destination_table',
   134         self.parser.add_argument('--dst-table', metavar='destination_table',
   149         # dest defaults to source
   156         # dest defaults to source
   150         if not self.schema2:
   157         if not self.schema2:
   151             self.schema2 = self.schema1
   158             self.schema2 = self.schema1
   152         if not self.table2:
   159         if not self.table2:
   153             self.table2 = self.table1
   160             self.table2 = self.table1
       
   161 
       
   162         # swap src, dst when in reverse mode
       
   163         if self.is_reversed():
       
   164             self.schema1, self.schema2 = self.schema2, self.schema1
       
   165             self.table1, self.table2 = self.table2, self.table1
   154 
   166 
   155     def tables(self):
   167     def tables(self):
   156         '''Generator. Yields schema1, table1, schema2, table2.'''
   168         '''Generator. Yields schema1, table1, schema2, table2.'''
   157         srcconn = self.pgm.get_conn('src')
   169         srcconn = self.pgm.get_conn('src')
   158         try:
   170         try: