Skip to content

Commit

Permalink
Move format_map to mdcmd.py
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenChen committed Mar 26, 2024
1 parent 5cce3da commit 4d812f8
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions utils/mdcmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
# compatibility
systems = systems_no_aliases + tuple(aliases) + ('legacy',)

format_map = {
'nx_firmware': '16.0.1',
'ams_ver': '1.5.1',
'hekate_ver': '6.0.2',
'last_revision': 'March 28th, 2023',
}


def parse_header(header_raw: str) -> 'dict[str, Optional[str]]':
header: dict[str, Optional[str]] = {
Expand Down Expand Up @@ -109,15 +116,14 @@ def create_embed(header: 'dict[str, Optional[str]]', body: 'list[tuple[str, str]
return embed


def parse_md_command(md_text: str, format_map: dict | None, embed_color: discord.Color) -> tuple[dict, discord.Embed]:
def parse_md_command(md_text: str, embed_color: discord.Color) -> tuple[dict, discord.Embed]:
parts = md_text.split('\n\n', maxsplit=1)
if len(parts) == 1:
# in case there is nobody
parts.append('')
header_raw, body_raw = parts

if format_map:
body_raw = body_raw.format_map(format_map)
body_raw = body_raw.format_map(format_map)

header = parse_header(header_raw)
body = parse_body(body_raw)
Expand All @@ -129,7 +135,7 @@ def parse_md_command(md_text: str, format_map: dict | None, embed_color: discord
return header, create_embed(header, body, embed_color)


def md_file_to_embed(md_path: str, format_map: dict) -> tuple[str, str, dict, discord.Embed]:
def md_file_to_embed(md_path: str) -> tuple[str, str, dict, discord.Embed]:
colors = {
'3ds': ConsoleColor.n3ds(),
'wiiu': ConsoleColor.wiiu(),
Expand All @@ -144,7 +150,7 @@ def md_file_to_embed(md_path: str, format_map: dict) -> tuple[str, str, dict, di
with open(md_path, 'r', encoding='utf-8') as f:
fn = basename(md_path)
name, console, _ = fn.rsplit('.', maxsplit=2)
header, embed = parse_md_command(f.read(), format_map, colors[console])
header, embed = parse_md_command(f.read(), colors[console])
return name, console, header, embed


Expand All @@ -163,7 +169,7 @@ def get_console_name(console):
return aliases.get(console, console)


def add_md_files_as_commands(cog_class: 'Type[AssistanceCogs]', md_dir: str = None, *, namespace=commands, format_map=None, console_cmd: str = None):
def add_md_files_as_commands(cog_class: 'Type[AssistanceCogs]', md_dir: str = None, *, namespace=commands, console_cmd: str = None):

def make_cmd(name: str, help_desc: 'Optional[str]', embeds: 'dict[str, discord.Embed]', cooldown: 'tuple[int, int]', aliases: list[str]) -> commands.Command:
if len(embeds) > 1:
Expand Down Expand Up @@ -228,14 +234,8 @@ async def simple_cmd(self, ctx: commands.Context):
if md_dir is None:
md_dir = cog_class.data_dir

if format_map is None:
try:
format_map = cog_class.format_map
except AttributeError:
format_map = None

for md in iglob(join(md_dir, '*.md')):
command, console, header, embed = md_file_to_embed(md, format_map)
command, console, header, embed = md_file_to_embed(md)
new_commands[command][console] = embed
if header['aliases']:
aliases[command].extend(header['aliases'].split(','))
Expand Down

0 comments on commit 4d812f8

Please sign in to comment.