@@ -99,44 +99,45 @@ def make_renderer(name, metadata, file_path, url, assets_url, export_url):
99
99
)
100
100
101
101
102
- def get_renderer_name (name ) :
102
+ def get_renderer_name (name : str ) -> str :
103
103
""" Return the name of the renderer used for a certain file extension.
104
104
105
105
:param str name: The name of the extension to get the renderer name for. (.jpg, .docx, etc)
106
106
107
107
:rtype : `str`
108
108
"""
109
109
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 )
118
114
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 :
122
118
return ''
123
119
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
124
125
- def get_exporter_name (name ) :
125
+ def get_exporter_name (name : str ) -> str :
126
126
""" Return the name of the exporter used for a certain file extension.
127
127
128
128
:param str name: The name of the extension to get the exporter name for. (.jpg, .docx, etc)
129
129
130
130
:rtype : `str`
131
131
"""
132
132
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 )
134
137
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 ]
140
141
141
142
142
143
def sizeof_fmt (num , suffix = 'B' ):
0 commit comments