Skip to content

Commit

Permalink
refactor: Clean up imports and resolve Ruff
Browse files Browse the repository at this point in the history
linting issues
- Remove unused imports
- Fix f-strings without placeholders
- Resolve undefined name warnings
  • Loading branch information
tahadnan committed Dec 13, 2024
1 parent d4ed04e commit e488025
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 32 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
test.py
README_PyPi.md
setup.py
pyproject.toml
MANIFEST.in

# Byte-compiled / optimized / DLL files
Expand Down
2 changes: 1 addition & 1 deletion fun_apis/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .cli import main_entery
from .cli import main_entery as main_entery
10 changes: 5 additions & 5 deletions fun_apis/apis_handling/__init__.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion fun_apis/apis_handling/animals.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion fun_apis/apis_handling/celebrities.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion fun_apis/apis_handling/facts.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions fun_apis/apis_handling/quotes.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 4 additions & 5 deletions fun_apis/apis_handling/superheroes.py
Original file line number Diff line number Diff line change
@@ -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] :
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions fun_apis/cli.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion fun_apis/configuration/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .config_manager import load_api_key, configure_api_key
from .config_manager import load_api_key, configure_api_key
__all__ = ["load_api_key", "configure_api_key"]
9 changes: 4 additions & 5 deletions fun_apis/configuration/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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":
Expand Down
8 changes: 7 additions & 1 deletion fun_apis/constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
SUPERHEROES_IDS_NAMES_TABLE
)
from .typed_dicts import *
from .interactive_mode_constants import *
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'
]
2 changes: 1 addition & 1 deletion fun_apis/constants/general_constants.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion fun_apis/interactive_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .interactive import interactive_mode
from .interactive import interactive_mode as interactive_mode
5 changes: 2 additions & 3 deletions fun_apis/interactive_cli/interactive.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.")

Expand Down Expand Up @@ -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"<ansibrightcyan>What would you like to do? Select an option by its <u>number</u> or <u>name</u>: </ansibrightcyan>"), completer=cli_prompt_completer)
option = prompt(HTML("<ansibrightcyan>What would you like to do? Select an option by its <u>number</u> or <u>name</u>: </ansibrightcyan>"), completer=cli_prompt_completer)
launch_interactive(option)
2 changes: 1 addition & 1 deletion fun_apis/utils/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"}
]
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"

0 comments on commit e488025

Please sign in to comment.