Clean out uneeded code
[gentoo-keys.git] / gkeys / config.py
1 #
2 #-*- coding:utf-8 -*-
3
4 """
5     Gentoo-keys - config.py
6
7     Holds configuration keys and values
8
9     @copyright: 2012 by Brian Dolbec <dol-sen@gentoo.org>
10     @license: GNU GNU GPL2, see COPYING for details.
11 """
12
13 import os
14 import ConfigParser
15 from collections import namedtuple
16
17
18 from pygpg.config import GPGConfig
19
20 from gkeys import log
21 from gkeys.utils import path
22
23 logger = log.logger
24
25
26 # establish the eprefix, initially set so eprefixify can
27 # set it on install
28 EPREFIX = "@GENTOO_PORTAGE_EPREFIX@"
29
30 # check and set it if it wasn't
31 if "GENTOO_PORTAGE_EPREFIX" in EPREFIX:
32     EPREFIX = ''
33
34
35
36 class GKeysConfig(GPGConfig):
37     """ Configuration superclass which holds our gentoo-keys
38     config settings for pygpg """
39
40     def __init__ (self, config=None, root=None, read_configfile=False):
41         """ Class initialiser """
42         GPGConfig.__init__(self)
43
44         self.root = root or ''
45         if config:
46             self.defaults['config'] = config
47             self.defaults['configdir'] = os.path.dirname(config)
48         else:
49             self.defaults['configdir'] = path([self.root, EPREFIX, '/etc/gentoo-keys'])
50             self.defaults['config'] = '%(configdir)s/gkeys.conf'
51         self.configparser = None
52         if read_configfile:
53             self.read_config()
54
55
56     def _add_gkey_defaults(self):
57         self.defaults['keysdir'] = path([self.root, EPREFIX, '/var/gentoo/gkeys'])
58         self.defaults['dev-keydir'] = '%(keysdir)s/devs'
59         self.defaults['release-keydir'] = '%(keysdir)s/release'
60         self.defaults['overlays-keydir'] = '%(keysdir)s/overlays'
61         self.defaults['known-keysfile'] = '%(keysdir)s/knownkeys'
62         self.defaults['release-seedfile'] = '%(configdir)s/release.seeds'
63         self.defaults['dev-seedfile'] = '%(configdir)s/developer.seeds'
64
65
66
67     def read_config(self):
68         '''Reads the config file into memory
69         '''
70         if "%(configdir)s" in self.defaults['config']:
71             # fix the config path
72             self.defaults['config'] = self.defaults['config'] \
73                 % {'configdir': self.defaults['configdir']}
74         defaults = self.get_defaults()
75         # remove some defaults from being entered into the configparser
76         for key in ['gpg_defaults', 'only_usable', 'refetch', 'tasks']:
77             defaults.pop(key)
78         self.configparser = ConfigParser.ConfigParser(defaults)
79         self.configparser.add_section('MAIN')
80         self.configparser.read(defaults['config'])
81
82
83     def get_key(self, key, subkey=None):
84         return self._get_(key, subkey)
85
86
87     def _get_(self, key, subkey=None):
88         if self.configparser and self.configparser.has_option('MAIN', key):
89             if logger:
90                 logger.debug("Found %s in configparser... %s"
91                     % (key, str(self.configparser.get('MAIN', key))))
92                 #logger.debug("type(key)= %s"
93                 #    % str(type(self.configparser.get('MAIN', key))))
94             return self.configparser.get('MAIN', key)
95         else:
96             return super(GKeysConfig, self)._get_(key, subkey)
97
98
99 class GKEY(namedtuple('GKEY', ['nick', 'name', 'keyid', 'longkeyid',
100     'keyring', 'fingerprint'])):
101     '''Class to hold the relavent info about a key'''
102
103     field_types = {'nick': str, 'name': str, 'keyid': list,
104         'longkeyid': list, 'keyring': str, 'fingerprint': list}
105     field_separator = "|"
106     list_separator = ":"
107     __slots__ = ()
108
109     def _packed_values(self):
110         '''Returns a list of the field values'''
111         v = []
112         for f in self._fields:
113             v.append(self._pack(f))
114         return v
115
116     @property
117     def packed_string(self):
118         '''Returns a separator joined string of the field values'''
119         return self.field_separator.join([x for x in self._packed_values()])
120
121     def _unpack_string(self, packed_data):
122         '''Returns a list of the separator joined string of the field values'''
123         values = []
124         data = packed_data.split(self.field_separator)
125         for x in self._fields:
126             values.append(self._unpack(x, data.pop(0)))
127         return values
128
129     def _pack(self, field):
130         '''pack field data into a string'''
131         if self.field_types[field] == str:
132             return getattr(self, field)
133         elif self.field_types[field] == list:
134             info = getattr(self, field)
135             if info:
136                 return self.list_separator.join(info)
137             else:
138                 # force an empty list to None
139                 return 'None'
140         else:
141             raise "ERROR packing %s" %str(getattr(self, field))
142
143     def _unpack(self, field, data):
144         '''unpack field data to the desired type'''
145         if self.field_types[field] == str:
146             result = data
147             if result == 'None':
148                 result = None
149         else:
150             if data == 'None':
151                 # make it an empty list
152                 result = []
153             else:
154                 result = data.split(self.list_separator)
155         return result
156
157     def make_packed(self, packed_string):
158         '''Creates a new instance of Gkey from the packed
159         value string
160
161         @param packed_string: string of data separated by field_separator
162         @return new GKEY instance containing the data
163         '''
164         return GKEY._make(self._unpack_string(packed_string))