-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_notebooks.py
More file actions
1189 lines (1017 loc) · 44.3 KB
/
process_notebooks.py
File metadata and controls
1189 lines (1017 loc) · 44.3 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Process a set of notebooks to include internal links and contents pages.
Usage:
process_notebooks.py [options] [<folder>]
process_notebooks.py --help
Options:
--verbose -v Verbose mode: list any file changes.
"""
from docopt import docopt
import pathlib
import re
import calendar
# Settings
PAGE_SUFFIX = '.md'
HOME_DESCRIPTOR = 'Home'
HOMEPAGE_FILENAME = 'Home'
CONTENTS_DESCRIPTOR = 'Contents'
CONTENTS_FILENAME = 'Contents'
README_DESCRIPTOR = 'Readme'
README_FILENAME = 'Readme'
LOGBOOK_FOLDER_NAME = 'Logbook'
UNKNOWN_DESCRIPTOR = 'Unknown'
ATTACHMENTS_FOLDER_NAME = 'Attachments'
FOLDERS_DESCRIPTOR = 'Folders'
PAGES_DESCRIPTOR = 'Pages'
# Constants
BLANK_LINE = ''
class TreeItem():
"""Base class for all objects that can be held in a tree."""
_descriptor = 'base class'
_suffix = ''
def __init__(self, path=None, filename=None, title=None, parent=None):
self.parent = None
self.filename = None
self.title = None
self.link = None
self.path = None
self.contents = []
if parent is not None:
if not self._is_valid_parent(parent):
raise ValueError(f'Received invalid parent: {parent}')
self.parent = parent
self.parent.contents.append(self)
if filename is not None:
if not self._is_valid_filename(filename):
raise ValueError(f'Received invalid filename: {filename}')
self.filename = filename
else:
self.filename = UNKNOWN_DESCRIPTOR
if title is not None:
if not self._is_valid_title(title):
raise ValueError(f'Received invalid title: {title}')
self.title = title
else:
self.title = self._get_title_from_filename() or UNKNOWN_DESCRIPTOR
if path is not None:
if not self._is_valid_path(path):
raise ValueError(f'Not a valid {self._descriptor} path: {path}')
self.path = path
new_filename = self._get_filename_from_path() or self.filename
if filename is not None and new_filename != filename:
raise ValueError(f'Conflicting filename and path parameters '
f'when initialising {self._descriptor}')
self.filename = new_filename or UNKNOWN_DESCRIPTOR
self.title = (self._get_title_from_filename()
or self.title
or UNKNOWN_DESCRIPTOR)
self.load_contents(path)
new_title = self._get_title_from_contents() or self.title
if title is not None and new_title != title:
raise ValueError(f'Conflicting title and contents '
f'when initialising {self._descriptor}')
self.title = new_title or self.title or UNKNOWN_DESCRIPTOR
self.link = self._get_link_from_filename() or UNKNOWN_DESCRIPTOR
def load_contents(self, path=None):
"""Load the contents of the object from a file on disk."""
if path is None:
path = self.path
if path is None:
self.contents = []
else:
if self._is_valid_path(path):
self.contents = []
self._load_contents_from_path(path)
parsed_title = self._get_title_from_contents()
if self._is_valid_title(parsed_title):
self.title = parsed_title
else:
raise ValueError(f'Not a valid {self._descriptor} path: {path}')
def get_root(self):
"""Find the top-level item in the tree."""
item = self
while item.parent is not None:
item = item.parent
return item
def get_parents(self):
"""Return a list of all parents from self back to root."""
parents = []
item = self
while item.parent is not None:
parents.append(item.parent)
item = item.parent
return parents
def get_common_parent(self, other):
"""Return the first item that this item has in common with another."""
other_path = other.get_parents()
if not isinstance(other, Page):
other_path = [other] + other_path
item = self
while item not in other_path:
item = item.parent
return item
def get_relative_path(self, other):
"""Return the path of an item relative to this item."""
common_parent = self.get_common_parent(other)
reverse_path = ''
if isinstance(self, Page):
item = self.parent
else:
item = self
while item is not None:
if item == common_parent:
break
item = item.parent
reverse_path = '../' + reverse_path
forward_path = other.link
item = other
while item is not None:
if item == common_parent:
break
if isinstance(item, Notebook):
forward_path = item.filename + '/' + forward_path
item = item.parent
return reverse_path + forward_path
def get_relative_link(self, other):
"""Return a Markdown link to the given item, relative to this item."""
if other == self.get_root():
title = HOME_DESCRIPTOR
else:
title = other.title
return f'[{title}]({self.get_relative_path(other)})'
def get_navigation(self):
"""Return a line containing breadcrumb links to current item."""
if self.get_root() == self:
return None
breadcrumbs = [self.get_relative_link(parent)
for parent in list(reversed(self.get_parents()))]
breadcrumbs.append(self.title)
return ' > '.join(breadcrumbs)
def get_summary(self):
raise NotImplementedError
def get_outline(self):
raise NotImplementedError
def _is_valid_parent(self, parent):
return isinstance(parent, TreeItem)
def _is_valid_filename(self, filename):
return isinstance(filename, str)
def _is_valid_title(self, title):
return isinstance(title, str)
def _is_valid_link(self, link):
return isinstance(link, str)
def _is_valid_path(self, path):
return path.is_file() or path.is_dir()
def _load_contents_from_path(self, path):
raise NotImplementedError
def _get_link_from_filename(self):
if self.filename is not None:
new_link = self.filename
if self._is_valid_link(new_link):
return new_link
def _get_title_from_filename(self):
if self.filename is not None:
new_title = self.filename.replace('_', ' ').replace('-', ' ').strip()
if self._is_valid_title(new_title):
return new_title
def _get_filename_from_path(self):
if self.path is not None:
new_filename = self.path.stem
if self._is_valid_filename(new_filename):
return new_filename
def _get_title_from_contents(self):
raise NotImplementedError
def _get_path_from_filename(self):
if (self.filename is not None
and self._is_valid_filename(self.filename)
and self.filename != UNKNOWN_DESCRIPTOR):
if self.path is not None:
if (self.parent is not None
and self.parent.path is not None
and self.path.parent != self.parent.path):
raise ValueError(f'Path conflict: '
f'parent path is {self.parent.path} '
f'but child path is {self.path}')
return self.path.parent.joinpath(self.filename + self._suffix)
elif self.parent is not None:
parent_path = (self.parent.path
or self.parent._get_path_from_filename())
if parent_path is not None:
return parent_path.joinpath(self.filename + self._suffix)
class Page(TreeItem):
"""Standard page in a notebook."""
_descriptor = 'page'
_suffix = PAGE_SUFFIX
def rebuild(self):
"""Rebuild the navigation line for a standard page."""
if len(self.contents) > 0:
old_contents = self.contents
while (len(old_contents) > 0
and (self._is_navigation_line(old_contents[0])
or self._is_blank_line(old_contents[0]))):
old_contents = old_contents[1:]
self.contents = []
navigation_line = self.get_navigation()
if navigation_line is not None:
self.contents.append(navigation_line)
self.contents.append(BLANK_LINE)
self.contents += old_contents
def save(self, verbose=False):
"""Write page contents to disk."""
if self.path is None:
self.path = self._get_path_from_filename()
if self.path is None:
raise ValueError('Cannot save page data without setting path.')
if not self.path.is_file() or self.modified():
if verbose:
print(f'Writing {self.path}')
with open(self.path, 'w', encoding='UTF-8') as f:
f.writelines([line + '\n' for line in self.contents])
def get_summary(self):
if self._has_summary():
return self._get_summary(self.contents)
def get_outline(self):
summary = self.get_summary()
if summary is not None:
outline = [summary, BLANK_LINE]
else:
outline = []
sections = self._get_sections(self.contents)
for section in sections:
outline = outline + self._get_bullets(section)
if outline == []:
return None
while outline[-1] == '':
outline = outline[:-1]
return outline
def modified(self):
"""Check whether the page has been modified since loading from file."""
if self.path is None:
raise ValueError(f'Cannot compare with file as path not known.')
if not self._is_valid_path(self.path):
raise ValueError(f'Invalid path: {self.path}')
return not self._contents_match(self.path)
def _is_valid_parent(self, parent):
if type(self) in [Page, ContentsPage]:
return (isinstance(parent, Notebook)
and not isinstance(parent, Logbook))
else:
return isinstance(parent, Notebook)
def _is_valid_path(self, file_path):
return _is_valid_page_file(file_path)
def _load_contents_from_path(self, file_path):
"""Load the content of the page from file."""
self.contents = _load_file(file_path)
def _get_title_from_contents(self):
return self._get_title(self.contents)
def _get_title(self, contents):
if contents is not None:
if sum([self._is_title_line(line) for line in contents]) == 1:
for line in contents:
if self._is_navigation_line(line):
continue
elif self._is_blank_line(line):
continue
elif self._is_title_line(line):
return self._strip_links(line[2:].strip(), 'all')
else:
return None
def _get_summary(self, contents):
start_line = self._find_first_text_line(contents)
if start_line is None:
return None
subsection = self._find_first_subtitle(contents)
if subsection is None or start_line < subsection:
lines = (self._find_first_blank_line(contents[start_line:])
or len(contents[start_line:]))
summary = ' '.join(contents[start_line:start_line+lines]).strip()
summary = self._strip_links(summary, 'reference')
if summary.find(r': * ') > 0:
summary = summary[:summary.find(r': * ')] + '.'
if summary[-1] == ':':
summary = summary[:-1] + '.'
return summary
def _get_sections(self, contents):
if self._get_title(contents) is not None:
section_heading = '## '
else:
section_heading = '# '
section_ids = [idx for idx, line in enumerate(contents)
if line.startswith(section_heading)]
sections = [contents[i:j]
for i, j in zip(section_ids, section_ids[1:]+[None])]
if section_heading == '## ':
return [[line.replace('## ', '# ') for line in section]
for section in sections]
else:
return sections
def _get_bullets(self, section, bullet='*'):
if bullet == '*':
next_bullet = ' -'
elif bullet == ' -':
next_bullet = ' +'
elif bullet == ' +':
next_bullet = None
else:
raise ValueError(f'Invalid bullet type: {bullet}')
title = self._get_title(section)
summary = self._get_summary(section)
if title is not None:
text = f'{bullet} {title}'
if summary is not None:
summary = summary.replace('\n', '')
text = f'{text}: {summary}'
bullets = [text]
if next_bullet is not None:
for subsection in self._get_sections(section):
bullets = bullets + self._get_bullets(subsection, next_bullet)
return bullets
def _has_summary(self):
return self._get_summary(self.contents) is not None
def _contents_match(self, file_path):
"""Compare the current content of the page with file contents."""
return self.contents == _load_file(file_path)
def _is_blank_line(self, line):
return line.strip() == ''
def _is_navigation_line(self, line):
link = r'\[[^]]*\]\([^\)]*\)'
text = r'[A-Za-z0-9-.:` ]*'
separator = r'>'
pattern = f'^{link}(( {separator} {link})* {separator} ({link}|{text}))?$'
if re.search(pattern, line) is not None:
return True
return False
def _is_title_line(self, line):
return line.startswith('# ')
def _is_subtitle_line(self, line, starting_level=1):
if starting_level == 0:
return line.startswith('#')
else:
return line.startswith('##')
def _is_bullet_line(self, line):
return line.startswith('* ')
def _is_link_line(self, line):
reference = r'\[[^]]*\]\: [^\s]*'
descriptive = r'(\w+( \w+)?)\: \[[^]]*\](\([^\)]*\)|\[[^\]]*\])'
if re.search(f'^({reference}|{descriptive})$', line) is not None:
return True
return False
def _is_image_line(self, line):
if re.search(r'^\!\[([^\]]*)\]\([^\)]*\)$', line) is not None:
return True
return False
def _is_text_line(self, line):
if not isinstance(line, str):
raise ValueError(f'Not a valid content line: {line}')
elif self._is_blank_line(line):
return False
elif self._is_title_line(line):
return False
elif self._is_subtitle_line(line):
return False
elif self._is_link_line(line):
return False
elif self._is_navigation_line(line):
return False
elif self._is_bullet_line(line):
return False
elif self._is_image_line(line):
return False
else:
return True
def _find_first_blank_line(self, content):
return next((idx for idx, line in enumerate(content)
if self._is_blank_line(line)), None)
def _find_first_text_line(self, content):
return next((idx for idx, line in enumerate(content)
if self._is_text_line(line)), None)
def _find_first_title_line(self, content):
return next((idx for idx, line in enumerate(content)
if self._is_title_line(line)), None)
def _find_first_subtitle(self, content):
if self._get_title(content) is not None:
starting_level = 1
else:
starting_level = 0
return next((idx for idx, line in enumerate(content)
if self._is_subtitle_line(line, starting_level)), None)
def _strip_links(self, line, types='reference'):
if types not in ['default', 'reference', 'absolute', 'all']:
raise ValueError(f'Invalid link type for stripping: {types}')
if types in ['default', 'reference', 'all']:
line = self._strip_reference_links(line)
if types in ['absolute', 'all']:
line = self._strip_absolute_links(line)
return line
def _strip_reference_links(self, line):
if self._is_link_line(line):
return ''
return re.sub(r'\[([^\]]*)\]\[[^\]]*\]', r'\1', line)
def _strip_absolute_links(self, line):
return re.sub(r'\!?\[([^\]]*)\]\([^\)]*\)', r'\1', line)
class HomePage(Page):
"""A special page showing the overall contents at the root level."""
_descriptor = 'home page'
def __init__(self, *args, **kwargs):
if ('filename' in kwargs and kwargs['filename'] is not None
and kwargs['filename'] != HOMEPAGE_FILENAME):
raise ValueError(f'Invalid filename for {self._descriptor}: '
f"{kwargs['filename']}")
if ('title' in kwargs and kwargs['title'] is not None
and kwargs['title'] != HOME_DESCRIPTOR):
raise ValueError(f'Invalid title for {self._descriptor}: '
f"{kwargs['title']}")
kwargs['filename'] = HOMEPAGE_FILENAME
kwargs['title'] = HOME_DESCRIPTOR
super().__init__(*args, **kwargs)
def rebuild(self):
"""Home pages are manually edited, so cannot be rebuilt."""
raise TypeError('Cannot rebuild a home page.')
def get_navigation(self):
"""Don't return any navigation as already at home page."""
return None
def _is_valid_parent(self, parent):
"""Home pages must be contained at the root level."""
return isinstance(parent, Notebook) and parent.get_root() == parent
def _is_valid_path(self, page_file):
return _is_valid_home_page_file(page_file)
class ContentsPage(Page):
"""A special automatically-generated page showing the notebook contents."""
_descriptor = 'contents page'
def __init__(self, *args, **kwargs):
if ('filename' in kwargs and kwargs['filename'] is not None
and kwargs['filename'] != CONTENTS_FILENAME):
raise ValueError(f'Invalid filename for {self._descriptor}: '
f"{kwargs['filename']}")
if ('title' in kwargs and kwargs['title'] is not None
and kwargs['title'] != CONTENTS_DESCRIPTOR):
raise ValueError(f'Invalid title for {self._descriptor}: '
f"{kwargs['title']}")
kwargs['filename'] = CONTENTS_FILENAME
kwargs['title'] = CONTENTS_DESCRIPTOR
super().__init__(*args, **kwargs)
def rebuild(self):
"""Rebuild contents by summarising relevant pages."""
self.contents = []
if self.parent is not None:
nav = self.get_navigation()
if nav is not None:
self.contents.append(nav)
self.contents.append(BLANK_LINE)
title = self.parent.title
if title is not None and title != UNKNOWN_DESCRIPTOR:
self.contents.append(_title(title, title_level=1))
self.contents.append(BLANK_LINE)
summary = self.parent.get_summary()
if summary is not None:
self.contents.append(summary)
self.contents.append(BLANK_LINE)
self.contents.append(BLANK_LINE)
folders = sorted(self.parent.get_notebooks(),
key=lambda item: item.title + item.link)
folders = folders + self.parent.get_logbooks()
if len(folders) > 0:
self.contents.append(_title(FOLDERS_DESCRIPTOR, title_level=2))
self.contents.append(BLANK_LINE)
for folder in folders:
self.contents.append(_title(self.get_relative_link(folder),
title_level=3))
self.contents.append(BLANK_LINE)
summary = folder.get_summary()
if summary is not None:
self.contents.append(summary)
self.contents.append(BLANK_LINE)
pages = sorted(self.parent.get_pages(),
key=lambda item: item.title + item.link)
if len(pages) > 0:
self.contents.append(_title(PAGES_DESCRIPTOR, title_level=2))
self.contents.append(BLANK_LINE)
for page in pages:
self.contents.append(_title(self.get_relative_link(page),
title_level=3))
self.contents.append(BLANK_LINE)
summary = page.get_summary()
if summary is not None:
self.contents.append(summary)
self.contents.append(BLANK_LINE)
while (len(self.contents) > 0
and self._is_blank_line(self.contents[-1])):
self.contents = self.contents[:-1]
return self.contents
def get_navigation(self):
"""Return navigation link for the notebook rather than its contents page."""
if self.parent is not None:
return self.parent.get_navigation()
def _is_valid_path(self, page_file):
return _is_valid_contents_page_file(page_file)
def _get_title_from_contents(self):
"""Return `None` because contents pages have a fixed title."""
return None
class ReadmePage(Page):
"""A special descriptive page showing the notebook contents."""
_descriptor = 'readme page'
def __init__(self, *args, **kwargs):
if ('filename' in kwargs and kwargs['filename'] is not None
and kwargs['filename'] != README_FILENAME):
raise ValueError(f'Invalid filename for {self._descriptor}: '
f"{kwargs['filename']}")
kwargs['filename'] = README_FILENAME
super().__init__(*args, **kwargs)
def rebuild(self):
"""Readme pages are static, so cannot be rebuilt."""
raise TypeError('Cannot rebuild a readme page.')
def get_navigation(self):
"""Don't return any navigation as readme pages should remain clean."""
return None
def _is_valid_path(self, page_file):
return _is_valid_readme_page_file(page_file)
class LogbookPage(Page):
"""Logbook page in a notebook, with date attributes."""
_descriptor = 'logbook page'
def __lt__(self, other):
return (self.filename < other.filename)
def __gt__(self, other):
return(self.filename > other.filename)
def rebuild(self):
"""Rebuild the navigation lines for a logbook page."""
if len(self.contents) > 0:
old_contents = self.contents
while (len(old_contents) > 0
and (self._is_navigation_line(old_contents[0])
or self._is_blank_line(old_contents[0]))):
old_contents = old_contents[1:]
self.contents = []
parent_navigation_line = self.get_parent_navigation()
if parent_navigation_line is not None:
self.contents.append(parent_navigation_line)
self.contents.append(BLANK_LINE)
navigation_line = self.get_navigation()
if navigation_line is not None:
self.contents.append(navigation_line)
self.contents.append(BLANK_LINE)
self.contents += old_contents
def get_month(self):
if len(self.filename) >= 7:
if (self.filename[:4].isnumeric()
and self.filename[4:5] == '-'
and self.filename[5:7].isnumeric()):
return self.filename[:7]
def get_navigation(self):
"""Return links to surrounding pages."""
left = self.get_previous()
right = self.get_next()
links = []
if left is not None:
links.append(f'[< {left.title}]({self.get_relative_path(left)})')
if self.title is not None and self.title != UNKNOWN_DESCRIPTOR:
links.append(self.title)
if right is not None:
links.append(f'[{right.title} >]({self.get_relative_path(right)})')
if len(links) > 0:
return ' | '.join(links)
def get_parent_navigation(self):
if self.parent is not None:
navigation = self.parent.get_navigation()
if navigation is not None:
navigation = navigation.replace(
self.parent.title, self.get_relative_link(self.parent))
up = self.get_up()
if up is not None and up != self.parent:
navigation = ' > '.join([navigation,
self.get_relative_link(up)])
return navigation
def get_up(self):
if self.parent is not None:
return next((item for item in self.parent.get_pages('months')
if item.get_month() == self.get_month()), None)
def get_previous(self):
if self.parent is not None:
past = [item for item in self._get_siblings()
if item < self]
if len(past) > 0:
past.sort()
return past[-1]
def get_next(self):
if self.parent is not None:
future = [item for item in self._get_siblings()
if item > self]
if len(future) > 0:
future.sort()
return future[0]
def _is_valid_parent(self, parent):
return isinstance(parent, Logbook)
def _is_valid_path(self, page_file):
return _is_valid_logbook_page_file(page_file)
def _is_valid_filename(self, filename):
return _is_valid_logbook_filename(filename)
def _get_title_from_filename(self):
if self.filename is not None:
new_title = self.filename.strip()
if self._is_valid_title(new_title):
return new_title
def _get_title_from_contents(self):
"""Logbook page titles are set from the date, not the contents."""
if type(self) == LogbookPage:
return None
return super()._get_title_from_contents()
def _get_sections(self, contents):
"""Logbook pages without summaries need to split from the first title."""
if type(self) == LogbookPage and contents == self.contents:
if not self._has_summary():
contents = ['Dummy summary', ''] + self.contents
return super()._get_sections(contents)
def _get_siblings(self):
return self.parent.get_pages('days')
def _has_summary(self):
"""Logbook pages cannot have summaries after a title line."""
if type(self) == LogbookPage:
title_line = self._find_first_title_line(self.contents)
start_line = self._find_first_text_line(self.contents)
if (title_line is not None
and start_line is not None
and title_line < start_line):
return False
return super()._has_summary()
def _get_date_pattern(self):
return r'[0-9]{4}-[0-9]{2}-[0-9]{2}'
def _is_navigation_line(self, line):
if super()._is_navigation_line(line):
return True
link = r'\[[^]]*\]\([^\)]*\)'
text = self._get_date_pattern()
separator = r'\|'
pattern = f'^({link}|{text})( {separator} ({link}|{text}))?( {separator} ({link}|{text}))?$'
if re.search(pattern, line) is not None:
return True
return False
class LogbookMonth(LogbookPage):
"""Special page in a notebook that summarises the month's entries."""
_descriptor = 'logbook month page'
def rebuild(self):
"""Rebuild contents by summarising relevant pages."""
self.contents = []
pages = self.get_pages()
if len(pages) == 0:
return None
parent_navigation_line = self.get_parent_navigation()
if parent_navigation_line is not None:
self.contents.append(parent_navigation_line)
self.contents.append(BLANK_LINE)
self.contents.append(self.get_navigation())
self.contents.append(BLANK_LINE)
self.contents.append(_title(self.title))
self.contents.append(BLANK_LINE)
for page in pages:
self.contents.append(_title(self.get_relative_link(page),
title_level=2))
self.contents.append(BLANK_LINE)
self.contents = self.contents + page.get_outline()
self.contents.append(BLANK_LINE)
self.contents.append(BLANK_LINE)
while self.contents[-1] == BLANK_LINE:
self.contents = self.contents[:-1]
return self.contents
def get_up(self):
if self.parent is not None:
return self.parent
def get_pages(self):
if self.parent is not None:
page_list = [item for item in self.parent.get_pages('days')
if item.get_month() == self.get_month()]
page_list.sort()
return page_list
return []
def _is_valid_path(self, page_file):
return _is_valid_logbook_month_file(page_file)
def _is_valid_filename(self, filename):
return _is_valid_logbook_month_filename(filename)
def _get_siblings(self):
return self.parent.get_pages('months')
def _get_title_from_filename(self):
if (self.filename is not None
and len(self.filename) >= 7
and self.filename[:4].isnumeric()
and self.filename[4:5] == '-'
and self.filename[5:7].isnumeric()):
year = int(self.filename[:4])
month = int(self.filename[5:7])
new_title = f'{calendar.month_name[month]} {year}'
if self._is_valid_title(new_title):
return new_title
def _get_date_pattern(self):
month = '(January|February|March|April|May|June|July|August|September|October|November|December)'
year = r'[0-9]{4}'
return f'{month} {year}'
class LogbookContents(ContentsPage):
"""Logbook contents are built by date rather than file names."""
_descriptor = 'logbook contents page'
def rebuild(self):
"""Rebuild contents by summarising relevant pages."""
self.contents = []
if self.parent is not None:
nav = self.get_navigation()
if nav is not None:
self.contents.append(nav)
self.contents.append(BLANK_LINE)
months = sorted(self.parent.get_pages(types='months'),
key=lambda item: item.filename)
for month in months:
self.contents.append(_title(self.get_relative_link(month)))
self.contents.append(BLANK_LINE)
month.rebuild()
this_content = month.contents
while (len(this_content) > 0
and (month._is_navigation_line(this_content[0])
or month._is_title_line(this_content[0])
or month._is_blank_line(this_content[0]))):
this_content = this_content[1:]
self.contents += this_content
self.contents.append(BLANK_LINE)
self.contents.append(BLANK_LINE)
while (len(self.contents) > 0
and self._is_blank_line(self.contents[-1])):
self.contents = self.contents[:-1]
return self.contents
def _is_valid_parent(self, parent):
return isinstance(parent, Logbook)
class Notebook(TreeItem):
"""Standard notebook object containing pages."""
_descriptor = 'notebook'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.get_root() == self:
self.link = HOMEPAGE_FILENAME
else:
self.link = CONTENTS_FILENAME
def rebuild(self):
"""Rebuild pages and nested notebooks within the notebook."""
for notebook in self.get_notebooks():
notebook.rebuild()
for logbook in self.get_logbooks():
logbook.rebuild()
for page in self.get_pages():
page.rebuild()
if self.get_root() != self and len(self.contents) > 0:
contents = self.get_contents_page()
if contents is None:
contents = self.add_contents_page()
contents.rebuild()
def save(self, verbose=False):
"""Write notebook contents to disk, including pages and subfolders."""
if verbose:
print(f'Checking {self.title}')
if self.path is None:
self.path = self._get_path_from_filename()
if self.path is None:
raise ValueError('Cannot save page data without setting path.')
for item in self.contents:
item.save(verbose)
def add_page(self, page_path=None):
"""Add a page to a notebook."""
return Page(page_path, parent=self)
def add_home_page(self, page_path=None):
"""Add a home page to a notebook."""
if self.get_root() != self:
raise ValueError('Can only add home page at the root level.')
if self.get_home_page() is not None:
raise ValueError('Cannot add more than one home page.')
return HomePage(path=page_path, parent=self)
def add_contents_page(self, page_path=None):
"""Add a contents page to a notebook."""
if self.get_contents_page() is not None:
raise ValueError('Cannot add more than one contents page.')
return ContentsPage(path=page_path, parent=self)
def add_readme_page(self, page_path=None):
"""Add a readme page to a notebook."""
if self.get_readme_page() is not None:
raise ValueError('Cannot add more than one readme page.')
return ReadmePage(path=page_path, parent=self)
def add_notebook(self, notebook_path=None):
"""Add a nested notebook inside a notebook."""
return Notebook(path=notebook_path, parent=self)
def add_logbook(self, logbook_path=None):
"""Add a nested logbook inside a notebook."""
return Logbook(path=logbook_path, parent=self)
def get_pages(self):
"""Return a list of contents that are (standard) pages."""
return [item for item in self.contents if type(item) == Page]
def get_home_page(self):
"""Returns the home page if it exists, assuming there is only one."""
if self.get_root() == self:
return next((item for item in self.contents
if isinstance(item, HomePage)), None)
def get_contents_page(self):
"""Returns the contents page if it exists, assuming there is only one."""
return next((item for item in self.contents
if isinstance(item, ContentsPage)), None)
def get_readme_page(self):
"""Returns the readme page if it exists, assuming there is only one."""
return next((item for item in self.contents
if isinstance(item, ReadmePage)), None)
def get_notebooks(self):
"""Return a list of contents that are notebooks."""
return [item for item in self.contents if type(item) == Notebook]
def get_logbooks(self):
"""Return a list of contents that are logbooks."""
return [item for item in self.contents if type(item) == Logbook]
def get_summary(self):
if self._has_readme_page():
return self.get_readme_page().get_summary()
def _is_valid_path(self, file_path):
return _is_valid_notebook_folder(file_path)
def _is_valid_page_file(self, page_path):
return _is_valid_page_file(page_path)
def _is_valid_home_page_file(self, page_path):
return _is_valid_home_page_file(page_path)
def _is_valid_contents_page_file(self, page_path):
return _is_valid_contents_page_file(page_path)
def _is_valid_readme_page_file(self, page_path):
return _is_valid_readme_page_file(page_path)
def _is_valid_notebook_folder(self, file_path):
return _is_valid_notebook_folder(file_path)
def _is_valid_logbook_folder(self, file_path):
return _is_valid_logbook_folder(file_path)
def _is_valid_parent(self, parent):
return isinstance(parent, Notebook) and not isinstance(parent, Logbook)
def _load_contents_from_path(self, folder_path):
for item in folder_path.iterdir():
if item.is_file():
if self._is_valid_home_page_file(item):
self.add_home_page(item)
elif self._is_valid_contents_page_file(item):
self.add_contents_page(item)
elif self._is_valid_readme_page_file(item):
self.add_readme_page(item)
elif self._is_valid_page_file(item):
self.add_page(item)
else:
continue
elif item.is_dir():
if self._is_valid_logbook_folder(item):
self.add_logbook(item)
elif self._is_valid_notebook_folder(item):
self.add_notebook(item)
else:
continue
def _get_title_from_contents(self):
if self._has_readme_page():
readme_title = self.get_readme_page().title
if self._is_valid_title(readme_title):
if readme_title not in [UNKNOWN_DESCRIPTOR, README_FILENAME]:
return readme_title
def _has_contents_page(self):