ó ÈÏRc@sidZddlZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z m Z ddlmZmZd „Zd efd „ƒYZd efd „ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdS(s  jinja2.loaders ~~~~~~~~~~~~~~ Jinja loader classes. :copyright: (c) 2010 by the Jinja Team. :license: BSD, see LICENSE for more details. iÿÿÿÿN(t ModuleType(tpath(tsha1(tTemplateNotFound(topen_if_existst internalcode(t string_typest iteritemscCs‹g}x~|jdƒD]m}tj|ksRtjrCtj|ksR|tjkrat|ƒ‚q|r|dkr|j|ƒqqW|S(s‰Split a path into segments and perform a sanity check. If it detects '..' in the path it will raise a `TemplateNotFound` error. t/t.(tsplitRtseptaltseptpardirRtappend(ttemplatetpiecestpiece((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pytsplit_template_pathst BaseLoadercBs8eZdZeZd„Zd„Zedd„ƒZ RS(sÍ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 file(path) as f: source = f.read().decode('utf-8') return source, path, lambda: mtime == getmtime(path) cCs2|js"td|jjƒ‚nt|ƒ‚dS(sË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 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. s&%s cannot provide access to the sourceN(thas_source_accesst RuntimeErrort __class__t__name__R(tselft environmentR((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyt get_sourceFs cCstdƒ‚dS(s”Iterates over all templates. If the loader does not support that it should raise a :exc:`TypeError` which is the default behavior. s-this loader cannot iterate over all templatesN(t TypeError(R((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pytlist_templates]sc Csàd}|dkri}n|j||ƒ\}}}|j}|dk ro|j||||ƒ} | j}n|dkr“|j|||ƒ}n|dk rÇ| jdkrÇ|| _|j| ƒn|jj||||ƒS(scLoads 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( tNoneRtbytecode_cachet get_buckettcodetcompilet set_bucketttemplate_classt from_code( RRtnametglobalsR tsourcetfilenametuptodatetbcctbucket((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pytloadcs       N( Rt __module__t__doc__tTrueRRRRRR,(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR%s   tFileSystemLoadercBs,eZdZdd„Zd„Zd„ZRS(s=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. sutf-8cCs7t|tƒr|g}nt|ƒ|_||_dS(N(t isinstanceRtlistt searchpathtencoding(RR3R4((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyt__init__™s csµt|ƒ}x–|jD]‹}tj||Œ‰tˆƒ}|dkrLqnz|jƒj|jƒ}Wd|j ƒXtj ˆƒ‰‡‡fd†}|ˆ|fSWt |ƒ‚dS(Ncs0ytjˆƒˆkSWntk r+tSXdS(N(RtgetmtimetOSErrortFalse((R(tmtime(s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR)¬s ( RR3RtjoinRRtreadtdecodeR4tcloseR6R(RRRRR3tftcontentsR)((R(R9s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRŸs    cCsÔtƒ}x¾|jD]³}xªtj|ƒD]™\}}}x‡|D]}tjj||ƒt|ƒjtjjƒj tjjdƒ}|d dkr¢|d}n||kr?|j |ƒq?q?Wq)WqWt |ƒS(NRis./( tsetR3tostwalkRR:tlentstripR treplacetaddtsorted(RtfoundR3tdirpathtdirnamest filenamesR(R((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR´s    (RR-R.R5RR(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR0Šs   t PackageLoadercBs/eZdZddd„Zd„Zd„ZRS(s,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. t templatessutf-8cCseddlm}m}m}||ƒ}||_|ƒ|_t||ƒ|_||_||_ dS(Niÿÿÿÿ(tDefaultProvidertResourceManagert get_provider( t pkg_resourcesRNRORPR4tmanagerR1tfilesystem_boundtprovidert package_path(Rt package_nameRUR4RNRORPRT((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR5Òs    csÌt|ƒ}dj|jft|ƒƒ}|jj|ƒsLt|ƒ‚nd‰}|jr›|jj |j |ƒ‰t j ˆƒ‰‡‡fd†}n|jj |j |ƒ}|j|jƒˆ|fS(NRcs0ytjˆƒˆkSWntk r+tSXdS(N(RR6R7R8((R(R9(s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR)çs (RR:RUttupleRTt has_resourceRRRStget_resource_filenameRRRR6tget_resource_stringR<R4(RRRRtpR)R'((R(R9s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRÝs   cs}ˆj}|d dkr&|d}n|dkr;d}nt|ƒ‰g‰‡‡‡‡fd†‰ˆ|ƒˆjƒˆS(Nis./R tcshxaˆjj|ƒD]M}|d|}ˆjj|ƒrFˆ|ƒqˆj|ˆjdƒƒqWdS(NR(RTtresource_listdirtresource_isdirRtlstrip(RR(tfullname(t_walktoffsettresultsR(s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRaøs  (RURCtsort(RR((RaRbRcRs2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRðs       (RR-R.R5RR(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRLÃs   t DictLoadercBs)eZdZd„Zd„Zd„ZRS(sLoads 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. cCs ||_dS(N(tmapping(RRf((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR5 scsHˆˆjkr8ˆjˆ‰ˆd‡‡‡fd†fStˆƒ‚dS(NcsˆˆjjˆƒkS(N(Rftget((RR'R(s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyts(RfRR(RRR((RR'Rs2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRs cCs t|jƒS(N(RGRf(R((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRs(RR-R.R5RR(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRes  tFunctionLoadercBs eZdZd„Zd„ZRS(s¼A loader that is passed a function which does the loading. The function becomes the name of the template passed 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. cCs ||_dS(N(t load_func(RRj((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR5,scCsJ|j|ƒ}|dkr*t|ƒ‚nt|tƒrF|ddfS|S(N(RjRRR1R(RRRtrv((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR/s   (RR-R.R5R(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRis t PrefixLoadercBsGeZdZdd„Zd„Zd„Zedd„ƒZd„Z RS(sA 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. RcCs||_||_dS(N(Rft delimiter(RRfRm((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR5Gs cCs\y,|j|jdƒ\}}|j|}Wn#ttfk rQt|ƒ‚nX||fS(Ni(R RmRft ValueErrortKeyErrorR(RRtprefixR%tloader((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyt get_loaderKs cCsM|j|ƒ\}}y|j||ƒSWntk rHt|ƒ‚nXdS(N(RrRR(RRRRqR%((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRSs  cCsP|j|ƒ\}}y|j|||ƒSWntk rKt|ƒ‚nXdS(N(RrR,R(RRR%R&Rqt local_name((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR,\s  cCsYg}xLt|jƒD];\}}x,|jƒD]}|j||j|ƒq/WqW|S(N(RRfRRRm(RtresultRpRqR((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRfs  N( RR-R.R5RrRRRR,R(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRl8s     t ChoiceLoadercBs;eZdZd„Zd„Zedd„ƒZd„ZRS(sª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. cCs ||_dS(N(tloaders(RRv((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR5|scCsLx9|jD].}y|j||ƒSWq tk r7q Xq Wt|ƒ‚dS(N(RvRR(RRRRq((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRs  cCsOx<|jD]1}y|j|||ƒSWq tk r:q Xq Wt|ƒ‚dS(N(RvR,R(RRR%R&Rq((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR,‡s  cCs:tƒ}x$|jD]}|j|jƒƒqWt|ƒS(N(R@RvtupdateRRG(RRHRq((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRs N( RR-R.R5RRRR,R(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRuns    t_TemplateModulecBseZdZRS(s9Like a normal module but with support for weak references(RR-R.(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRx—st ModuleLoadercBsMeZdZeZd„Zed„ƒZed„ƒZe dd„ƒZ RS(s6This 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`. cs„dt|ƒ‰tˆƒ}t|tƒr7|g}n t|ƒ}||_tj|‡fd†ƒtj ˆ<||_ ˆ|_ dS(Ns_jinja2_module_templates_%xcstjjˆdƒS(N(tsystmodulestpopR(tx(RV(s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRh·s( tidRxR1RR2t__path__tweakreftproxyRzR{tmoduleRV(RRtmod((RVs2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR5ªs      cCsdt|jdƒƒjƒS(Nttmpl_sutf-8(Rtencodet hexdigest(R%((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pytget_template_key¿scCstj|ƒdS(Ns.py(RyR‡(R%((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pytget_module_filenameÃscCs®|j|ƒ}d|j|f}t|j|dƒ}|dkr•yt|dddgƒ}Wntk r~t|ƒ‚nXtj j |dƒn|j j ||j |ƒS(Ns%s.%stroot(R‡RVtgetattrR‚Rt __import__t ImportErrorRRzR{R|R#tfrom_module_dictt__dict__(RRR%R&tkeyR‚Rƒ((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR,Çs   N( RR-R.R8RR5t staticmethodR‡RˆRRR,(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRy›s  (R.RARzR€ttypesRRthashlibRtjinja2.exceptionsRt jinja2.utilsRRtjinja2._compatRRRtobjectRR0RLReRiRlRuRxRy(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyt s&    e9A6)