;i]PdZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddlmZddlmZddlmZdd lmZejr dd lmZdd lmZd ed ejefdZGddZGddeZejdkrdejd ej ee!ffdZ"ndejd ej ee!ffdZ"GddeZ#GddeZ$GddeZ%GddeZ&Gdd eZ'Gd!d"eZ(Gd#d$eZ)dS)%zKAPI and implementations for loading templates from different data sources. N)abc)sha1) import_module) ModuleType)TemplateNotFound) internalcode) 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 BC:\PYTHON\MyICR_Workspace\venv\Lib\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 r cv|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)selfrr s r get_sourcezBaseLoader.get_sourceKsF(% ::&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_templatesesGHHHrNnameglobalsr 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%rr+r,r0sourcefilenameuptodatebccbuckets rloadzBaseLoader.loadks ?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 ddejedejejedffdededd fd Z d d d edej eeej gefffdZ dej efdZd S)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 searchpathos.PathLike[str]encoding followlinksr Nct|tjrt|tr|g}d|D|_||_||_dS)Nc6g|]}tj|SrHrfspath.0ps r z-FileSystemLoader.__init__..s <<z.FileSystemLoader.get_source..s(!C!Ca$q''!C!C!C!C!C!Crz not found in search z: )rNr cn tjkS#t$rYdSwxYw)NF)rrgetmtimeOSError)r6mtimesrr7z-FileSystemLoader.get_source..uptodatesE w''11U::   uu s "& 44)rrL posixpathjoinrrisfilelenropenrNreadrarDnormpath) r%rr rrLplural paths_strfcontentsr7r6rcs @@rr&zFileSystemLoader.get_sources%X../  J!~j:6:::Hw~~h''   #4?33q88VVgF !C!C4?!C!C!CCCI"IIFIIiII  (T] 3 3 3 qvvxxH                 ** $       ))(33X==s=CC"%C"ct}|jD]}tj||j}|D]\}}}|D]}tj||t|dtjj  tjj d}|dddkr |dd}||vr| |t|S)N)rOrz./) setrLrwalkrOrrergstriprreplaceaddsorted) r%foundrLwalk_dirdirpath_ filenamesr6r s rr*zFileSystemLoader.list_templatess/ , ,Jwzt7GHHHH)1 , ,%I ) , ,H Wh77J8I8IJrw{++ c22  |t++#+ABB<u,, (+++ , ,e}}r)rKF)r$r<r=r>r@Unionr?SequencerDrZrArCr&rEr*rHrrrJrJs> ! ' 'G #QZ=O8O0P%Q Q  '  '  '  ' ' ' ' >( >47 > c1:b$h// 0 > > > >Ds rrJ) zcl |j}n"#t$r}td|d}~wwxYw|SNzFThis zip import does not have the required metadata to list templates.) _get_filesAttributeErrorr()r get_fileses r_get_zipimporter_filesrsY  II   /   y{{ )$)c\ |j}n"#t$r}td|d}~wwxYw|Sr)_filesrr()rfilesrs rrrsR HEE   /    rc 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. templatesrK package_name package_pathr?rNr Nctj|tjj}|tjjkrd}n9|ddtjjtjjzkr |dd}||_||_||_t|tj |}| Jd|j }| Jd||_d|_t!|t"jrp|j|_t)t+|j}tj||tjj}ng}|jr||jn>|j7|tj|j|st9d|d|D]E} tj| |} tj| r| }nFt9d|d|d ||_dS) Nrpz-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.zPackageLoader could not find a z directory in the z package.)rrrjrstriprcurdirrrrNr importlibutil find_specloader_loader_archiverX zipimport zipimporterarchivenextitersubmodule_search_locationsreextendoriginrdirname ValueErrorisdir_template_root) r%rrrNspecrpkgdir template_rootrootsroots rrZzPackageLoader.__init__/sb w'' 55< ) )LL "1" "'+!= = ='+L((   l###~'' 55!P!!#L!!!  fi3 4 4 "NDM$t>??@@FGLL>>EEbgkRRMM!#E. ; T<====( RW__T[99:::  ;<;;;   w||D,777==&&$(ME!9l99+999 ,rrr r ctjtj|jgt |R|jtjst|td5}| }dddn #1swxYwYtj dtffd }n@ |j}n"#t $r}t||d}~wwxYwd}||j|fS)Nrbr ctjo"tjkSr;)rrrfra)rcrVsr up_to_datez,PackageLoader.get_source..up_to_dates1w~~a((IRW-=-=a-@-@E-IIr)rrrjrdrerrrrfrrhrirarDrget_datarbdecoderN) r%rr rmr5rrrcrVs @@rr&zPackageLoader.get_sourcens G   N4. O1DX1N1N O O O   = 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* B**B.1B.$C?? D DDc g}|jt|j}tj|jD]S\}}|dtjj|fd|DTnt|j }|jt|jdtjjtjjz}t|}|D]r}| |r[|dtjjkr@| ||d tjjds||S)Nc3K|]G}tj|tjjdVHdS)rN)rrrertr)rUr+rys rr_z/PackageLoader.list_templates..s[GLL$//77 SIIrr)rrgrrrrlstriprrrrr startswithrrtsort) r%resultsoffsetrzr{rprefixr+rys @rr*zPackageLoader.list_templatess! = ,--F)+1D)E)E  %I!&''*11"'+>> ) +4<88E#C $6$6$8$89@@MM'+ [[F L L??6**LtBx27;/F/FNN4=#8#8c#J#JKKK r)rrK)r$r<r=r>r?rZr@rArBrCrDr&rEr*rHrrrrsF* =,=,=,=, =,  =,=,=,=,~$;($;47$; c1:ajT&:;; <$;$;$;$;Ls rrc 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 by default. mappingr Nc||_dSr;)r)r%rs rrZzDictLoader.__init__  rrr r cfjvrjdfdfSt)Nc@jkSr;)rget)r%r5r srz'DictLoader.get_source..s4<3C3CH3M3M)Mr)rr)r%rr r5s` `@rr&zDictLoader.get_sourcesK t| # #\(+F4!M!M!M!M!M!MM Mx(((rc*t|jSr;)rvrr)s rr*zDictLoader.list_templatessdl###r)r$r<r=r>r@Mappingr?rZrArCrDr&rEr*rHrrrrs #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_funcr Nc||_dSr;)r)r%rs rrZzFunctionLoader.__init__s#rrr r c||}|t|t|tr|ddfS|Sr;)rrrXr?)r%rr rvs rr&zFunctionLoader.get_sourcesK^^H % % :"8,, , b#   "tT> ! r) r$r<r=r>r@rCr?rBr|rArDrZr&rHrrrrs" #: 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 delimiterr Nc"||_||_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@rr?rrZrArrBrCrDr&r rFrGr:rEr*rHrrrrsg  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. loadersr Nc||_dSr;)r)r%rs rrZzChoiceLoader.__init__Frrrr r c|jD]*} |||cS#t$rY'wxYwt|r;)rr&r)r%rr rs rr&zChoiceLoader.get_sourceIsdl  F ((h?????#    x(((s # 00r+r,r c|jD]+} ||||cS#t$rY(wxYwt|r;)rr:r)r%rr+r,rs rr:zChoiceLoader.loadSsdl  F {{;g>>>>>#    t$$$s $ 11ct}|jD])}||*t |Sr;)rqrupdater*rv)r%rwrs rr*zChoiceLoader.list_templatesasIl 2 2F LL..00 1 1 1 1e}}rr;)r$r<r=r>r@r}rrZr?rArBrCrDr&r rFrGr:rEr*rHrrrr8s    : 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>rHrrrrhsCCCCrrc eZdZdZdZdejedejejedffddfdZ e dedefd Z e dedefd Z e dd d ded ejejeejfddfdZdS) ModuleLoaderzThis loader loads templates from precompiled templates. Example usage: >>> loader = ModuleLoader('/path/to/compiled/templates') Templates can be precompiled with :meth:`Environment.compile_templates`. FrrMr Nc8dt|dt}t|tjrt|t r|g}d|D|_tj|fdtj <||_ |_ dS)N_jinja2_module_templates_xc6g|]}tj|SrHrRrTs rrWz)ModuleLoader.__init__..s 333 ! 333rcDtjdSr;)sysmodulespop)rrs rrz'ModuleLoader.__init__..s3;??<>>r) idrrXrrYr?__path__weakrefproxyrrmoduler)r%rmodrs @rrZzModuleLoader.__init__xs @2d88??? l++$ -- D#1F1F 6D33d333 $+M >>>>% %  L! (rr+cpdt|dzS)Ntmpl_rK)rencode hexdigestr+s rget_template_keyzModuleLoader.get_template_keys-dkk'2233==????rc<t|dzS)Nz.py)rrrs rget_module_filenamez ModuleLoader.get_module_filenames,,T22U::rrr r,r cl||}|jd|}t|j|d}|W t |dddg}n"#t $r}t ||d}~wwxYwtj |d|i}|j ||j |S)Nrr) rrgetattrr __import__ ImportErrorrrrrr3from_module_dict__dict__)r%rr+r,keyrrrs rr:zModuleLoader.loads##D))%----dk6400 ; 4 tfX>> 4 4 4&t,,!3 4 KOOFD ) ) ) ?G):: w   sA A.A))A.r;)r$r<r=r>r!r@r|r?r}rZ staticmethodrrr rBrFrGr:rHrrrrls:)g #QZ=O8O0P%Q Q )  ))))6@s@s@@@\@;#;#;;;\; =A   "  A,S!%Z89      \   rr)*r>importlib.utilrrrdrtypingr@rr collectionsrhashlibrrtypesr exceptionsrutilsr TYPE_CHECKINGrr r r?rErrrJ version_inforGDictobjectrrrrrrrrrHrrrsC  ######((((((?&((((((%%%%%%#!&+"k k k k k k k k \[[[[[z[[[|w!%AF3;,?!%AF3;,?cccccJcccL$$$$$$$$0*****Z***Z<<<<<:<<<~-----:---`DDDDDjDDDI I I I I :I I I I I r