From 30a7466008b1e776ce63c7ebd9fd1e7064a8286b Mon Sep 17 00:00:00 2001 From: Gedas Date: Thu, 11 May 2023 17:18:24 +0300 Subject: [PATCH 1/3] made notes accessible from nested directories inside /data --- flatnotes/flatnotes.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/flatnotes/flatnotes.py b/flatnotes/flatnotes.py index 782387b7..78d84e03 100644 --- a/flatnotes/flatnotes.py +++ b/flatnotes/flatnotes.py @@ -43,10 +43,10 @@ def __init__(self, message="The specified title is invalid"): class Note: def __init__( - self, flatnotes: "Flatnotes", title: str, new: bool = False - ) -> None: + self, flatnotes: "Flatnotes", title: str, new: bool = False) -> None: self._flatnotes = flatnotes self._title = title.strip() + self.subdirs = os.path.join("") if not self._is_valid_title(self._title): raise InvalidTitleError if new and os.path.exists(self.filepath): @@ -54,9 +54,23 @@ def __init__( elif new: open(self.filepath, "w").close() + def set_subdirs(self, subdirs): + self.subdirs = subdirs + @property def filepath(self): - return os.path.join(self._flatnotes.dir, self.filename) +# return os.path.join(self._flatnotes.dir, self.subdirs, self.filename) +# filepath = os.path.join(self._flatnotes.dir, self.subdirs, self.filename) + filepath = os.path.join("") + dirlist = glob.glob(os.path.join(self._flatnotes.dir, "**/*" + MARKDOWN_EXT), recursive=True) + for file in dirlist: + if self.filename in file: + filepath = file + return filepath + + @property + def dir_listing(self): + return os.walk(self._flatnotes.dir) @property def filename(self): @@ -77,7 +91,7 @@ def title(self, new_title): if not self._is_valid_title(new_title): raise InvalidTitleError new_filepath = os.path.join( - self._flatnotes.dir, new_title + MARKDOWN_EXT + self._flatnotes.dir, self.subdirs, new_title + MARKDOWN_EXT ) os.rename(self.filepath, new_filepath) self._title = new_title @@ -229,12 +243,15 @@ def _add_note_to_index( def _get_notes(self) -> List[Note]: """Return a list containing a Note object for every file in the notes directory.""" - return [ - Note(self, strip_ext(os.path.split(filepath)[1])) - for filepath in glob.glob( - os.path.join(self.dir, "*" + MARKDOWN_EXT) - ) - ] + notes = [] + for filepath in glob.glob(os.path.join(self.dir, "**/*" + MARKDOWN_EXT), recursive=True): + name = strip_ext(os.path.split(filepath)[1]) + name = str(name) + note = Note(self, name) + subdirs = os.path.relpath(os.path.dirname(filepath), self.dir) + note.set_subdirs(subdirs) + notes.append(note) + return notes def update_index(self, clean: bool = False) -> None: """Synchronize the index with the notes directory. From 70f20324071a55c7da9ae64278ac08307527d884 Mon Sep 17 00:00:00 2001 From: Gedas Date: Sat, 13 May 2023 00:49:03 +0300 Subject: [PATCH 2/3] fixed bug where saving wouldn't work for notes in nested directories --- flatnotes/flatnotes.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/flatnotes/flatnotes.py b/flatnotes/flatnotes.py index 78d84e03..d1cb2cee 100644 --- a/flatnotes/flatnotes.py +++ b/flatnotes/flatnotes.py @@ -24,7 +24,6 @@ StemmingFoldingAnalyzer = StemmingAnalyzer() | CharsetFilter(accent_map) - class IndexSchema(SchemaClass): filename = ID(unique=True, stored=True) last_modified = DATETIME(stored=True, sortable=True) @@ -46,7 +45,6 @@ def __init__( self, flatnotes: "Flatnotes", title: str, new: bool = False) -> None: self._flatnotes = flatnotes self._title = title.strip() - self.subdirs = os.path.join("") if not self._is_valid_title(self._title): raise InvalidTitleError if new and os.path.exists(self.filepath): @@ -54,24 +52,17 @@ def __init__( elif new: open(self.filepath, "w").close() - def set_subdirs(self, subdirs): - self.subdirs = subdirs - @property def filepath(self): -# return os.path.join(self._flatnotes.dir, self.subdirs, self.filename) -# filepath = os.path.join(self._flatnotes.dir, self.subdirs, self.filename) filepath = os.path.join("") - dirlist = glob.glob(os.path.join(self._flatnotes.dir, "**/*" + MARKDOWN_EXT), recursive=True) + dirlist = glob.glob(os.path.join( + self._flatnotes.dir, "**/*" + MARKDOWN_EXT), + recursive=True) for file in dirlist: if self.filename in file: filepath = file return filepath - @property - def dir_listing(self): - return os.walk(self._flatnotes.dir) - @property def filename(self): return self._title + MARKDOWN_EXT @@ -90,8 +81,10 @@ def title(self, new_title): new_title = new_title.strip() if not self._is_valid_title(new_title): raise InvalidTitleError + old_filepath = self.filepath + path = os.path.split(old_filepath)[0] new_filepath = os.path.join( - self._flatnotes.dir, self.subdirs, new_title + MARKDOWN_EXT + path, new_title + MARKDOWN_EXT ) os.rename(self.filepath, new_filepath) self._title = new_title @@ -248,8 +241,6 @@ def _get_notes(self) -> List[Note]: name = strip_ext(os.path.split(filepath)[1]) name = str(name) note = Note(self, name) - subdirs = os.path.relpath(os.path.dirname(filepath), self.dir) - note.set_subdirs(subdirs) notes.append(note) return notes From 11e3eecc80619f8584ca2a8b871c0ce1505451d9 Mon Sep 17 00:00:00 2001 From: Gedas Date: Sat, 13 May 2023 01:02:31 +0300 Subject: [PATCH 3/3] fixed bug where new notes wouldn't get created --- flatnotes/flatnotes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flatnotes/flatnotes.py b/flatnotes/flatnotes.py index d1cb2cee..25999e4c 100644 --- a/flatnotes/flatnotes.py +++ b/flatnotes/flatnotes.py @@ -54,7 +54,8 @@ def __init__( @property def filepath(self): - filepath = os.path.join("") + filepath = os.path.join( + self._flatnotes.dir, self._title + MARKDOWN_EXT) dirlist = glob.glob(os.path.join( self._flatnotes.dir, "**/*" + MARKDOWN_EXT), recursive=True)