forked from dullage/flatnotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_system.py
More file actions
394 lines (349 loc) · 14.6 KB
/
file_system.py
File metadata and controls
394 lines (349 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import glob
import os
import re
import shutil
import time
from datetime import datetime
from typing import List, Literal, Set, Tuple
import whoosh
from whoosh import writing
from whoosh.analysis import CharsetFilter, StemmingAnalyzer
from whoosh.fields import DATETIME, ID, KEYWORD, TEXT, SchemaClass
from whoosh.highlight import ContextFragmenter, WholeFragmenter
from whoosh.index import Index, LockError
from whoosh.qparser import MultifieldParser
from whoosh.qparser.dateparse import DateParserPlugin
from whoosh.query import Every
from whoosh.searching import Hit
from whoosh.support.charset import accent_map
from helpers import get_env, is_valid_filename
from logger import logger
from ..base import BaseNotes
from ..models import Note, NoteCreate, NoteUpdate, SearchResult
MARKDOWN_EXT = ".md"
INDEX_SCHEMA_VERSION = "5"
StemmingFoldingAnalyzer = StemmingAnalyzer() | CharsetFilter(accent_map)
class IndexSchema(SchemaClass):
filename = ID(unique=True, stored=True)
last_modified = DATETIME(stored=True, sortable=True)
title = TEXT(
field_boost=2.0, analyzer=StemmingFoldingAnalyzer, sortable=True
)
content = TEXT(analyzer=StemmingFoldingAnalyzer)
tags = KEYWORD(lowercase=True, field_boost=2.0)
class FileSystemNotes(BaseNotes):
TAGS_RE = re.compile(r"(?:(?<=^#)|(?<=\s#))[a-zA-Z0-9_-]+(?=\s|$)")
CODEBLOCK_RE = re.compile(r"`{1,3}.*?`{1,3}", re.DOTALL)
TAGS_WITH_HASH_RE = re.compile(
r"(?:(?<=^)|(?<=\s))#[a-zA-Z0-9_-]+(?=\s|$)"
)
def __init__(self):
self.storage_path = get_env("FLATNOTES_PATH", mandatory=True)
if not os.path.exists(self.storage_path):
raise NotADirectoryError(
f"'{self.storage_path}' is not a valid directory."
)
self.index = self._load_index()
self._sync_index_with_retry(optimize=True)
def create(self, data: NoteCreate) -> Note:
"""Create a new note."""
filepath = self._path_from_title(data.title)
self._write_file(filepath, data.content)
return Note(
title=data.title,
content=data.content,
last_modified=os.path.getmtime(filepath),
)
def get(self, title: str) -> Note:
"""Get a specific note."""
is_valid_filename(title)
filepath = self._path_from_title(title)
content = self._read_file(filepath)
return Note(
title=title,
content=content,
last_modified=os.path.getmtime(filepath),
)
def update(self, title: str, data: NoteUpdate) -> Note:
"""Update a specific note."""
is_valid_filename(title)
filepath = self._path_from_title(title)
if data.new_title is not None:
new_filepath = self._path_from_title(data.new_title)
if filepath != new_filepath and os.path.isfile(new_filepath):
raise FileExistsError(
f"Failed to rename. '{data.new_title}' already exists."
)
os.rename(filepath, new_filepath)
title = data.new_title
filepath = new_filepath
if data.new_content is not None:
self._write_file(filepath, data.new_content, overwrite=True)
content = data.new_content
else:
content = self._read_file(filepath)
return Note(
title=title,
content=content,
last_modified=os.path.getmtime(filepath),
)
def delete(self, title: str) -> None:
"""Delete a specific note."""
is_valid_filename(title)
filepath = self._path_from_title(title)
os.remove(filepath)
def search(
self,
term: str,
sort: Literal["score", "title", "last_modified"] = "score",
order: Literal["asc", "desc"] = "desc",
limit: int = None,
) -> Tuple[SearchResult, ...]:
"""Search the index for the given term."""
self._sync_index_with_retry()
term = self._pre_process_search_term(term)
with self.index.searcher() as searcher:
# Parse Query
if term == "*":
query = Every()
else:
parser = MultifieldParser(
self._fieldnames_for_term(term), self.index.schema
)
parser.add_plugin(DateParserPlugin())
query = parser.parse(term)
# Determine Sort By
# Note: For the 'sort' option, "score" is converted to None as
# that is the default for searches anyway and it's quicker for
# Whoosh if you specify None.
sort = sort if sort in ["title", "last_modified"] else None
# Determine Sort Direction
# Note: Confusingly, when sorting by 'score', reverse = True means
# asc so we have to flip the logic for that case!
reverse = order == "desc"
if sort is None:
reverse = not reverse
# Run Search
results = searcher.search(
query,
sortedby=sort,
reverse=reverse,
limit=limit,
terms=True,
)
return tuple(self._search_result_from_hit(hit) for hit in results)
def get_tags(self) -> list[str]:
"""Return a list of all indexed tags. Note: Tags no longer in use will
only be cleared when the index is next optimized."""
self._sync_index_with_retry()
with self.index.reader() as reader:
tags = reader.field_terms("tags")
return [tag for tag in tags]
@property
def _index_path(self):
return os.path.join(self.storage_path, ".flatnotes")
def _path_from_title(self, title: str) -> str:
return os.path.join(self.storage_path, title + MARKDOWN_EXT)
def _get_by_filename(self, filename: str) -> Note:
"""Get a note by its filename."""
return self.get(self._strip_ext(filename))
def _load_index(self) -> Index:
"""Load the note index or create new if not exists."""
index_dir_exists = os.path.exists(self._index_path)
if index_dir_exists and whoosh.index.exists_in(
self._index_path, indexname=INDEX_SCHEMA_VERSION
):
logger.info("Loading existing index")
return whoosh.index.open_dir(
self._index_path, indexname=INDEX_SCHEMA_VERSION
)
else:
if index_dir_exists:
logger.info("Deleting outdated index")
self._clear_dir(self._index_path)
else:
os.mkdir(self._index_path)
logger.info("Creating new index")
return whoosh.index.create_in(
self._index_path, IndexSchema, indexname=INDEX_SCHEMA_VERSION
)
@classmethod
def _extract_tags(cls, content) -> Tuple[str, Set[str]]:
"""Strip tags from the given content and return a tuple consisting of:
- The content without the tags.
- A set of tags converted to lowercase."""
content_ex_codeblock = re.sub(cls.CODEBLOCK_RE, "", content)
_, tags = cls._re_extract(cls.TAGS_RE, content_ex_codeblock)
content_ex_tags, _ = cls._re_extract(cls.TAGS_RE, content)
try:
tags = [tag.lower() for tag in tags]
return (content_ex_tags, set(tags))
except IndexError:
return (content, set())
def _add_note_to_index(
self, writer: writing.IndexWriter, note: Note
) -> None:
"""Add a Note object to the index using the given writer. If the
filename already exists in the index an update will be performed
instead."""
content_ex_tags, tag_set = self._extract_tags(note.content)
tag_string = " ".join(tag_set)
writer.update_document(
filename=note.title + MARKDOWN_EXT,
last_modified=datetime.fromtimestamp(note.last_modified),
title=note.title,
content=content_ex_tags,
tags=tag_string,
)
def _list_all_note_filenames(self) -> List[str]:
"""Return a list of all note filenames."""
return [
os.path.split(filepath)[1]
for filepath in glob.glob(
os.path.join(self.storage_path, "*" + MARKDOWN_EXT)
)
]
def _sync_index(self, optimize: bool = False, clean: bool = False) -> None:
"""Synchronize the index with the notes directory.
Specify clean=True to completely rebuild the index"""
indexed = set()
writer = self.index.writer()
if clean:
writer.mergetype = writing.CLEAR # Clear the index
with self.index.searcher() as searcher:
for idx_note in searcher.all_stored_fields():
idx_filename = idx_note["filename"]
idx_filepath = os.path.join(self.storage_path, idx_filename)
# Delete missing
if not os.path.exists(idx_filepath):
writer.delete_by_term("filename", idx_filename)
logger.info(f"'{idx_filename}' removed from index")
# Update modified
elif (
datetime.fromtimestamp(os.path.getmtime(idx_filepath))
!= idx_note["last_modified"]
):
logger.info(f"'{idx_filename}' updated")
self._add_note_to_index(
writer, self._get_by_filename(idx_filename)
)
indexed.add(idx_filename)
# Ignore already indexed
else:
indexed.add(idx_filename)
# Add new
for filename in self._list_all_note_filenames():
if filename not in indexed:
self._add_note_to_index(
writer, self._get_by_filename(filename)
)
logger.info(f"'{filename}' added to index")
writer.commit(optimize=optimize)
logger.info("Index synchronized")
def _sync_index_with_retry(
self,
optimize: bool = False,
clean: bool = False,
max_retries: int = 8,
retry_delay: float = 0.25,
) -> None:
for _ in range(max_retries):
try:
self._sync_index(optimize=optimize, clean=clean)
return
except LockError:
logger.warning(f"Index locked, retrying in {retry_delay}s")
time.sleep(retry_delay)
logger.error(f"Failed to sync index after {max_retries} retries")
@classmethod
def _pre_process_search_term(cls, term):
term = term.strip()
# Replace "#tagname" with "tags:tagname"
term = re.sub(
cls.TAGS_WITH_HASH_RE,
lambda tag: "tags:" + tag.group(0)[1:],
term,
)
return term
@staticmethod
def _re_extract(pattern, string) -> Tuple[str, List[str]]:
"""Similar to re.sub but returns a tuple of:
- `string` with matches removed
- list of matches"""
matches = []
text = re.sub(pattern, lambda tag: matches.append(tag.group()), string)
return (text, matches)
@staticmethod
def _strip_ext(filename):
"""Return the given filename without the extension."""
return os.path.splitext(filename)[0]
@staticmethod
def _clear_dir(path):
"""Delete all contents of the given directory."""
for item in os.listdir(path):
item_path = os.path.join(path, item)
if os.path.isfile(item_path):
os.remove(item_path)
elif os.path.isdir(item_path):
shutil.rmtree(item_path)
def _search_result_from_hit(self, hit: Hit):
matched_fields = self._get_matched_fields(hit.matched_terms())
title = self._strip_ext(hit["filename"])
last_modified = hit["last_modified"].timestamp()
# If the search was ordered using a text field then hit.score is the
# value of that field. This isn't useful so only set self._score if it
# is a float.
score = hit.score if type(hit.score) is float else None
if "title" in matched_fields:
hit.results.fragmenter = WholeFragmenter()
title_highlights = hit.highlights("title", text=title)
else:
title_highlights = None
if "content" in matched_fields:
hit.results.fragmenter = ContextFragmenter()
content = self._read_file(self._path_from_title(title))
content_ex_tags, _ = FileSystemNotes._extract_tags(content)
content_highlights = hit.highlights(
"content",
text=content_ex_tags,
)
else:
content_highlights = None
tag_matches = (
[field[1] for field in hit.matched_terms() if field[0] == "tags"]
if "tags" in matched_fields
else None
)
return SearchResult(
title=title,
last_modified=last_modified,
score=score,
title_highlights=title_highlights,
content_highlights=content_highlights,
tag_matches=tag_matches,
)
def _fieldnames_for_term(self, term: str) -> List[str]:
"""Return a list of field names to search based on the given term. If
the term includes a phrase then only search title and content. If the
term does not include a phrase then also search tags."""
fields = ["title", "content"]
if '"' not in term:
# If the term does not include a phrase then also search tags
fields.append("tags")
return fields
@staticmethod
def _get_matched_fields(matched_terms):
"""Return a set of matched fields from a set of ('field', 'term') "
"tuples generated by whoosh.searching.Hit.matched_terms()."""
return set([matched_term[0] for matched_term in matched_terms])
@staticmethod
def _read_file(filepath: str):
logger.debug(f"Reading from '{filepath}'")
with open(filepath, "r") as f:
content = f.read()
return content
@staticmethod
def _write_file(filepath: str, content: str, overwrite: bool = False):
logger.debug(f"Writing to '{filepath}'")
with open(filepath, "w" if overwrite else "x") as f:
f.write(content)