https://t.me/RX1948
Server : Apache
System : Linux iad1-shared-b8-43 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
User : dh_edsupp ( 6597262)
PHP Version : 8.2.26
Disable Function : NONE
Directory :  /lib/python3/dist-packages/trac/mimeview/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/trac/mimeview/__pycache__/api.cpython-310.pyc
o

�k�`��@sndZddlZddlZddlZddlmZddlmZmZm	Z	ddl
mZmZm
Z
mZmZddlmZddlmZmZddlmZmZmZdd	lmZmZdd
lmZmZgd�ZGdd
�d
e �Z!idd�dd�dd�dd�dd�dd�dd�dd�dd�d d!�d"d#�d$d%�d&d'�d(d)�d*d+�d,d-�d.d/�id0d1�d2d3�d4d5�d6d7�d8d9�d:d;�d<d=�d>d?�d@dA�dBdC�dDdE�dFdG�dHdI�dJdK�dLdM�dNdO�dPdQ��idRdS�dTdU�dVdW�dXdY�dZd[�d\d]�d^d_�d`da�dbdc�ddde�dfdg�dhdi�djdk�dldm�dndo�dpdq��Z"e"D]Z#e"e#�$�Z%e#�&dr��re%�'e#e(dr�d��e%e"e#<q�dsZ)e)�$�D]Z*e"�+dte*g��'e*��q"iZ,e"�-�D]\Z#Z.e#e,e#<e,�/dudv�e.D���q7e�0dw�1dx�ej2�Z3de,ifdydz�Z4d{d|�Z5d}d~�Z6dd��Z7d�d��Z8Gd�d��d�e
�Z9Gd�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�Z?Gd�d��d�e�Z@Gd�d��d�e�ZAdS)�a	
File metadata management
------------------------

The `trac.mimeview` package centralizes the intelligence related to
file metadata, principally concerning the `type` (MIME type) of the
content and, if relevant, concerning the text encoding (charset) used
by the content.

There are primarily two approaches for getting the MIME type of a
given file, either taking advantage of existing conventions for the
file name, or examining the file content and applying various
heuristics.

The module also knows how to convert the file content from one type to
another type.

In some cases, only the `url` pointing to the file's content is
actually needed, that's why we avoid to read the file's content when
it's not needed.

The actual `content` to be converted might be a `str` object, but
it can also be the raw byte string (`bytes`) object, or simply an object
that can be `read()`.

.. note:: (for plugin developers)

  The Mimeview API is quite complex and many things there are
  currently a bit difficult to work with (e.g. what an actual
  `content` might be, see the last paragraph of this description).

  So this area is mainly in a ''work in progress'' state, which will
  be improved along the lines described in :teo:`#3332`.

  In particular, if you are interested in writing `IContentConverter`
  and `IHTMLPreviewRenderer` components, note that those interfaces
  will be merged into a new style `IContentConverter`.  Feel free to
  contribute remarks and suggestions for improvements to the
  corresponding ticket (#3332 as well).
�N)�
namedtuple)�	IntOption�
ListOption�Option)�	Component�ExtensionPoint�	Interface�	TracError�
implements)�Resource)�Ranges�content_disposition)�Fragment�Markup�tag)�exception_to_unicode�
to_unicode)�_�tag_)�Mimeview�RenderingContext�get_mimetype�	is_binary�detect_unicode�content_to_unicode�ct_mimetypec@sZeZdZdZddd�Zdd�Zddd	�ZeZd
d�Zdd
�Z	ddd�Z
dd�Zdd�ZdS)ra�A rendering context specifies ''how'' the content should be rendered.

    It holds together all the needed contextual information that will be
    needed by individual renderer components.

    To that end, a context keeps track of the Href instance
    (``.href``) which should be used as a base for building URLs.

    It also provides a `PermissionCache` (``.perm``) which can be used
    to restrict the output so that only the authorized information is
    shown.

    A rendering context may also be associated to some Trac resource which
    will be used as the implicit reference when rendering relative links
    or for retrieving relative content and can be used to retrieve related
    metadata.

    Rendering contexts can be nested, and a new context can be created from
    an existing context using the call syntax. The previous context can be
    retrieved using the ``.parent`` attribute.

    For example, when rendering a wiki text of a wiki page, the context will
    be associated to a resource identifying that wiki page.

    If that wiki text contains a `[[TicketQuery]]` wiki macro, the macro will
    set up nested contexts for each matching ticket that will be used for
    rendering the ticket descriptions.

    :since: version 1.0

    NcCs2d|_||_||_|r|r||�n||_d|_dS)aDirectly create a `RenderingContext`.

        :param resource: the associated resource
        :type resource: `Resource`
        :param href: an `Href` object suitable for creating URLs
        :param perm: a `PermissionCache` object used for restricting the
                     generated output to "authorized" information only.

        The actual `.perm` attribute of the rendering context will be bound
        to the given `resource` so that fine-grained permission checks will
        apply to that.
        N)�parent�resource�href�perm�_hints)�selfrrr�r"�3/usr/lib/python3/dist-packages/trac/mimeview/api.py�__init__rs


