commit:     3ea4ac8ff20626008d02a44c09211e2a67c54fcd
Author:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
AuthorDate: Tue Dec 30 19:38:23 2014 +0000
Commit:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
CommitDate: Thu Jan  1 02:39:51 2015 +0000
URL:        
http://sources.gentoo.org/gitweb/?p=proj/gentoo-keys.git;a=commit;h=3ea4ac8f

gkeys/config.py: Make it possible to read more than one config file

Save homedir to the config class for re-use in gkeys-gen
multiple configs fix

---
 gkeys/gkeys/base.py   | 19 ++++++++++++-------
 gkeys/gkeys/cli.py    | 12 +++++++++---
 gkeys/gkeys/config.py | 25 +++++++++++++++----------
 3 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/gkeys/gkeys/base.py b/gkeys/gkeys/base.py
index d03efc4..052d45f 100644
--- a/gkeys/gkeys/base.py
+++ b/gkeys/gkeys/base.py
@@ -185,10 +185,10 @@ class CliBase(object):
             help='The user ID, gpg key uid')
 
 
-    def parse_args(self, args):
+    def parse_args(self, argv):
         '''Parse a list of aruments
 
-        @param args: list
+        @param argv: list
         @returns argparse.Namespace object
         '''
         #self.logger.debug('CliBase: parse_args; args: %s' % args)
@@ -225,7 +225,7 @@ class CliBase(object):
             action_parser.set_defaults(action=name)
             self._add_options(action_parser, 
self.cli_config['Action_Options'][name])
 
-        parsed_args = parser.parse_args(args)
+        parsed_args = parser.parse_args(argv)
         action = getattr(parsed_args, 'action', None)
         if not action:
             parser.print_usage()
@@ -241,10 +241,11 @@ class CliBase(object):
             getattr(self, '_option_%s' % opt)(parser)
 
 
-    def run(self, args):
-        '''Run the args passed in
+    def setup(self, args, configs):
+        '''Set up the args and configs passed in
 
         @param args: list or argparse.Namespace object
+        @param configs: list
         '''
         message = None
         if not args:
@@ -253,8 +254,9 @@ class CliBase(object):
             args = self.parse_args(args)
         if args.config:
             self.config.defaults['config'] = args.config
-        # now make it load the config file
-        self.config.read_config()
+            self.config.read_config()
+        else:
+            self.config.read_config(configs)
 
         # establish our logger and update it in the imported files
         self.logger = set_logger(self.cli_config['prog'], 
self.config['logdir'], args.debug,
@@ -278,7 +280,10 @@ class CliBase(object):
             cat = args.category
         if not self._check_category(cat):
             return False
+        return True
 
+
+    def run(self, args):
         # establish our actions instance
         self.actions = self.cli_config['Actions'](self.config, 
self.output_results, self.logger)
 

diff --git a/gkeys/gkeys/cli.py b/gkeys/gkeys/cli.py
index df81882..31457e0 100644
--- a/gkeys/gkeys/cli.py
+++ b/gkeys/gkeys/cli.py
@@ -13,6 +13,7 @@
 from __future__ import print_function
 
 
+import os
 import sys
 
 from gkeys.base import CliBase
@@ -52,7 +53,12 @@ class Main(CliBase):
                      Defaults to sys.argv[1:]
         """
         if args:
-            return self.run(self.parse_args(args))
+            ok = self.setup(args)
+            if ok:
+                return self.run(self.parse_args(args))
         else:
-            return self.run(self.parse_args(sys.argv[1:]))
-
+            args = self.parse_args(sys.argv[1:])
+            ok = self.setup(args, 
os.path.join(self.config['configdir'],'gkeys.conf'))
+            if ok:
+                return self.run(args)
+        return False

diff --git a/gkeys/gkeys/config.py b/gkeys/gkeys/config.py
index 8fa4c1b..5d3923f 100644
--- a/gkeys/gkeys/config.py
+++ b/gkeys/gkeys/config.py
@@ -48,9 +48,9 @@ class GKeysConfig(GPGConfig):
             self.defaults['config'] = config
             self.defaults['configdir'] = os.path.dirname(config)
         else:
-            homedir = os.path.expanduser('~')
-            self.defaults['configdir'] = homedir
-            self.defaults['config']= os.path.join(homedir, '.gkeys.conf')
+            self.homedir = os.path.expanduser('~')
+            self.defaults['configdir'] = self.homedir
+            self.defaults['config']= os.path.join(self.homedir, '.gkeys.conf')
             if not os.path.exists(self.defaults['config']):
                 self.defaults['configdir'] = path([self.root, EPREFIX, 
'/etc/gkeys'])
                 self.defaults['config'] = '%(configdir)s/gkeys.conf'
@@ -83,7 +83,7 @@ class GKeysConfig(GPGConfig):
         self.defaults['verify-seeds'] = {}
 
 
-    def read_config(self):
+    def read_config(self, filename=None):
         '''Reads the config file into memory
         '''
         if "%(configdir)s" in self.defaults['config']:
@@ -97,18 +97,23 @@ class GKeysConfig(GPGConfig):
         for key in ['gkeysdir', 'keyring', 'sign-keydir', 'logdir', 'seedsdir',
             'keyserver']:
             defaults[key] = self.defaults[key]
+        if filename == None:
+            filename = self.defaults['config']
         self.configparser = SaneConfigParser(defaults)
-        self.configparser.read(self.defaults['config'])
-        # I consider this hacky, but due to shortcomings of ConfigParser
-        # we need to reset the defaults redefined in the 'base' section
-        for key in self.configparser.options('base'):
-            self.defaults[key] = self.configparser.get('base', key)
-            defaults[key] = self.defaults[key]
+        self.configparser.read(filename)
+        if self.configparser.has_section('base'):
+            # I consider this hacky, but due to shortcomings of ConfigParser
+            # we need to reset the defaults redefined in the 'base' section
+            for key in self.configparser.options('base'):
+                self.defaults[key] = self.configparser.get('base', key)
+                defaults[key] = self.defaults[key]
         self.configparser._defaults = defaults
         for section in self.configparser.sections():
             if section == 'base':
                 continue
             for key in self.configparser.options(section):
+                if section not in self.defaults:
+                    self.defaults[section] = {}
                 self.defaults[section][key] = self.configparser.get(section, 
key)
 
     def get_key(self, key, subkey=None):

Reply via email to