4 from TkAlExceptions
import AllInOneError
9 Dictionary which handles updates of values for already existing keys
11 Instead of replacing the old value, the new value is appended to the
12 value string separated by `self.getSep()`.
13 This dictionary is used in the class `BetterConfigParser` instead of the
14 default `dict_type` of the `ConfigParser` class.
19 This method returns the separator used to separate the values for
20 duplicate options in a config.
24 def __setitem__(self, key, value, dict_setitem=dict.__setitem__):
26 od.__setitem__(i, y) <==> od[i]=y
27 Updating an existing key appends the new value to the old value
28 separated by `self.getSep()` instead of replacing it.
31 - `key`: key part of the key-value pair
32 - `value`: value part of the key-value pair
33 - `dict_item`: method which is used for finally setting the item
36 if "__name__" in self
and self[
"__name__"]==
"validation" \
37 and key
in self
and value!=self[key][0]:
38 the_value = [self[key][0]+self.
getSep()+value[0]]
41 dict_setitem(self, key, the_value)
46 ConfigParser.ConfigParser.__init__(self,dict_type=AdaptedDict)
48 self.
_sep = dummyDict.getSep()
59 items = self.items(section)
60 except ConfigParser.NoSectionError:
70 for option
in self.options( section ):
71 result[option] = self.get( section, option )
72 if "local"+section.title()
in self.sections():
73 for option
in self.options(
"local"+section.title() ):
74 result[option] = self.get(
"local"+section.title(),
76 except ConfigParser.NoSectionError, section:
77 msg = (
"%s in configuration files. This section is mandatory."
78 %(str(section).
replace(
":",
"", 1)))
83 result = copy.deepcopy(defaultDict)
84 for option
in demandPars:
86 result[option] = self.get( section, option )
87 except ConfigParser.NoOptionError, globalSectionError:
88 globalSection = str( globalSectionError ).
split(
"'" )[-2]
89 splittedSectionName = section.split(
":" )
90 if len( splittedSectionName ) > 1:
91 localSection = (
"local"+section.split(
":" )[0].
title()+
":"
92 +section.split(
":")[1])
94 localSection = (
"local"+section.split(
":" )[0].
title())
95 if self.has_section( localSection ):
97 result[option] = self.get( localSection, option )
98 except ConfigParser.NoOptionError, option:
99 msg = (
"%s. This option is mandatory."
102 "section '"+globalSection+
"' or", 1)))
105 msg = (
"%s. This option is mandatory."
106 %(str(globalSectionError).
replace(
":",
"", 1)))
113 for section
in self.sections():
114 if "alignment:" in section:
115 alignments.append( Alignment( section.split(
"alignment:" )[1],
121 for section
in self.sections():
122 if "compare:" in section:
124 knownSimpleOptions = [
"levels",
"dbOutput",
126 levels = self.get( section,
"levels" )
127 dbOutput = self.get( section,
"dbOutput" )
128 compares[section.split(
":")[1]] = ( levels, dbOutput )
133 "jobmode":
"interactive",
134 "datadir":os.getcwd(),
135 "logdir":os.getcwd(),
139 self.
checkInput(
"general", knownSimpleOptions = defaults.keys())
141 internal_section =
"internals"
142 if not self.has_section(internal_section):
143 self.add_section(internal_section)
144 if not self.has_option(internal_section,
"workdir"):
145 self.set(internal_section,
"workdir",
"/tmp/$USER")
146 general[
"workdir"] = self.get(internal_section,
"workdir")
149 def checkInput(self, section, knownSimpleOptions=[], knownKeywords=[],
152 Method which checks, if the given options in `section` are in the
153 list of `knownSimpleOptions` or match an item of `knownKeywords`.
154 This is basically a check for typos and wrong parameters.
157 - `section`: Section of a configuration file
158 - `knownSimpleOptions`: List of allowed simple options in `section`.
159 - `knownKeywords`: List of allowed keywords in `section`.
162 for option
in self.options( section ):
163 if option
in knownSimpleOptions:
165 elif option.split()[0]
in knownKeywords:
167 elif option
in ignoreOptions:
168 print (
"Ignoring option '%s' in section '[%s]'."
171 msg = (
"Invalid or unknown parameter '%s' in section '%s'!"