Add loopquery tool. Log notices to console from all tools (toolbase).
authorRadek Brich <radek.brich@devl.cz>
Fri, 05 Oct 2012 14:31:25 +0200
changeset 51 bdc44f96cb0b
parent 50 f71d3abbb18f
child 52 26121a8fe78b
Add loopquery tool. Log notices to console from all tools (toolbase).
loopquery.py
pgtoolkit/toolbase.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loopquery.py	Fri Oct 05 14:31:25 2012 +0200
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3.2
+"""
+loopquery
+
+Execute some query in loop, waiting for some time interval before next run.
+
+"""
+
+from pgtoolkit import toolbase
+
+import time
+
+
+class LoopQueryTool(toolbase.SimpleTool):
+    def __init__(self):
+        toolbase.SimpleTool.__init__(self, name='loopquery', desc='Run query in loop.')
+        self.parser.add_argument('-q', dest='query', type=str, help='Query to run.')
+        self.parser.add_argument('-i', dest='interval', type=float, help='Run interval in seconds.')
+        self.init()
+
+    def main(self):
+        while True:
+            self.log.info('Executing: %s', self.args.query)
+            with self.pgm.cursor('target') as curs:
+                curs.execute(self.args.query)
+            self.log.info('Done.')
+            time.sleep(self.args.interval)
+
+
+tool = LoopQueryTool()
+tool.main()
+
--- a/pgtoolkit/toolbase.py	Sat Sep 29 14:01:47 2012 +0200
+++ b/pgtoolkit/toolbase.py	Fri Oct 05 14:31:25 2012 +0200
@@ -7,11 +7,11 @@
 from pgtoolkit.highlight import highlight
 
 
-class ConnectionInfoNotFound(Exception):    
+class ConnectionInfoNotFound(Exception):
     pass
 
 
-class BadArgsError(Exception):    
+class BadArgsError(Exception):
     pass
 
 
@@ -20,20 +20,20 @@
         self.parser = argparse.ArgumentParser(description=desc)
         self.parser.add_argument('-d', dest='debug', action='store_true',
             help='Debug mode - print database queries.')
-        
+
         self.config = config.ConfigParser()
         self.config.add_argument('databases', type=dict)
         self.config.add_argument('meta_db')
         self.config.add_argument('meta_query')
-        
+
         self.pgm = pgmanager.get_instance()
         self.target_isolation_level = None
-        
+
     def init(self):
         self.config.load('pgtoolkit.conf')
         self.args = self.parser.parse_args()
         self.init_logging()
-    
+
     def init_logging(self):
         # logging
         handler = logging.StreamHandler()
@@ -43,21 +43,22 @@
         self.log = logging.getLogger('main')
         self.log.addHandler(handler)
         self.log.setLevel(logging.DEBUG)
-        
+
+        logger_notices = logging.getLogger('pgmanager_notices')
+        logger_notices.addHandler(handler)
+        logger_notices.setLevel(logging.DEBUG)
+
         if self.args.debug:
-            handler = logging.StreamHandler()
-            handler.setFormatter(format)
-            handler.setLevel(logging.DEBUG)
-            logger = logging.getLogger('pgmanager_sql')
-            logger.addHandler(handler)
-            logger.setLevel(logging.DEBUG)
+            logger_sql = logging.getLogger('pgmanager_sql')
+            logger_sql.addHandler(handler)
+            logger_sql.setLevel(logging.DEBUG)
 
     def prepare_conn_from_metadb(self, name, lookup_name):
         '''Create connection in pgmanager using meta DB.
-        
+
         name -- Name for connection in pgmanager.
         lookup_name -- Name of connection in meta DB.
-        
+
         '''
         with self.pgm.cursor('meta') as curs:
             curs.execute(self.config.meta_query, [lookup_name])
@@ -82,7 +83,7 @@
     def prepare_conns_from_cmdline_args(self, *pgm_names):
         if self.config.meta_db:
             self.pgm.create_conn(name='meta', dsn=self.config.meta_db)
-        
+
         for name in pgm_names:
             lookup_name = self.args.__dict__[name]
             found = self.prepare_conn_from_config(name, lookup_name)
@@ -90,16 +91,16 @@
                 found = self.prepare_conn_from_metadb(name, lookup_name)
             if not found:
                 raise ConnectionInfoNotFound('Connection name "%s" not found in config nor in meta DB.' % lookup_name)
-        
+
         if self.config.meta_db:
             self.pgm.close_conn('meta')
 
 
 class SimpleTool(ToolBase):
     def __init__(self, name, desc):
-        ToolBase.__init__(self, name, desc)        
+        ToolBase.__init__(self, name, desc)
         self.parser.add_argument('target', metavar='target', type=str, help='Target database')
-      
+
     def init(self):
         ToolBase.init(self)
         self.prepare_conns_from_cmdline_args('target')
@@ -107,10 +108,10 @@
 
 class SrcDstTool(ToolBase):
     def __init__(self, name, desc):
-        ToolBase.__init__(self, name, desc)        
+        ToolBase.__init__(self, name, desc)
         self.parser.add_argument('src', metavar='source', type=str, help='Source database')
         self.parser.add_argument('dst', metavar='destination', type=str, help='Destination database')
-      
+
     def init(self):
         ToolBase.init(self)
         self.prepare_conns_from_cmdline_args('src', 'dst')
@@ -128,15 +129,15 @@
         self.parser.add_argument('--dst-schema', metavar='destination_schema',
             dest='dstschema', type=str, default='', help='Destination schema name (default=source_schema).')
         self.parser.add_argument('--regex', action='store_true', help="Use RE in schema or table name.")
-    
+
     def init(self):
         SrcDstTool.init(self)
-        
+
         self.schema1 = self.args.srcschema
         self.table1 = self.args.srctable
         self.schema2 = self.args.dstschema
         self.table2 = self.args.dsttable
-        
+
         # check regex - it applies to source name, dest name must not be specified
         # applies to only one - schema or table name
         if self.args.regex:
@@ -180,7 +181,7 @@
                     yield (self.schema1, self.table1, self.schema2, self.table2)
         finally:
             self.pgm.put_conn(srcconn, 'src')
-    
+
     def _iter_schemas_regex(self, browser, regex):
         for schema in browser.list_schemas():
             if schema['system']:
@@ -189,7 +190,7 @@
             if re.match(regex, schemaname):
                 for item in self._iter_tables_regex(browser, schemaname, schemaname, ''):
                     yield item
-    
+
     def _iter_tables_regex(self, browser, schema1, schema2, regex):
         for table in browser.list_tables(schema1):
             tablename = table['name']