Only save wallet if modified

This commit is contained in:
Neil Booth 2015-09-11 14:02:01 +09:00
parent f710d872c7
commit b5e0363f85
1 changed files with 13 additions and 5 deletions

View File

@ -51,6 +51,7 @@ class WalletStorage(PrintError):
self.data = {} self.data = {}
self.path = path self.path = path
self.file_exists = False self.file_exists = False
self.modified = False
self.print_error("wallet path", self.path) self.print_error("wallet path", self.path)
if self.path: if self.path:
self.read(self.path) self.read(self.path)
@ -94,7 +95,7 @@ class WalletStorage(PrintError):
v = default v = default
else: else:
v = copy.deepcopy(v) v = copy.deepcopy(v)
return v return v
def put(self, key, value, save = True): def put(self, key, value, save = True):
try: try:
@ -105,16 +106,22 @@ class WalletStorage(PrintError):
return return
with self.lock: with self.lock:
if value is not None: if value is not None:
self.data[key] = copy.deepcopy(value) if self.data.get(key) != value:
self.modified = True
self.data[key] = copy.deepcopy(value)
elif key in self.data: elif key in self.data:
self.modified = True
self.data.pop(key) self.data.pop(key)
if save: if save:
self.write() self.write()
def write(self): def write(self):
assert not threading.currentThread().isDaemon() assert not threading.currentThread().isDaemon()
if not self.modified:
return
with self.lock:
s = json.dumps(self.data, indent=4, sort_keys=True)
temp_path = "%s.tmp.%s" % (self.path, os.getpid()) temp_path = "%s.tmp.%s" % (self.path, os.getpid())
s = json.dumps(self.data, indent=4, sort_keys=True)
with open(temp_path, "w") as f: with open(temp_path, "w") as f:
f.write(s) f.write(s)
f.flush() f.flush()
@ -128,6 +135,7 @@ class WalletStorage(PrintError):
if 'ANDROID_DATA' not in os.environ: if 'ANDROID_DATA' not in os.environ:
import stat import stat
os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE) os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
self.print_error("saved")