@@ -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
141143def sizeof_fmt (num , suffix = 'B' ):
0 commit comments