Added a new save method that saves the current config contents into the configuration file by overwriting the old one.
def save(self): self._contents_lock.acquire() with open(path, 'w') as f: for entry in self.keys(): f.write('%s %s\n' % (entry, self.get(entry))) self._contents_lock.release()
If we want to retain the comments --
def save(self): self._contents_lock.acquire() with open(path, 'w') as f: for line in self._raw_contents: key, value = "" comment_start = line.find("#") if comment_start != -1: comment = line[comment_start:] line = line[:comment_start] line = line.strip() if line: try: key, value = line.split(" ", 1) except ValueError: key, value = line, "" f.write("%s %s %s\n" % (key, self.get(key, value), comment) self._contents_lock.release()
Since we strip the lines, we don't really know what the formatting is like. This messes up the white spaces a bit.
Do we have an alternative? Or is one these fine?