Refactored setup.py again, confirmed that all filters are now working as expected. Work will resume on main branch now

This commit is contained in:
mwisnowski 2025-01-13 11:35:11 -08:00
parent c4d773d663
commit 000d804ba7
6 changed files with 584 additions and 262 deletions

143
main.py
View file

@ -2,69 +2,104 @@ from __future__ import annotations
import inquirer.prompt # type: ignore
import sys
import logging
from pathlib import Path
from typing import NoReturn, Optional
import setup
import card_info
import tagger
Path('csv_files').mkdir(parents=True, exist_ok=True)
Path('staples').mkdir(parents=True, exist_ok=True)
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(),
logging.FileHandler('main.log', mode='w')
]
)
while True:
print('What would you like to do?')
choice = 'Menu'
while choice == 'Menu':
question = [
inquirer.List('menu',
choices=['Setup', 'Build a Deck', 'Get Card Info', 'Quit'],
carousel=True)
]
# Menu constants
MENU_SETUP = 'Setup'
MENU_BUILD_DECK = 'Build a Deck'
MENU_CARD_INFO = 'Get Card Info'
MAIN_TAG = 'Tag CSV Files'
MENU_QUIT = 'Quit'
MENU_CHOICES = [MENU_SETUP, MENU_BUILD_DECK, MENU_CARD_INFO, MAIN_TAG, MENU_QUIT]
def get_menu_choice() -> Optional[str]:
"""Display the main menu and get user choice.
Returns:
Optional[str]: The selected menu option or None if cancelled
"""
question = [
inquirer.List('menu',
choices=MENU_CHOICES,
carousel=True)
]
try:
answer = inquirer.prompt(question)
return answer['menu'] if answer else None
except (KeyError, TypeError) as e:
logging.error(f"Error getting menu choice: {e}")
return None
def handle_card_info() -> None:
"""Handle the card info menu option with proper error handling."""
try:
while True:
card_info.get_card_info()
question = [
inquirer.Confirm('continue',
message='Would you like to look up another card?')
]
try:
answer = inquirer.prompt(question)
if not answer or not answer['continue']:
break
except (KeyError, TypeError) as e:
logging.error(f"Error in card info continuation prompt: {e}")
break
except Exception as e:
logging.error(f"Error in card info handling: {e}")
def run_menu() -> NoReturn:
"""Main menu loop with improved error handling and logging."""
logging.info("Starting MTG Python Deckbuilder")
Path('csv_files').mkdir(parents=True, exist_ok=True)
while True:
try:
answer = inquirer.prompt(question)
if answer is None:
print("Operation cancelled. Returning to menu...")
choice = 'Menu'
print('What would you like to do?')
choice = get_menu_choice()
if choice is None:
logging.info("Menu operation cancelled")
continue
choice = answer['menu']
except (KeyError, TypeError):
print("Invalid input. Please try again.")
choice = 'Menu'
# Run through initial setup
while choice == 'Setup':
setup.setup()
choice = 'Menu'
logging.info(f"User selected: {choice}")
# Make a new deck
while choice == 'Build a Deck':
print('Deck building not yet implemented')
choice = 'Menu'
match choice:
case 'Setup':
setup.setup()
tagger.run_tagging()
case 'Build a Deck':
logging.info("Deck building not yet implemented")
print('Deck building not yet implemented')
case 'Get Card Info':
handle_card_info()
case 'Tag CSV Files':
tagger.run_tagging()
case 'Quit':
logging.info("Exiting application")
sys.exit(0)
case _:
logging.warning(f"Invalid menu choice: {choice}")
# Get a cards info
while choice == 'Get Card Info':
card_info.get_card_info()
question = [
inquirer.Confirm('continue',
message='Would you like to look up another card?'
)
]
try:
answer = inquirer.prompt(question)
if answer is None:
print("Operation cancelled. Returning to menu...")
choice = 'Menu'
continue
new_card = answer['continue']
if new_card:
choice = 'Get Card Info' # Fixed == to = for assignment
except (KeyError, TypeError):
print("Invalid input. Returning to menu...")
choice = 'Menu'
except Exception as e:
logging.error(f"Unexpected error in main menu: {e}")
# Quit
while choice == 'Quit':
sys.exit()
if __name__ == "__main__":
run_menu()