def save_data(profile_name, data, _compress=True): """Handle the safe saving of profiles. Instead of overwriting, it will save as a temprary file and attempt to rename. At any point in time there are two copies of the save. """ #This is to allow pre-compressed data to be sent in if _compress: data = prepare_file(data) paths = _get_paths(profile_name) if create_folder(paths['BackupFolder'], is_file=False): hide_file(paths['BackupFolder']) if create_folder(paths['TempFolder'], is_file=False): hide_file(paths['TempFolder']) with open(paths['Temp'], 'wb') as f: f.write(data) remove_file(paths['Backup']) rename_file(paths['Main'], paths['Backup']) if rename_file(paths['Temp'], paths['Main']): return True else: remove_file(paths['Temp']) return False
def create(self): """Open a new locked file, or return None if it already exists.""" if not file_exists(self._name) or remove_file(self._name): f = open(self._name, 'w') hide_file(self._name) else: f = None return f
def release(self): """Release the locked file, and delete if possible. Issue with multithreading where the file seems impossible to delete, so just ignore for now. """ if not self.closed: if self._file is not None: self._file.close() self.closed = True return remove_file(self._name)
def save_program(program_name, data, compress=True): if compress: compressed_data = prepare_file(data) else: compressed_data = data paths = _get_paths(program_name) if create_folder(paths['BackupFolder']): hide_file(paths['BackupFolder']) if create_folder(paths['TempFolder']): hide_file(paths['TempFolder']) with open(paths['Temp'], 'wb') as f: f.write(compressed_data) remove_file(paths['Backup']) rename_file(paths['Main'], paths['Backup']) if rename_file(paths['Temp'], paths['Main']): return True else: remove_file(paths['Temp']) return False
def cache_delete(self, file_name): remove_file(file_name)