zRenderingContext.__init__cCsJg}|}|r|jjr|�t|j��|j}|sdt|�jd�t|��fS)Nz<%s %s>z - )	r�realm�append�reprr�type�__name__�join�reversed)r!�path�contextr"r"r#�__repr__�s�zRenderingContext.__repr__FcCsJ|rt||||d�}n|j}t||j|jd�}||_t|d�r#|j|_|S)a�Create a nested rendering context.

        `self` will be the parent for the new nested context.

        :param resource: either a `Resource` object or the realm string for a
                         resource specification to be associated to the new
                         context. If `None`, the resource will be the same
                         as the resource of the parent context.
        :param id: the identifier part of the resource specification
        :param version: the version of the resource specification
        :return: the new context object
        :rtype: `RenderingContext`

        >>> context = RenderingContext('wiki', 'WikiStart')
        >>> ticket1 = Resource('ticket', 1)
        >>> context.child('ticket', 1).resource == ticket1
        True
        >>> context.child(ticket1).resource is ticket1
        True
        >>> context.child(ticket1)().resource is ticket1
        True
        )�id�versionr)rr�req)rrrrrr�hasattrr1)r!rr/r0rr-r"r"r#�child�s�
zRenderingContext.childcCs@|}|r|jr|jj|jkr|jj|jkrdS|j}|sdSdS)a6Check whether a resource is in the rendering path.

        The primary use for this check is to avoid to render the content of a
        resource if we're already embedded in a context associated to that
        resource.

        :param resource: a `Resource` specification which will be checked for
        TN)rr%r/r)r!rr-r"r"r#�__contains__�s	�zRenderingContext.__contains__cKs<|jduri|_|��}|dur|j�|�|j�|�dS)aQSet rendering hints for this rendering context.

        >>> ctx = RenderingContext('timeline')
        >>> ctx.set_hints(wiki_flavor='oneliner', shorten_lines=True)
        >>> t_ctx = ctx('ticket', 1)
        >>> t_ctx.set_hints(wiki_flavor='html', preserve_newlines=True)
        >>> (t_ctx.get_hint('wiki_flavor'), t_ctx.get_hint('shorten_lines'),              t_ctx.get_hint('preserve_newlines'))
        ('html', True, True)
        >>> (ctx.get_hint('wiki_flavor'), ctx.get_hint('shorten_lines'),              ctx.get_hint('preserve_newlines'))
        ('oneliner', True, None)
        N)r �
_parent_hints�update)r!�	keyvalues�hintsr"r"r#�	set_hints�s
zRenderingContext.set_hintscCs.|j}|dur|��}|dur|S|�||�S)aSRetrieve a rendering hint from this context or an ancestor context.

        >>> ctx = RenderingContext('timeline')
        >>> ctx.set_hints(wiki_flavor='oneliner')
        >>> t_ctx = ctx('ticket', 1)
        >>> t_ctx.get_hint('wiki_flavor')
        'oneliner'
        >>> t_ctx.get_hint('preserve_newlines', True)
        True
        N)r r5�get)r!�hint�defaultr8r"r"r#�get_hint�szRenderingContext.get_hintcCs*|j}|dur|��}|durdS||vS)abTest whether a rendering hint is defined in this context or in some
        ancestor context.

        >>> ctx = RenderingContext('timeline')
        >>> ctx.set_hints(wiki_flavor='oneliner')
        >>> t_ctx = ctx('ticket', 1)
        >>> t_ctx.has_hint('wiki_flavor')
        True
        >>> t_ctx.has_hint('preserve_newlines')
        False
        NF)r r5)r!r;r8r"r"r#�has_hint�szRenderingContext.has_hintcCs2|j}|r|jdur|j}|r|jdus
