pgtoolkit/toolbase.py
changeset 102 fda45bdfd68d
parent 101 2a2d0d5df03b
--- a/pgtoolkit/toolbase.py	Tue May 06 18:37:43 2014 +0200
+++ b/pgtoolkit/toolbase.py	Wed May 07 18:33:50 2014 +0200
@@ -43,12 +43,21 @@
         self.config.add_option('databases', dict)
         self.config.add_option('meta_db')
         self.config.add_option('meta_query')
-        self.parser.add_argument('-Q', dest='queries', action='store_true',
+        self.parser.add_argument('-Q', dest='show_queries', action='store_true',
             help='Print database queries.')
+        self.parser.add_argument('-C', dest='config_file', type=str,
+            help='Additional config file (besides pgtoolkit.conf).')
 
     def load_args(self, args=None, config_file=None):
-        self.config.load(config_file or 'pgtoolkit.conf')
+        # Parse command line arguments
         self.args = self.parser.parse_args(args)
+        # Load global config
+        self.config.load('/etc/pgtoolkit.conf', must_exist=False)
+        # Load local config
+        self.config.load(config_file or 'pgtoolkit.conf', must_exist=False)
+        # Load additional config
+        if self.args.config_file:
+            self.config.load(self.args.config_file)
 
     def init_logging(self):
         # logging
@@ -64,7 +73,7 @@
         log_notices.addHandler(handler)
         log_notices.setLevel(logging.DEBUG)
 
-        if self.args.queries:
+        if self.args.show_queries:
             log_sql = logging.getLogger('pgmanager_sql')
             log_sql.addHandler(handler)
             log_sql.setLevel(logging.DEBUG)
@@ -123,11 +132,16 @@
 
     def specify_args(self):
         ToolBase.specify_args(self)
-        self.parser.add_argument('target', metavar='target', type=str, help='Target database')
+        self.config.add_option('target', type=str, default=None)
+        self.parser.add_argument('target', nargs='?', type=str, help='Target database')
+
+    def load_args(self, args=None, config_file=None):
+        ToolBase.load_args(self, args, config_file)
+        self.target = self.args.target or self.config.target or 'default'
 
     def setup(self, args=None):
         ToolBase.setup(self, args)
-        self.prepare_conns(target=self.args.target)
+        self.prepare_conns(target=self.target)
 
 
 class SrcDstTool(ToolBase):