2 Flat ASCII Database to load WIN.INI-like files.
7 from m_lib.flad import flad
10 from .flad import checking_error
11 class error(checking_error):
14 class section_error(checking_error):
18 re_section = re.compile("^ *\[(.+)\] *$")
21 class Flad_WIni(flad.Flad):
23 FLAD database is a list of records, where every record is
24 a tuple (section_name, keys, section_dictionary).
25 Sounds similary to Flad? But it is Flad! The only difference is that
26 Flad_WIni has section names and keys (i.e. list of keys and comments
27 in every section to preserve comments and desired order of keys).
30 flad.Flad.__init__(self, key_sep = '=')
31 self.first_section = 1
34 def __parse_line(self, record, line): # Internal function
35 match = re_section.match(line) # Record separator is section name
37 return match.group(1) # Signal to stop filling the record (section) and start a new one
39 if self.first_section:
40 if line.strip() != '':
41 raise error("non-empty line before 1st section")
43 elif (line.strip() == '') or (line.lstrip()[0] == ';') : # Empty line or comment
44 record[0].append(line)
47 key, value = self.split_key(line)
48 if key in record[1].keys(): # This have to be done with check_record
49 # but the record is not complete now,
50 # so it is not ready to be checked :(
51 # And, of course, two keys with the same name
52 # cannot be added to dictionary
53 raise KeyError("field key \"" + key + "\" already in record")
56 record[1][key] = value
61 def create_new_record(self):
65 def feed(self, record, line):
67 section = self.__parse_line(record, line)
69 if not self.first_section:
70 self.append((self.section, record[0], record[1]))
71 self.section = section
73 return 1 # Section filled - create new section
75 self.first_section = 0
76 self.section = section
79 if self.first_section and (line.strip() != ''):
80 raise error("non-empty line before 1st section")
81 # else: line had been appended to section in __parse_line()
83 else: # This called after last line of the source file
84 self.append((self.section, record[0], record[1]))
85 del self.section, self.first_section
87 # Now remove last empty line in every section
92 if klist[l].strip() == '':
98 def store_to_file(self, f):
99 if type(f) == type(''): # If f is string - use it as file's name
100 outfile = open(f, 'w')
102 outfile = f # else assume it is opened file (fileobject) or
103 # "compatible" object (must has write() method)
105 flush_section = 0 # Do not close 1st section
109 outfile.write('\n') # Close section
111 flush_section = 1 # Set flag for all but 1st section
113 outfile.write('[' + record[0] + ']\n') # Section
116 for key in record[1]:
117 if key.strip() == '' or key.lstrip()[0] == ';' :
120 outfile.write(key + self.key_sep + record[2][key] + '\n')
122 if type(f) == type(''): # If f was opened - close it
126 def find_section(self, section):
127 for i in range(0, len(self)):
129 if record[0] == section:
135 def add_section(self, section):
136 rec_no = self.find_section(section)
138 raise section_error("section [%s] already exists" % section)
140 self.append((section, [], {}))
143 def del_section(self, section):
144 rec_no = self.find_section(section)
146 raise section_error("section [%s] does not exists" % section)
151 def set_keyvalue(self, section, key, value):
152 rec_no = self.find_section(section)
154 record = (section, [key], {key: value})
158 record = self[rec_no]
159 if key not in record[1]:
160 record[1].append(key)
161 record[2][key] = value
164 def get_keyvalue(self, section, key):
165 rec_no = self.find_section(section)
167 raise section_error("section [%s] does not exists" % section)
169 record = self[rec_no]
170 if key not in record[1]:
171 raise KeyError("section [%s] does not has `%s' key" % (section, key))
173 return record[2][key]
176 def get_keydefault(self, section, key, default):
177 rec_no = self.find_section(section)
181 record = self[rec_no]
182 if key not in record[1]:
185 return record[2][key]
188 def del_key(self, section, key):
189 rec_no = self.find_section(section)
191 raise section_error("section [%s] does not exists" % section)
193 record = self[rec_no]
194 if key not in record[1]:
195 raise KeyError("section [%s] does not has `%s' key" % (section, key))
198 del klist[klist.index(key)]
209 def load_from_file(f):