Skip to content

Commit 9803c5f

Browse files
cslzchenfelliott
authored andcommitted
Update renderer and exporter to use lower case names
- get_renderer_name() and get_exporter_name() take self.metadata.ext as the ext name. Must convert it to lower case to find the renderer/exporter.
1 parent f1eb153 commit 9803c5f

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

mfr/core/utils.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,44 +98,46 @@ def make_renderer(name, metadata, file_path, url, assets_url, export_url):
9898
}
9999
)
100100

101-
def get_renderer_name(name):
101+
102+
def get_renderer_name(name: str) -> str:
102103
""" Return the name of the renderer used for a certain file extension.
103104
104105
:param str name: The name of the extension to get the renderer name for. (.jpg, .docx, etc)
105106
106107
:rtype : `str`
107108
"""
108109

109-
# This can give back empty tuples
110-
try:
111-
entry_attrs = pkg_resources.iter_entry_points(group='mfr.renderers', name=name)
112-
113-
# ep.attrs is a tuple of attributes. There should only ever be one or `None`.
114-
# None case occurs when trying to render an unsupported file type
115-
# entry_attrs is an iterable object, so we turn into a list to index it
116-
return list(entry_attrs)[0].attrs[0]
110+
# `ep_iterator` is an iterable object. Must convert it to a `list` for access.
111+
# `list()` can only be called once because the iterator moves to the end after conversion.
112+
ep_iterator = pkg_resources.iter_entry_points(group='mfr.renderers', name=name.lower())
113+
ep_list = list(ep_iterator)
117114

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

123-
def get_exporter_name(name):
120+
# If file type is supported, there must be only one element in the list.
121+
assert len(ep_list) == 1
122+
return ep_list[0].attrs[0]
123+
124+
125+
def get_exporter_name(name: str) -> str:
124126
""" Return the name of the exporter used for a certain file extension.
125127
126128
:param str name: The name of the extension to get the exporter name for. (.jpg, .docx, etc)
127129
128130
:rtype : `str`
129131
"""
130132

131-
# `make_renderer` should have already caught if an extension doesn't exist.
132-
133-
# should be a list of length one, since we don't have multiple entrypoints per group
134-
entry_attrs = pkg_resources.iter_entry_points(group='mfr.exporters', name=name)
133+
# `ep_iterator` is an iterable object. Must convert it to a `list` for access.
134+
# `list()` can only be called once because the iterator moves to the end after conversion.
135+
ep_iterator = pkg_resources.iter_entry_points(group='mfr.exporters', name=name.lower())
136+
ep_list = list(ep_iterator)
135137

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

140142

141143
def sizeof_fmt(num, suffix='B'):

tests/core/test_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def test_get_renderer_name(self):
2222
def test_get_renderer_name_no_entry_point(self):
2323
assert mfr_utils.get_renderer_name('jpg') == ''
2424

25+
2526
class TestGetExporterName:
2627

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

3738
def test_get_exporter_name_no_entry_point(self):
38-
with pytest.raises(IndexError):
39+
with pytest.raises(AssertionError):
3940
mfr_utils.get_exporter_name('jpg')

0 commit comments

Comments
 (0)