|o|jS�N)rr )r!�pr"r"r#r5
s
�
zRenderingContext._parent_hints�NN)NFFFr?)
r)�
__module__�__qualname__�__doc__r$r.r3�__call__r4r9r=r>r5r"r"r"r#rQs
 
	(
rzapplication/javascript�jszapplication/mswordzdoc dotzapplication/pdf�pdfzapplication/postscript�pszapplication/rtf�rtfzapplication/x-dos-batchzbat batch cmd doszapplication/x-sh�shzapplication/x-csh�cshzapplication/x-genshi�genshizapplication/x-troffznroff roff troffzapplication/x-yamlzyml yamlzapplication/rss+xml�rsszapplication/xsl+xml�xslzapplication/xslt+xml�xsltzimage/x-icon�icoz
image/svg+xml�svgz
model/vrmlzvrml wrlztext/css�cssz	text/htmlzhtml htmztext/html+jinja�jinja�
text/plainz=txt TXT text README INSTALL AUTHORS COPYING ChangeLog RELEASEztext/xml�xmlztext/x-apacheconf�apacheztext/x-csrczc xsztext/x-chdr�hz
text/x-c++srczcc CC cpp C c++ C++z
text/x-c++hdrzhh HH hpp Hz
text/x-csharpzcs c# C#ztext/x-diff�patchz
text/x-eiffel�eztext/x-elisp�elztext/x-fortran�fztext/x-haskell�hsz
text/x-inizini cfgztext/x-nginx-conf�nginxztext/x-objczm mmztext/x-ocamlzml mliztext/x-makefilezmake mk Makefile GNUMakefilez
text/x-pascal�pasztext/x-perlzpl pm PLz
text/x-phpz	php3 php4z
text/x-python�pyztext/x-python-doctest�pyconztext/x-pyrex�pyxztext/x-ruby�rbz
text/x-scheme�scmztext/x-textile�txtlz
text/x-vbaz
vb vba basztext/x-verilog�vztext/x-vhdl�vhdztext/x-zN
    ada asm asp awk idl inf java ksh lua m4 mail psp rfc rst sql tcl tex zsh
z	text/x-%sccs�|]}|tfVqdSr?)�t)�.0rYr"r"r#�	<genexpr>ds�ria3
      \#!.+?env\s+(\w+)                     # 1. look for shebang with env
    | \#!(?:[/\w.-_]+/)?(\w+)               # 2. look for regular shebang
    | -\*-\s*(?:mode:\s*)?([\w+-]+)\s*-\*-  # 3. look for Emacs' -*- mode -*-
    | vim:.*?(?:syntax|filetype|ft)=(\w+)   # 4. look for VIM's syntax=<n>
    �utf-8c
Cs|��D]
\}}|�|�r|Sq|�d�d}||vr!||Sd}z	t�|�d}Wn	ty5Ynw|s�|r�|dd�|dd�f}t|t�rRtdd�|�}d	�	|�}t
�|�}|r�t|�d
�pt|�d�pt|�d�pt|�d
��
�d�}	|	|vr||	S|St|�r�dS|S)z�Guess the most probable MIME type of a file with the given name.

    `filename` is either a filename (the lookup will then use the suffix)
    or some arbitrary keyword.

    `content` is either a `bytes` or a `str` string.
    �.���Nr��i���cSs
|�d�S�Nrj)�encode)rer"r"r#�<lambda>��
zget_mimetype.<locals>.<lambda>s

����z
iso-8859-1zapplication/octet-stream)�items�match�split�	mimetypes�
guess_type�	Exception�
isinstance�str�mapr*�MODE_RE�search�group�lowerr)
�filename�content�mime_map�mime_map_patterns�mimetype�regexp�suffix�chunkrw�moder"r"r#ros@

��


���rcCs|pd�d�d��S)z+Return the mimetype part of a content type.��;r)rx�strip)�content_typer"r"r#r�srcCsHt|t�rt|�rdS|�ddd�dkSt|t�r"|�ddd�dkSdS)z|Detect binary content by checking the first thousand bytes for zeroes.

    Operate on either `bytes` or `str` strings.
    Frrmrl�)r|�bytesr�findr}��datar"r"r#r�s

rcCs.|�d�rdS|�d�rdS|�d�rdSdS)z}Detect different unicode charsets by looking for BOMs (Byte Order Mark).

    Operate obviously only on `bytes` objects.
    s��z	utf-16-les��z	utf-16-besrjN��
startswithr�r"r"r#r�s


