Your IP : 3.14.131.115


Current Path : /opt/alt/python37/lib/python3.7/site-packages/jinja2/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/jinja2/__pycache__/loaders.cpython-37.opt-1.pyc

B

G؊a�X�@sNdZddlZddlZddlZddlZddlZddlZddl	m
Z
ddlmZddlm
Z
ddlmZddlmZdd	lmZdd
lmZejr�ddlmZddlmZeejed
�dd�ZGdd�d�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Z Gdd�de�Z!Gdd�de�Z"Gdd�de�Z#Gd d!�d!e�Z$dS)"zKAPI and implementations for loading templates from different data
sources.
�N)�abc)�sha1)�
import_module)�
ModuleType�)�TemplateNotFound)�internalcode)�open_if_exists)�Environment)�Template)�template�returncCshg}x^|�d�D]P}tjj|ks@tjjr4tjj|ks@|tjjkrJt|��q|r|dkr|�|�qW|S)z�Split a path into segments and perform a sanity check.  If it detects
    '..' in the path it will raise a `TemplateNotFound` error.
    �/�.)�split�os�path�sep�altsep�pardirr�append)r�piecesZpiece�r�?/opt/alt/python37/lib/python3.7/site-packages/jinja2/loaders.py�split_template_paths
rc	@s�eZdZdZdZdeejeejeejej	ge
ffd�dd�Zejed�dd	�Z
eddeejejeejfdd�d
d��Zd
S)�
BaseLoadera�Baseclass for all loaders.  Subclass this and override `get_source` to
    implement a custom loading mechanism.  The environment provides a
    `get_template` method that calls the loader's `load` method to get the
    :class:`Template` object.

    A very basic example for a loader that looks up templates on the file
    system could look like this::

        from jinja2 import BaseLoader, TemplateNotFound
        from os.path import join, exists, getmtime

        class MyLoader(BaseLoader):

            def __init__(self, path):
                self.path = path

            def get_source(self, environment, template):
                path = join(self.path, template)
                if not exists(path):
                    raise TemplateNotFound(template)
                mtime = getmtime(path)
                with open(path) as f:
                    source = f.read()
                return source, path, lambda: mtime == getmtime(path)
    Tr
)�environmentrr
cCs&|jstt|�j�d���t|��dS)a�Get the template source, filename and reload helper for a template.
        It's passed the environment and template name and has to return a
        tuple in the form ``(source, filename, uptodate)`` or raise a
        `TemplateNotFound` error if it can't locate the template.

        The source part of the returned tuple must be the source of the
        template as a string. The filename should be the name of the
        file on the filesystem if it was loaded from there, otherwise
        ``None``. The filename is used by Python for the tracebacks
        if no loader extension is used.

        The last item in the tuple is the `uptodate` function.  If auto
        reloading is enabled it's always called to check if the template
        changed.  No arguments are passed so the function must store the
        old state somewhere (for example in a closure).  If it returns `False`
        the template will be reloaded.
        z$ cannot provide access to the sourceN)�has_source_access�RuntimeError�type�__name__r)�selfrrrrr�
get_sourceJszBaseLoader.get_source)r
cCstd��dS)z�Iterates over all templates.  If the loader does not support that
        it should raise a :exc:`TypeError` which is the default behavior.
        z-this loader cannot iterate over all templatesN)�	TypeError)r!rrr�list_templatesdszBaseLoader.list_templatesNr)r�name�globalsr
c
Cs�d}|dkri}|�||�\}}}|j}|dk	rF|�||||�}	|	j}|dkr\|�|||�}|dk	r~|	jdkr~||	_|�|	�|j�||||�S)acLoads a template.  This method looks up the template in the cache
        or loads one by calling :meth:`get_source`.  Subclasses should not
        override this method as loaders working on collections of other
        loaders (such as :class:`PrefixLoader` or :class:`ChoiceLoader`)
        will not call this method but `get_source` directly.
        N)r"Zbytecode_cacheZ
get_bucket�code�compileZ
set_bucket�template_classZ	from_code)
r!rr%r&r'�source�filename�uptodateZbccZbucketrrr�loadjs

