-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathttt.py
78 lines (58 loc) · 1.77 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
75
76
77
78
"""
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 io import StringIO
from typing import (
NamedTuple,
Iterator,
Sequence,
Optional,
)
from itertools import chain
from functools import partial
from more_itertools import unique_everseen
from dataclasses import dataclass
from my.core import get_files, Stats, Paths, make_logger
from my.utils.time import parse_datetime_sec
from my.utils.parse_csv import parse_csv_file
logger = make_logger(__name__)
@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() -> Results:
func = partial(parse_csv_file, parse_function=_parse_text, logger=logger)
yield from unique_everseen(
chain(*map(func, inputs())),
key=lambda e: (
e.dt,
e.command,
),
)
def _parse_text(data: str) -> Results:
csv_reader = csv.reader(
StringIO(data), delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
for row in csv_reader:
yield Entry(
dt=parse_datetime_sec(row[0]),
command=row[2],
directory=None if row[1] == "-" else row[1],
)
def stats() -> Stats:
from my.core import stat
return {**stat(history)}