def load_location_as_dictionaries (self, location): if not os.path.isfile(location): raise IOError("File '%s' not found" % location) # FIXME: Check why it does not work directly on fileobj fileobj = open(location, 'rU') striofile = StringIO(fileobj.read()) fileobj.close() res = [] obj = read_object(striofile) while obj: res.append(obj) obj = read_object(striofile) return res
def read(self, fileobj, match=None): if not match: for line in fileobj: if line.strip(): match = self.match_re.match(line) if not match or not match.groups(): fileobj.seek(-len(line), os.SEEK_CUR) break name = match and match.group(1) or "" obj = make_object(name, {}) for line in fileobj: if not line.strip(): continue match = self.match_re.match(line) if match and match.groups(): if match.group(1)[: len(name)] != name or len(name) >= len(match.group(1)): # A section, but not a sub-section fileobj.seek(-len(line), os.SEEK_CUR) break obj.append(match.group(1), self.read(fileobj, match)) else: fileobj.seek(-len(line), os.SEEK_CUR) tobj = read_object(fileobj) obj.append(tobj.name, tobj) return obj
def test_read_object_simple (self): obj = make_object('Key1', 'Simple Text') write_object(obj, self.fileobj) self.fileobj.seek(0) nobj = read_object(self.fileobj) self.assertEqual(obj, nobj)
def test_read_object_empty (self): obj = make_object('Key0', '') StringObject().write(obj, self.fileobj) self.fileobj.seek(0) nobj = read_object(self.fileobj) self.assertEqual(obj, nobj)
def test_read_object_multiline_empty (self): obj = make_object('Key3', 'MultiLine\n\nw/ empty line') write_object(obj, self.fileobj) self.fileobj.seek(0) nobj = read_object(self.fileobj) self.assertEqual(obj, nobj)
def test_read_object_multiline (self): obj = make_object('Key2', 'MultiLine text\nSecondLine') write_object(obj, self.fileobj) self.fileobj.seek(0) nobj = read_object(self.fileobj) self.assertEqual(obj, nobj)
def test_read_object_key_empty (self): obj = make_object('Section', {}) write_object(obj, self.fileobj) self.fileobj.seek(0) nobj = read_object(self.fileobj) self.assertEqual(obj, nobj)
def test_read_object_empty_empty (self): obj = make_object('', {}) write_object(obj, self.fileobj) self.fileobj.seek(0) nobj = read_object(self.fileobj, as_dictionary=True) self.assertEqual(obj, nobj)
def test_read_object_empty_strings_simple (self): obj = make_object('', {}) obj.append('Key1', 'Value1') obj.append('Key2', 'Value2') obj.append('Key3', 'Value3') write_object(obj, self.fileobj) self.fileobj.seek(0) nobj = read_object(self.fileobj, as_dictionary=True) self.assertEqual(obj, nobj)