U*gdZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddlm Z ddlmZddlmZdd lmZdd lmZejr dd lmZdd lmZd edejefdZGddZGddeZGddeZGddeZGddeZ GddeZ!GddeZ"GddeZ#Gd d!eZ$dS)"zKAPI and implementations for loading templates from different data sources. N)abc)sha1) import_module) ModuleType)TemplateNotFound) internalcode)open_if_exists) Environment)Templatetemplatereturnc*g}|dD]z}tjj|vs9tjjrtjj|vs|tjjkrt ||r|dkr||{|S)zSplit a path into segments and perform a sanity check. If it detects '..' in the path it will raise a `TemplateNotFound` error. /.)splitospathsepaltseppardirrappend)r piecespieces /srv/buildsys-work-dir/castor/build_node/builder-2/WGSG1/unpkd_srcs/cloudlinux-venv-1.0.6/venv/lib/python3.11/site-packages/jinja2/loaders.pysplit_template_pathrsF$$!! GK5  !#%7>U#:#:&&"8,, ,  !u|| MM% Mc eZdZdZdZdddedejeejeejej ge fffdZ dej efdZ e dddd ed ejejeejfdd fd Zd S) BaseLoaderaBaseclass 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) T environmentr r rcv|js$tt|jdt |)aGet 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 source)has_source_access RuntimeErrortype__name__r)selfr r s r get_sourcezBaseLoader.get_sourceJsF(% ::&LLL x(((rc td)zIterates 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 templates) TypeErrorr&s rlist_templateszBaseLoader.list_templatesdsGHHHrNnameglobalsr cHd}|i}|||\}}}|j}||||||} | j}|||||}|#| j|| _|| |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'bytecode_cache get_bucketcodecompile set_buckettemplate_class from_code) r&r r,r-r1sourcefilenameuptodatebccbuckets rloadzBaseLoader.loadjs ?G&*__[$%G%G"(( ?^^KxHHF;D <&&vtX>>D ?v{2FK NN6 " " ")33 w   rN)r% __module__ __qualname____doc__r"strtTupleOptionalCallableboolr'Listr+r MutableMappingAnyr;rrrr)s<)()47) ajoqz!*RX2F'GG H))))4Is IIII  =A ) ) ") ) A,S!%Z89 )  ) ) ) \) ) ) rrc eZdZdZ ddejeejej ejeejffdede ddfd Z d d d edej eeej ge fffd ZdejefdZdS)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-8F searchpathencoding followlinksrNct|tjrt|tr|g}d|D|_||_||_dS)Nc6g|]}tj|SrIrfspath.0ps r z-FileSystemLoader.__init__..s <<.uptodatesE!7++H55>>!!! 55!s "& 44) rrMrrjoinr readdecoderNcloser]rEr) r&r r rrMfcontentsr8r7r_s @@rr'zFileSystemLoader.get_sources%X../ 0 0Jw|J8888Hx((Ay 6688??4=99  G$$X..E !d ! ! ! ! ! ! ! Xx/ / / /x(((s ,BBct}|jD]}tj||j}|D]\}}}|D]}tj||t|dtjj  tjj d}|dddkr |dd}||vr| |t|S)N)rOrz./) setrMrwalkrOrr`lenstriprreplaceaddsorted) r&foundrMwalk_dirdirpath_ filenamesr7r s rr+zFileSystemLoader.list_templatess/ , ,Jwzt7GHHHH)1 , ,%I ) , ,H Wh77J8I8IJrw{++ c22  |t++#+ABB<u,, (+++ , ,e}}r)rLF)r%r=r>r?rAUnionr@rPathLikeSequencerErZrBrDr'rFr+rIrrrKrKs: ! ' 'GCajbkAQ9R.SST ' ' '  ' ' ' ')()47) c1:b$h// 0))))2s rrKc eZdZdZ ddedddedd fd Zd d d edejeeejej ge fffdZ dej efdZ d S) 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. templatesrL package_name package_pathr@rNrNcjtj|tjj}|tjjkrd}n9|ddtjjtjjzkr |dd}||_||_||_t|tj |}| Jd|j }| Jd||_d|_d}t!|t"jrN|j|_t)t+|j}tj||}ng}|jr||jn>|j7|tj|j|D]E} tj| |} tj| r| }nF|t;d|d||_dS)Nrgz-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.)rrnormpathrstriprcurdirr{rzrNr importlibutil find_specloader_loader_archiverX zipimport zipimporterarchivenextitersubmodule_search_locationsr`extendoriginrdirnameisdir ValueError_template_root) r&rzr{rNspecr template_rootpkgdirrootsroots rrZzPackageLoader.__init__ s w'' 55< ) )LL "1" "'+!= = ='+L((   l###~'' 55!P!!#L!!!   fi3 4 4 "NDM$t>??@@FGLL>>MM!#E. ; T<====( RW__T[99:::  w||D,777==&&$(ME  7|777  ,rr r r cPtjj|jgt |R|jtjst|td5}| }dddn #1swxYwYtj dtffd }n@ |j }n"#t$r}t||d}~wwxYwd}||j|fS)Nrbrctjo"tjkSr<)rrisfiler])r_rVsr up_to_datez,PackageLoader.get_source..up_to_dateVs1w~~a((IRW-=-=a-@-@E-IIr)rrr`rrrrropenrar]rErget_datar^rbrN) r&r r rdr6rer_rVs @@rr'zPackageLoader.get_sourceFs GL, M/B8/L/L M M M = 7>>!$$ 1&x000a "! " " " " " " " " " " " " " " "G$$Q''E J J J J J J J J J  8..q11 8 8 8&x00a7 8 J}}T]++Q ::s*1BBB C'' D1DDczg}|jt|j}tj|jD]S\}}|dtjj|fd|DTnt|j dstd|jt|jdtjjtjjz}t|}|j j D]r}||r[|dtjjkr@|||dtjjds||S)Nc3K|]G}tj|tjjdVHdS)rN)rrr`rlr)rUr,rqs r z/PackageLoader.list_templates..ps[GLL$//77 SIIr_fileszFThis zip import does not have the required metadata to list templates.r)rrjrrrilstriprrrhasattrrr)rkeys startswithrrlsort)r&resultsoffsetrrrsprefixr,rqs @rr+zPackageLoader.list_templatesgs! = ,--F)+1D)E)E  %I!&''*11"'+>> ) 4<22 3#C $6$6$8$89@@MM'+ [[F +0022 L L??6**LtBx27;/F/FNN4=#8#8c#J#JKKK r)ryrL)r%r=r>r?r@rZrArBrCrDrEr'rFr+rIrrrxrxsF* 9,9,9,9, 9,  9,9,9,9,v;(;47; c1:ajT&:;; <;;;;B!s !!!!!!rrxc eZdZdZdejeefddfdZdddedejedej ge fffd Z dej efd 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. mappingrNc||_dSr<)r)r&rs rrZzDictLoader.__init__  rr r r cfjvrjdfdfSt)Nc@jkSr<)rget)r&r6r srz'DictLoader.get_source..s4<3C3CH3M3M)Mr)rr)r&r r r6s` `@rr'zDictLoader.get_sourcesK t| # #\(+F4!M!M!M!M!M!MM Mx(((rc*t|jSr<)rnrr*s rr+zDictLoader.list_templatessdl###r)r%r=r>r?rAMappingr@rZrBrDrEr'rFr+rIrrrrs #s( 3)()47) dAJr4x00 1))))$s $$$$$$rrc FeZdZdZdejegejejeej eejeejejge ffffddfdZ dddedej eejeejejge fffd Z dS) FunctionLoaderaA 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. load_funcrNc||_dSr<)r)r&rs rrZzFunctionLoader.__init__s#rr r r c||}|t|t|tr|ddfS|Sr<)rrrXr@)r&r r rvs rr'zFunctionLoader.get_sourcesK^^H % % :"8,, , b#   "tT> ! r) r%r=r>r?rArDr@rCrtrBrErZr'rIrrrrs" #: E Jajoqz!*RQUXBV7W!WXX    #  # # # # ( 47  ajoqz!*RX2F'GG H      rrc leZdZdZ ddejeefdeddfdZdedej eeffd Z d d dedej eej eej ej ge fffd Ze dd d d edej ejeejfddfdZdejefdZdS) 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. rr delimiterrNc"||_||_dSr<)rr)r&rrs rrZzPrefixLoader.__init__s "rr c ||jd\}}|j|}n)#ttf$r}t ||d}~wwxYw||fS)Nr)rrrrKeyErrorr)r&r rr,rrs r get_loaderzPrefixLoader.get_loadersl 4#>>$.!<r?rArr@rrZrBrrCrDrEr'r rGrHr;rFr+rIrrrrsg  EH##yj1#>A# #### 317:s?+C 4( 447 4 ajoqz!*RX2F'GG H 4 4 4 4 =A 0 0" 0 0A,S!%Z89 0  0 0 0\ 0s rrc 6eZdZdZdejeddfdZdddedej eej eej ej ge fffd Z e dddd ed ej ejeejfdd fd ZdejefdZdS) ChoiceLoaderaThis 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. loadersrNc||_dSr<)r)r&rs rrZzChoiceLoader.__init__rrr r r c|jD]*} |||cS#t$rY'wxYwt|r<)rr'r)r&r r rs rr'zChoiceLoader.get_source sdl  F ((h?????#    x(((s # 00r,r-r c|jD]+} ||||cS#t$rY(wxYwt|r<)rr;r)r&r r,r-rs rr;zChoiceLoader.load*sdl  F {{;g>>>>>#    t$$$s $ 11ct}|jD])}||*t |Sr<)rhrupdater+rn)r&rors rr+zChoiceLoader.list_templates8sIl 2 2F LL..00 1 1 1 1e}}rr<)r%r=r>r?rArvrrZr@rBrCrDrEr'r rGrHr;rFr+rIrrrrs    : 64)()47) ajoqz!*RX2F'GG H)))) =A % %" % %A,S!%Z89 %  % % %\ %s rrceZdZdZdS)_TemplateModulez9Like a normal module but with support for weak referencesN)r%r=r>r?rIrrrr?sCCCCrrc ,eZdZdZdZdejeej ej ejeej ffddfdZ e dedefdZ e dedefd Ze dd d ded ejejeejfdd fdZdS) 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`. FrrNc8dt|dt}t|tjrt|t r|g}d|D|_tj|fdtj <||_ |_ dS)N_jinja2_module_templates_xc6g|]}tj|SrIrRrTs rrWz)ModuleLoader.__init__..^s 333 ! 333rcDtjdSr<)sysmodulespop)rrzs rrz'ModuleLoader.__init__..as3;??<>>r) idrrXrrYr@__path__weakrefproxyrrmodulerz)r&rmodrzs @rrZzModuleLoader.__init__Rs@2d88??? l++$ -- D#1F1F 6D33d333 $+M >>>>% %  L! (rr,cpdt|dzS)Ntmpl_rL)rencode hexdigestr,s rget_template_keyzModuleLoader.get_template_keyjs-dkk'2233==????rc<t|dzS)Nz.py)rrrs rget_module_filenamez ModuleLoader.get_module_filenamens,,T22U::rr r r-r cl||}|jd|}t|j|d}|W t |dddg}n"#t $r}t ||d}~wwxYwtj |d|i}|j ||j |S)Nrr) rrzgetattrr __import__ ImportErrorrrrrr4from_module_dict__dict__)r&r r,r-keyrrrs rr;zModuleLoader.loadrs##D))%----dk6400 ; 4 tfX>> 4 4 4&t,,!3 4 KOOFD ) ) ) ?G):: w   sA A.A))A.r<)r%r=r>r?r"rArtr@rrurvrZ staticmethodrrr rCrGrHr;rIrrrrCs7  )GCajbkAQ9R.SST) ))))0@s@s@@@\@;#;#;;;\; =A   "  A,S!%Z89      \   rr)%r?importlib.utilrrrtypingrArr collectionsrhashlibrrtypesr exceptionsrutilsr r TYPE_CHECKINGr r r r@rFrrrKrxrrrrrrrIrrrs ######((((((!!!!!!?&((((((%%%%%%#!&+"k k k k k k k k \PPPPPzPPPf^^^^^J^^^B$$$$$$$$0*****Z***Z<<<<<:<<<~-----:---`DDDDDjDDDI I I I I :I I I I I r