-
Notifications
You must be signed in to change notification settings - Fork 76
[SVCS-159] Add renderer/exporter names to cached files #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import pkg_resources | ||
| from stevedore import driver | ||
|
|
||
| from mfr.core import exceptions | ||
|
|
@@ -97,6 +98,46 @@ def make_renderer(name, metadata, file_path, url, assets_url, export_url): | |
| } | ||
| ) | ||
|
|
||
| def get_renderer_name(name): | ||
| """ 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] | ||
|
|
||
| # 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: | ||
| return '' | ||
|
|
||
| def get_exporter_name(name): | ||
| """ 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.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] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original code works. However, |
||
|
|
||
|
|
||
| def sizeof_fmt(num, suffix='B'): | ||
| for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']: | ||
| if abs(num) < 1000.0: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import pytest | ||
| import pkg_resources | ||
|
|
||
| from mfr.core import utils as mfr_utils | ||
|
|
||
|
|
||
| class TestGetRendererName: | ||
|
|
||
| def test_get_renderer_name_explicit_assertions(self): | ||
| assert mfr_utils.get_renderer_name('.jpg') == 'ImageRenderer' | ||
| assert mfr_utils.get_renderer_name('.txt') == 'CodePygmentsRenderer' | ||
| assert mfr_utils.get_renderer_name('.xlsx') == 'TabularRenderer' | ||
| assert mfr_utils.get_renderer_name('.odt') == 'UnoconvRenderer' | ||
| assert mfr_utils.get_renderer_name('.pdf') == 'PdfRenderer' | ||
|
|
||
| def test_get_renderer_name(self): | ||
| entry_points = pkg_resources.iter_entry_points(group='mfr.renderers') | ||
| for ep in entry_points: | ||
| expected = ep.attrs[0] | ||
| assert mfr_utils.get_renderer_name(ep.name) == expected | ||
|
|
||
| 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): | ||
| assert mfr_utils.get_exporter_name('.jpg') == 'ImageExporter' | ||
| assert mfr_utils.get_exporter_name('.odt') == 'UnoconvExporter' | ||
|
|
||
| def test_get_exporter_name(self): | ||
| entry_points = pkg_resources.iter_entry_points(group='mfr.exporters') | ||
| for ep in entry_points: | ||
| expected = ep.attrs[0] | ||
| assert mfr_utils.get_exporter_name(ep.name) == expected | ||
|
|
||
| def test_get_exporter_name_no_entry_point(self): | ||
| with pytest.raises(IndexError): | ||
| mfr_utils.get_exporter_name('jpg') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