5 |
5 |
6 |
6 |
7 class ToolBase: |
7 class ToolBase: |
8 def __init__(self, name, desc): |
8 def __init__(self, name, desc): |
9 self.parser = argparse.ArgumentParser(description=desc) |
9 self.parser = argparse.ArgumentParser(description=desc) |
10 self.parser.add_argument('target', metavar='target', type=str, help='Target database') |
|
11 |
10 |
12 self.config = config.ConfigParser() |
11 self.config = config.ConfigParser() |
13 self.config.add_argument('databases', type=dict) |
12 self.config.add_argument('databases', type=dict) |
14 self.config.add_argument('meta_db') |
13 self.config.add_argument('meta_db') |
15 self.config.add_argument('meta_query') |
14 self.config.add_argument('meta_query') |
17 self.pgm = pgmanager.get_instance() |
16 self.pgm = pgmanager.get_instance() |
18 |
17 |
19 def init(self): |
18 def init(self): |
20 self.config.load('pgtoolkit.conf') |
19 self.config.load('pgtoolkit.conf') |
21 self.args = self.parser.parse_args() |
20 self.args = self.parser.parse_args() |
22 |
|
23 if self.config.meta_db: |
|
24 self.pgm.create_conn(name='meta', dsn=self.config.meta_db) |
|
25 self.buildconn('target', self.args.target) |
|
26 self.pgm.close_conn('meta') |
|
27 |
21 |
28 def buildconn(self, name, targetname): |
22 def buildconn(self, name, targetname): |
29 with self.pgm.cursor('meta') as curs: |
23 with self.pgm.cursor('meta') as curs: |
30 curs.execute(self.config.meta_query, [targetname]) |
24 curs.execute(self.config.meta_query, [targetname]) |
31 row = curs.fetchone_dict() |
25 row = curs.fetchone_dict() |
34 if not row: |
28 if not row: |
35 raise Exception('Unknown database "%s"' % targetname) |
29 raise Exception('Unknown database "%s"' % targetname) |
36 |
30 |
37 self.pgm.create_conn(name=name, **row) |
31 self.pgm.create_conn(name=name, **row) |
38 |
32 |
|
33 def prepareconns(self, *args): |
|
34 if self.config.meta_db: |
|
35 self.pgm.create_conn(name='meta', dsn=self.config.meta_db) |
|
36 for name in args: |
|
37 self.buildconn(name, self.args.__dict__[name]) |
|
38 self.pgm.close_conn('meta') |
|
39 |
|
40 |
|
41 class SimpleTool(ToolBase): |
|
42 def __init__(self, name, desc): |
|
43 ToolBase.__init__(self, name, desc) |
|
44 self.parser.add_argument('target', metavar='target', type=str, help='Target database') |
|
45 |
|
46 def init(self): |
|
47 ToolBase.init(self) |
|
48 self.prepareconns('target') |
|
49 |
|
50 |
|
51 class SrcDstTool(ToolBase): |
|
52 def __init__(self, name, desc): |
|
53 ToolBase.__init__(self, name, desc) |
|
54 self.parser.add_argument('src', metavar='source', type=str, help='Source database') |
|
55 self.parser.add_argument('dst', metavar='destination', type=str, help='Destination database') |
|
56 |
|
57 def init(self): |
|
58 ToolBase.init(self) |
|
59 self.prepareconns('src', 'dst') |
|
60 |