Repository navigation
Expand file tree
/
Copy pathpatterns.py
More file actions
35 lines (24 loc) · 1.43 KB
/
Copy pathpatterns.py
File metadata and controls
35 lines (24 loc) · 1.43 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
"""The patterns that can be drawn, read from one text file each in the patterns/ folder.
A file is 7 lines, Sunday to Saturday: a digit is the number of commits that day, a dot is
none. Dots rather than spaces, because editors often strip trailing spaces and the rows
would no longer line up. Lines starting with # are comments.
"""
from pathlib import Path
FOLDER = Path(__file__).parent / "patterns"
def read_pattern(path: Path) -> list[str]:
"""The 7 rows of one pattern file, dots turned into spaces; ValueError if malformed."""
lines = path.read_text(encoding="utf-8").splitlines()
rows = [line.rstrip() for line in lines if line.strip() and not line.startswith("#")]
if len(rows) != 7:
raise ValueError(f"{path.name}: needs 7 rows (Sunday to Saturday), has {len(rows)}")
if len({len(row) for row in rows}) != 1:
raise ValueError(f"{path.name}: rows must all be the same width (blanks are dots)")
if any(char != "." and not char.isdigit() for row in rows for char in row):
raise ValueError(f"{path.name}: use only digits and dots")
return [row.replace(".", " ") for row in rows]
def load_patterns(folder: Path = FOLDER) -> dict[str, list[str]]:
"""Every pattern in the folder, by file name without .txt, in alphabetical order."""
return {path.stem: read_pattern(path) for path in sorted(folder.glob("*.txt"))}
patterns = load_patterns()
def get_pattern(name):
return patterns.get(name)