Gentoo Archives: gentoo-commits

From: Brian Dolbec <dolsen@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/gentoo-keys:master commit in: gkeys/gkeys/
Date: Thu, 01 Jan 2015 17:44:59
Message-Id: 1420079991.3ea4ac8ff20626008d02a44c09211e2a67c54fcd.dolsen@gentoo
1 commit: 3ea4ac8ff20626008d02a44c09211e2a67c54fcd
2 Author: Brian Dolbec <dolsen <AT> gentoo <DOT> org>
3 AuthorDate: Tue Dec 30 19:38:23 2014 +0000
4 Commit: Brian Dolbec <dolsen <AT> gentoo <DOT> org>
5 CommitDate: Thu Jan 1 02:39:51 2015 +0000
6 URL: http://sources.gentoo.org/gitweb/?p=proj/gentoo-keys.git;a=commit;h=3ea4ac8f
7
8 gkeys/config.py: Make it possible to read more than one config file
9
10 Save homedir to the config class for re-use in gkeys-gen
11 multiple configs fix
12
13 ---
14 gkeys/gkeys/base.py | 19 ++++++++++++-------
15 gkeys/gkeys/cli.py | 12 +++++++++---
16 gkeys/gkeys/config.py | 25 +++++++++++++++----------
17 3 files changed, 36 insertions(+), 20 deletions(-)
18
19 diff --git a/gkeys/gkeys/base.py b/gkeys/gkeys/base.py
20 index d03efc4..052d45f 100644
21 --- a/gkeys/gkeys/base.py
22 +++ b/gkeys/gkeys/base.py
23 @@ -185,10 +185,10 @@ class CliBase(object):
24 help='The user ID, gpg key uid')
25
26
27 - def parse_args(self, args):
28 + def parse_args(self, argv):
29 '''Parse a list of aruments
30
31 - @param args: list
32 + @param argv: list
33 @returns argparse.Namespace object
34 '''
35 #self.logger.debug('CliBase: parse_args; args: %s' % args)
36 @@ -225,7 +225,7 @@ class CliBase(object):
37 action_parser.set_defaults(action=name)
38 self._add_options(action_parser, self.cli_config['Action_Options'][name])
39
40 - parsed_args = parser.parse_args(args)
41 + parsed_args = parser.parse_args(argv)
42 action = getattr(parsed_args, 'action', None)
43 if not action:
44 parser.print_usage()
45 @@ -241,10 +241,11 @@ class CliBase(object):
46 getattr(self, '_option_%s' % opt)(parser)
47
48
49 - def run(self, args):
50 - '''Run the args passed in
51 + def setup(self, args, configs):
52 + '''Set up the args and configs passed in
53
54 @param args: list or argparse.Namespace object
55 + @param configs: list
56 '''
57 message = None
58 if not args:
59 @@ -253,8 +254,9 @@ class CliBase(object):
60 args = self.parse_args(args)
61 if args.config:
62 self.config.defaults['config'] = args.config
63 - # now make it load the config file
64 - self.config.read_config()
65 + self.config.read_config()
66 + else:
67 + self.config.read_config(configs)
68
69 # establish our logger and update it in the imported files
70 self.logger = set_logger(self.cli_config['prog'], self.config['logdir'], args.debug,
71 @@ -278,7 +280,10 @@ class CliBase(object):
72 cat = args.category
73 if not self._check_category(cat):
74 return False
75 + return True
76
77 +
78 + def run(self, args):
79 # establish our actions instance
80 self.actions = self.cli_config['Actions'](self.config, self.output_results, self.logger)
81
82
83 diff --git a/gkeys/gkeys/cli.py b/gkeys/gkeys/cli.py
84 index df81882..31457e0 100644
85 --- a/gkeys/gkeys/cli.py
86 +++ b/gkeys/gkeys/cli.py
87 @@ -13,6 +13,7 @@
88 from __future__ import print_function
89
90
91 +import os
92 import sys
93
94 from gkeys.base import CliBase
95 @@ -52,7 +53,12 @@ class Main(CliBase):
96 Defaults to sys.argv[1:]
97 """
98 if args:
99 - return self.run(self.parse_args(args))
100 + ok = self.setup(args)
101 + if ok:
102 + return self.run(self.parse_args(args))
103 else:
104 - return self.run(self.parse_args(sys.argv[1:]))
105 -
106 + args = self.parse_args(sys.argv[1:])
107 + ok = self.setup(args, os.path.join(self.config['configdir'],'gkeys.conf'))
108 + if ok:
109 + return self.run(args)
110 + return False
111
112 diff --git a/gkeys/gkeys/config.py b/gkeys/gkeys/config.py
113 index 8fa4c1b..5d3923f 100644
114 --- a/gkeys/gkeys/config.py
115 +++ b/gkeys/gkeys/config.py
116 @@ -48,9 +48,9 @@ class GKeysConfig(GPGConfig):
117 self.defaults['config'] = config
118 self.defaults['configdir'] = os.path.dirname(config)
119 else:
120 - homedir = os.path.expanduser('~')
121 - self.defaults['configdir'] = homedir
122 - self.defaults['config']= os.path.join(homedir, '.gkeys.conf')
123 + self.homedir = os.path.expanduser('~')
124 + self.defaults['configdir'] = self.homedir
125 + self.defaults['config']= os.path.join(self.homedir, '.gkeys.conf')
126 if not os.path.exists(self.defaults['config']):
127 self.defaults['configdir'] = path([self.root, EPREFIX, '/etc/gkeys'])
128 self.defaults['config'] = '%(configdir)s/gkeys.conf'
129 @@ -83,7 +83,7 @@ class GKeysConfig(GPGConfig):
130 self.defaults['verify-seeds'] = {}
131
132
133 - def read_config(self):
134 + def read_config(self, filename=None):
135 '''Reads the config file into memory
136 '''
137 if "%(configdir)s" in self.defaults['config']:
138 @@ -97,18 +97,23 @@ class GKeysConfig(GPGConfig):
139 for key in ['gkeysdir', 'keyring', 'sign-keydir', 'logdir', 'seedsdir',
140 'keyserver']:
141 defaults[key] = self.defaults[key]
142 + if filename == None:
143 + filename = self.defaults['config']
144 self.configparser = SaneConfigParser(defaults)
145 - self.configparser.read(self.defaults['config'])
146 - # I consider this hacky, but due to shortcomings of ConfigParser
147 - # we need to reset the defaults redefined in the 'base' section
148 - for key in self.configparser.options('base'):
149 - self.defaults[key] = self.configparser.get('base', key)
150 - defaults[key] = self.defaults[key]
151 + self.configparser.read(filename)
152 + if self.configparser.has_section('base'):
153 + # I consider this hacky, but due to shortcomings of ConfigParser
154 + # we need to reset the defaults redefined in the 'base' section
155 + for key in self.configparser.options('base'):
156 + self.defaults[key] = self.configparser.get('base', key)
157 + defaults[key] = self.defaults[key]
158 self.configparser._defaults = defaults
159 for section in self.configparser.sections():
160 if section == 'base':
161 continue
162 for key in self.configparser.options(section):
163 + if section not in self.defaults:
164 + self.defaults[section] = {}
165 self.defaults[section][key] = self.configparser.get(section, key)
166
167 def get_key(self, key, subkey=None):