fix missed quotes
[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 sys
15
16 # py3.2
17 if sys.hexversion >= 0x30200f0:
18     import configparser as ConfigParser
19 else:
20     import ConfigParser
21
22 from collections import namedtuple
23
24
25 from pyGPG.config import GPGConfig
26
27 from gkeys import log
28 from gkeys.utils import path
29
30 logger = log.logger
31
32
33 # establish the eprefix, initially set so eprefixify can
34 # set it on install
35 EPREFIX = "@GENTOO_PORTAGE_EPREFIX@"
36
37 # check and set it if it wasn't
38 if "GENTOO_PORTAGE_EPREFIX" in EPREFIX:
39     EPREFIX = ''
40
41
42
43 class GKeysConfig(GPGConfig):
44     """ Configuration superclass which holds our gentoo-keys
45     config settings for pygpg """
46
47     def __init__ (self, config=None, root=None, read_configfile=False):
48         """ Class initialiser """
49         GPGConfig.__init__(self)
50
51         self.root = root or ''
52         if config:
53             self.defaults['config'] = config
54             self.defaults['configdir'] = os.path.dirname(config)
55         else:
56             self.defaults['configdir'] = path([self.root, EPREFIX, '/etc/gentoo-keys'])
57             self.defaults['config'] = '%(configdir)s/gkeys.conf'
58         self.configparser = None
59         if read_configfile:
60             self.read_config()
61
62
63     def _add_gkey_defaults(self):
64         self.defaults['keysdir'] = path([self.root, EPREFIX, '/var/gentoo/gkeys'])
65         self.defaults['dev-keydir'] = '%(keysdir)s/devs'
66         self.defaults['release-keydir'] = '%(keysdir)s/release'
67         self.defaults['overlays-keydir'] = '%(keysdir)s/overlays'
68         self.defaults['logdir'] = '%(keysdir)s/logs'
69         # local directory to scan for seed files installed via ebuild, layman
70         # or manual install.
71         self.defaults['seedsdir'] = '%(keysdir)s/seeds'
72         self.defaults['release-seedfile'] = '%(seedsdir)s/release.seeds'
73         self.defaults['dev-seedfile'] = '%(seedsdir)s/developer.seeds'
74         self.defaults['keyserver'] = 'pool.sks-keyservers.net'
75         self.defaults['seedurls'] = {
76             'release.seeds': 'https://dev.gentoo.org/~dolsen/gkey-seeds/release.seeds',
77             'developers.seeds': 'https://dev.gentoo.org/~dolsen/gkey-seeds/developer.seeds',
78         }
79
80
81     def read_config(self):
82         '''Reads the config file into memory
83         '''
84         if "%(configdir)s" in self.defaults['config']:
85             # fix the config path
86             self.defaults['config'] = self.defaults['config'] \
87                 % {'configdir': self.defaults['configdir']}
88         defaults = self.get_defaults()
89         # remove some defaults from being entered into the configparser
90         for key in ['gpg_defaults', 'only_usable', 'refetch', 'tasks']:
91             defaults.pop(key)
92         self.configparser = ConfigParser.ConfigParser(defaults)
93         self.configparser.add_section('MAIN')
94         self.configparser.read(defaults['config'])
95
96
97     def get_key(self, key, subkey=None):
98         return self._get_(key, subkey)
99
100
101     def _get_(self, key, subkey=None):
102         if self.configparser and self.configparser.has_option('MAIN', key):
103             if logger:
104                 logger.debug("Found %s in configparser... %s"
105                     % (key, str(self.configparser.get('MAIN', key))))
106                 #logger.debug("type(key)= %s"
107                 #    % str(type(self.configparser.get('MAIN', key))))
108             return self.configparser.get('MAIN', key)
109         else:
110             return super(GKeysConfig, self)._get_(key, subkey)
111
112
113 # some constants used in gkeyldap/actions.py
114 # they map the index values of the GKEY input data fields
115 NICK = 0
116 NAME = 1
117 KEYID = 2
118 LONGKEYID = 3
119 KEYDIR = 4
120 FINGERPRINT = 5
121
122 # set some defaults
123 KEY_LEN = {
124     'keyid': 8,
125     'longkeyid': 16,
126 }
127
128
129 class GKEY(namedtuple('GKEY', ['nick', 'name', 'keyid', 'longkeyid',
130     'keydir', 'fingerprint'])):
131     '''Class to hold the relavent info about a key'''
132
133     field_types = {'nick': str, 'name': str, 'keyid': list,
134         'longkeyid': list, 'keydir': str, 'fingerprint': list}
135     field_separator = "|"
136     list_separator = ":"
137     __slots__ = ()
138
139     def _packed_values(self):
140         '''Returns a list of the field values'''
141         v = []
142         for f in self._fields:
143             v.append(self._pack(f))
144         return v
145
146     @property
147     def packed_string(self):
148         '''Returns a separator joined string of the field values'''
149         return self.field_separator.join([x for x in self._packed_values()])
150
151     def _unpack_string(self, packed_data):
152         '''Returns a list of the separator joined string of the field values'''
153         values = []
154         data = packed_data.split(self.field_separator)
155         for x in self._fields:
156             values.append(self._unpack(x, data.pop(0)))
157         return values
158
159     def _pack(self, field):
160         '''pack field data into a string'''
161         if self.field_types[field] == str:
162             return getattr(self, field)
163         elif self.field_types[field] == list:
164             info = getattr(self, field)
165             if info:
166                 return self.list_separator.join(info)
167             else:
168                 # force an empty list to None
169                 return 'None'
170         else:
171             raise "ERROR packing %s" %str(getattr(self, field))
172
173     def _unpack(self, field, data):
174         '''unpack field data to the desired type'''
175         if self.field_types[field] == str:
176             result = data
177             if result == 'None':
178                 result = None
179         else:
180             if data == 'None':
181                 # make it an empty list
182                 result = []
183             else:
184                 result = data.split(self.list_separator)
185         return result
186
187     def make_packed(self, packed_string):
188         '''Creates a new instance of Gkey from the packed
189         value string
190
191         @param packed_string: string of data separated by field_separator
192         @return new GKEY instance containing the data
193         '''
194         return GKEY._make(self._unpack_string(packed_string))