Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Refactor mechanism for saving files. #2588

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

Merged
merged 2 commits into from
Nov 27, 2013
Merged

Refactor mechanism for saving files. #2588

merged 2 commits into from
Nov 27, 2013

Conversation

pwuertz
Copy link
Contributor

@pwuertz pwuertz commented Nov 9, 2013

I was trying to fix bbox_inches support for backend_pgf (issue #2586). However, I can't do this properly since tight_bbox needs to know if a backend uses figure.dpi or fixed 72dpi. Right now it derives this information from the file-type, which doesn't really work since different backends may be used for the same type. To fix this problem I need to know which backend is used when saving files in FigureCanvasBase.print_figure().

This required some major changes. I'll break them down to the following steps:

  • Removed print_ methods from FigureCanvasBase and thus a lot of code duplication. Moved print_jpeg and print_tiff to the Agg backend where they are actually implemented.
  • Picked up the rather dormant system for mapping file-types to backends in backend_bases and made it the default reference for finding an appropriate FigureCanvas for a given file type. FigureCanvas is now a canonical alias in each backend. Deferred imports of those are now handled centrally in get_registered_canvas_class().
  • Default format-to-backend mappings can be added or changed by register_backend(). This is also reflected in the save dialogues of graphical user interfaces.

The print_figure() method now works in the following way:

  • Check if the current FigureCanvas supports the requested file type
  • Check if there is a registered FigureCanvas for handling the file type
  • Else, report file format error.

Most code parts I modified came from @mdboom and @leejjoon, so I humbly ask you for your opinions.

@mdboom
Copy link
Member

mdboom commented Nov 11, 2013

Looks good to me. I think this really simplifies things. It appears that importlib is not available on Python 2.6, so we'll have to find a way to include backports of the stuff we need there (as we already do for some parts of subprocess).

@@ -2057,29 +2018,23 @@ def get_supported_filetypes_grouped(cls):
groupings[name].sort()
return groupings

def _get_print_method(self, format):
def _get_print_canvas(self, format):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how comfortably this method name sits with me. Since it is private, I'd be happy to let that slip for a decent 1liner docstring. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I kind of tried to keep the naming similar to their previous counterparts so it's more obvious what I'm doing. But you're right, I'm going to change that :)

@pelson
Copy link
Member

pelson commented Nov 12, 2013

The documentation will need to change for backend bases (you must now explicitly have a FigureCanvas alias. Non matplotlib backends (such as IPython's) are probably going to break as a result.

Other than that 👍 - this is a really beneficial change. Thanks @pwuertz

@pwuertz
Copy link
Contributor Author

pwuertz commented Nov 12, 2013

@pelson, I didn't find any good place to document this new FigureCanvas alias other than putting it in backend_template.py.
Also, I think this alias convention won't break anything at all. The alias is only required if you want deferred imports when saving to known/registered file formats - the behaviour of this wasn't configurable yet. But I must admit that I don't know any implementation details concerning IPython.

@pwuertz
Copy link
Contributor Author

pwuertz commented Nov 12, 2013

@pelson Do you have a suggestion where this could/should be documented? I guess a documentation and some examples regarding the file-format-register-functionality would be nice too.

Move all print_ methods to their backends.
Manage default format-to-backend mappings in backend_bases.
print_figure() can now determine the backend for saving figures.
Remove print_ code duplication, add canonical FigureCanvas names.
@pwuertz
Copy link
Contributor Author

pwuertz commented Nov 26, 2013

@pelson: I think I found a nice place for adding some documentation, an example is in the backend_template.py docstring now. Also that canvas getter method is renamed and documented.

@pelson
Copy link
Member

pelson commented Nov 27, 2013

I was thinking the backend_bases.py file, but it may be the template is just as good.

@mdboom - I support this change, but it is sufficiently big that I'd like a second opinion.

👍

@mdboom
Copy link
Member

mdboom commented Nov 27, 2013

I agree, this really simplifies things. Merging.

mdboom added a commit that referenced this pull request Nov 27, 2013
Refactor mechanism for saving files.
@mdboom mdboom merged commit be24fc9 into matplotlib:master Nov 27, 2013
@pwuertz pwuertz deleted the print_refactor branch November 27, 2013 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants