Skip to content

Commit 39ebdca

Browse files
committed
Refactor get_[renderer|exporter]_name()
- Improve code and comments
1 parent 913051e commit 39ebdca

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

mfr/core/utils.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,44 +99,45 @@ def make_renderer(name, metadata, file_path, url, assets_url, export_url):
9999
)
100100

101101

102-
def get_renderer_name(name):
102+
def get_renderer_name(name: str) -> str:
103103
""" Return the name of the renderer used for a certain file extension.
104104
105105
:param str name: The name of the extension to get the renderer name for. (.jpg, .docx, etc)
106106
107107
:rtype : `str`
108108
"""
109109

110-
# This can give back empty tuples
111-
try:
112-
entry_attrs = pkg_resources.iter_entry_points(group='mfr.renderers', name=name.lower())
113-
114-
# ep.attrs is a tuple of attributes. There should only ever be one or `None`.
115-
# None case occurs when trying to render an unsupported file type
116-
# entry_attrs is an iterable object, so we turn into a list to index it
117-
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)
118114

119-
# This means the file type is not supported. Just return the blank string so `make_renderers`
120-
# can log a real exception with all the variables and names it has
121-
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:
122118
return ''
123119

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+
124124

125-
def get_exporter_name(name):
125+
def get_exporter_name(name: str) -> str:
126126
""" Return the name of the exporter used for a certain file extension.
127127
128128
:param str name: The name of the extension to get the exporter name for. (.jpg, .docx, etc)
129129
130130
:rtype : `str`
131131
"""
132132

133-
# `make_renderer` should have already caught if an extension doesn't exist.
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)
134137

135-
# should be a list of length one, since we don't have multiple entrypoints per group
136-
entry_attrs = pkg_resources.iter_entry_points(group='mfr.exporters', name=name.lower())
137-
# ep.attrs is a tuple of attributes. There should only ever be one or `None`.
138-
# For our case however there shouldn't be `None`
139-
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]
140141

141142

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

0 commit comments

Comments
 (0)