diff --git a/iosfu/backup.py b/iosfu/backup.py index 049d8ff..f34ce82 100644 --- a/iosfu/backup.py +++ b/iosfu/backup.py @@ -1,10 +1,12 @@ +from __future__ import with_statement + from os import listdir from os.path import join as join_paths, basename, isdir, isfile from plistlib import readPlist from biplist import readPlist as readBinaryPlist -from .conf import BACKUPS_PATH +from .conf import BACKUPS_PATH, BACKUP_DEFAULT_SETTINGS from iosfu import utils @@ -84,14 +86,10 @@ class Backup(object): def read_data_file(self): try: handler = open(self._data_file) - except FileNotFoundError: + except (OSError, IOError): + # Create default config file if non-existant handler = open(self._data_file, 'w+') - # Initial data - data = { - "id": self.id, - "cache": {} - } - handler.write(utils.serialize(data)) + handler.write(utils.serialize(BACKUP_DEFAULT_SETTINGS)) handler.seek(0) finally: with handler as f: diff --git a/iosfu/conf.py b/iosfu/conf.py index ebe0318..27887ca 100644 --- a/iosfu/conf.py +++ b/iosfu/conf.py @@ -7,3 +7,9 @@ ROOT_PATH = getcwd() # Backups folder name BACKUPS_PATH = join_paths(ROOT_PATH, '_backups') + +# Backup default settings +BACKUP_DEFAULT_SETTINGS = { + "cache.enabled": False, + "cache": {} +} diff --git a/iosfu/utils.py b/iosfu/utils.py index 18a0c8e..aee6b6d 100644 --- a/iosfu/utils.py +++ b/iosfu/utils.py @@ -6,9 +6,8 @@ import json class IOSFUEncoder(json.JSONEncoder): def default(self, obj): - # DATETIME -> TIMESTAMP - if isinstance(obj, datetime.datetime): + if isinstance(obj, datetime): return "timestamp:{}".format(obj.timestamp()) # Let the base class default method raise the TypeError @@ -41,7 +40,7 @@ def slugify(string): def serialize(dictionary): - return json.dumps(dictionary, indent=4 * ' ', cls=IOSFUEncoder) + return json.dumps(dictionary, indent=4, cls=IOSFUEncoder) def deserialize(string):