Rc@sidZddlZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z m Z ddlmZmZd Zd efd YZd efd YZdefdYZdefdYZdefdYZdefdYZdefdYZdefdYZdefdYZdS(s jinja2.loaders ~~~~~~~~~~~~~~ Jinja loader classes. :copyright: (c) 2010 by the Jinja Team. :license: BSD, see LICENSE for more details. iN(t ModuleType(tpath(tsha1(tTemplateNotFound(topen_if_existst internalcode(t string_typest iteritemscCsg}x~|jdD]m}tj|ksRtjrCtj|ksR|tjkrat|q|r|dkr|j|qqW|S(sSplit 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 BaseLoadercBs8eZdZeZdZdZeddZ RS(sBaseclass 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|jjnt|dS(sGet 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 cCstddS(sIterates 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 Csd}|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,eZdZddZdZdZRS(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|tr|g}nt||_||_dS(N(t isinstanceRtlistt searchpathtencoding(RR3R4((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyt__init__s cst|}x|jD]}tj||t}|dkrLqnz|jj|j}Wd|j Xtj fd}||fSWt |dS(Ncs0ytjkSWntk 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.pyRs    cCst}x|jD]}xtj|D]\}}}x|D]}tjj||t|jtjjj 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.pyRs    (RR-R.R5RR(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR0s   t PackageLoadercBs/eZdZdddZdZdZRS(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.pyR5s    cst|}dj|jft|}|jj|sLt|nd}|jr|jj |j |t j fd}n|jj |j |}|j|j|fS(NRcs0ytjkSWntk 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.pyRs   cs}j}|d dkr&|d}n|dkr;d}nt|gfd|jS(Nis./R tcshxajj|D]M}|d|}jj|rF|qj|jdqWdS(NR(RTtresource_listdirtresource_isdirRtlstrip(RR(tfullname(t_walktoffsettresultsR(s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRas  (RURCtsort(RR((RaRbRcRs2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRs       (RR-R.R5RR(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRLs   t DictLoadercBs)eZdZdZdZdZRS(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 scsHjkr8jdfdfStdS(NcsjjkS(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|jS(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 eZdZdZdZRS(sA 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|trF|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 PrefixLoadercBsGeZdZddZdZdZeddZdZ 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|jD];\}}x,|jD]}|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;eZdZdZdZeddZdZRS(sThis 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|jqWt|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.pyRxst ModuleLoadercBsMeZdZeZdZedZedZe ddZ 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`. csdt|t}t|tr7|g}n t|}||_tj|fdtj <||_ |_ dS(Ns_jinja2_module_templates_%xcstjjdS(N(tsystmodulestpopR(tx(RV(s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRhs( tidRxR1RR2t__path__tweakreftproxyRzR{tmoduleRV(RRtmod((RVs2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR5s      cCsdt|jdjS(Nttmpl_sutf-8(Rtencodet hexdigest(R%((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pytget_template_keyscCstj|dS(Ns.py(RyR(R%((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pytget_module_filenamescCs|j|}d|j|f}t|j|d}|dkryt|dddg}Wntk r~t|nXtj j |dn|j j ||j |S(Ns%s.%stroot(RRVtgetattrRRt __import__t ImportErrorRRzR{R|R#tfrom_module_dictt__dict__(RRR%R&tkeyRR((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyR,s   N( RR-R.R8RR5t staticmethodRRRRR,(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyRys  (R.RARzRttypesRRthashlibRtjinja2.exceptionsRt jinja2.utilsRRtjinja2._compatRRRtobjectRR0RLReRiRlRuRxRy(((s2/usr/lib/python2.7/site-packages/jinja2/loaders.pyt s&    e9A6)