Skip to content

Commit

Permalink
refactor: type hints and clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
vile committed May 28, 2024
1 parent 590785f commit 2be5c7b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
6 changes: 4 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ def run(config: dict) -> None:
if "error" in guild_info:
print(f"{Fore.RED}[!] There was an error getting guild info: {guild_info['error']}{Fore.RESET}") # fmt: skip
else:
display.display_guild_info(guild_info, config)
display.guild_info(guild_info, config)

if config["scrape_permission_info"]:
guild_roles: list = guild.scrape_guild_roles(token, server_id)
guild_formatted: str = display.display_guild_roles(guild_roles, config)
guild_formatted: str = display.build_permissions_table(
guild_roles, config["permissions_to_scrape"]
)
print(guild_formatted)

if config["export_results"]:
Expand Down
23 changes: 13 additions & 10 deletions utils/display.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import string
from typing import Union

from colorama import Fore
from tabulate import tabulate


def display_guild_info(guild_info: dict, config: dict) -> None:
def guild_info(guild_info: dict, config: dict) -> None:
for item, value in guild_info.items():
if (
item in config["guild_info_to_scrape"]
Expand All @@ -13,14 +14,17 @@ def display_guild_info(guild_info: dict, config: dict) -> None:
print(f"{Fore.GREEN}{item}{Fore.RESET}: {value}")


def display_guild_roles(guild_roles: list, config: dict) -> str:
def build_permissions_table(
guild_roles: list, attributes: dict[str, Union[bool, int]]
) -> str:
roles: list = sorted(guild_roles, key=lambda role: role["position"], reverse=True)
table_data: list = []
table_data: list[str] = []

for role in roles:
role_info: list = []
for item, value in config["permissions_to_scrape"].items():
# Non-permission flags
role_info: list[str] = []

for item, value in attributes.items():
# Non-permission flags (tags, mentionable)
if type(value) is bool and value:
if item == "tags":
if "tags" in role:
Expand All @@ -45,7 +49,7 @@ def display_guild_roles(guild_roles: list, config: dict) -> str:
)
continue

# Catch all for non-edge case properties
# Catch all for non-edge case properties (name, id, position, etc.)
role_info.append(
"".join(filter(lambda x: x in string.printable, str(role[item])))
)
Expand All @@ -62,9 +66,8 @@ def display_guild_roles(guild_roles: list, config: dict) -> str:

tab_data = tabulate(
table_data,
headers=list(
key for key, value in config["permissions_to_scrape"].items() if value
),
headers=list(key for key, value in attributes.items() if value),
tablefmt="github",
)

return tab_data

0 comments on commit 2be5c7b

Please sign in to comment.