-
Notifications
You must be signed in to change notification settings - Fork 6
/
ttt.py
74 lines (55 loc) · 1.94 KB
/
ttt.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
"""
Parses history from https://github.com/purarue/ttt
"""
# see https://github.com/purarue/dotfiles/blob/master/.config/my/my/config/__init__.py for an example
from my.config import ttt as user_config # type: ignore[attr-defined]
import csv
from pathlib import Path
from datetime import datetime
from typing import NamedTuple, Iterator, Sequence, Optional
from itertools import chain
from more_itertools import unique_everseen
from dataclasses import dataclass
from my.core import get_files, Stats, Paths
from my.utils.time import parse_datetime_sec
from my.utils.input_source import InputSource
@dataclass
class config(user_config):
# path[s]/glob to the backed up ttt history files
# (can be a list if you want to provide the live file)
export_path: Paths
def inputs() -> Sequence[Path]:
return get_files(config.export_path)
# represents one history entry (command)
class Entry(NamedTuple):
dt: datetime
command: str
directory: Optional[str]
Results = Iterator[Entry]
def history(from_paths: InputSource = inputs) -> Results:
yield from unique_everseen(
chain(*map(_parse_file, from_paths())),
key=lambda e: (
e.dt,
e.command,
),
)
def _parse_file(histfile: Path) -> Results:
# TODO: helper function to read 'regular' QUOTE_MINIMAL csv files
# without failing due to encoding errors?
with histfile.open("r", encoding="utf-8", newline="") as f:
csv_reader = csv.reader(
f, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
try:
for row in csv_reader:
yield Entry(
dt=parse_datetime_sec(row[0]),
command=row[2],
directory=None if row[1] == "-" else row[1],
)
except csv.Error as e:
print(f"While parsing {histfile}... {e}")
def stats() -> Stats:
from my.core import stat
return {**stat(history)}