A Formatter for arbitrary mime-types. Unlike other `_repr_ _` methods, `_repr_mimebundle_` should return mime-bundle data, either the mime-keyed `data` dictionary or the tuple `(data, metadata)`. Any mime-type is valid. To define the callables that compute the mime-bun
| 920 | |
| 921 | |
| 922 | class MimeBundleFormatter(BaseFormatter): |
| 923 | """A Formatter for arbitrary mime-types. |
| 924 | |
| 925 | Unlike other `_repr_<mimetype>_` methods, |
| 926 | `_repr_mimebundle_` should return mime-bundle data, |
| 927 | either the mime-keyed `data` dictionary or the tuple `(data, metadata)`. |
| 928 | Any mime-type is valid. |
| 929 | |
| 930 | To define the callables that compute the mime-bundle representation of your |
| 931 | objects, define a :meth:`_repr_mimebundle_` method or use the :meth:`for_type` |
| 932 | or :meth:`for_type_by_name` methods to register functions that handle |
| 933 | this. |
| 934 | |
| 935 | .. versionadded:: 6.1 |
| 936 | """ |
| 937 | print_method = ObjectName('_repr_mimebundle_') |
| 938 | _return_type = dict |
| 939 | |
| 940 | def _check_return(self, r, obj): |
| 941 | r = super(MimeBundleFormatter, self)._check_return(r, obj) |
| 942 | # always return (data, metadata): |
| 943 | if r is None: |
| 944 | return {}, {} |
| 945 | if not isinstance(r, tuple): |
| 946 | return r, {} |
| 947 | return r |
| 948 | |
| 949 | @catch_format_error |
| 950 | def __call__(self, obj, include=None, exclude=None): |
| 951 | """Compute the format for an object. |
| 952 | |
| 953 | Identical to parent's method but we pass extra parameters to the method. |
| 954 | |
| 955 | Unlike other _repr_*_ `_repr_mimebundle_` should allow extra kwargs, in |
| 956 | particular `include` and `exclude`. |
| 957 | """ |
| 958 | if self.enabled: |
| 959 | # lookup registered printer |
| 960 | try: |
| 961 | printer = self.lookup(obj) |
| 962 | except KeyError: |
| 963 | pass |
| 964 | else: |
| 965 | return printer(obj) |
| 966 | # Finally look for special method names |
| 967 | method = get_real_method(obj, self.print_method) |
| 968 | |
| 969 | if method is not None: |
| 970 | return method(include=include, exclude=exclude) |
| 971 | return None |
| 972 | else: |
| 973 | return None |
| 974 | |
| 975 | |
| 976 | FormatterABC.register(BaseFormatter) |
no outgoing calls
no test coverage detected