From e488025da5ad4f4d37be50f633915dc51ada7ae4 Mon Sep 17 00:00:00 2001 From: Taha Yasser Adnan Date: Fri, 13 Dec 2024 23:02:28 +0100 Subject: [PATCH] refactor: Clean up imports and resolve Ruff linting issues - Remove unused imports - Fix f-strings without placeholders - Resolve undefined name warnings --- .gitignore | 1 - fun_apis/__init__.py | 2 +- fun_apis/apis_handling/__init__.py | 10 +++--- fun_apis/apis_handling/animals.py | 2 +- fun_apis/apis_handling/celebrities.py | 2 +- fun_apis/apis_handling/facts.py | 2 +- fun_apis/apis_handling/quotes.py | 3 +- fun_apis/apis_handling/superheroes.py | 9 +++-- fun_apis/cli.py | 2 -- fun_apis/configuration/__init__.py | 3 +- fun_apis/configuration/config_manager.py | 9 +++-- fun_apis/constants/__init__.py | 8 ++++- fun_apis/constants/general_constants.py | 2 +- fun_apis/interactive_cli/__init__.py | 2 +- fun_apis/interactive_cli/interactive.py | 5 ++- fun_apis/utils/helper_functions.py | 2 +- pyproject.toml | 43 ++++++++++++++++++++++++ 17 files changed, 75 insertions(+), 32 deletions(-) create mode 100644 pyproject.toml diff --git a/.gitignore b/.gitignore index 3327d4e..4140395 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ test.py README_PyPi.md setup.py -pyproject.toml MANIFEST.in # Byte-compiled / optimized / DLL files diff --git a/fun_apis/__init__.py b/fun_apis/__init__.py index effe876..3bc2e53 100644 --- a/fun_apis/__init__.py +++ b/fun_apis/__init__.py @@ -1 +1 @@ -from .cli import main_entery \ No newline at end of file +from .cli import main_entery as main_entery \ No newline at end of file diff --git a/fun_apis/apis_handling/__init__.py b/fun_apis/apis_handling/__init__.py index a039fa7..ee13c76 100644 --- a/fun_apis/apis_handling/__init__.py +++ b/fun_apis/apis_handling/__init__.py @@ -1,5 +1,5 @@ -from .animals import animal -from .facts import fact -from .quotes import quote -from .celebrities import celebrity -from .superheroes import superhero +from .animals import animal as animal +from .facts import fact as fact +from .quotes import quote as quote +from .celebrities import celebrity as celebrity +from .superheroes import superhero as superhero diff --git a/fun_apis/apis_handling/animals.py b/fun_apis/apis_handling/animals.py index e133025..287bac0 100644 --- a/fun_apis/apis_handling/animals.py +++ b/fun_apis/apis_handling/animals.py @@ -1,5 +1,5 @@ import requests -from typing import List, Dict, Optional +from typing import List, Optional from ..constants import AnimalsInfo, console from ..utils import requests_error_handler diff --git a/fun_apis/apis_handling/celebrities.py b/fun_apis/apis_handling/celebrities.py index 7451c8a..45e285b 100644 --- a/fun_apis/apis_handling/celebrities.py +++ b/fun_apis/apis_handling/celebrities.py @@ -1,5 +1,5 @@ import requests -from typing import Union, List, Dict, Optional +from typing import List, Optional import pycountry from ..constants import console, CelebrityInfo from ..utils import requests_error_handler, meters_to_freedom_units diff --git a/fun_apis/apis_handling/facts.py b/fun_apis/apis_handling/facts.py index 42bdada..d51e851 100644 --- a/fun_apis/apis_handling/facts.py +++ b/fun_apis/apis_handling/facts.py @@ -1,4 +1,4 @@ -from typing import List, TypedDict, Optional +from typing import List, Optional import requests from ..constants import console, Fact from ..utils import requests_error_handler diff --git a/fun_apis/apis_handling/quotes.py b/fun_apis/apis_handling/quotes.py index acade31..f31532e 100644 --- a/fun_apis/apis_handling/quotes.py +++ b/fun_apis/apis_handling/quotes.py @@ -1,6 +1,5 @@ -import json import random -from typing import Union, List, TypedDict, Optional +from typing import Union, List, Optional import requests from ..constants import console, categories, categories_noun, Quote from ..utils import requests_error_handler diff --git a/fun_apis/apis_handling/superheroes.py b/fun_apis/apis_handling/superheroes.py index 40e1b82..c37fba0 100644 --- a/fun_apis/apis_handling/superheroes.py +++ b/fun_apis/apis_handling/superheroes.py @@ -1,8 +1,7 @@ from typing import Union, Optional, Dict, List -import json import requests from ..constants import console, SuperHeroInfo -from ..utils import requests_error_handler, verify_superhero, create_powerstats_barplot, measure_function_execution_time +from ..utils import requests_error_handler, verify_superhero, create_powerstats_barplot @requests_error_handler def fetch_superhero_info(api_key : str , superhero_id_or_name : Union[int, str]) -> Optional[SuperHeroInfo] : @@ -16,7 +15,7 @@ def fetch_superhero_info(api_key : str , superhero_id_or_name : Union[int, str]) if response.status_code == 200: response = response.json() if response.get('response') == 'error' and response.get('error') == 'access denied': - console.print(f"[red]Access denied. Please check your API key.[/red]") + console.print("[red]Access denied. Please check your API key.[/red]") return None else: json_response.append(response) @@ -30,7 +29,7 @@ def fetch_superhero_info(api_key : str , superhero_id_or_name : Union[int, str]) if response.status_code == 200: json_response : Optional[SuperHeroInfo] = response.json() if json_response.get('response') == 'error' and json_response.get('error') == 'access denied': - console.print(f"[red]Access denied. Please check your API key.[/red]") + console.print("[red]Access denied. Please check your API key.[/red]") return None return json_response else: @@ -49,7 +48,7 @@ def display_superhero_info(*sups_info : Optional[Union[SuperHeroInfo, List[Super for superhero_info in sup_info: display_one_superhero_info(superhero_info) else: - console.print(f"Found 1 superhero.") + console.print("Found 1 superhero.") display_one_superhero_info(sup_info) def display_one_superhero_info(superhero_info: Optional[SuperHeroInfo]) -> None: diff --git a/fun_apis/cli.py b/fun_apis/cli.py index 0b51083..9426a50 100644 --- a/fun_apis/cli.py +++ b/fun_apis/cli.py @@ -1,6 +1,4 @@ import argparse -from typing import Union -import requests from .apis_handling import fact, animal, quote, celebrity, superhero from .configuration import load_api_key, configure_api_key from .interactive_cli import interactive_mode diff --git a/fun_apis/configuration/__init__.py b/fun_apis/configuration/__init__.py index be59ed2..8d1425c 100644 --- a/fun_apis/configuration/__init__.py +++ b/fun_apis/configuration/__init__.py @@ -1 +1,2 @@ -from .config_manager import load_api_key, configure_api_key \ No newline at end of file +from .config_manager import load_api_key, configure_api_key +__all__ = ["load_api_key", "configure_api_key"] \ No newline at end of file diff --git a/fun_apis/configuration/config_manager.py b/fun_apis/configuration/config_manager.py index 8539126..c5bf1de 100644 --- a/fun_apis/configuration/config_manager.py +++ b/fun_apis/configuration/config_manager.py @@ -5,13 +5,12 @@ from prompt_toolkit import prompt, HTML from prompt_toolkit.shortcuts import confirm from prompt_toolkit.completion import WordCompleter -from rich.prompt import Prompt from ..constants import CONFIG_FILE_PATH, console, valid_options, valid_options_string, valid_apis_keys from ..utils import cli_errors_handler def verify_api_key_legibility(api_key : str) -> None: if api_key.lower().strip() not in valid_apis_keys: - console.print(f"[red]Invalid API choice: '{ api_key}'. Please check the documentation for valid and needed API keys.[/red]") + console.print(f"[red]Invalid API choice: '{api_key}'. Please check the documentation for valid and needed API keys.[/red]") return False else: return True @@ -40,7 +39,7 @@ def input_api_key(which_api_key : Literal[valid_apis_keys]) -> str: verify_api_key_legibility(which_api_key) api_key = prompt(f"Enter your {which_api_key}: ", is_password=True).strip() if not api_key: - console.print(f"[red]Input field cannot be empty. Exiting...[/red]") + console.print("[red]Input field cannot be empty. Exiting...[/red]") exit(1) return api_key @@ -49,10 +48,10 @@ def configure_api_key() -> str: prompt_completer = WordCompleter(valid_options) console.print(f"Which API key are you willing to configure [bright_blue i]({valid_options_string})[/bright_blue i]") while True: - which_api_key = prompt(HTML(f"> "), completer=prompt_completer).strip().lower() + which_api_key = prompt(HTML("> "), completer=prompt_completer).strip().lower() all = False if not which_api_key: - console.print(f"[red i]Input cannot be empty, please choose a valid option.") + console.print("[red i]Input cannot be empty, please choose a valid option.") console.print(f"Available options => [bright_blue i]({valid_options_string})[/bright_blue i]") continue if which_api_key == "all": diff --git a/fun_apis/constants/__init__.py b/fun_apis/constants/__init__.py index 2341d70..c426ba1 100644 --- a/fun_apis/constants/__init__.py +++ b/fun_apis/constants/__init__.py @@ -10,4 +10,10 @@ SUPERHEROES_IDS_NAMES_TABLE ) from .typed_dicts import * -from .interactive_mode_constants import * \ No newline at end of file +from .interactive_mode_constants import * + +__all__ = [ + 'console', 'CONFIG_FILE_PATH', 'SUPERHEROES_JSON_FILE_PATH', + 'valid_apis_keys', 'valid_options', 'valid_options_string', + 'categories', 'categories_noun', 'SUPERHEROES_IDS_NAMES_TABLE' +] \ No newline at end of file diff --git a/fun_apis/constants/general_constants.py b/fun_apis/constants/general_constants.py index 10fbc46..5d622e2 100644 --- a/fun_apis/constants/general_constants.py +++ b/fun_apis/constants/general_constants.py @@ -1,6 +1,6 @@ import os import json -from typing import Union, Optional, List, Dict, TypedDict +from typing import List, Dict from rich.console import Console from rich.table import Table from platformdirs import user_config_dir diff --git a/fun_apis/interactive_cli/__init__.py b/fun_apis/interactive_cli/__init__.py index fbf4de9..42bb6b3 100644 --- a/fun_apis/interactive_cli/__init__.py +++ b/fun_apis/interactive_cli/__init__.py @@ -1 +1 @@ -from .interactive import interactive_mode \ No newline at end of file +from .interactive import interactive_mode as interactive_mode \ No newline at end of file diff --git a/fun_apis/interactive_cli/interactive.py b/fun_apis/interactive_cli/interactive.py index d738e56..b82df67 100644 --- a/fun_apis/interactive_cli/interactive.py +++ b/fun_apis/interactive_cli/interactive.py @@ -1,4 +1,3 @@ -import os from ..apis_handling import fact, animal, quote, celebrity, superhero from ..configuration import load_api_key, configure_api_key from ..constants import welcome_message, quote_topics, help_message, console, no_api_key_needed, AVAILABLE_QUOTE_TOPICS @@ -15,7 +14,7 @@ def exit_script() -> None: def invalid_option(option : str) -> None: if option.strip() == "": - console.print(f"[orange_red1]Input cannot be empty, run 'help' to check the available commands.") + console.print("[orange_red1]Input cannot be empty, run 'help' to check the available commands.") else: console.print(f"[orange_red1]{option} is invalid, run 'help' to check the available commands.") @@ -87,5 +86,5 @@ def interactive_mode() -> None: console.print(f"[green bold]{welcome_message}[/green bold]") while True: cli_prompt_completer = WordCompleter(list(options.keys()),ignore_case=True) - option = prompt(HTML(f"What would you like to do? Select an option by its number or name: "), completer=cli_prompt_completer) + option = prompt(HTML("What would you like to do? Select an option by its number or name: "), completer=cli_prompt_completer) launch_interactive(option) diff --git a/fun_apis/utils/helper_functions.py b/fun_apis/utils/helper_functions.py index 30bc672..0df0b7c 100644 --- a/fun_apis/utils/helper_functions.py +++ b/fun_apis/utils/helper_functions.py @@ -2,7 +2,7 @@ import json from json import JSONDecodeError from functools import wraps -from typing import Callable, Union, Any, Optional +from typing import Callable, Union, Any, Dict from time import perf_counter from requests import ConnectionError from ..constants import console, SUPERHEROES_JSON_FILE_PATH, SUPERHEROES_IDS_NAMES_TABLE diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..44ba79c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,43 @@ +[build-system] +requires = ["setuptools>=61.0", "wheel"] +build-backend = "setuptools.build_meta" + +[tool.ruff] +lint.ignore= ["F403"] + +[project] +name = "fun-apis-cli" +version = "0.2.0" +description = "A fun Python CLI tool for fetching random facts, animal information, quotes, and much more from various APIs." +readme = "README_PyPi.md" +authors = [ + {name = "Taha Yasser Adnan", email = "taha.y.adnan@gmail.com"} +] +license = {text = "MIT"} +urls = {Homepage = "https://github.com/tahadnan/Fun-APIs-CLI",Repository = "https://github.com/tahadnan/Fun-APIs-CLI"} +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent" +] + +requires-python = ">=3.6" + +dependencies = [ + "prompt_toolkit", + "pycountry", + "requests", + "rich", + "plotext", + "platformdirs" +] + +[tool.setuptools.packages.find] +where = ["."] +include = ["fun_apis*"] + +[tool.setuptools.package-data] +fun_apis = ["constants/*.json"] + +[project.scripts] +fun-apis-cli = "fun_apis:main_entery"