Skip to content

Commit 29ec48f

Browse files
committed
refactor persistence to organize all files of a note in one directory
1 parent 60bdfda commit 29ec48f

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

note.py

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import webbrowser
1515
import base64
1616
import uuid
17+
import shutil
1718
from tkinterweb import HtmlFrame
1819
from tkinter import scrolledtext
1920
from tkinter import ttk
@@ -27,67 +28,62 @@
2728

2829
class Persistence:
2930
def __init__(self):
30-
self._basepath = os.path.join(Path.home(), ".notepy")
31-
self._mkdir(self._basepath)
32-
self._notespath = os.path.join(self._basepath, "notes")
33-
self._mkdir(self._notespath)
34-
self._assetspath = os.path.join(self._notespath, self.relative_assetspath())
35-
self._mkdir(self._assetspath)
31+
self.__basepath = os.path.join(Path.home(), ".notepy")
32+
self.__mkdir(self.__basepath)
33+
self.__notespath = os.path.join(self.__basepath, "notes")
34+
self.__mkdir(self.__notespath)
3635

37-
def _mkdir(self, path):
36+
def __mkdir(self, path):
3837
if not os.path.isdir(path):
3938
os.mkdir(path)
4039

41-
def _note_filename(self, name):
42-
return os.path.join(self._notespath, name + ".md")
40+
def __note_filename(self, name):
41+
return os.path.join(self.__notespath, name, "note.md")
4342

44-
def assetspath(self):
45-
return self._assetspath
46-
47-
def relative_assetspath(self):
48-
return "assets"
49-
5043
def notespath(self):
5144
return self._notespath
45+
46+
def note_path(self, name):
47+
return os.path.join(self.__notespath, name)
5248

5349
def list_notes(self):
5450
notes = []
55-
for filename in os.listdir(self._notespath):
56-
fullpath = os.path.join(self._notespath, filename)
57-
if os.path.isfile(fullpath) and filename.endswith(".md"):
58-
notes.append(os.path.splitext(filename)[0])
51+
for name in os.listdir(self.__notespath):
52+
notefile = self.__note_filename(name)
53+
if os.path.isfile(notefile):
54+
notes.append(name)
5955
return notes
6056

6157
def read_note(self, name):
62-
filename = self._note_filename(name)
63-
if os.path.isfile(filename):
64-
with open(filename, "rb") as f:
65-
data = f.read().decode("utf-8")
66-
return data
67-
else:
68-
return ""
58+
filename = self.__note_filename(name)
59+
if not os.path.isfile(filename):
60+
self.write_note(name, "")
61+
with open(filename, "rb") as f:
62+
data = f.read().decode("utf-8")
63+
return data
6964

7065
def write_note(self, name, text):
71-
filename = self._note_filename(name)
66+
self.__mkdir(self.note_path(name))
67+
filename = self.__note_filename(name)
7268
with open(filename, "wb") as f:
7369
f.write(text.encode("utf-8"))
7470

7571
def rename_note(self, oldname, newname):
76-
filename_old = self._note_filename(oldname)
77-
filename_new = self._note_filename(newname)
78-
os.rename(filename_old, filename_new)
72+
old_path = self.note_path(oldname)
73+
new_path = self.note_path(newname)
74+
os.rename(old_path, new_path)
7975

8076
def remove_note(self, name):
81-
filename = self._note_filename(name)
82-
if os.path.isfile(filename):
83-
os.remove(filename)
77+
note_path = self.note_path(name)
78+
if os.path.isdir(note_path):
79+
shutil.rmtree(note_path)
8480

85-
def screenshot(self):
81+
def screenshot(self, name):
8682
filename = "screenshot_" + str(uuid.uuid4()) + ".png"
87-
full_filename = os.path.join(self._assetspath, filename)
83+
full_filename = os.path.join(self.note_path(name), filename)
8884
status = os.system('gnome-screenshot -a -f %s' % full_filename)
8985
exit_code = os.waitstatus_to_exitcode(status)
90-
return os.path.join(self.relative_assetspath(), filename) if 0 == exit_code else None
86+
return filename if 0 == exit_code else None
9187

9288

9389

@@ -114,8 +110,9 @@ def __init__(self, parent, persistence, name, isvalid=True):
114110
self.__parent = parent
115111
self.__persistence = persistence
116112
self.__name = name
117-
self.__contents = self.__persistence.read_note(self.__name)
113+
self.__contents = self.__persistence.read_note(self.__name) if isvalid else ""
118114
self.isvalid = isvalid
115+
119116

120117
def __repr__(self):
121118
return self.__name
@@ -142,10 +139,10 @@ def delete(self):
142139
self.__parent.note_changed()
143140

144141
def screenshot(self):
145-
return self.__persistence.screenshot() if self.isvalid else None
142+
return self.__persistence.screenshot(self.__name) if self.isvalid else None
146143

147144
def base_path(self):
148-
return self.__persistence.notespath()
145+
return self.__persistence.note_path(self.__name)
149146

150147

151148
class NoteCollection:
@@ -415,7 +412,10 @@ def __init__(self, model=AppModel()):
415412
self.root.bind("<Control-p>", lambda e: self.noteframe.screenshot())
416413

417414
def onclose(self):
418-
self.noteframe.save()
415+
try:
416+
self.noteframe.save()
417+
except:
418+
print("error: failed to save note")
419419
self.root.destroy()
420420

421421
def run(self):

0 commit comments

Comments
 (0)