Fix flake8 warnings for lektor_project_data.py

This commit is contained in:
Jeff Dairiki 2022-07-27 12:31:20 -07:00
parent 41e6091e0d
commit 850e52f598
1 changed files with 16 additions and 10 deletions

View File

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import cgi import cgi
import pkg_resources
import readme_renderer.markdown import readme_renderer.markdown
import readme_renderer.rst import readme_renderer.rst
@ -17,6 +16,7 @@ _RENDERERS = {
'text/markdown': readme_renderer.markdown, 'text/markdown': readme_renderer.markdown,
} }
class ProjectDataPlugin(Plugin): class ProjectDataPlugin(Plugin):
name = 'Project Data' name = 'Project Data'
description = u'Retrieve project information from PyPI.' description = u'Retrieve project information from PyPI.'
@ -24,27 +24,33 @@ class ProjectDataPlugin(Plugin):
data = {} data = {}
def render(self, value, content_type=None): def render(self, value, content_type=None):
"""Taken from https://github.com/pypa/warehouse/blob/master/warehouse/filters.py """Render project description.
to ensure compliance and not reinvent the wheel. We don't want to be creative here.
This is taken from
https://github.com/pypa/warehouse/blob/master/warehouse/filters.py
to ensure compliance and not reinvent the wheel. We don't
want to be creative here.
""" """
content_type, parameters = cgi.parse_header(content_type or '') content_type, parameters = cgi.parse_header(content_type or '')
# Get the appropriate renderer # Get the appropriate renderer
renderer = _RENDERERS.get(content_type, readme_renderer.txt) renderer = _RENDERERS.get(content_type, readme_renderer.txt)
# Actually render the given value, this will not only render the value, but # Actually render the given value, this will not only render
# also ensure that it's had any disallowed markup removed. # the value, but also ensure that it's had any disallowed
# markup removed.
rendered = renderer.render(value, **parameters) rendered = renderer.render(value, **parameters)
# If the content was not rendered, we'll render as plaintext instead. The # If the content was not rendered, we'll render as plaintext
# reason it's necessary to do this instead of just accepting plaintext is # instead. The reason it's necessary to do this instead of
# that readme_renderer will deal with sanitizing the content. # just accepting plaintext is that readme_renderer will deal
# with sanitizing the content.
if rendered is None: if rendered is None:
rendered = readme_renderer.txt.render(value) rendered = readme_renderer.txt.render(value)
return rendered return rendered
def package_data(self, name, entry_point=None): def package_data(self, name, entry_point=None):
if not entry_point: if not entry_point:
entry_point = 'https://pypi.org/pypi' entry_point = 'https://pypi.org/pypi'
@ -64,7 +70,7 @@ class ProjectDataPlugin(Plugin):
self.data['description'] = self.render( self.data['description'] = self.render(
self.data['description'], self.data['description_content_type']) self.data['description'], self.data['description_content_type'])
if not self.data.get('home_page'): if not self.data.get('home_page'):
self.data['home_page'] = 'https://pypi.org/project/{}/'.format(name) self.data['home_page'] = f'https://pypi.org/project/{name}/'
def github_data(self, owner=None, repo=None): def github_data(self, owner=None, repo=None):
url = 'https://api.github.com/repos/{}/{}'.format(owner, repo) url = 'https://api.github.com/repos/{}/{}'.format(owner, repo)