author | Radek Brich <brich.radek@ifortuna.cz> |
Mon, 21 Nov 2011 10:43:37 +0100 | |
changeset 16 | cb7e13711a99 |
parent 14 | a900bc629ecc |
child 17 | f768a3529ee7 |
permissions | -rw-r--r-- |
2 | 1 |
import argparse |
13
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
2 |
import logging |
2 | 3 |
|
11
bc69eca59041
PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
4 |
from pgtoolkit import pgmanager, config |
13
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
5 |
from pgtoolkit.coloredformatter import ColoredFormatter |
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
6 |
from pgtoolkit.highlight import highlight |
2 | 7 |
|
8 |
||
9 |
class ToolBase: |
|
10 |
def __init__(self, name, desc): |
|
11 |
self.parser = argparse.ArgumentParser(description=desc) |
|
13
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
12 |
self.parser.add_argument('-d', dest='debug', action='store_true', |
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
13 |
help='Debug mode - print database queries.') |
2 | 14 |
|
15 |
self.config = config.ConfigParser() |
|
16 |
self.config.add_argument('databases', type=dict) |
|
17 |
self.config.add_argument('meta_db') |
|
18 |
self.config.add_argument('meta_query') |
|
19 |
||
20 |
self.pgm = pgmanager.get_instance() |
|
21 |
||
22 |
def init(self): |
|
23 |
self.config.load('pgtoolkit.conf') |
|
24 |
self.args = self.parser.parse_args() |
|
14
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
25 |
self.init_logging() |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
26 |
|
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
27 |
def init_logging(self): |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
28 |
# logging |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
29 |
handler = logging.StreamHandler() |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
30 |
format = ColoredFormatter(highlight(1,7,0)+'%(asctime)s %(levelname)-5s'+highlight(0)+' %(message)s', '%H:%M:%S') |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
31 |
handler.setFormatter(format) |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
32 |
handler.setLevel(logging.DEBUG) |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
33 |
self.log = logging.getLogger('main') |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
34 |
self.log.addHandler(handler) |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
35 |
self.log.setLevel(logging.DEBUG) |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
36 |
|
13
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
37 |
if self.args.debug: |
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
38 |
handler = logging.StreamHandler() |
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
39 |
handler.setFormatter(format) |
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
40 |
handler.setLevel(logging.DEBUG) |
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
41 |
logger = logging.getLogger('pgmanager') |
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
42 |
logger.addHandler(handler) |
2 | 43 |
|
44 |
def buildconn(self, name, targetname): |
|
45 |
with self.pgm.cursor('meta') as curs: |
|
46 |
curs.execute(self.config.meta_query, [targetname]) |
|
47 |
row = curs.fetchone_dict() |
|
48 |
curs.connection.commit() |
|
49 |
||
50 |
if not row: |
|
51 |
raise Exception('Unknown database "%s"' % targetname) |
|
52 |
||
53 |
self.pgm.create_conn(name=name, **row) |
|
54 |
||
5 | 55 |
def prepareconns(self, *args): |
56 |
if self.config.meta_db: |
|
57 |
self.pgm.create_conn(name='meta', dsn=self.config.meta_db) |
|
58 |
for name in args: |
|
59 |
self.buildconn(name, self.args.__dict__[name]) |
|
60 |
self.pgm.close_conn('meta') |
|
61 |
||
62 |
||
63 |
class SimpleTool(ToolBase): |
|
64 |
def __init__(self, name, desc): |
|
65 |
ToolBase.__init__(self, name, desc) |
|
66 |
self.parser.add_argument('target', metavar='target', type=str, help='Target database') |
|
67 |
||
68 |
def init(self): |
|
69 |
ToolBase.init(self) |
|
70 |
self.prepareconns('target') |
|
71 |
||
72 |
||
73 |
class SrcDstTool(ToolBase): |
|
74 |
def __init__(self, name, desc): |
|
75 |
ToolBase.__init__(self, name, desc) |
|
76 |
self.parser.add_argument('src', metavar='source', type=str, help='Source database') |
|
77 |
self.parser.add_argument('dst', metavar='destination', type=str, help='Destination database') |
|
78 |
||
79 |
def init(self): |
|
80 |
ToolBase.init(self) |
|
81 |
self.prepareconns('src', 'dst') |
|
82 |