def append_reload_and_write(self, s, filename, encoding): if not self.hist_size: return self.append(s) try: with codecs.open(filename, 'a+', encoding, 'ignore') as hfile: with FileLock(hfile): # read entries hfile.seek(0, os.SEEK_SET) entries = self.load_from(hfile) self.append_to(entries, s) # write new entries hfile.seek(0, os.SEEK_SET) hfile.truncate() self.save_to(hfile, entries, self.hist_size) self.entries = entries except EnvironmentError as err: raise RuntimeError( _('Error occurred while writing to file %s (%s)') % (filename, err.strerror)) else: if len(self.entries) == 0: # Make sure that entries contains at least one element. If the # file and s are empty, this can occur. self.entries = ['']
def save(self, filename, encoding, lines=0): fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.TRUNC, stat.S_IRUSR | stat.S_IWUSR) with io.open(fd, 'w', encoding=encoding, errors='ignore') as hfile: with FileLock(hfile): self.save_to(hfile, self.entries, lines)
def save(self, filename, encoding, lines=0): with codecs.open(filename, 'w', encoding, 'ignore') as hfile: with FileLock(hfile): self.save_to(hfile, self.entries, lines)
def load(self, filename, encoding): with codecs.open(filename, 'r', encoding, 'ignore') as hfile: with FileLock(hfile): self.entries = self.load_from(hfile)
def load(self, filename, encoding): with io.open(filename, 'r', encoding=encoding, errors='ignore') as hfile: with FileLock(hfile): self.entries = self.load_from(hfile)