Skip to content

Change pep command to use .txt by default #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 46 additions & 44 deletions bot/cogs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,58 @@ async def pep_command(self, ctx: Context, pep_number: str):
else:
return await ctx.invoke(self.bot.get_command("help"), "pep")

# Newer PEPs are written in RST instead of txt
if pep_number > 542:
pep_url = f"{self.base_github_pep_url}{pep_number:04}.rst"
else:
pep_url = f"{self.base_github_pep_url}{pep_number:04}.txt"

# Attempt to fetch the PEP
log.trace(f"Requesting PEP {pep_number} with {pep_url}")
response = await self.bot.http_session.get(pep_url)

if response.status == 200:
log.trace("PEP found")

pep_content = await response.text()

# Taken from https://github.com/python/peps/blob/master/pep0/pep.py#L179
pep_header = HeaderParser().parse(StringIO(pep_content))

# Assemble the embed
pep_embed = Embed(
title=f"**PEP {pep_number} - {pep_header['Title']}**",
description=f"[Link]({self.base_pep_url}{pep_number:04})",
)

pep_embed.set_thumbnail(url="https://www.python.org/static/opengraph-icon-200x200.png")

# Add the interesting information
if "Status" in pep_header:
pep_embed.add_field(name="Status", value=pep_header["Status"])
if "Python-Version" in pep_header:
pep_embed.add_field(name="Python-Version", value=pep_header["Python-Version"])
if "Created" in pep_header:
pep_embed.add_field(name="Created", value=pep_header["Created"])
if "Type" in pep_header:
pep_embed.add_field(name="Type", value=pep_header["Type"])
possible_extensions = ['.txt', '.rst']
found_pep = False
for extension in possible_extensions:
# Attempt to fetch the PEP
pep_url = f"{self.base_github_pep_url}{pep_number:04}{extension}"
log.trace(f"Requesting PEP {pep_number} with {pep_url}")
response = await self.bot.http_session.get(pep_url)

if response.status == 200:
log.trace("PEP found")
found_pep = True

pep_content = await response.text()

# Taken from https://github.com/python/peps/blob/master/pep0/pep.py#L179
pep_header = HeaderParser().parse(StringIO(pep_content))

# Assemble the embed
pep_embed = Embed(
title=f"**PEP {pep_number} - {pep_header['Title']}**",
description=f"[Link]({self.base_pep_url}{pep_number:04})",
)

elif response.status == 404:
pep_embed.set_thumbnail(url="https://www.python.org/static/opengraph-icon-200x200.png")

# Add the interesting information
if "Status" in pep_header:
pep_embed.add_field(name="Status", value=pep_header["Status"])
if "Python-Version" in pep_header:
pep_embed.add_field(name="Python-Version", value=pep_header["Python-Version"])
if "Created" in pep_header:
pep_embed.add_field(name="Created", value=pep_header["Created"])
if "Type" in pep_header:
pep_embed.add_field(name="Type", value=pep_header["Type"])

elif response.status != 404:
# any response except 200 and 404 is expected
found_pep = True # actually not, but it's easier to display this way
log.trace(f"The user requested PEP {pep_number}, but the response had an unexpected status code: "
f"{response.status}.\n{response.text}")

error_message = "Unexpected HTTP error during PEP search. Please let us know."
pep_embed = Embed(title="Unexpected error", description=error_message)
pep_embed.colour = Colour.red()
break

if not found_pep:
log.trace("PEP was not found")
not_found = f"PEP {pep_number} does not exist."
pep_embed = Embed(title="PEP not found", description=not_found)
pep_embed.colour = Colour.red()

else:
log.trace(f"The user requested PEP {pep_number}, but the response had an unexpected status code: "
f"{response.status}.\n{response.text}")

error_message = "Unexpected HTTP error during PEP search. Please let us know."
pep_embed = Embed(title="Unexpected error", description=error_message)
pep_embed.colour = Colour.red()

await ctx.message.channel.send(embed=pep_embed)

@command()
Expand Down