2 Flat ASCII Database to load WIN.INI-like files.
7 from m_lib.flad import flad
11 section_error = "fladw.section_error"
14 re_section = re.compile("^ *\[(.+)\] *$")
17 class Flad_WIni(flad.Flad):
19 FLAD database is a list of records, where every record is
20 a tuple (section_name, keys, section_dictionary).
21 Sounds similary to Flad? But it is Flad! The only difference is that
22 Flad_WIni has section names and keys (i.e. list of keys and comments
23 in every section to preserve comments and desired order of keys).
26 flad.Flad.__init__(self, key_sep = '=')
27 self.first_section = 1
30 def __parse_line(self, record, line): # Internal function
31 match = re_section.match(line) # Record separator is section name
33 return match.group(1) # Signal to stop filling the record (section) and start a new one
35 if self.first_section:
36 if line.strip() != '':
37 raise error("non-empty line before 1st section")
39 elif (line.strip() == '') or (line.lstrip()[0] == ';') : # Empty line or comment
40 record[0].append(line)
43 key, value = self.split_key(line)
44 if key in record[1].keys(): # This have to be done with check_record
45 # but the record is not complete now,
46 # so it is not ready to be checked :(
47 # And, of course, two keys with the same name
48 # cannot be added to dictionary
49 raise KeyError("field key \"" + key + "\" already in record")
52 record[1][key] = value
57 def create_new_record(self):
61 def feed(self, record, line):
63 section = self.__parse_line(record, line)
65 if not self.first_section:
66 self.append((self.section, record[0], record[1]))
67 self.section = section
69 return 1 # Section filled - create new section
71 self.first_section = 0
72 self.section = section
75 if self.first_section and (line.strip() != ''):
76 raise error("non-empty line before 1st section")
77 # else: line had been appended to section in __parse_line()
79 else: # This called after last line of the source file
80 self.append((self.section, record[0], record[1]))
81 del self.section, self.first_section
83 # Now remove last empty line in every section
88 if klist[l].strip() == '':
94 def store_to_file(self, f):
95 if type(f) == type(''): # If f is string - use it as file's name
96 outfile = open(f, 'w')
98 outfile = f # else assume it is opened file (fileobject) or
99 # "compatible" object (must has write() method)
101 flush_section = 0 # Do not close 1st section
105 outfile.write('\n') # Close section
107 flush_section = 1 # Set flag for all but 1st section
109 outfile.write('[' + record[0] + ']\n') # Section
112 for key in record[1]:
113 if key.strip() == '' or key.lstrip()[0] == ';' :
116 outfile.write(key + self.key_sep + record[2][key] + '\n')
118 if type(f) == type(''): # If f was opened - close it
122 def find_section(self, section):
123 for i in range(0, len(self)):
125 if record[0] == section:
131 def add_section(self, section):
132 rec_no = self.find_section(section)
134 raise section_error("section [%s] already exists" % section)
136 self.append((section, [], {}))
139 def del_section(self, section):
140 rec_no = self.find_section(section)
142 raise section_error("section [%s] does not exists" % section)
147 def set_keyvalue(self, section, key, value):
148 rec_no = self.find_section(section)
150 record = (section, [key], {key: value})
154 record = self[rec_no]
155 if key not in record[1]:
156 record[1].append(key)
157 record[2][key] = value
160 def get_keyvalue(self, section, key):
161 rec_no = self.find_section(section)
163 raise section_error("section [%s] does not exists" % section)
165 record = self[rec_no]
166 if key not in record[1]:
167 raise KeyError("section [%s] does not has `%s' key" % (section, key))
169 return record[2][key]
172 def get_keydefault(self, section, key, default):
173 rec_no = self.find_section(section)
177 record = self[rec_no]
178 if key not in record[1]:
181 return record[2][key]
184 def del_key(self, section, key):
185 rec_no = self.find_section(section)
187 raise section_error("section [%s] does not exists" % section)
189 record = self[rec_no]
190 if key not in record[1]:
191 raise KeyError("section [%s] does not has `%s' key" % (section, key))
194 del klist[klist.index(key)]
205 def load_from_file(f):