-
Notifications
You must be signed in to change notification settings - Fork 12
/
games.py
121 lines (100 loc) · 3.62 KB
/
games.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
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
from typing import Callable, Dict, List, Optional, Set, Tuple, Union
import typer
from omegaconf import OmegaConf
from rich import print
from rich.console import Console
from rich.progress import track
from chessli.cli.option_callbacks import since_callback
from chessli.enums import PerfType, SinceEnum
from chessli.games import GamesCollection, GamesFetcher, GamesReader
from chessli.utils import (
as_title,
convert_since_enum_to_millis,
create_config_from_options,
extract_context_info,
)
app = typer.Typer()
console = Console()
@app.callback(invoke_without_command=True)
def main(ctx: typer.Context,):
"""Fetch, store, show and ankify games and mistakes"""
ctx.params = ctx.parent.params
print(as_title("chessli games"), end="\n\n")
@app.command()
def ls(
ctx: typer.Context,
perf_type: Optional[PerfType] = typer.Option(
None, help="Filter games to the selected `perf-type`"
),
):
"""List your games"""
chessli_paths, cli_config = extract_context_info(ctx)
games_reader = GamesReader(chessli_paths, cli_config)
games_reader.ls()
@app.command()
def fetch(
ctx: typer.Context,
verbose: int = typer.Option(
1, "--verbose", "-v", count=True, help="Select the verbosity level"
),
perf_type: Optional[List[PerfType]] = typer.Option(
[], help="Filter fetching of games to the selected `perf_types`"
),
since_enum: SinceEnum = typer.Option(
SinceEnum.last_time,
"--since",
help="Filter fetching of games to those played since `since`",
callback=since_callback,
),
max: Optional[int] = typer.Option(30, help="Limit fetching of games to `max`",),
store: bool = typer.Option(False, help="Select if fetched games should be stored"),
):
"""Fetch games from lichess and optionally store them"""
chessli_paths, cli_config = extract_context_info(ctx)
cli_config["since_millis"] = convert_since_enum_to_millis(
since_enum, chessli_paths.user_games_config
)
cli_config["store_config_path"] = str(chessli_paths.user_games_config_path)
games_fetcher = GamesFetcher(chessli_paths, cli_config)
new_games = games_fetcher.fetch_games()
@app.command()
def ankify(
ctx: typer.Context,
new_games_only: bool = typer.Option(
True, help="Fetch new games and only ankify those"
),
since_enum: SinceEnum = typer.Option(
SinceEnum.last_time,
"--since",
help="Filter fetching of games to those played since `since`",
callback=since_callback,
),
max: Optional[int] = typer.Option(30, help="Limit fetching of games to `max`",),
perf_type: Optional[List[PerfType]] = typer.Option(
None, help="Filter fetching of games to the selected `perf_types`"
),
export_only: bool = typer.Option(
True,
"--export-only/--directly",
help="Select to only export the created anki cards",
),
):
"""Parse your games to find mistakes and create Anki cards"""
chessli_paths, cli_config = extract_context_info(ctx)
cli_config["since_millis"] = convert_since_enum_to_millis(
since_enum, chessli_paths.user_games_config
)
cli_config["store_config_path"] = str(chessli_paths.user_games_config_path)
if new_games_only:
games = GamesFetcher(chessli_paths, cli_config).fetch_games()
else:
games = GamesReader(chessli_paths, cli_config).games
games_collection = GamesCollection(
config=cli_config, paths=chessli_paths, games=games
)
if export_only:
games_collection.export_csv()
else:
games_collection.ankify_games()
if __name__ == "__main__":
app()