rcCsJt|�}t|d�r|�|j�}|�||�}|r#|ddkr#|dd�}|S)u�Retrieve an `str` object from a `content` to be previewed.

    In case the raw content had an unicode BOM, we remove it.

    >>> from trac.test import EnvironmentStub
    >>> env = EnvironmentStub()
    >>> content_to_unicode(env, "No BOM! hé !", '')
    'No BOM! h\xe9 !'
    >>> content_to_unicode(env, bytes([0xEF, 0xBB, 0xBF]) + b'No BOM! h' +
    ...                         bytes([0xC3, 0xA9]) + b' !', '')
    'No BOM! h\xe9 !'

    �readrurrN)rr2r��max_preview_sizer)�envr�r��mimeview�ur"r"r#r�s
rc@s2eZdZdZdZdZdd�Zdd�Zd
dd	�ZdS)�IHTMLPreviewRenderera�Extension point interface for components that add HTML renderers of
    specific content types to the `Mimeview` component.

    .. note::

      This interface will be merged with IContentConverter, as
      conversion to text/html will simply be a particular content
      conversion.

      Note however that the IHTMLPreviewRenderer will still be
      supported for a while through an adapter, whereas the
      IContentConverter interface itself will be changed.

      So if all you want to do is convert to HTML and don't feel like
      following the API changes, you should rather implement this
      interface for the time being.
    FcC�dS)a�Augment the Mimeview system with new mimetypes associations.

        This is an optional method. Not implementing the method or
        returning nothing is fine, the component will still be asked
        via `get_quality_ratio` if it supports a known mimetype.  But
        implementing it can be useful when the component knows about
        additional mimetypes which may augment the list of already
        mimetype to keywords associations.

        Generate ``(mimetype, keywords)`` pairs for each additional
        mimetype, with ``keywords`` being a list of keywords or
        extensions that can be used as aliases for the mimetype
        (typically file suffixes or Wiki processor keys).

        .. versionadded:: 1.0
        Nr"r"r"r"r#�get_extra_mimetypes��z(IHTMLPreviewRenderer.get_extra_mimetypescCr�)z�Return the level of support this renderer provides for the `content`
        of the specified MIME type. The return value must be a number between
        0 and 9, where 0 means no support and 9 means "perfect" support.
        Nr")r�r"r"r#�get_quality_ratior�z&IHTMLPreviewRenderer.get_quality_ratioNcCr�)a5Render an XHTML preview of the raw ``content`` in a
        `RenderingContext`.

        The `content` might be:
         * a `bytes` object
         * an `unicode` string
         * any object with a `read` method, returning one of the above

        It is assumed that the content will correspond to the given `mimetype`.

        Besides the `content` value, the same content may eventually
        be available through the `filename` or `url` parameters.
        This is useful for renderers that embed objects, using <object> or
        <img> instead of including the content inline.

        Can return the generated XHTML text as a single string or as an
        iterable that yields strings. In the latter case, the list will
        be considered to correspond to lines of text in the original content.

        Nr")r-r�r�r��urlr"r"r#�renderr�zIHTMLPreviewRenderer.renderrA)	r)rBrCrD�expand_tabs�returns_sourcer�r�r�r"r"r"r#r��sr�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�IHTMLPreviewAnnotatorz�Extension point interface for components that can annotate an XHTML
    representation of file contents with additional information.cCr�)a�Return a (type, label, description) tuple
        that defines the type of annotation and provides human readable names.
        The `type` element should be unique to the annotator.
        The `label` element is used as column heading for the table,
        while `description` is used as a display name to let the user
        toggle the appearance of the annotation type.
        Nr"r"r"r"r#�get_annotation_type"r�z)IHTMLPreviewAnnotator.get_annotation_typecCr�)z�Return some metadata to be used by the `annotate_row` method below.

        This will be called only once, before lines are processed.
        If this raises an error, that annotator won't be used.
        Nr")r-r"r"r#�get_annotation_data+r�z)IHTMLPreviewAnnotator.get_annotation_datacCr�)a�Return the XHTML markup for the table cell that contains the
        annotation data.

        `context` is the context corresponding to the content being annotated,
        `row` is the tr Element being built, `number` is the line number being
        processed and `line` is the line's actual content.
        `data` is whatever additional data the `get_annotation_data` method
        decided to provide.
        Nr")r-�row�number�liner�r"r"r#�annotate_row2r�z"IHTMLPreviewAnnotator.annotate_rowN)r)rBrCrDr�r�r�r"r"r"r#r�s
	r�c@s eZdZdZdd�Zdd�ZdS)�IContentConverterz�An extension point interface for generic MIME based content
    conversion.

    .. note:: This api will likely change in the future (see :teo:`#3332`)

    cCr�)aoReturn an iterable of tuples in the form (key, name, extension,
        in_mimetype, out_mimetype, quality) representing the MIME conversions
        supported and
        the quality ratio of the conversion in the range 0 to 9, where 0 means
        no support and 9 means "perfect" support. eg. ('latex', 'LaTeX', 'tex',
        'text/x-trac-wiki', 'text/plain', 8)Nr"r"r"r"r#�get_supported_conversionsFr�z+IContentConverter.get_supported_conversionscCr�)a0Convert the given content from mimetype to the output MIME type
        represented by key. Returns a tuple in the form (content,
        output_mime_type) or None if conversion is not possible.

        content must be a `bytes` instance or an iterable instance which
        iterates `bytes` instances.Nr")r1r�r��keyr"r"r#�convert_contentNr�z!IContentConverter.convert_contentN)r)rBrCrDr�r�r"r"r"r#r�>sr�c@s*eZdZdZdd�Zd