zBaseLoader.load)N)r �
__module__�__qualname__�__doc__r�str�t�Tuple�Optional�Callable�boolr"�Listr$r�MutableMapping�Anyr-rrrrr)s0rc@s�eZdZdZdejeejej	ejeejffee
dd�dd�Zdeejeeej
ge
ffd	�d
d�Zejed�d
d�ZdS)�FileSystemLoaderaLoad templates from a directory in the file system.

    The path can be relative or absolute. Relative paths are relative to
    the current working directory.

    .. code-block:: python

        loader = FileSystemLoader("templates")

    A list of paths can be given. The directories will be searched in
    order, stopping at the first matching template.

    .. code-block:: python

        loader = FileSystemLoader(["/override/templates", "/default/templates"])

    :param searchpath: A path, or list of paths, to the directory that
        contains the templates.
    :param encoding: Use this encoding to read the text from template
        files.
    :param followlinks: Follow symbolic links in the path.

    .. versionchanged:: 2.8
        Added the ``followlinks`` parameter.
    �utf-8FN)�
searchpath�encoding�followlinksr
cCs<t|tj�rt|t�r|g}dd�|D�|_||_||_dS)NcSsg|]}t�|��qSr)r�fspath)�.0�prrr�
<listcomp>�sz-FileSystemLoader.__init__.<locals>.<listcomp>)�
isinstancer�Iterabler1r<r=r>)r!r<r=r>rrr�__init__�s
zFileSystemLoader.__init__r
)rrr
c	s�t|�}xz|jD]p}tjj|f|���t��}|dkr8qz|���|j�}Wd|�	�Xtj�
���td���fdd�}|�|fSWt|��dS)N)r
cs*ytj����kStk
r$dSXdS)NF)rr�getmtime�OSErrorr)r+�mtimerrr,�sz-FileSystemLoader.get_source.<locals>.uptodate)
rr<rr�joinr	�read�decoder=�closerFr6r)r!rrrr<�f�contentsr,r)r+rHrr"�s
zFileSystemLoader.get_source)r
c	Cs�t�}x�|jD]�}tj||jd�}x~|D]v\}}}xj|D]b}tj�||�t|�d��tjj	��
tjj	d�}|dd�dkr�|dd�}||kr8|�|�q8Wq(WqWt|�S)N)r>r�z./)
�setr<r�walkr>rrI�len�stripr�replace�add�sorted)	r!�foundr<Zwalk_dir�dirpath�_�	filenamesr+rrrrr$�s

zFileSystemLoader.list_templates)r;F)r r.r/r0r2�Unionr1r�PathLike�Sequencer6rEr3r5r"r7r$rrrrr:�s(	$r:c	@sheZdZdZdededd�dd�Zd	eejeeejej	ge
ffd
�dd�Zejed
�dd�Z
dS)�
PackageLoaderalLoad templates from a directory in a Python package.

    :param package_name: Import name of the package that contains the
        template directory.
    :param package_path: Directory within the imported package that
        contains the templates.
    :param encoding: Encoding of template files.

    The following example looks up templates in the ``pages`` directory
    within the ``project.ui`` package.

    .. code-block:: python

        loader = PackageLoader("project.ui", "pages")

    Only packages installed as directories (standard pip behavior) or
    zip/egg files (less common) are supported. The Python API for
    introspecting data in packages is too limited to support other
    installation methods the way this loader requires.

    There is limited support for :pep:`420` namespace packages. The
    template directory is assumed to only be in one namespace
    contributor. Zip files contributing to a namespace are not
    supported.

    .. versionchanged:: 3.0
        No longer uses ``setuptools`` as a dependency.

    .. versionchanged:: 3.0
        Limited PEP 420 namespace package support.
    �	templates�utf-8r1N)�package_name�package_pathr=r
c
Csltj�|��tjj�}|tjjkr(d}n(|dd�tjjtjjkrP|dd�}||_||_||_t	|�t
j�|�}|dk	s�t
d��|j}|dk	s�t
d��||_d|_d}t|tj�r�|j|_tt|j��}tj�||�}njg}|jr�|�|j�n |jdk	�r|�tj�|j��x0|D](}	tj�|	|�}	tj�|	��r|	}P�qW|dk�rbtd|�d���||_dS)N�rOz-An import spec was not found for the package.z'A loader was not found for the package.zThe zC package was not installed in a way that PackageLoader understands.) rr�normpath�rstripr�curdirrbrar=r�	importlib�util�	find_spec�AssertionError�loader�_loader�_archiverC�	zipimport�zipimporter�archive�next�iter�submodule_search_locationsrI�extend�originr�dirname�isdir�
ValueError�_template_root)
r!rarbr=�specrkZ
template_rootZpkgdir�roots�rootrrrrEsD

