lfEdZddlZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z dd l m Z dd l m Z dd lmZdd lmZdd lmZdZGddeZGddeZGddeZGddeZGddeZGddeZGddeZGddeZGdd eZdS)!zKAPI and implementations for loading templates from different data sources. N)sha1)path) ModuleType)abcfspath) iteritems) string_types)TemplateNotFound) internalcode)open_if_existscg}|dD]f}tj|vs*tjrtj|vs|tjkrt ||r|dkr||g|S)zSplit a path into segments and perform a sanity check. If it detects '..' in the path it will raise a `TemplateNotFound` error. /.)splitrsepaltseppardirr append)templatepiecespieces n/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib/python3.11/site-packages/jinja2/loaders.pysplit_template_pathrsF$$!! H     $ u 4 4 ##"8,, ,  !u|| MM% Mc:eZdZdZdZdZdZeddZdS) 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 file(path) as f: source = f.read().decode('utf-8') return source, path, lambda: mtime == getmtime(path) Tcf|jstd|jjzt |)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 unicode string or a ASCII bytestring. 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&%s cannot provide access to the source)has_source_access RuntimeError __class____name__r )self environmentrs r get_sourcezBaseLoader.get_sourceGs>$% 84>;RR 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_templates_sGHHHrNcHd}|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%nameglobalsr.sourcefilenameuptodatebccbuckets rloadzBaseLoader.loades ?G&*__[$%G%G"(( ?^^KxHHF;D <&&vtX>>D ?v{2FK NN6 " " ")33 w   rN) r# __module__ __qualname____doc__r r&r*r r:rrrr&sg<)))0III $ $ $ \$ $ $ rrc&eZdZdZddZdZdZdS) FileSystemLoadera#Loads templates from the file system. This loader can find templates in folders on the file system and is the preferred way to load them. The loader takes the path to the templates as string, or if multiple locations are wanted a list of them which is then looked up in the given order:: >>> loader = FileSystemLoader('/path/to/templates') >>> loader = FileSystemLoader(['/path/to/templates', '/other/path']) Per default the template encoding is ``'utf-8'`` which can be changed by setting the `encoding` parameter to something else. To follow symbolic links, set the *followlinks* parameter to ``True``:: >>> loader = FileSystemLoader('/path/to/templates', followlinks=True) .. versionchanged:: 2.8 The ``followlinks`` parameter was added. utf-8Fct|tjrt|tr|g}d|D|_||_||_dS)Nc,g|]}t|Sr?r.0ps r z-FileSystemLoader.__init__..s9996!99999r) isinstancerIterabler searchpathencoding followlinks)r$rKrLrMs r__init__zFileSystemLoader.__init__sa*cl33 &z  8 8  &%J:9j999  &rc t|}|jD]}tj|g|Rt }|& ||j}|n#|wxYwtj   fd}||fcSt|)NcX tjkS#t$rYdSwxYwNFrgetmtimeOSErrorr6mtimesrr7z-FileSystemLoader.get_source..uptodate?!=22e;;!!! 55!  ))) rrKrjoinrreaddecoderLcloserSr ) r$r%rrrKfcontentsr7r6rVs @@rr&zFileSystemLoader.get_sources$X../ 0 0Jy5f555Hx((Ay 6688??4=99  M(++E ! ! ! ! ! ! Xx/ / / /x(((s ,BBct}|jD]}tj||j}|D]\}}}|D]}tj||t|dtjj  tjj d}|dddkr |dd}||vr| |t|S)N)rMr./) setrKoswalkrMrrYlenstriprreplaceaddsorted) r$foundrKwalk_dirdirpath_ filenamesr6rs rr*zFileSystemLoader.list_templatess/ , ,Jwzt7GHHHH)1 , ,%I ) , ,H Wh77J8I8IJrw{++ c22  |t++#+ABB<u,, (+++ , ,e}}rN)rBFr#r<r=r>rNr&r*r?rrrArAsP* ' ' ' '))).rrAc&eZdZdZddZdZdZdS) PackageLoadera,Load templates from python eggs or packages. It is constructed with the name of the python package and the path to the templates in that package:: loader = PackageLoader('mypackage', 'views') If the package path is not given, ``'templates'`` is assumed. Per default the template encoding is ``'utf-8'`` which can be changed by setting the `encoding` parameter to something else. Due to the nature of eggs it's only possible to reload templates if the package was loaded from the file system and not a zip file. templatesrBcddlm}ddlm}ddlm}||}||_||_t |||_||_||_ dS)Nr)DefaultProvider) get_provider)ResourceManager) pkg_resourcesrtrurvrLmanagerrIfilesystem_boundprovider package_path)r$ package_namer{rLrtrurvrzs rrNzPackageLoader.__init__s111111......111111< --  &(( *8_ E E  (rct|}d|jft|z}|j|st |dx}|jr:|j|j |tj fd}|j |j |}| |j|fS)NrcX tjkS#t$rYdSwxYwrQrRrUsrr7z*PackageLoader.get_source..uptodaterWrX)rrYr{tuplerz has_resourcer ryget_resource_filenamerxrrSget_resource_stringr[rL) r$r%rrrGr7r5r6rVs @@rr&zPackageLoader.get_sources$X.. HHd')E&MM9 : :}))!,, -"8,, ,""8   !}::4<KKHM(++E ! ! ! ! ! ! 224<CC}}T]++Xx??rcj}|dddkr |dd}n|dkrd}t|gfd|S)Nr`rarcj|D]`}|dz|z}j|r |0|ddadS)Nr)rzresource_listdirresource_isdirrlstrip)rr6fullname_walkoffsetresultsr$s rrz+PackageLoader.list_templates.._walks M::4@@ B B#:0=//99BE(OOOONN8FGG#4#;#;C#@#@AAAA  B Br)r{resort)r$rrrrs` @@@rr*zPackageLoader.list_templates s  8t  8DD S[[DT B B B B B B B B d  rN)rrrBror?rrrqrqsS   ) ) ) )@@@,rrqc$eZdZdZdZdZdZdS) DictLoaderaLoads a template from a python dict. It's passed a dict of unicode strings bound to template names. This loader is useful for unittesting: >>> loader = DictLoader({'index.html': 'source here'}) Because auto reloading is rarely useful this is disabled per default. c||_dSr;)mapping)r$rs rrNzDictLoader.__init__,  rcfjvrjdfdfSt)Nc@jkSr;)rget)r$r5rsrz'DictLoader.get_source..2s4<3C3CH3M3M)Mr)rr )r$r%rr5s` `@rr&zDictLoader.get_source/sI t| # #\(+F4!M!M!M!M!M!MM Mx(((rc*t|jSr;)rirr)s rr*zDictLoader.list_templates5sdl###rNror?rrrr#sK))) $$$$$rrceZdZdZdZdZdS)FunctionLoaderaA loader that is passed a function which does the loading. The function receives the name of the template and has to return either an unicode 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. c||_dSr;) load_func)r$rs rrNzFunctionLoader.__init__Ks "rc||}|t|t|tr|ddfS|Sr;)rr rIr )r$r%rrvs rr&zFunctionLoader.get_sourceNsI ^^H % % :"8,, , L ) ) "tT> ! rN)r#r<r=r>rNr&r?rrrr9s<"###rrcDeZdZdZd dZdZdZed dZdZ dS) 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. rc"||_||_dSr;)r delimiter)r$rrs rrNzPrefixLoader.__init__fs "rc ||jd\}}|j|}n$#ttf$rt |wxYw||fS)Nr)rrr ValueErrorKeyErrorr )r$rprefixr3loaders r get_loaderzPrefixLoader.get_loaderjsf -#>>$.!<rNrr&r r:r*r?rrrrWs  ####---)))\)rrc<eZdZdZdZdZeddZdZdS) 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. c||_dSr;)loaders)r$rs rrNzChoiceLoader.__init__rrc|jD]*} |||cS#t$rY'wxYwt|r;)rr&r )r$r%rrs rr&zChoiceLoader.get_sourcesbl  F ((h?????#    x(((s # 00Nc|jD]+} ||||cS#t$rY(wxYwt|r;)rr:r )r$r%r3r4rs rr:zChoiceLoader.loadsbl  F {{;g>>>>>#    t$$$s $ 11ct}|jD])}||*t |Sr;)rbrupdater*ri)r$rjrs rr*zChoiceLoader.list_templatessIl 2 2F LL..00 1 1 1 1e}}rr;) r#r<r=r>rNr&r r:r*r?rrrrsk  )))%%%\%rrceZdZdZdS)_TemplateModulez9Like a normal module but with support for weak referencesN)r#r<r=r>r?rrrrsCCCCrrc`eZdZdZdZdZedZedZe ddZ dS) 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`. Fc6dt|zt}t|tjrt|t r|g}d|D|_tj|fdtj <||_ |_ dS)Nz_jinja2_module_templates_%xc,g|]}t|Sr?rrEs rrHz)ModuleLoader.__init__..s000aq 000rcDtjdSr;)sysmodulespop)xr|s rrz'ModuleLoader.__init__..s3;??<>>r) idrrIrrJr __path__weakrefproxyrrmoduler|)r$rmodr|s @rrNzModuleLoader.__init__s4r$xx? l++$ -- D,1O1O 6D004000 $+M >>>>% %  L! (rcpdt|dzS)Ntmpl_rB)rencode hexdigestr3s rget_template_keyzModuleLoader.get_template_keys-dkk'2233==????rc<t|dzS)Nz.py)rrrs rget_module_filenamez ModuleLoader.get_module_filenames,,T22U::rNcZ||}|jd|}t|j|d}|R t |dddg}n#t $rt |wxYwtj |d|j ||j |S)Nrroot) rr|getattrr __import__ ImportErrorr rrrr1from_module_dict__dict__)r$r%r3r4keyrrs rr:zModuleLoader.loads##D)) ---ss3dk6400 ; - tfX>> - - -&t,,, - KOOFD ) ) )):: w   s AA)r;) r#r<r=r>r rN staticmethodrrr r:r?rrrrs  ))),@@\@;;\;   \   rr)r>rcrrhashlibrrtypesr_compatrr r r exceptionsr utilsr rrobjectrrArqrrrrrrr?rrrs !!!!!!((((((!!!!!!"d d d d d d d d NIIIIIzIIIXGGGGGJGGGT$$$$$$$$,Z<33333:333l&&&&&:&&&RDDDDDjDDD> > > > > :> > > > > r