예제 #1
0
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
예제 #2
0
 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
예제 #3
0
def load_data(profile_name=None,
              _reset_sessions=True,
              _update_metadata=True,
              _create_new=True):
    """Read a profile (or create new one) and run it through the update.
    Use LoadData class instead of this.
    """
    paths = _get_paths(profile_name)
    new_file = False

    #Load the main file
    try:
        with CustomOpen(paths['Main'], 'rb') as f:
            loaded_data = decode_file(f, legacy=f.zip is None)

    #Load backup if file is corrupted
    except (zlib.error, ValueError):
        try:
            with CustomOpen(paths['Backup'], 'rb') as f:
                loaded_data = decode_file(f, legacy=f.zip is None)

        except (IOError, zlib.error, ValueError):
            new_file = True

            #Move corrupt file into a folder instead of just silently delete
            if create_folder(paths['CorruptedFolder'], is_file=False):
                hide_file(paths['CorruptedFolder'])
            rename_file(paths['Main'], '{}.{}'.format(paths['Corrupted'],
                                                      int(time.time())))

    #Don't load backup if file has been deleted
    except IOError:
        new_file = True

    #Create empty data
    if new_file:
        if _create_new:
            loaded_data = {}
        else:
            return None

    return upgrade_version(loaded_data,
                           reset_sessions=_reset_sessions,
                           update_metadata=_update_metadata)
예제 #4
0
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