dd�Zdd�Zd	S)�Contentz=A lazy file-like object that only reads `input` if necessary.cCs||_||_d|_dSr?)�input�max_sizer�)r!r�r�r"r"r#r$Ys
zContent.__init__rlcCs8|dkrdS|jdurt�|j�|j��|_|j�|�S)Nrr�)r��io�BytesIOr�r�r�)r!�sizer"r"r#r�^s

zContent.readcCs|jdur
|j�d�dSdS�Nr)r��seek�r!r"r"r#�resetes
�z
Content.resetN)rl)r)rBrCrDr$r�r�r"r"r"r#r�Ws

r�c@s eZdZdZdZee�Zee�Z	ee
�Zedddd�Z
eddd	d
�Zedddd
�Zeddddd�Zeddddd�Zeddddd�Zdd�Zdd�Z		d:dd�Zd d!�Z		d;d"d#�Zd$d%�Zd<d'd(�Zed)d*��Zd=d+d,�Zed-d.��Zd>d/d0�Zd?d1d2�Zd3d4�Z 	d:d5d6�Z!d@d8d9�Z"dS)Arz6Generic HTML renderer for data, typically source code.T�trac�default_charsetrjz!Charset to be used when in doubt.�
mimeviewer�	tab_width�z$Displayed tab width in file preview.r�iz#Maximum file size for HTML preview.r�z6text/x-dylan:dylan, text/x-idl:ice, text/x-ada:ads:adbz�List of additional MIME types and keyword mappings.
        Mappings are comma-separated, and for each MIME type,
        there's a colon (":") separated list of associated keywords
        or file extensions.
        )�docr�z5text/plain:README(?!\.rst)|INSTALL(?!\.rst)|COPYING.*aList of additional MIME types associated to filename patterns.
        Mappings are comma-separated, and each mapping consists of a MIME type
        and a Python regexp used for matching filenames, separated by a colon
        (":"). (''since 1.0'')
        �treat_as_binaryzfapplication/octet-stream, application/pdf, application/postscript, application/msword, application/rtfzZComma-separated list of MIME types that should be treated as
        binary data.
        cCsd|_d|_dSr?)�	_mime_map�_mime_map_patternsr�r"r"r#r$�s
zMimeview.__init__cCs|d}td|�}g}|jD]&}|��pgD]\}}}}	}
}|	|kr1|dkr1|�|||||	|
||��qqt|dd�dd�}|S)aReturn a list of target MIME types as instances of the `namedtuple`
        `MimeConversion`. Output is ordered from best to worst quality.

        The `MimeConversion` `namedtuple` has fields: key, name, extension,
        in_mimetype, out_mimetype, quality, converter.
        )r��name�	extension�in_mimetype�out_mimetype�quality�	converter�MimeConversionrcSs|jSr?)r�)�ir"r"r#rp�sz4Mimeview.get_supported_conversions.<locals>.<lambda>T)r��reverse)r�
convertersr�r&�sorted)r!r��fields�_MimeConversionr��c�k�nrY�im�om�qr"r"r#r��s

