import repository from arizona
[raven.git] / lib / ravenlib / ravenconfigparser.py
1 import sys,os,ConfigParser
2
3 class RavenConfigParser(ConfigParser.SafeConfigParser):
4     def __init__(self):
5         ConfigParser.SafeConfigParser.__init__(self)
6         self.ordered_options = {}
7         self.ordered_sections = []
8         self.update_order = False
9
10     def optionxform(self, x):
11         x=ConfigParser.SafeConfigParser.optionxform(self, x)
12
13         # this is a bunch of machinery to keep self.ordered_options and
14         # self.ordered_sections up to date
15
16         if not self.update_order:
17             return x
18
19         keys = self.sections()
20         for key in keys:
21             if not key in self.ordered_sections:
22                 self.ordered_sections.append(key)
23
24         if not self.ordered_sections:
25             return x
26
27         cursect_name = self.ordered_sections[-1]
28         if not cursect_name:
29             return x
30
31         options = self.ordered_options.get(cursect_name, [])
32         if not x in options:
33             options.append(x)
34             self.ordered_options[cursect_name] = options
35
36         return x
37
38     def GetOpt(self, section, option, default):
39         try:
40             value = self.get(section, option)
41         except ConfigParser.NoOptionError:
42             value = default
43         except ConfigParser.NoSectionError:
44             value = default
45         return value
46
47     def GetOptBool(self, section, option, default):
48         value = self.GetOpt(section, option, default)
49         if value.lower() == "true":
50             return True
51         elif value.lower() == "false":
52             return False
53         else:
54             return value
55
56     def GetOptInt(self, section, option, default):
57         value = self.GetOpt(section, option, default)
58         try:
59             value = int(value)
60         except:
61             value = 0
62         return value
63
64     def SetBool(self, section, option, value):
65         if value:
66             self.set(section, option, "True")
67         else:
68             self.set(section, option, "False")
69
70     def SetInt(self, section, option, value):
71         self.set(section, option, str(value))
72
73     def Get(self, section, option):
74         try:
75             value = self.get(section, option)
76         except ConfigParser.NoOptionError:
77             print "No option '%s' in section '%s' in  file '%s'" % (option,section,self.path)
78             exit(1)
79         except ConfigParser.NoSectionError:
80             print "No section '%s' in config file '%s'" % (section,self.path)
81             exit(1)
82         return value
83
84     def Read(self, path, bail):
85         self.path = path
86         f = []
87         try:
88             f = self.read(path)
89         except ConfigParser.ParsingError :
90             print "Unable to parse '%s'" % (self.path)
91             if (bail):
92                 exit(1)
93         return f