Skip to content

Commit

Permalink
Merge pull request #11 from clach04/issue_10_fix_corruption_data_loss
Browse files Browse the repository at this point in the history
Fix #10 -  corruption data loss
  • Loading branch information
linickx authored Nov 25, 2024
2 parents 7f90b5a + 2467c62 commit 6cf6c94
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions simplenote_sync/notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ def __init__(self, config, logger):
self.log.debug("Exception: %s", sys.exc_info()[1])
sys.exit(1)

def write(self, note, filename, access_time):
try:
f = open(filename, 'w', encoding='utf-8')
f.write(note['content'])
f.close()
self.log.info("Writing %s", filename)

os.utime(filename, (access_time, float(note['modifydate'])))
return filename
except:
self.log.error("Error writing note id: %s, %r", note['key'], filename, exc_info=True)
self.log.debug("Exception: %s", sys.exc_info()[1])
return False

def new(self, note):
"""
Create a new note file, returns filename
Expand All @@ -67,18 +81,8 @@ def new(self, note):
if os.path.isfile(path + "/" + filename):
filename = filetime + "_" + filename # Don't blast over files with same name, i.e. same first line.

try:
f = open(path + "/" + filename, 'w')
f.write(note['content'])
f.close()
self.log.info("Writing %s", filename)

os.utime(path + "/" + filename, (access_time, float(note['modifydate'])))

return filename
except:
self.log.error("Error writing note: %s", note['key'])
self.log.debug("Exception: %s", sys.exc_info()[1])
result = self.write(note, path + "/" + filename, access_time)
return result
else:
self.log.error("Error generating filename for note: %s", note['key'])

Expand Down Expand Up @@ -145,19 +149,9 @@ def update(self, note, nf_meta):
filename = nf_meta['filename']
access_time = time.time()

try:
f = open(path + "/" + filename, 'w')
f.write(note['content'])
f.close()
self.log.info("Writing %s", filename)

os.utime(path + "/" + filename, (access_time, float(note['modifydate'])))

result = self.write(note, path + "/" + filename, access_time)
if result:
return True
except:
self.log.error("Error writing note: %s", note['key'])
self.log.debug("Exception: %s", sys.exc_info()[1])

return False

def open(self, filename):
Expand All @@ -169,7 +163,7 @@ def open(self, filename):

if os.path.isfile(path + "/" + filename):
try:
f = open(path + "/" + filename, 'r')
f = open(path + "/" + filename, 'r', encoding='utf-8')
notefile['content'] = f.read()
f.close()
except:
Expand Down

0 comments on commit 6cf6c94

Please sign in to comment.