11import os
22import typer
33from rich import print , box
4- from rich .prompt import Prompt , IntPrompt , Confirm
4+ from rich .prompt import Prompt , IntPrompt
55from rich .table import Table
66from pathlib import Path
77from contextlib import contextmanager
1414FILE_DIR = Path ("./files/" )
1515SUMMARY_DIR = FILE_DIR / "summaries"
1616
17+
1718def dir_check ():
1819 """Make sure the directories for saving files exist"""
1920 FILE_DIR .mkdir (parents = True , exist_ok = True )
2021 SUMMARY_DIR .mkdir (parents = True , exist_ok = True )
2122
23+
2224dir_check ()
2325
2426app = typer .Typer ()
@@ -33,7 +35,8 @@ def get_default_books():
3335 for book in books :
3436 with books_db () as db :
3537 db .add_book (Book .from_dict (books [book ]))
36-
38+
39+
3740@app .command ()
3841def default ():
3942 """
@@ -47,28 +50,30 @@ def default():
4750 get_default_books ()
4851
4952 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" )
5457
5558 with books_db () as db :
5659 books = db .list_books ()
5760 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 } " )
5962 order_num += 1
60- print (' \n ' )
63+ print (" \n " )
6164 print (table )
62- print (' \n ' )
65+ print (" \n " )
6366
6467 max_choice = len (books )
6568 choice = Prompt .ask ("Select a book by number" )
6669 while not choice .isdigit () or int (choice ) < 1 or int (choice ) > max_choice :
6770 choice = Prompt .ask ("[red]Please choose a number between 1 and 32" )
6871
6972 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+ )
7277 filepath = FILE_DIR / Path (selected_book .filename )
7378
7479 if filepath .exists ():
@@ -78,23 +83,24 @@ def default():
7883 write_text_to_file (selected_book .url , filepath )
7984 print (f"\n Text of { selected_book .title } saved to { filepath } ." )
8085
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" ])
8287 chunks = IntPrompt .ask ("How many lines per chunk?" , default = 400 )
8388
8489 # if chunks < 50:
8590 # print("[red bold]Warning[/red bold]: choosing a low value could take a lot of time and resources.")
8691 # confirmation = Confirm.ask("Are you sure?")
87-
88- if choice == 'p' :
92+
93+ if choice == "p" :
8994 print_summary (filepath , chunks )
9095 else :
9196 target_filepath = SUMMARY_DIR / Path (selected_book .filename )
9297 save_summary (filepath , target_filepath , chunks )
93- print (f' \n Summary saved to { target_filepath } .' )
98+ print (f" \n Summary saved to { target_filepath } ." )
9499
95100 with books_db () as db :
96101 db .delete_all ()
97102
103+
98104def get_path ():
99105 db_path_env = os .getenv ("BOOKS_DB_DIR" , "" )
100106 if db_path_env :
@@ -103,6 +109,7 @@ def get_path():
103109 db_path = Path (__file__ ).parent / "books_db"
104110 return db_path
105111
112+
106113@contextmanager
107114def books_db ():
108115 db_path = get_path ()
@@ -111,5 +118,3 @@ def books_db():
111118 yield db
112119 finally :
113120 db .close ()
114-
115-
0 commit comments