zPackageLoader.__init__r
)rrr
c
s�tjj|jft|����|jdkrvtj���s6t|��t�d��}|�	�}WdQRXtj�
���td���fdd�}nBy|j�
��}Wn,tk
r�}zt|�|�Wdd}~XYnXd}|�|j��|fS)N�rb)r
cstj���otj����kS)N)rr�isfilerFr)rHrArr�
up_to_dateVsz,PackageLoader.get_source.<locals>.up_to_date)rrrIryrrmr~r�openrJrFr6rl�get_datarGrKr=)r!rrrMr*r�er)rHrArr"Fs
zPackageLoader.get_source)r
csg}|jdkrdt|j�}x�t�|j�D]8\�}}�|d��tjj��|��fdd�|D��q&Wn�t	|j
d�sxtd��|jt|j�d��tjj�tjj}t|�}xL|j
j�
�D]<}|�|�r�|dtjjkr�|�||d��tjjd��q�W|��|S)Nc3s(|] }tj��|��tjjd�VqdS)rN)rrrIrTr)r@r%)rXrr�	<genexpr>qsz/PackageLoader.list_templates.<locals>.<genexpr>�_fileszFThis zip import does not have the required metadata to list templates.���r)rmrRryrrQ�lstriprrrt�hasattrrlr#r��keys�
startswithrrT�sort)r!Zresults�offsetrYrZ�prefixr%r)rXrr$gs&



"zPackageLoader.list_templates)r_r`)r r.r/r0r1rEr2r3r4r5r6r"r7r$rrrrr^�s7*r^c@sfeZdZdZejeefdd�dd�Zdeejedej	ge
ffd�dd	�Zejed
�dd�Z
dS)
�
DictLoaderaLoads a template from a Python dict mapping template names to
    template source.  This loader is useful for unittesting:

    >>> loader = DictLoader({'index.html': 'source here'})

    Because auto reloading is rarely useful this is disabled per default.
    N)�mappingr
cCs
||_dS)N)r�)r!r�rrrrE�szDictLoader.__init__r
)rrr
cs6��jkr*�j���d���fdd�fSt���dS)Ncs��j���kS)N)r��getr)r!r*rrr�<lambda>��z'DictLoader.get_source.<locals>.<lambda>)r�r)r!rrr)r!r*rrr"�s

zDictLoader.get_source)r
cCs
t|j�S)N)rVr�)r!rrrr$�szDictLoader.list_templates)r r.r/r0r2�Mappingr1rEr3r5r6r"r7r$rrrrr��s
$r�c@s�eZdZdZejegejejeej	eejeejejge
ffffdd�dd�Zdeej	eejeejejge
ffd�dd	�ZdS)
�FunctionLoadera�A loader that is passed a function which does the loading.  The
    function receives the name of the template and has to return either
    a string with the template source, a tuple in the form ``(source,
    filename, uptodatefunc)`` or `None` if the template does not exist.

    >>> def load_template(name):
    ...     if name == 'index.html':
    ...         return '...'
    ...
    >>> loader = FunctionLoader(load_template)

    The `uptodatefunc` is a function that is called if autoreload is enabled
    and has to return `True` if the template is still up to date.  For more
    details have a look at :meth:`BaseLoader.get_source` which has the same
    return value.
    N)�	load_funcr
cCs
||_dS)N)r�)r!r�rrrrE�szFunctionLoader.__init__r
)rrr
cCs2|�|�}|dkrt|��t|t�r.|ddfS|S)N)r�rrCr1)r!rr�rvrrrr"�s


zFunctionLoader.get_source)
r r.r/r0r2r5r1r4r[r3r6rEr"rrrrr��s0r�c	@s�eZdZdZdejeefedd�dd�Zeej	eefd�dd	�Z
d
eej	eejeejejge
ffd�dd
�Zedd
eejejeejfdd�dd��Zejed�dd�ZdS)�PrefixLoaderaA loader that is passed a dict of loaders where each loader is bound
    to a prefix.  The prefix is delimited from the template by a slash per
    default, which can be changed by setting the `delimiter` argument to
    something else::

        loader = PrefixLoader({
            'app1':     PackageLoader('mypackage.app1'),
            'app2':     PackageLoader('mypackage.app2')
        })

    By loading ``'app1/index.html'`` the file from the app1 package is loaded,
    by loading ``'app2/index.html'`` the file from the second.
    rN)r��	delimiterr
