/[partial-mirror]/branches/rewrite/src/Config.py
ViewVC logotype

Contents of /branches/rewrite/src/Config.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 141 - (hide annotations) (download) (as text)
Sat Jul 3 20:48:36 2004 UTC (8 years, 10 months ago) by natbudin-guest
File MIME type: text/x-python
File size: 5501 byte(s)
Required options now really required.
1 otavio 7 # debpartial-mirror - partial debian mirror package tool
2 natbudin-guest 121 # (c) 2004 Otavio Salvador <otavio@debian.org>, Nat Budin <natb@brandeis.edu>
3 otavio 7 #
4     # This program is free software; you can redistribute it and/or modify
5     # it under the terms of the GNU General Public License as published by
6     # the Free Software Foundation; either version 2 of the License, or
7     # (at your option) any later version.
8     #
9     # This program is distributed in the hope that it will be useful,
10     # but WITHOUT ANY WARRANTY; without even the implied warranty of
11     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     # GNU General Public License for more details.
13     #
14     # You should have received a copy of the GNU General Public License
15     # along with this program; if not, write to the Free Software
16     # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17     # $Id$
18    
19 otavio 119 from logging import *
20 otavio 127 from ConfigParser import ConfigParser
21 natbudin-guest 123 from sys import exit
22 otavio 2
23 otavio 124 class InvalidOption(Exception):
24     """
25     Exception called when a invalid option is found in configuration
26     file.
27    
28     Attributes:
29     section -- Where the invalid option was set;
30     option -- The name of invalid option.
31     """
32     def __init__(self, section, option):
33     self.section = section
34     self.option = option
35    
36 natbudin-guest 141 class RequiredOptionMissing(Exception):
37     """
38     Exception called when a required option in the config file is not
39     present.
40    
41     Attributes:
42     section -- Where the invalid option was set;
43     option -- The name of invalid option.
44     """
45     def __init__(self, section, option):
46     self.section = section
47     self.option = option
48    
49 otavio 133 class InvalidSection(Exception):
50     """
51     Exception called when a invalid section is found in configuration
52     file.
53    
54     Attributes:
55     section -- The wrong section name.
56     """
57     def __init__(self, section):
58     self.section = section
59    
60 otavio 119 class Config(dict):
61     """
62     Store the configurations used by our system.
63     """
64 henrique 30
65 otavio 127 required_in_global = [
66 otavio 119 'mirror_dir',
67     'architectures',
68     'sections',
69     'distributions',
70     'get_suggests',
71     'get_recommends',
72     'get_provides',
73     ]
74    
75 natbudin-guest 141 allowed_in_global = [
76     'debug',
77     ]
78    
79 otavio 119 allowed_in_backend = [
80     'server',
81     'architectures',
82     'sections',
83     'distributions',
84     'filter',
85     'filter_@BACKEND@',
86     'get_suggests',
87     'get_recommends',
88     'get_provides',
89 natbudin-guest 123 'include_from_task',
90     'exclude_from_task',
91     'backends',
92     'name',
93     'resolve_deps_using',
94 otavio 119 ]
95 otavio 128
96 otavio 2 def __init__(self, filename):
97 otavio 119 conf = ConfigParser()
98     conf.read(filename)
99 otavio 128
100 otavio 127 # Read the global section.
101     self['GLOBAL'] = {}
102 natbudin-guest 141 global_items = self.allowed_in_global + self.required_in_global
103 otavio 127 for item, value in conf.items('GLOBAL'):
104 natbudin-guest 141 if item not in global_items:
105 otavio 127 debug("[%s] is not allowed in global section." % (item))
106     raise InvalidOption('GLOBAL', item)
107     self['GLOBAL'][item] = value
108    
109 natbudin-guest 141 for item in self.required_in_global:
110     if not self['GLOBAL'].has_key(item):
111     debug("Required option [%s] not found in global section." % (item))
112     raise RequiredOptionMissing('GLOBAL', item)
113    
114 natbudin-guest 121 for section in conf.sections():
115 otavio 127 if section == 'GLOBAL': continue
116 natbudin-guest 121 self[section] = {}
117     for item, value in conf.items(section):
118 otavio 127 if item not in self.allowed_in_backend:
119     for allowed_key in self.allowed_in_backend:
120 otavio 124 # check the allowed_in_backend keys with
121     # @VARIABLES@
122 natbudin-guest 123 if allowed_key.find('@') != -1:
123 natbudin-guest 129 left_length = allowed_key.find('@')
124     right_length = allowed_key[left_length+1:].find('@') + left_length + 2
125     if (allowed_key[:left_length] == item[:left_length]
126     and allowed_key[right_length:] == item[right_length:]):
127 natbudin-guest 123 # found it!
128 natbudin-guest 129 variable = allowed_key[left_length:right_length]
129     match = item[left_length:len(item)-(len(allowed_key)-right_length)]
130     if variable == '@BACKEND@':
131     if match in conf.sections():
132     break
133     else:
134 otavio 136 print("You found a bug: [%s] matches unknown variable [%s]! "
135 otavio 133 "Please report it." % (item, variable))
136     exit(1)
137 otavio 127 else:
138 otavio 124 debug("[%s] is not allowed in a backend section (it was found in [%s])."
139 otavio 133 % (item, section))
140 otavio 124 raise InvalidOption(section, item)
141 natbudin-guest 121 self[section][item] = value
142 otavio 128
143 otavio 134 def getOption(self, option, section='GLOBAL'):
144 otavio 124 # get a config value for a certain section. if it's not
145 otavio 127 # specified, fall back to GLOBAL
146 natbudin-guest 135 if not self.has_key(section):
147 otavio 133 debug("no config section called [%s]." % (section))
148     raise InvalidSection(section)
149 natbudin-guest 135 if self[section].has_key(option):
150 otavio 134 return self[section][option]
151 natbudin-guest 135 if not self['GLOBAL'].has_key(option):
152 otavio 134 debug("[%s] is not present in section [%s] nor the global section of config file." % (option, section))
153     raise InvalidOption(section, option)
154     return self['GLOBAL'][option]
155 otavio 128
156 otavio 119 def dump(self):
157 otavio 127 for section, options in self.items():
158     print section
159     for item, value in options.items():
160     print " %s = %s" %(item, value)
161 otavio 2

  ViewVC Help
Powered by ViewVC 1.1.5