Format: application/x-python-code
⬇ Download File (31.2 KB)
   1  o
   2  
   3  ��h���@sxdZddlZddlZddlZddlZddlmZddlmZddlm	Z	ddl
   4  mZdd	lm
   5  Z
   6  dd
   7  lmZddlmZddlmZmZmZmZmZmZmZmZmZmZdd
   8  lmZGdd�de	�Zdd�Z Gdd�de!�Z"edd��Z#Gdd�dej$d�Z%dd�Z&dd�Z'e
   9  de(d�Z)Gdd �d e	�Z*Gd!d"�d"e*�Z+Gd#d$�d$e*�Z,Gd%d&�d&e*�Z-Gd'd(�d(e*�Z.Gd)d*�d*e*�Z/Gd+d,�d,e*�Z0Gd-d.�d.e*�Z1Gd/d0�d0e*�Z2Gd1d2�d2e*�Z3Gd3d4�d4e*�Z4Gd5d6�d6e*�Z5Gd7d8�d8e*�Z6e%�7e*�e%�7e+�e%�7e,�e%�7e-�e%�7e.�e%�7e/�e%�7e4�e%�7e0�e%�7e1�e%�7e2�e%�7e3�e%�7e5�e%�7e6�d;d9d:�Z8dS)<a�Display formatters.
  10  
  11  This module defines the base instances in order to implement custom
  12  formatters/mimetypes
  13  got objects:
  14  
  15  As we want to see internal IPython working we are going to use the following
  16  function to diaply objects instead of the normal print or display method:
  17  
  18      >>> ip = get_ipython()
  19      >>> ip.display_formatter.format(...)
  20      ({'text/plain': 'Ellipsis'}, {})
  21  
  22  This return a tuple with the mimebumdle for the current object, and the
  23  associated metadata.
  24  
  25  
  26  We can now define our own formatter and register it:
  27  
  28  
  29      >>> from IPython.core.formatters import BaseFormatter, FormatterABC
  30  
  31  
  32      >>> class LLMFormatter(BaseFormatter):
  33      ...
  34      ...     format_type = 'x-vendor/llm'
  35      ...     print_method = '_repr_llm_'
  36      ...     _return_type = (dict, str)
  37  
  38      >>> llm_formatter = LLMFormatter(parent=ip.display_formatter)
  39  
  40      >>> ip.display_formatter.formatters[LLMFormatter.format_type] = llm_formatter
  41  
  42  Now any class that define `_repr_llm_` will return a x-vendor/llm as part of
  43  it's display data:
  44  
  45      >>> class A:
  46      ...
  47      ...     def _repr_llm_(self, *kwargs):
  48      ...         return 'This a A'
  49      ...
  50  
  51      >>> ip.display_formatter.format(A())
  52      ({'text/plain': '<IPython.core.formatters.A at ...>', 'x-vendor/llm': 'This a A'}, {})
  53  
  54  As usual, you can register methods for third party types (see
  55  :ref:`third_party_formatting`)
  56  
  57      >>> def llm_int(obj):
  58      ...      return 'This is the integer %s, in between %s and %s'%(obj, obj-1, obj+1)
  59  
  60      >>> llm_formatter.for_type(int, llm_int)
  61  
  62      >>> ip.display_formatter.format(42)
  63      ({'text/plain': '42', 'x-vendor/llm': 'This is the integer 42, in between 41 and 43'}, {})
  64  
  65  
  66  Inheritance diagram:
  67  
  68  .. inheritance-diagram:: IPython.core.formatters
  69     :parts: 3
  70  �N)�StringIO)�	decorator)�Configurable�)�get_ipython�)�Sentinel)�get_real_method)�pretty)
  71  �Bool�Dict�Integer�Unicode�CUnicode�
  72  ObjectName�List�ForwardDeclaredInstance�default�observe)�Anyc@s�eZdZee�dd�jdd�Zed�dd��Ze	d�dd	��Z
  73  ed
  74  �Zed�dd
  75  ��Z
  76  ed
  77  �Zed�dd��Ze�Zed�dd��Zddd�Zedd��ZdS)�DisplayFormatterz�List of currently active mime-types to display.
  78          You can use this to set a white-list for formats to display.
  79  
  80          Most users will not need to change this value.
  81          ��helpT��config�active_typescCs|jS�N)�format_types��self�r �Mc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/formatters.py�_active_types_defaultdsz&DisplayFormatter._active_types_defaultcCs2|j��D]\}}||dvrd|_qd|_qdS)N�newTF)�
  82  formatters�items�enabled)r�change�key�	formatterr r r!�_active_types_changedhs
  83  �z&DisplayFormatter._active_types_changed�FormatterABC�ipython_display_formattercC�
  84  t|d�S�N��parent)�IPythonDisplayFormatterrr r r!�_default_formatterr�
  85  z#DisplayFormatter._default_formatter�mimebundle_formattercCr-r.)�MimeBundleFormatterrr r r!�_default_mime_formatterxr3z(DisplayFormatter._default_mime_formatterr$c
  86  Cs>tttttttttt	g
  87  }i}|D]}||d�}|||j
  88  <q|S)z Activate the default formatters.r/)�PlainTextFormatter�
  89  HTMLFormatter�MarkdownFormatter�SVGFormatter�PNGFormatter�PDFFormatter�
  90  JPEGFormatter�LatexFormatter�
  91  JSONFormatter�JavascriptFormatter�format_type)rZformatter_classes�d�cls�fr r r!�_formatters_default�s �
  92  z$DisplayFormatter._formatters_defaultNc
  93  	sPi}i}|�|�r
  94  iifS|j|��d�\}}|s|rK�r3�fdd�|��D�}�fdd�|��D�}�rK�fdd�|��D�}�fdd�|��D�}|j��D]S\}}||vriz|�|�Wn	tyhYqPw�rp|�vrpqP�rw|�vrwqPd}z||�}	Wn�t|	t�r�t|	�dkr�|	\}	}|	dur�|	||<|dur�|||<qP||fS)	a�Return a format data dict for an object.
  95  
  96          By default all format types will be computed.
  97  
  98          The following MIME types are usually implemented:
  99  
 100          * text/plain
 101          * text/html
 102          * text/markdown
 103          * text/latex
 104          * application/json
 105          * application/javascript
 106          * application/pdf
 107          * image/png
 108          * image/jpeg
 109          * image/svg+xml
 110  
 111          Parameters
 112          ----------
 113          obj : object
 114              The Python object whose format data will be computed.
 115          include : list, tuple or set; optional
 116              A list of format type strings (MIME types) to include in the
 117              format data dict. If this is set *only* the format types included
 118              in this list will be computed.
 119          exclude : list, tuple or set; optional
 120              A list of format type string (MIME types) to exclude in the format
 121              data dict. If this is set all format types will be computed,
 122              except for those included in this argument.
 123              Mimetypes present in exclude will take precedence over the ones in include
 124  
 125          Returns
 126          -------
 127          (format_dict, metadata_dict) : tuple of two dicts
 128              format_dict is a dictionary of key/value pairs, one of each format that was
 129              generated for the object. The keys are the format types, which
 130              will usually be MIME type strings and the values and JSON'able
 131              data structure containing the raw data for the representation in
 132              that format.
 133  
 134              metadata_dict is a dictionary of metadata about each mime-type output.
 135              Its keys will be a strict subset of the keys in format_dict.
 136  
 137          Notes
 138          -----
 139              If an object implement `_repr_mimebundle_` as well as various
 140              `_repr_*_`, the data returned by `_repr_mimebundle_` will take
 141              precedence and the corresponding `_repr_*_` for this mimetype will
 142              not be called.
 143  
 144          ��include�excludec�i|]\}}|�vr||�qSr r ��.0�k�v�rGr r!�
 145  <dictcomp>��z+DisplayFormatter.format.<locals>.<dictcomp>crIr r rJrNr r!rO�rPc�i|]\}}|�vr||�qSr r rJ�rHr r!rO�rPcrQr r rJrRr r!rO�rPNr)	r,r4r%r$�lookup�KeyError�
 146  isinstance�tuple�len)
 147  r�objrGrHZformat_dictZmd_dictrAr)�md�datar )rHrGr!�format�sH4
 148  ��zDisplayFormatter.formatcCst|j���S)z>Return the format types (MIME types) of the active formatters.)�listr$�keysrr r r!r�szDisplayFormatter.format_types�NN)�__name__�
 149  __module__�__qualname__rr�tagrrr"rr*rr,r2r4r6rr$rEr[�propertyrr r r r!rZs,��
 150  
 151  
 152  
 153  
 154  
 155  hrc
 156  Cs8zt|�WSty}z
 157  d|WYd}~Sd}~ww)zNTry to return a repr of an object
 158  
 159      always returns a string, at least.
 160      zun-repr-able object (%r)N)�repr�	Exception)rX�er r r!�
 161  _safe_reprs
 162  ��rgc@seZdZdZdS)�FormatterWarningz&Warning class for errors in formattersN)r_r`ra�__doc__r r r r!rhsrhcOs�z
 163  ||g|�Ri|��}Wn7ty|�d|d�YStyDt��}t�}|dur5|�|�ntj|�|�d|d�YSw|�||d�S)z$show traceback on failed format callNr)	�NotImplementedError�
 164  _check_returnre�sys�exc_infor�
 165  showtraceback�	traceback�print_exception)�methodr�args�kwargs�rrm�ipr r r!�catch_format_errors
 166  �rvc@s&eZdZdZdZdZejdd��ZdS)r+aE Abstract base class for Formatters.
 167  
 168      A formatter is a callable class that is responsible for computing the
 169      raw format data for a particular format type (MIME type). For example,
 170      an HTML formatter would have a format type of `text/html` and would return
 171      the HTML representation of the object when called.
 172      �
 173  text/plainTcCst|�S)z�Return a JSON'able representation of the object.
 174  
 175          If the object cannot be formatted by this formatter,
 176          warn and return None.
 177          )rd)rrXr r r!�__call__8szFormatterABC.__call__N)	r_r`rarirAr&�abc�abstractmethodrxr r r r!r+)s	r+)�	metaclasscCs t|dd�}t|dd�}||fS)zgReturn a (__module__, __name__) tuple for a type.
 178  
 179      Used as key in Formatter.deferred_printers.
 180      r`Nr_)�getattr)�typ�module�namer r r!�
 181  _mod_name_keyBsr�cCst|dd�p	t|�S)z2Return the type of an instance (old and new-style)�	__class__N)r|�type)rXr r r!�	_get_typeLsr��_raise_key_errorzr
 182  Special value to raise a KeyError
 183  
 184  Raise KeyError in `BaseFormatter.pop` if passed as the default value to `pop`
 185  c@s�eZdZUdZed�ZeZee	d<e
 186  d�jdd�Ze
 187  d�Ze�jdd�Ze�jdd�Ze�jdd�Zedd��Zd	d
 188  �Zdd�Zd
 189  d�Zdd�Zddd�Zddd�Zefdd�Zdd�ZdS)�
 190  BaseFormattera�A base formatter class that is configurable.
 191  
 192      This formatter should usually be used as the base class of all formatters.
 193      It is a traited :class:`Configurable` class and includes an extensible
 194      API for users to determine how their objects are formatted. The following
 195      logic is used to find a function to format an given object.
 196  
 197      1. The object is introspected to see if it has a method with the name
 198         :attr:`print_method`. If is does, that object is passed to that method
 199         for formatting.
 200      2. If no print method is found, three internal dictionaries are consulted
 201         to find print method: :attr:`singleton_printers`, :attr:`type_printers`
 202         and :attr:`deferred_printers`.
 203  
 204      Users should use these dictionaries to register functions that will be
 205      used to compute the format data for their objects (if those objects don't
 206      have the special print methods). The easiest way of using these
 207      dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name`
 208      methods.
 209  
 210      If no function/callable is found to compute the format data, ``None`` is
 211      returned and this format type is not used.
 212      rw�_return_typeTr�__repr__cCsP|jr&z
 213  |�|�}W||�StyYnwt||j�}|dur$|�SdSdS)�!Compute the format for an object.N�r&rSrTr	�print_method�rrX�printerrqr r r!rx�s��zBaseFormatter.__call__cCs&z|�|�WdStyYdSw)zmap in to lookup_by_typeFT)�lookup_by_typerT)rr}r r r!�__contains__�s��zBaseFormatter.__contains__cCs^|dust||j�st|t�r|rt|d|j�r|St�d|jt|�|jt|�ft�dS)�vCheck that a return value is appropriate
 214  
 215          Return the value if so, None otherwise, warning if invalid.
 216          NrzB%s formatter returned invalid type %s (expected %s) for object: %s)	rUr�rV�warnings�warnrAr�rgrh�rrtrXr r r!rk�s�����zBaseFormatter._check_returncCs*t|�}||jvr|j|S|�t|��S)aALook up the formatter for a given instance.
 217  
 218          Parameters
 219          ----------
 220          obj : object instance
 221  
 222          Returns
 223          -------
 224          f : callable
 225              The registered formatting callable for the type.
 226  
 227          Raises
 228          ------
 229          KeyError if the type has not been registered.
 230          )�id�singleton_printersr�r�)rrX�obj_idr r r!rS�s
 231  
 232  zBaseFormatter.lookupcCs�t|t�r+t|�dd��}||jvr&|jD]}t|�|kr$|j|Sqn|j|St�|�D]}||jvs<|�	|�rC|j|Sq0t
 233  d�|���)abLook up the registered formatter for a type.
 234  
 235          Parameters
 236          ----------
 237          typ : type or '__module__.__name__' string for a type
 238  
 239          Returns
 240          -------
 241          f : callable
 242              The registered formatting callable for the type.
 243  
 244          Raises
 245          ------
 246          KeyError if the type has not been registered.
 247          �.rzNo registered printer for {0!r})rU�strrV�rsplit�deferred_printers�
 248  type_printersr�r
 249  Z_get_mro�_in_deferred_typesrTr[)rr}�typ_keyrCr r r!r��s
 250  
 251  
 252  ��
 253  �zBaseFormatter.lookup_by_typeNcCsdt|t�r|�dd�\}}|�|||�Sz|�|�}Wnty&d}Ynw|dur0||j|<|S)atAdd a format function for a given type.
 254  
 255          Parameters
 256          ----------
 257          typ : type or '__module__.__name__' string for a type
 258              The class of the object that will be formatted using `func`.
 259  
 260          func : callable
 261              A callable for computing the format data.
 262              `func` will be called with the object to be formatted,
 263              and will return the raw data in this formatter's format.
 264              Subclasses may use a different call signature for the
 265              `func` argument.
 266  
 267              If `func` is None or not specified, there will be no change,
 268              only returning the current value.
 269  
 270          Returns
 271          -------
 272          oldfunc : callable
 273              The currently registered callable.
 274              If you are registering a new formatter,
 275              this will be the previous value (to enable restoring later).
 276          r�rN)rUr�r��for_type_by_namer�rTr�)rr}�func�type_module�	type_name�oldfuncr r r!�for_type�s
 277  �
 278  zBaseFormatter.for_typecCsH||f}z	|�d|�}Wntyd}Ynw|dur"||j|<|S)a"Add a format function for a type specified by the full dotted
 279          module and name of the type, rather than the type of the object.
 280  
 281          Parameters
 282          ----------
 283          type_module : str
 284              The full dotted name of the module the type is defined in, like
 285              ``numpy``.
 286  
 287          type_name : str
 288              The name of the type (the class name), like ``dtype``
 289  
 290          func : callable
 291              A callable for computing the format data.
 292              `func` will be called with the object to be formatted,
 293              and will return the raw data in this formatter's format.
 294              Subclasses may use a different call signature for the
 295              `func` argument.
 296  
 297              If `func` is None or unspecified, there will be no change,
 298              only returning the current value.
 299  
 300          Returns
 301          -------
 302          oldfunc : callable
 303              The currently registered callable.
 304              If you are registering a new formatter,
 305              this will be the previous value (to enable restoring later).
 306          z%s.%sN)r�rTr�)rr�r�r�r(r�r r r!r�s�
 307  zBaseFormatter.for_type_by_namecCs�t|t�r0t|�dd��}||jvr)|jD]}t|�|kr%|j�|�}nq|}n|j�|�}n||jvr<|j�|�}n	|j�t|�|�}|turPt	d�
 308  |���|S)a�Pop a formatter for the given type.
 309  
 310          Parameters
 311          ----------
 312          typ : type or '__module__.__name__' string for a type
 313          default : object
 314              value to be returned if no formatter is registered for typ.
 315  
 316          Returns
 317          -------
 318          obj : object
 319              The last registered object for the type.
 320  
 321          Raises
 322          ------
 323          KeyError if the type is not registered and default is not specified.
 324          r�rzNo registered value for {0!r})rUr�rVr�r�r�r��popr�rTr[)rr}rr�rC�oldr r r!r�=s"
 325  
 326  
 327  ��
 328  zBaseFormatter.popcCsHt|dd�}t|dd�}||f}||jvr"|j�|�}||j|<dSdS)z�
 329          Check if the given class is specified in the deferred type registry.
 330  
 331          Successful matches will be moved to the regular type registry for future use.
 332          r`Nr_TF)r|r�r�r�)rrC�modrr(r�r r r!r�fs
 333  
 334  z BaseFormatter._in_deferred_typesr)r_r`rarirrAr�r�r�__annotations__rrbr&rr�rr�r�r�rvrxr�rkrSr�r�r�r�r�r�r r r r!r�\s&
 335  
 336  	
 337  "
 338  ())r�c@s�eZdZdZed�Zed�jdd�Ze	e
 339  jdd�jdd�Ze
 340  d�Zed�jdd�Zed�jdd�Ze	d	�jdd�Zed
 341  �jdd�Zed�Zed�jdd�Zed
 342  �dd��Zed�dd��Zed�dd��Zed�dd��Zedd��ZdS)r7a`The default pretty-printer.
 343  
 344      This uses :mod:`IPython.lib.pretty` to compute the format data of
 345      the object. If the object cannot be pretty printed, :func:`repr` is used.
 346      See the documentation of :mod:`IPython.lib.pretty` for details on
 347      how to write pretty printers.  Here is a simple example::
 348  
 349          def dtype_pprinter(obj, p, cycle):
 350              if cycle:
 351                  return p.text('dtype(...)')
 352              if hasattr(obj, 'fields'):
 353                  if obj.fields is None:
 354                      p.text(repr(obj))
 355                  else:
 356                      p.begin_group(7, 'dtype([')
 357                      for i, field in enumerate(obj.descr):
 358                          if i > 0:
 359                              p.text(',')
 360                              p.breakable()
 361                          p.pretty(field)
 362                      p.end_group(7, '])')
 363      rwTFrzwTruncate large collections (lists, dicts, tuples, sets) to this size.
 364  
 365          Set to 0 to disable truncation.
 366          rZ
 367  _repr_pretty_�O�
 368  �%r��float_precisionc
 369  Cs|d}d|vr$|}z|dWnlty#}ztd|�|�d}~ww|rlzt|�}|dks1J�Wn%tyE}ztd|�|�d}~wtyW}ztd|�|�d}~wwd|}d	tjvrkddl}|j|d
 370  �nd}d	tjvr}ddl}|jdd
 371  �||_dS)
 372  a�float_precision changed, set float_format accordingly.
 373  
 374          float_precision can be set by int or str.
 375          This will set float_format, after interpreting input.
 376          If numpy has been imported, numpy print precision will also be set.
 377  
 378          integer `n` sets format to '%.nf', otherwise, format set directly.
 379  
 380          An empty string returns to defaults (repr for float, 8 for numpy).
 381  
 382          This parameter can be set via the '%precision' magic.
 383          r#�%gn���!	@z.Precision must be int or format string, not %rNrz*int precision must be non-negative, not %rz%%.%if�numpy)�	precisionr��)	re�
 384  ValueError�int�AssertionErrorrl�modulesr�Zset_printoptions�float_format)rr'r#�fmtrf�ir�r r r!�_float_precision_changed�s<�����
 385  �
 386  
 387  z+PlainTextFormatter._float_precision_changedr�cC�
 388  tj��Sr)r
 389  Z_singleton_pprinters�copyrr r r!�_singleton_printers_default�r3z.PlainTextFormatter._singleton_printers_defaultr�csBtj��}�fdd�|t<dtjvrddl}�fdd�||j<|S)Nc�|��j|�Sr��textr��rX�p�cyclerr r!�<lambda>��z;PlainTextFormatter._type_printers_default.<locals>.<lambda>r�rcr�rr�r�rr r!r��r�)r
 390  Z_type_pprintersr��floatrlr�r�Zfloat64)rrBr�r rr!�_type_printers_default�s
 391  
 392  z)PlainTextFormatter._type_printers_defaultr�cCr�r)r
 393  Z_deferred_type_pprintersr�rr r r!�_deferred_printers_default�r3z-PlainTextFormatter._deferred_printers_defaultc
 394  CsV|jst|�St�}tj||j|j|j|j|j	|j
 395  |jd�}|�|�|��|�
 396  �S)z0Compute the pretty representation of the object.)�max_seq_lengthZsingleton_pprintersZtype_pprintersZdeferred_pprinters)�pprintrdrr
 397  ZRepresentationPrinter�verbose�	max_width�newliner�r�r�r��flush�getvalue)rrX�streamr�r r r!rx�s
 398  �
 399  zPlainTextFormatter.__call__N)r_r`rarirrArrbr&r
 400  r
 401  ZMAX_SEQ_LENGTHr�rr�r�r�r�r�r�rr�rr�rr�r�r�rvrxr r r r!r7ws4��
 402  .
 403  
 404  
 405  
 406  r7c@� eZdZdZed�Zed�ZdS)r8a�An HTML formatter.
 407  
 408      To define the callables that compute the HTML representation of your
 409      objects, define a :meth:`_repr_html_` method or use the :meth:`for_type`
 410      or :meth:`for_type_by_name` methods to register functions that handle
 411      this.
 412  
 413      The return value of this formatter should be a valid HTML snippet that
 414      could be injected into an existing DOM. It should *not* include the
 415      ```<html>`` or ```<body>`` tags.
 416      z	text/html�_repr_html_N�r_r`rarirrArr�r r r r!r8�r8c@r�)r9aQA Markdown formatter.
 417  
 418      To define the callables that compute the Markdown representation of your
 419      objects, define a :meth:`_repr_markdown_` method or use the :meth:`for_type`
 420      or :meth:`for_type_by_name` methods to register functions that handle
 421      this.
 422  
 423      The return value of this formatter should be a valid Markdown.
 424      z
 425  text/markdown�_repr_markdown_Nr�r r r r!r9s	r9c@r�)r:a�An SVG formatter.
 426  
 427      To define the callables that compute the SVG representation of your
 428      objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type`
 429      or :meth:`for_type_by_name` methods to register functions that handle
 430      this.
 431  
 432      The return value of this formatter should be valid SVG enclosed in
 433      ```<svg>``` tags, that could be injected into an existing DOM. It should
 434      *not* include the ```<html>`` or ```<body>`` tags.
 435      z
 436  image/svg+xml�
 437  _repr_svg_Nr�r r r r!r:&r�r:c@�(eZdZdZed�Zed�Zee	fZ
 438  dS)r;aXA PNG formatter.
 439  
 440      To define the callables that compute the PNG representation of your
 441      objects, define a :meth:`_repr_png_` method or use the :meth:`for_type`
 442      or :meth:`for_type_by_name` methods to register functions that handle
 443      this.
 444  
 445      The return value of this formatter should be raw PNG data, *not*
 446      base64 encoded.
 447      z	image/png�
 448  _repr_png_N�r_r`rarirrArr��bytesr�r�r r r r!r;7�
 449  
 450  r;c@r�)r=a\A JPEG formatter.
 451  
 452      To define the callables that compute the JPEG representation of your
 453      objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type`
 454      or :meth:`for_type_by_name` methods to register functions that handle
 455      this.
 456  
 457      The return value of this formatter should be raw JPEG data, *not*
 458      base64 encoded.
 459      z
 460  image/jpeg�_repr_jpeg_Nr�r r r r!r=Ir�r=c@r�)r>a�A LaTeX formatter.
 461  
 462      To define the callables that compute the LaTeX representation of your
 463      objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type`
 464      or :meth:`for_type_by_name` methods to register functions that handle
 465      this.
 466  
 467      The return value of this formatter should be a valid LaTeX equation,
 468      enclosed in either ```$```, ```$$``` or another LaTeX equation
 469      environment.
 470      z
 471  text/latex�_repr_latex_Nr�r r r r!r>[r�r>cs8eZdZdZed�ZeefZe	d�Z
 472  �fdd�Z�ZS)r?a�A JSON string formatter.
 473  
 474      To define the callables that compute the JSONable representation of
 475      your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type`
 476      or :meth:`for_type_by_name` methods to register functions that handle
 477      this.
 478  
 479      The return value of this formatter should be a JSONable list or dict.
 480      JSON scalars (None, number, string) are not allowed, only dict or list containers.
 481      zapplication/json�_repr_json_csV|durdSd}t|t�r|\}}t|t�rJd��|dur"||f}tt|��||�S)r�Nz4JSON-as-string has been deprecated since IPython < 3)rUrVr��superr?rk)rrtrXrY�r�r r!rk|s
 482  ��zJSONFormatter._check_return)
 483  r_r`rarirrAr\�dictr�rr�rk�
 484  __classcell__r r r�r!r?ls
 485  r?c@r�)r@a�A Javascript formatter.
 486  
 487      To define the callables that compute the Javascript representation of
 488      your objects, define a :meth:`_repr_javascript_` method or use the
 489      :meth:`for_type` or :meth:`for_type_by_name` methods to register functions
 490      that handle this.
 491  
 492      The return value of this formatter should be valid Javascript code and
 493      should *not* be enclosed in ```<script>``` tags.
 494      zapplication/javascriptZ_repr_javascript_Nr�r r r r!r@�s
 495  r@c@r�)r<aXA PDF formatter.
 496  
 497      To define the callables that compute the PDF representation of your
 498      objects, define a :meth:`_repr_pdf_` method or use the :meth:`for_type`
 499      or :meth:`for_type_by_name` methods to register functions that handle
 500      this.
 501  
 502      The return value of this formatter should be raw PDF data, *not*
 503      base64 encoded.
 504      zapplication/pdfZ
 505  _repr_pdf_Nr�r r r r!r<�r�r<c@s0eZdZdZed�Zed�efZe	dd��Z
 506  dS)r1a�An escape-hatch Formatter for objects that know how to display themselves.
 507  
 508      To define the callables that compute the representation of your
 509      objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type`
 510      or :meth:`for_type_by_name` methods to register functions that handle
 511      this. Unlike mime-type displays, this method should not return anything,
 512      instead calling any appropriate display methods itself.
 513  
 514      This display formatter has highest priority.
 515      If it fires, no other display formatter will be called.
 516  
 517      Prior to IPython 6.1, `_ipython_display_` was the only way to display custom mime-types
 518      without registering a new Formatter.
 519  
 520      IPython 6.1 introduces `_repr_mimebundle_` for displaying custom mime-types,
 521      so `_ipython_display_` should only be used for objects that require unusual
 522      display patterns, such as multiple display calls.
 523      Z_ipython_display_NcCsZ|jr)z|�|�}Wn	tyYnw||�dSt||j�}|dur+|�dSdSdS)r�TNr�r�r r r!rx�s��z IPythonDisplayFormatter.__call__)r_r`rarirr�r��boolr�rvrxr r r r!r1�sr1cs:eZdZdZed�ZeZ�fdd�Ze	ddd��Z
 524  �ZS)	r5a
 525  A Formatter for arbitrary mime-types.
 526  
 527      Unlike other `_repr_<mimetype>_` methods,
 528      `_repr_mimebundle_` should return mime-bundle data,
 529      either the mime-keyed `data` dictionary or the tuple `(data, metadata)`.
 530      Any mime-type is valid.
 531  
 532      To define the callables that compute the mime-bundle representation of your
 533      objects, define a :meth:`_repr_mimebundle_` method or use the :meth:`for_type`
 534      or :meth:`for_type_by_name` methods to register functions that handle
 535      this.
 536  
 537      .. versionadded:: 6.1
 538      �_repr_mimebundle_cs8tt|��||�}|duriifSt|t�s|ifS|Sr)r�r5rkrUrVr�r�r r!rk�s
 539  z!MimeBundleFormatter._check_returnNcCsV|jr)z
 540  |�|�}W||�StyYnwt||j�}|dur'|||d�SdSdS)z�Compute the format for an object.
 541  
 542          Identical to parent's method but we pass extra parameters to the method.
 543  
 544          Unlike other _repr_*_ `_repr_mimebundle_` should allow extra kwargs, in
 545          particular `include` and `exclude`.
 546          NrFr�)rrXrGrHr�rqr r r!rx�s	��zMimeBundleFormatter.__call__r^)r_r`rarirr�r�r�rkrvrxr�r r r�r!r5�s	r5cCs ddlm}|��j�|||�S)a�Return a format data dict for an object.
 547  
 548      By default all format types will be computed.
 549  
 550      Parameters
 551      ----------
 552      obj : object
 553          The Python object whose format data will be computed.
 554  
 555      Returns
 556      -------
 557      format_dict : dict
 558          A dictionary of key/value pairs, one or each format that was
 559          generated for the object. The keys are the format types, which
 560          will usually be MIME type strings and the values and JSON'able
 561          data structure containing the raw data for the representation in
 562          that format.
 563      include : list or tuple, optional
 564          A list of format type strings (MIME types) to include in the
 565          format data dict. If this is set *only* the format types included
 566          in this list will be computed.
 567      exclude : list or tuple, optional
 568          A list of format type string (MIME types) to exclude in the format
 569          data dict. If this is set all format types will be computed,
 570          except for those included in this argument.
 571      r)�InteractiveShell)Zinteractiveshellr��instance�display_formatterr[)rXrGrHr�r r r!�format_display_data!s
 572  �r�r^)9riryrlror��iorrZtraitlets.config.configurablerZ
 573  getipythonrZutils.sentinelrZ
 574  utils.dir2r	�libr
 575  Z	traitletsrrr
 576  rrrrrrr�typingrrrg�UserWarningrhrv�ABCMetar+r�r�r_r�r�r7r8r9r:r;r=r>r?r@r<r1r5�registerr�r r r r!�<module>stB0/
 577  
 578  �&)
 579  6
 580  
 581  
 582  
 583  
 584  
 585  
 586  
 587  
 588  
 589  
 590  
 591