Skip to content
Closed
Show file tree
Hide file tree
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
42 changes: 22 additions & 20 deletions mfr/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,44 +98,46 @@ def make_renderer(name, metadata, file_path, url, assets_url, export_url):
}
)

def get_renderer_name(name):

def get_renderer_name(name: str) -> str:
""" Return the name of the renderer used for a certain file extension.

:param str name: The name of the extension to get the renderer name for. (.jpg, .docx, etc)

:rtype : `str`
"""

# This can give back empty tuples
try:
entry_attrs = pkg_resources.iter_entry_points(group='mfr.renderers', name=name)

# ep.attrs is a tuple of attributes. There should only ever be one or `None`.
# None case occurs when trying to render an unsupported file type
# entry_attrs is an iterable object, so we turn into a list to index it
return list(entry_attrs)[0].attrs[0]
# `ep_iterator` is an iterable object. Must convert it to a `list` for access.
# `list()` can only be called once because the iterator moves to the end after conversion.
ep_iterator = pkg_resources.iter_entry_points(group='mfr.renderers', name=name.lower())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For both exporter and renderer, the primary functional change is name=name.lower().

ep_list = list(ep_iterator)

# This means the file type is not supported. Just return the blank string so `make_renderers`
# can log a real exception with all the variables and names it has
except IndexError:
# Empty list indicates unsupported file type.
# Return a blank string and let `make_renderer()` handle it.
if len(ep_list) == 0:
return ''

def get_exporter_name(name):
# If file type is supported, there must be only one element in the list.
assert len(ep_list) == 1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For both exporter and renderer, use assert for functionalities that are not expected to break.

return ep_list[0].attrs[0]


def get_exporter_name(name: str) -> str:
""" Return the name of the exporter used for a certain file extension.

:param str name: The name of the extension to get the exporter name for. (.jpg, .docx, etc)

:rtype : `str`
"""

# `make_renderer` should have already caught if an extension doesn't exist.

# should be a list of length one, since we don't have multiple entrypoints per group
entry_attrs = pkg_resources.iter_entry_points(group='mfr.exporters', name=name)
# `ep_iterator` is an iterable object. Must convert it to a `list` for access.
# `list()` can only be called once because the iterator moves to the end after conversion.
ep_iterator = pkg_resources.iter_entry_points(group='mfr.exporters', name=name.lower())
ep_list = list(ep_iterator)

# ep.attrs is a tuple of attributes. There should only ever be one or `None`.
# For our case however there shouldn't be `None`
return list(entry_attrs)[0].attrs[0]
# `make_renderer()` is called before `make_exporter()` to ensure the file type is supported
assert len(ep_list) == 1
return ep_list[0].attrs[0]


def sizeof_fmt(num, suffix='B'):
Expand Down
3 changes: 2 additions & 1 deletion tests/core/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_get_renderer_name(self):
def test_get_renderer_name_no_entry_point(self):
assert mfr_utils.get_renderer_name('jpg') == ''


class TestGetExporterName:

def test_get_exporter_name_explicit_assertions(self):
Expand All @@ -35,5 +36,5 @@ def test_get_exporter_name(self):
assert mfr_utils.get_exporter_name(ep.name) == expected

def test_get_exporter_name_no_entry_point(self):
with pytest.raises(IndexError):
with pytest.raises(AssertionError):
mfr_utils.get_exporter_name('jpg')