-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
80 lines (54 loc) · 2.21 KB
/
util.py
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
import re
from collections import defaultdict
from pathlib import Path
from typing import List, Generator, Tuple, Union
def list_duplicates(seq: List) -> Generator[Tuple, None, None]:
"""Helper function to list duplicates.
See https://stackoverflow.com/a/5419576
"""
tally = defaultdict(list)
for idx, item in enumerate(seq):
tally[item].append(idx)
return ((key, locs) for key, locs in tally.items()
if len(locs) > 1)
def get_note_paths(directory: str, extensions: List[str]) -> List[Path]:
"""Returns a list of all paths to notes in the given directory.
Filters out results based on extension."""
patterns = ['*.{}'.format(ext) for ext in extensions]
note_paths = []
for pattern in patterns:
note_paths += Path(directory).rglob(pattern)
remove_hidden_files(note_paths)
return note_paths
def get_id_from_path(path: Path, separator: str = ' ') -> str:
"""Returns the zettel ID for a note path.
This method assumes that IDs and note names are separated by a space.
Optionally you can define a different separator"""
return path.stem.split(separator)[0]
def line_prepender(path, lines):
"""Writes a lines to the start of the file at the specified path."""
with path.open(mode='r+') as f:
content = f.read()
f.seek(0, 0)
lines_flat = "\n".join(lines)
f.write(lines_flat + '\n' + content)
def remove_hidden_files(paths: List[Path]):
"""Takes a list of paths, removes in-place the files that are hidden.
Hidden means either:
1. The file itself starts with a .
2. The file is in a folder that starts with a .
"""
# we need list() to iterate over a copy of the list; we cannot delete
# elements from the list we are iterating over
for path in list(paths):
if any([part.startswith(".") for part in path.parts]):
paths.remove(path)
def has_zettel_id(path: Path, id_pattern) -> Union[str, bool]:
"""Checks if a note already has a zettel ID.
:return zettel ID if present; False otherwise
"""
pattern = re.compile(id_pattern)
match = pattern.match(get_id_from_path(path))
if match is not None:
return match.group()
return False