Coverage for src/zettel/notebook.py: 100%

1 from pathlib import Path

2 from .notes import Note

3

4 class Notebook:

5 def __init__(self, dir, notes=None):

6 if not Path(dir).is_dir():

7 raise NotADirectoryError(f'{dir} is not a directory.')

8 self.dir = Path(dir)

9 if notes:

10 self.notes = self.read_notes([Path(self.dir, note) for note in notes])

11 else:

12 self.notes = self.read_notes(self.dir.glob('*.md'))

13

14 def __repr__(self):

15 return(f'<Notebook at {self.dir}>')

16

17 def read_notes(self, files):

18 for note in (Note(f) for f in files):

19 yield note.id, note

20

21 def get_note_by_title(self, title):

22 for note_id, note in self.notes:

23 if title == note.title:

24 result = note

25 break

26 # result = [note for note in self.notes.values() if title == note.title][0]

27 result = result if 'result' in locals() else None

28 return result