2 Flat ASCII Database to load WIN.INI-like files.
4 Written by Broytman. Copyright (C) 1997-2005 PhiloSoft Design
13 section_error = "fladw.section_error"
16 re_section = re.compile("^ *\[(.+)\] *$")
19 class Flad_WIni(flad.Flad):
21 FLAD database is a list of records, where every record is
22 a tuple (section_name, keys, section_dictionary).
23 Sounds similary to Flad? But it is Flad! The only difference is that
24 Flad_WIni has section names and keys (i.e. list of keys and comments
25 in every section to preserve comments and desired order of keys).
28 flad.Flad.__init__(self, key_sep = '=')
29 self.first_section = 1
32 def __parse_line(self, record, line): # Internal function
33 match = re_section.match(line) # Record separator is section name
35 return match.group(1) # Signal to stop filling the record (section) and start a new one
37 if self.first_section:
38 if string.strip(line) <> '':
39 raise error, "non-empty line before 1st section"
41 elif (string.strip(line) == '') or (string.lstrip(line)[0] == ';') : # Empty line or comment
42 record[0].append(line)
45 key, value = self.split_key(line)
46 if key in record[1].keys(): # This have to be done with check_record
47 # but the record is not complete now,
48 # so it is not ready to be checked :(
49 # And, of course, two keys with the same name
50 # cannot be added to dictionary
51 raise KeyError, "field key \"" + key + "\" already in record"
54 record[1][key] = value
59 def create_new_record(self):
63 def feed(self, record, line):
65 section = self.__parse_line(record, line)
67 if not self.first_section:
68 self.append((self.section, record[0], record[1]))
69 self.section = section
71 return 1 # Section filled - create new section
73 self.first_section = 0
74 self.section = section
77 if self.first_section and (string.strip(line) <> ''):
78 raise error, "non-empty line before 1st section"
79 # else: line had been appended to section in __parse_line()
81 else: # This called after last line of the source file
82 self.append((self.section, record[0], record[1]))
83 del self.section, self.first_section
85 # Now remove last empty line in every section
90 if string.strip(klist[l]) == '':
96 def store_to_file(self, f):
97 if type(f) == type(''): # If f is string - use it as file's name
98 outfile = open(f, 'w')
100 outfile = f # else assume it is opened file (fileobject) or
101 # "compatible" object (must has write() method)
103 flush_section = 0 # Do not close 1st section
107 outfile.write('\n') # Close section
109 flush_section = 1 # Set flag for all but 1st section
111 outfile.write('[' + record[0] + ']\n') # Section
114 for key in record[1]:
115 if string.strip(key) == '' or string.lstrip(key)[0] == ';' :
118 outfile.write(key + self.key_sep + record[2][key] + '\n')
120 if type(f) == type(''): # If f was opened - close it
124 def find_section(self, section):
125 for i in range(0, len(self)):
127 if record[0] == section:
133 def add_section(self, section):
134 rec_no = self.find_section(section)
136 raise section_error, "section [%s] already exists" % section
138 self.append((section, [], {}))
141 def del_section(self, section):
142 rec_no = self.find_section(section)
144 raise section_error, "section [%s] does not exists" % section
149 def set_keyvalue(self, section, key, value):
150 rec_no = self.find_section(section)
152 record = (section, [key], {key: value})
156 record = self[rec_no]
157 if key not in record[1]:
158 record[1].append(key)
159 record[2][key] = value
162 def get_keyvalue(self, section, key):
163 rec_no = self.find_section(section)
165 raise section_error, "section [%s] does not exists" % section
167 record = self[rec_no]
168 if key not in record[1]:
169 raise KeyError, "section [%s] does not has `%s' key" % (section, key)
171 return record[2][key]
174 def get_keydefault(self, section, key, default):
175 rec_no = self.find_section(section)
179 record = self[rec_no]
180 if key not in record[1]:
183 return record[2][key]
186 def del_key(self, section, key):
187 rec_no = self.find_section(section)
189 raise section_error, "section [%s] does not exists" % section
191 record = self[rec_no]
192 if key not in record[1]:
193 raise KeyError, "section [%s] does not has `%s' key" % (section, key)
196 del klist[klist.index(key)]
207 def load_from_file(f):