Blob is no more. Notes now always have content.

This commit is contained in:
Andrew Mulbrook 2026-03-26 00:36:01 -05:00
parent 5df3c81417
commit ffefe4dd21
4 changed files with 28 additions and 48 deletions

View file

@ -4,7 +4,9 @@ import tempfile
import datetime as dt
from pathlib import Path
from typing import Optional, Dict, List, Self, Iterable
from .trove import Note, Trove, TreeNote, BlobNote, Blob, Tree, BadNoteType, TreeEntry, NoteNotFound
from .trove import Note, Trove, TreeNote, BadNoteType, TreeEntry, NoteNotFound
from . import fs_util as fsu
class FSNote(Note):
@ -61,22 +63,17 @@ class FSNote(Note):
def set_raw_metadata(self, key: str, value: bytes) -> None:
self._trove._set_metadata(self._inode, key, value)
class FSBlobNote(FSNote, BlobNote):
def read(self) -> bytes:
if self._inode is None:
return b""
return self._path.read_bytes()
def read_content(self) -> bytes:
"""Read the raw content of the note."""
content_file = fsu.get_content_path(self._path)
if content_file.exists():
return content_file.read_bytes()
return b""
def write(self, data: bytes) -> None:
self._path.write_bytes(data)
# Update cache just in case inode changed (some editors do this)
try:
new_inode = self._path.stat().st_ino
if new_inode != self._inode:
self._trove._update_cache(new_inode, self._path)
self._inode = new_inode
except OSError:
pass
def write_content(self, data:bytes) -> None:
"""Write the raw content of the note."""
content_file = fsu.get_content_path(self._path)
content_file.write_bytes(data)
class FSTreeNote(FSNote, TreeNote):
@property
@ -85,7 +82,7 @@ class FSTreeNote(FSNote, TreeNote):
return "inode/directory"
def link(self, name: str, note: Note):
if not isinstance(note, FSBlobNote):
if not isinstance(note, FSNote):
raise BadNoteType("Only blob notes can be linked")
target_path = self._path / name
@ -239,7 +236,7 @@ class FSTrove(Trove):
if target_path.is_dir():
return FSTreeNote(self, inode=note_id, path=target_path)
else:
return FSBlobNote(self, inode=note_id, path=target_path)
return FSNote(self, inode=note_id, path=target_path)
def get_raw_note(self, note_id: int) -> Note:
p = self.get_path_by_inode(note_id)
@ -248,7 +245,7 @@ class FSTrove(Trove):
return self.get_raw_note_by_path(p)
def create_blob(self, data: bytes | None = None) -> BlobNote:
def create_blob(self, data: bytes | None = None) -> Note:
fd, temp_path = tempfile.mkstemp(dir=self.working)
try:
if data:
@ -258,7 +255,7 @@ class FSTrove(Trove):
p = Path(temp_path)
inode = p.stat().st_ino
self._update_cache(inode, p)
return FSBlobNote(self, inode=inode, path=p)
return FSNote(self, inode=inode, path=p)
def get_root(self) -> TreeNote:
return FSTreeNote(self, inode=self._root_inode, path=self.root)