1
1
import os
2
2
import typer
3
3
from rich import print , box
4
- from rich .prompt import Prompt , IntPrompt , Confirm
4
+ from rich .prompt import Prompt , IntPrompt
5
5
from rich .table import Table
6
6
from pathlib import Path
7
7
from contextlib import contextmanager
14
14
FILE_DIR = Path ("./files/" )
15
15
SUMMARY_DIR = FILE_DIR / "summaries"
16
16
17
+
17
18
def dir_check ():
18
19
"""Make sure the directories for saving files exist"""
19
20
FILE_DIR .mkdir (parents = True , exist_ok = True )
20
21
SUMMARY_DIR .mkdir (parents = True , exist_ok = True )
21
22
23
+
22
24
dir_check ()
23
25
24
26
app = typer .Typer ()
@@ -33,7 +35,8 @@ def get_default_books():
33
35
for book in books :
34
36
with books_db () as db :
35
37
db .add_book (Book .from_dict (books [book ]))
36
-
38
+
39
+
37
40
@app .command ()
38
41
def default ():
39
42
"""
@@ -47,28 +50,30 @@ def default():
47
50
get_default_books ()
48
51
49
52
table = Table (box = box .SQUARE_DOUBLE_HEAD , border_style = "magenta" )
50
- table .add_column (' No.' )
51
- table .add_column (' [bold cyan]Title' , max_width = 75 , no_wrap = False )
52
- table .add_column (' [bold magenta]Author' )
53
- table .add_column (' [bold yellow]Fulltext URL' )
53
+ table .add_column (" No." )
54
+ table .add_column (" [bold cyan]Title" , max_width = 75 , no_wrap = False )
55
+ table .add_column (" [bold magenta]Author" )
56
+ table .add_column (" [bold yellow]Fulltext URL" )
54
57
55
58
with books_db () as db :
56
59
books = db .list_books ()
57
60
for order_num , book in enumerate (books , start = 1 ):
58
- table .add_row (f' { str (order_num )} .' , book .title , book .author , f"[yellow]{ book .url } " )
61
+ table .add_row (f" { str (order_num )} ." , book .title , book .author , f"[yellow]{ book .url } " )
59
62
order_num += 1
60
- print (' \n ' )
63
+ print (" \n " )
61
64
print (table )
62
- print (' \n ' )
65
+ print (" \n " )
63
66
64
67
max_choice = len (books )
65
68
choice = Prompt .ask ("Select a book by number" )
66
69
while not choice .isdigit () or int (choice ) < 1 or int (choice ) > max_choice :
67
70
choice = Prompt .ask ("[red]Please choose a number between 1 and 32" )
68
71
69
72
selected_book = books [int (choice ) - 1 ]
70
-
71
- print (f"\n You have chosen [bold cyan]{ selected_book .title } [/bold cyan] by [bold magenta]{ selected_book .author } [/bold magenta]." )
73
+
74
+ print (
75
+ f"\n You have chosen [bold cyan]{ selected_book .title } [/bold cyan] by [bold magenta]{ selected_book .author } [/bold magenta]."
76
+ )
72
77
filepath = FILE_DIR / Path (selected_book .filename )
73
78
74
79
if filepath .exists ():
@@ -78,23 +83,24 @@ def default():
78
83
write_text_to_file (selected_book .url , filepath )
79
84
print (f"\n Text of { selected_book .title } saved to { filepath } ." )
80
85
81
- choice = Prompt .ask ("\n Do you want to [P]rint or [S]ave your summary?" , choices = ['p' , 's' ])
86
+ choice = Prompt .ask ("\n Do you want to [P]rint or [S]ave your summary?" , choices = ["p" , "s" ])
82
87
chunks = IntPrompt .ask ("How many lines per chunk?" , default = 400 )
83
88
84
89
# if chunks < 50:
85
90
# print("[red bold]Warning[/red bold]: choosing a low value could take a lot of time and resources.")
86
91
# confirmation = Confirm.ask("Are you sure?")
87
-
88
- if choice == 'p' :
92
+
93
+ if choice == "p" :
89
94
print_summary (filepath , chunks )
90
95
else :
91
96
target_filepath = SUMMARY_DIR / Path (selected_book .filename )
92
97
save_summary (filepath , target_filepath , chunks )
93
- print (f' \n Summary saved to { target_filepath } .' )
98
+ print (f" \n Summary saved to { target_filepath } ." )
94
99
95
100
with books_db () as db :
96
101
db .delete_all ()
97
102
103
+
98
104
def get_path ():
99
105
db_path_env = os .getenv ("BOOKS_DB_DIR" , "" )
100
106
if db_path_env :
@@ -103,6 +109,7 @@ def get_path():
103
109
db_path = Path (__file__ ).parent / "books_db"
104
110
return db_path
105
111
112
+
106
113
@contextmanager
107
114
def books_db ():
108
115
db_path = get_path ()
@@ -111,5 +118,3 @@ def books_db():
111
118
yield db
112
119
finally :
113
120
db .close ()
114
-
115
-
0 commit comments