cCs||_||_dS)N)r�r�)r!r�r�rrrrE�szPrefixLoader.__init__)rr
c
CsZy |�|jd�\}}|j|}Wn0ttfk
rP}zt|�|�Wdd}~XYnX||fS)Nr)rr�r�rx�KeyErrorr)r!rr�r%rkr�rrr�
get_loader�szPrefixLoader.get_loaderr
)rrr
c
CsL|�|�\}}y|�||�Stk
rF}zt|�|�Wdd}~XYnXdS)N)r�r"r)r!rrrkr%r�rrrr"�s
zPrefixLoader.get_sourcer)rr%r&r
c
CsN|�|�\}}y|�|||�Stk
rH}zt|�|�Wdd}~XYnXdS)N)r�r-r)r!rr%r&rkZ
local_namer�rrrr-�s
zPrefixLoader.load)r
cCsFg}x<|j��D].\}}x$|��D]}|�||j|�q"WqW|S)N)r��itemsr$rr�)r!�resultr�rkrrrrr$s
zPrefixLoader.list_templates)r)N)r r.r/r0r2r�r1rrEr3r�r4r5r6r"rr8r9r-r7r$rrrrr��s
	0		r�c	@s�eZdZdZejedd�dd�Zdeej	eej
eej
ejgeffd�dd	�Z
eddeej
ejeejfd
d�dd
��Zejed�dd�ZdS)�ChoiceLoadera�This loader works like the `PrefixLoader` just that no prefix is
    specified.  If a template could not be found by one loader the next one
    is tried.

    >>> loader = ChoiceLoader([
    ...     FileSystemLoader('/path/to/user/templates'),
    ...     FileSystemLoader('/path/to/system/templates')
    ... ])

    This is useful if you want to allow users to override builtin templates
    from a different location.
    N)�loadersr
cCs
||_dS)N)r�)r!r�rrrrEszChoiceLoader.__init__r
)rrr
c	Cs>x0|jD]&}y|�||�Stk
r,YqXqWt|��dS)N)r�r"r)r!rrrkrrrr" s
zChoiceLoader.get_sourcer)rr%r&r
c	Cs@x2|jD](}y|�|||�Stk
r.YqXqWt|��dS)N)r�r-r)r!rr%r&rkrrrr-*s
zChoiceLoader.load)r
cCs,t�}x|jD]}|�|���qWt|�S)N)rPr��updater$rV)r!rWrkrrrr$8szChoiceLoader.list_templates)N)r r.r/r0r2r]rrEr1r3r4r5r6r"rr8r9r-r7r$rrrrr�s0r�c@seZdZdZdS)�_TemplateModulez9Like a normal module but with support for weak referencesN)r r.r/r0rrrrr�?sr�c@s�eZdZdZdZejeej	ej
ejeej	ffdd�dd�Zeeed�dd	��Z
eeed�d
d��Zeddeejejeejfd
d�dd��ZdS)�ModuleLoadera6This loader loads templates from precompiled templates.

    Example usage:

    >>> loader = ChoiceLoader([
    ...     ModuleLoader('/path/to/compiled/templates'),
    ...     FileSystemLoader('/path/to/templates')
    ... ])

    Templates can be precompiled with :meth:`Environment.compile_templates`.
    FN)rr
csndt|�d���t��}t|tj�r.t|t�r4|g}dd�|D�|_t�|�fdd��t	j
�<||_�|_dS)NZ_jinja2_module_templates_�xcSsg|]}t�|��qSr)rr?)r@rArrrrB^sz)ModuleLoader.__init__.<locals>.<listcomp>cstj��d�S)N)�sys�modules�pop)r�)rarrr�ar�z'ModuleLoader.__init__.<locals>.<lambda>)
�idr�rCrrDr1�__path__�weakref�proxyr�r��modulera)r!r�modr)rarrERszModuleLoader.__init__)r%r
cCsdt|�d����S)NZtmpl_zutf-8)r�encodeZ	hexdigest)r%rrr�get_template_keyjszModuleLoader.get_template_keycCst�|�dS)Nz.py)r�r�)r%rrr�get_module_filenamensz ModuleLoader.get_module_filenamer
r)rr%r&r
c
Cs�|�|�}|j�d|��}t|j|d�}|dkr�yt|dddg�}Wn,tk
rp}zt|�|�Wdd}~XYnXtj�	|d�|dkr�i}|j
�||j|�S)Nrr|)
r�ra�getattrr��
__import__�ImportErrorrr�r�r�r)Zfrom_module_dict�__dict__)r!rr%r&�keyr�r�r�rrrr-rs
zModuleLoader.load)N)r r.r/r0rr2r[r1rr\r]rE�staticmethodr�r�rr4r8r9r-rrrrr�Cs"r�)%r0�importlib.utilrgrr��typingr2r�rn�collectionsrZhashlibrr�typesr�
exceptionsrZutilsrr	Z
TYPE_CHECKINGrr
rr1r7rrr:r^r�r�r�r�r�r�rrrr�<module>s6nS"-?0

?>