��z"Mimeview.get_supported_conversionsNFc	s@�sdS|}|st�d�r��|j��|�|��}|r t|�}nd}}�fdd�|�|�D�}	|	s;ttd|�d���|	D]Y}
|
j�	||�|
j
�}|sLq=|\�}t�tt
f�}
|r`|
s_�f�n/|
r�t���zt���Wntywd�Ynwt�t�r�d	jndj}��fd
d�}||����||
jfSttd|�d���)z�Convert the given content to the target MIME type represented by
        `key`, which can be either a MIME type or a key. Returns a tuple of
        (content, output_mime_type, extension).)r�ztext/plain;charset=utf-8z.txtr�rTcs g|]}�|j|jfvr|�qSr")r�r�)rhr��r�r"r#�
<listcomp>�s�z,Mimeview.convert_content.<locals>.<listcomp>z5No available MIME conversions from %(old)s to %(new)s)�old�new�r�c3s��V�EdHdSr?r"r")r��firstr"r#�g�s�z#Mimeview.convert_content.<locals>.g)r2r�r�rrr�r	rr�r�r�r|r}r��iter�next�
StopIterationr*r�)r!r1r�r�r�r�r��iterable�
full_mimetype�
candidates�
conversion�outputr��is_iterabler*r�r")r�r�r�r#r��s\

��
���
��zMimeview.convert_contentccs�|jD]}|��VqdS)z6Generator that returns all available annotation types.N)�
annotatorsr�)r!�	annotatorr"r"r#�get_annotation_types�s�
�zMimeview.get_annotation_typesc
Cs�|sdS|}|st|d�r|�|j�}|�||�}|r t|�}nd}}g}	|jD]}
|
�|�}|dkr;|	�||
f�q)|	jdd�d�t|d�rOt	||j�}d}|	D]�\}}
|r`t
|
d	d
�s`qSt|t	�ri|��|rpd�
|�nd}
|j�d
|
jj|
�|}t
|
dd
�r�|dur�t|j||�}|�|j�}|}z|
�|||||�}Wn>ty�}z2|j�d|
jj|t|dd��|jr�|�d�s�ddlm}||jtd|
jjt|�d��WYd}~qSd}~ww|s�qS|�s	t
|
d	d
��s	t|t�r�t |�St|t!��r|�"�S|S|�r+|j�r|jj#�$d�nd}|�r"|j%|d�|�&|||�St|t'��r8t d��
|�}t(j)dd�t(�*|��SdS)a�Render an XHTML preview of the given `content`.

        `content` is the same as an `IHTMLPreviewRenderer.render`'s
        `content` argument.

        The specified `mimetype` will be used to select the most appropriate
        `IHTMLPreviewRenderer` implementation available for this MIME type.
        If not given, the MIME type will be infered from the filename or the
        content.

        Return a string containing the XHTML text.

        When rendering with an `IHTMLPreviewRenderer` fails, a warning is added
        to the request associated with the context (if any), unless the
        `disable_warnings` hint is set to `True`.
        r�r�rTrcSs
|dSr�r")�itemr"r"r#rprqz!Mimeview.render.<locals>.<lambda>r�Nr�Fz, zno annotationsz+Trying to render HTML preview using %s [%s]r�z(HTML preview using %s with %r failed: %sT)�	traceback�disable_warnings��add_warningz0HTML preview using %(renderer)s failed (%(err)s))�renderer�err�marks)r��
�code��class_)+r2r�r�rr�	renderersr�r&�sortr��getattrr|r�r*�log�debug�	__class__r)rr��
expandtabsr�r�r{�warningrr1r=�trac.web.chromer�rr}rr�__html__�argsr:r9�_render_source�listr�div�pre)r!r-r�r�r�r��annotations�force_sourcer�r�r��qr�expanded_content�	ann_names�rendered_content�resultrYr�r�r"r"r#r��s�



�

����
������
��zMimeview.rendercsRddlm}iii���|jD]}|��\}}}|�vr*|�|<|�|<|�|<q�fdd��D��t�t�r>��d��g��D]E}	�|	}z	||���f}
Wn0ty�}z$|j	�
d|	|�|�jt�
tdt�|	�t�|�d���d	}
WYd}~nd}~ww��|
�qB���fd
d�}���fdd
�}
tjdd�t�|��t�|
���S)Nrr�csg|]}|�vr|�qSr"r"�rh�a)r�r"r#r�Zsz+Mimeview._render_source.<locals>.<listcomp>TzCan't use annotator '%s': %sz,Can't use %(annotator)s annotator: %(error)s)r��errorrAcs*t���fdd��D�tjddd�g�S)Ncs$g|]}tj�||�|d��qS))r��title)r�thr)�labels�titlesr"r#r�ps�z>Mimeview._render_source.<locals>._head_row.<locals>.<listcomp>� r�r�)r�trrr")rrrr"r#�	_head_rowns���z*Mimeview._render_source.<locals>._head_rowc	3sn�t��D]/\}}t��}�D]\}}|r!|��||d||�q|�t���q|�t�|��|VqdS)Nrr)�	enumeraterrr�r&�td)�idxr�r�r�r�)�annotator_datasr-�linesr"r#�
_body_rowsus���z+Mimeview._render_source.<locals>._body_rowsr�r�)r�r�r�r�r|r}�
splitlinesr�r	r�r�r1r�strongr�emrr&�table�thead�tbody)r!r-rrr�r��atype�alabel�atitlerr�rYrrr")rrr�r-rrrr#rQsD
�


����


�zMimeview._render_sourcer�cCsN|r|�d�}|dkr||dd���St|t�r$t|�}|dur$|S|jS)arInfer the character encoding from the `content` or the `mimetype`.

        `content` is either a `bytes` or a `str` object.

        The charset will be determined using this order:
         * from the charset information present in the `mimetype` argument
         * auto-detection of the charset from the `content`
         * the configured `default_charset`
        zcharset=rr�N)r�r�r|r�rr�)r!r�r��ctpos�utfr"r"r#�get_charset�s


zMimeview.get_charsetcCs�|jsMt��|_|jD]!}t|d�r,|��pgD]\}}||j|<|D]}||j|<q#qq|jd�d�D]}d|vrL|�d�}|D]	}|d|j|<qBq5|jS)Nr�r�r��:r)	r��MIME_MAP�copyr�r2r��config�getlistrx)r!r�r��kwds�keyword�mapping�assocationsr"r"r#r��s"



��
�zMimeview.mime_mapcCsFt|||j|j�}d}|r|�||�}|r!|r!d|vr!|d|7}|S)aInfer the MIME type from the `filename` or the `content`.

        `content` is either a `bytes` or a `str` object.

        Return the detected MIME type, augmented by the
        charset information (i.e. "<mimetype>; charset=..."),
        or `None` if detection failed.
        N�charsetz
; charset=)rr�r�r&)r!r�r�r�r0r"r"r#r�s

�zMimeview.get_mimetypecCs�|jsEi|_|jd�d�D]6}d|vr|�dd�\}}z
t�|�|j|<WqtjyD}z|j�d||t	|��WYd}~qd}~ww|jS)Nr�r�r'rrzEmime_map_patterns contains invalid regexp '%s' for mimetype '%s' (%s))
r�r*r+rx�re�compiler
r�r�r)r!r.r�r�rYr"r"r#r��s �
���zMimeview.mime_map_patternscCsF|s
|r
|�||�}|rt|�}||jvrdS|dur!t|�r!dSdS)z-Check if a file must be considered as binary.TNF)rrr�r)r!r�r�r�r"r"r#r�s
zMimeview.is_binarycCs|s|�||�}t||�S)z�Convert `content` (an encoded `bytes` object) to a `str` object.

        This calls `trac.util.to_unicode` with the `charset` provided,
        or the one obtained by `Mimeview.get_charset()`.
        )r&r)r!r�r�r0r"r"r#r�s
zMimeview.to_unicodec
Csxid|}}|jd�|�D]*}|sqz|�d�\}}}|t|�f||<Wqttfy9|j�d||�Yqw|S)zAReturn a MIME type to `(mode,quality)` mapping for given `option`z%s_modesr�r'z.Invalid mapping '%s' specified in '%s' option.)r*r+rx�int�	TypeError�
ValueErrorr�r�)r!r��types�optionr.r�r�r�r"r"r#�configured_modes_mapping�s��z!Mimeview.configured_modes_mappingc		CsL|||jddd�}	||jkrd|	d<|	S|j|||||||d�}
|
|	d<|	S)z�Prepares a rendered preview of the given `content`.

        Note: `content` will usually be an object with a `read` method.
        FN)�raw_hrefr��
max_file_size�max_file_size_reached�renderedTr;)rr<)r�r�)r!r-r��lengthr�r�r�rrr�r
r"r"r#�preview_data�s�
��zMimeview.preview_data�filec
Cs�ddlm}ddlm}||j�j}|j|||||d�\}}	}
|r,dd�}||�}d}nt|t�r6|�	d�}t
|�}|�d	�|�d
|	�|durO|�d|�|r^|�dt
d
d||
f��|��|�|�|�)zwHelper method for converting `content` and sending it directly.

        `selector` can be either a key or a MIME Type.r)�Chrome)�RequestDone)r�css*�|D]}t|t�r|�d�}|VqdSrn)r|r}ro)r�r�r"r"r#�encoders�

�z(Mimeview.send_converted.<locals>.encoderNrj��zContent-TypezContent-LengthzContent-Disposition�
attachmentz%s.%s)r�r@�trac.web.apirAr��use_chunked_encodingr�r|r}ro�len�
send_response�send_headerr
�end_headers�write)
r!r1�in_typer��selectorr�r@rAr��output_type�extrBr=r"r"r#�send_converteds6
�



��
zMimeview.send_converted)NNF)NNNF)r�Nr?)NNNrA)r?)#r)rBrCrD�requiredrr�r�r�r�r�r�rr�rr�r�rr�r�r�r$r�r�r�r�rr&�propertyrrrr8r>rPr"r"r"r#rjsb������
�9
�h
5






�rc@s0eZdZdZee�dd�Zdd�Zdd�ZdS)	�LineNumberAnnotatorz4Text annotator that adds a column with line numbers.cCsdtd�td�fS)N�lineno�LinezLine numbers)rr�r"r"r#r�3sz'LineNumberAnnotator.get_annotation_typecCsNz	t|�d��}Wntyd}Ynw|�dd�d||�dd�dd�S)Nr�r/r�zL%srTrr)r/r��offset)rr=r5)r!r-r�r"r"r#r�6s��z'LineNumberAnnotator.get_annotation_datacCs^||d7}|d|}|dr||dvr|dd�|�tj|d�tj|d|d���dS)	NrVr/r��hiliter�)r/�#)r)r&rrr)r!r-r�rTr�r�r/r"r"r#r�As

(z LineNumberAnnotator.annotate_rowN)	r)rBrCrDr
r�r�r�r�r"r"r"r#rS-srSc@s2eZdZdZee�dZdZdd�Zddd�Z	dS)	�PlainTextRendererz�HTML preview renderer for plain text, and fallback for any kind of text
    for which no more specific renderer is available.
    TcCs|t|j�jvr
dSdS)Nrrr)rr�r��r!r�r"r"r#r�Tsz#PlainTextRenderer.get_quality_ratioNcCs2t|�r|j�d�dS|j�d�t|j||�S)Nz!Binary data; no preview availablez#Using default plain text mimeviewer)rr�r�rr��r!r-r�r�r�r�r"r"r#r�Ys
zPlainTextRenderer.renderrA)
r)rBrCrDr
r�r�r�r�r�r"r"r"r#rYKsrYc@�*eZdZdZee�dd�Zddd�ZdS)�
ImageRendererzQInline image display.

    This component doesn't need the `content` at all.
    cCs|�d�rdSdS)Nzimage/r�rr�rZr"r"r#r�is
zImageRenderer.get_quality_ratioNcCs |rtjtj||d�dd�SdS)N)�src�altz
image-filer�)rr�imgr[r"r"r#r�ns
��zImageRenderer.renderrA�r)rBrCrDr
r�r�r�r"r"r"r#r]bs
r]c@r\)�WikiTextRendererzEHTML renderer for files containing Trac's own Wiki formatting markup.cCs|dvrdSdS)N)ztext/x-trac-wikizapplication/x-trac-wikir�rr"rZr"r"r#r�xsz"WikiTextRenderer.get_quality_ratioNcCs$ddlm}||j|t|j||��S)Nr)�format_to_html)�trac.wiki.formatterrcr�r)r!r-r�r�r�r�rcr"r"r#r�}s�zWikiTextRenderer.renderrArar"r"r"r#rbts
rb)BrDr�ryr1�collectionsr�trac.configrrr�	trac.corerrrr	r
�
trac.resourcer�	trac.utilrr
�trac.util.htmlrrr�trac.util.textrr�trac.util.translationrr�__all__�objectr�KNOWN_MIME_TYPESrgrxr6r�r&rG�TEXT_X_TYPES�x�
setdefaultr(rv�extsr6r2ro�VERBOSErrrrrrr�r�r�r�rrSrYr]rbr"r"r"r#�<module>s,)E��������	�
��
������������� �!�"�#�$�%�&�'�(�)�*�+�,�-�.�/�0�1�2�3�4�5�6�7�8�9�;

�
�,J F

https://t.me/RX1948 - 2025