ToolBase - implement 'database' config option.
authorRadek Brich <radek.brich@devl.cz>
Mon, 21 Nov 2011 10:47:24 +0100
changeset 17 f768a3529ee7
parent 16 cb7e13711a99
child 18 a9e12b7cc207
ToolBase - implement 'database' config option.
pgtoolkit/config.py
pgtoolkit/toolbase.py
--- a/pgtoolkit/config.py	Mon Nov 21 10:43:37 2011 +0100
+++ b/pgtoolkit/config.py	Mon Nov 21 10:47:24 2011 +0100
@@ -9,6 +9,7 @@
 
     def add_argument(self, name, type=str, default=None):
         self.registered_args[name] = {'type':type, 'default':default}
+        self.args[name] = default
 
     def load(self, fname):
         # parse config file
@@ -25,14 +26,14 @@
             if key in self.registered_args:
                 # arg registered, check type
                 type = self.registered_args[key]['type']
-                if not isinstance(self.args[key], type):
+                if not isinstance(self.args[key], type) and not self.args[key] is None:
                     ok = False
                     self.log.error("Bad type of config parameter '%s': is %s but should be %s",
                         key, type(self.args[key]), type)
             else:
                 # arg not registered
-                    ok = False
-                    self.log.error("Unknown config parameter '%s'.", key)
+                ok = False
+                self.log.error("Unknown config parameter '%s'.", key)
         return ok
 
     def __getattr__(self, name):
--- a/pgtoolkit/toolbase.py	Mon Nov 21 10:43:37 2011 +0100
+++ b/pgtoolkit/toolbase.py	Mon Nov 21 10:47:24 2011 +0100
@@ -41,23 +41,32 @@
             logger = logging.getLogger('pgmanager')
             logger.addHandler(handler)
 
-    def buildconn(self, name, targetname):
+    def buildconn(self, name, lookup_name):
         with self.pgm.cursor('meta') as curs:
-            curs.execute(self.config.meta_query, [targetname])
+            curs.execute(self.config.meta_query, [lookup_name])
             row = curs.fetchone_dict()
             curs.connection.commit()
             
             if not row:
-                raise Exception('Unknown database "%s"' % targetname)
+                raise Exception('Unknown database "%s"' % lookup_name)
             
             self.pgm.create_conn(name=name, **row)
 
-    def prepareconns(self, *args):        
+    def prepareconns(self, *pgm_names):
+        # use meta db
         if self.config.meta_db:
             self.pgm.create_conn(name='meta', dsn=self.config.meta_db)
-            for name in args:
-                self.buildconn(name, self.args.__dict__[name])
+            for name in pgm_names:
+                lookup_name = self.args.__dict__[name]
+                self.buildconn(name, lookup_name)
             self.pgm.close_conn('meta')
+        
+        # use config dict 'databases'
+        if self.config.databases:
+            for name in pgm_names:
+                lookup_name = self.args.__dict__[name]
+                dsn = self.config.databases[lookup_name]
+                self.pgm.create_conn(name=name, dsn=dsn)
 
 
 class SimpleTool(ToolBase):