Cheetah.Compiler module

Compiler classes for Cheetah: ModuleCompiler aka ‘Compiler’ ClassCompiler MethodCompiler

If you are trying to grok this code start with ModuleCompiler.__init__, ModuleCompiler.compile, and ModuleCompiler.__getattr__.

class Cheetah.Compiler.AutoClassCompiler(className, mainMethodName='respond', moduleCompiler=None, fileName=None, settingsManager=None)

Bases: Cheetah.Compiler.ClassCompiler

class Cheetah.Compiler.AutoMethodCompiler(methodName, classCompiler, initialMethodComment=None, decorators=None)

Bases: Cheetah.Compiler.MethodCompiler

addMethArg(name, defVal=None)
addStop(expr=None)
cleanupState()
isClassMethod()
isStaticMethod()
methodSignature()
class Cheetah.Compiler.ClassCompiler(className, mainMethodName='respond', moduleCompiler=None, fileName=None, settingsManager=None)

Bases: Cheetah.Compiler.GenUtils

addAttribute(attribExpr)
addChunkToInit(chunk)
addClassDocString(line)
addDecorator(decoratorExpr)

Set the decorator to be used with the next method in the source.

See _spawnMethodCompiler() and MethodCompiler for the details of how this is used.

addErrorCatcherCall(codeChunk, rawCode='', lineCol='')
addSuper(argsList, parserComment=None)
attributes()
classDef()
classDocstring()
className()
classSignature()
cleanupState()
closeBlock()
closeDef()
methodCompilerClass

alias of AutoMethodCompiler

methodCompilerClassForInit

alias of MethodCompiler

methodDefs()
setBaseClass(baseClassName)
setClassName(name)
setMainMethodArgs(argsList)
setMainMethodName(methodName)
setting(key)
startMethodDef(methodName, argsList, parserComment)
wrapClassDef()
Cheetah.Compiler.Compiler

alias of ModuleCompiler

exception Cheetah.Compiler.Error

Bases: Exception

class Cheetah.Compiler.GenUtils

Bases: object

An abstract baseclass for the Compiler classes that provides methods that perform generic utility functions or generate pieces of output code from information passed in by the Parser baseclass. These methods don’t do any parsing themselves.

addGetTextVar(nameChunks)

Output something that gettext can recognize.

This is a harmless side effect necessary to make gettext work when it is scanning compiled templates for strings marked for translation.

@@TR: another marginally more efficient approach would be to put the output in a dummy method that is never called.

genCacheInfo(cacheTokenParts)

Decipher a placeholder cachetoken

genCacheInfoFromArgList(argList)
genCheetahVar(nameChunks, plain=False)
genNameMapperVar(nameChunks)

Generate valid Python code for a Cheetah $var, using NameMapper (Unified Dotted Notation with the SearchList).

nameChunks = list of var subcomponents represented as tuples
[ (name,useAC,remainderOfExpr), ]
where:
name = the dotted name base useAC = where NameMapper should use autocalling on namemapperPart remainderOfExpr = any arglist, index, or slice

If remainderOfExpr contains a call arglist (e.g. ‘(1234)’) then useAC is False, otherwise it defaults to True. It is overridden by the global setting ‘useAutocalling’ if this setting is False.

EXAMPLE:

if the raw Cheetah Var is
  $a.b.c[1].d().x.y.z

nameChunks is the list
  [ ('a.b.c',True,'[1]'), # A
    ('d',False,'()'),     # B
    ('x.y.z',True,''),    # C
  ]

When this method is fed the list above it returns:

VFN(VFN(VFFSL(SL, 'a.b.c',True)[1], 'd',False)(), 'x.y.z',True)

which can be represented as:

VFN(B`, name=C[0], executeCallables=(useAC and C[1]))C[2]

where:

VFN = NameMapper.valueForName
VFFSL = NameMapper.valueFromFrameOrSearchList
VFSL = NameMapper.valueFromSearchList # optionally used instead of VFFSL
SL = self.searchList()
useAC = self.setting('useAutocalling') # True in this example

A = ('a.b.c',True,'[1]')
B = ('d',False,'()')
C = ('x.y.z',True,'')

C` = VFN( VFN( VFFSL(SL, 'a.b.c',True)[1],
               'd',False)(),
          'x.y.z',True)
   = VFN(B`, name='x.y.z', executeCallables=True)

B` = VFN(A`, name=B[0], executeCallables=(useAC and B[1]))B[2]
A` = VFFSL(SL, name=A[0], executeCallables=(useAC and A[1]))A[2]

Note, if the compiler setting useStackFrames=False (default is true) then:

A` = VFSL([locals()]+SL+[globals(), builtin], name=A[0], executeCallables=(useAC and A[1]))A[2]

This option allows Cheetah to be used with Psyco, which doesn’t support stack frame introspection.

genPlainVar(nameChunks)

Generate Python code for a Cheetah $var without using NameMapper (Unified Dotted Notation with the SearchList).

genTimeInterval(timeString)
class Cheetah.Compiler.MethodCompiler(methodName, classCompiler, initialMethodComment=None, decorators=None)

Bases: Cheetah.Compiler.GenUtils

addAssert(expr)
addBreak(expr)
addChunk(chunk)
addClosure(functionName, argsList, parserComment)
addContinue(expr)
addDel(expr)
addEcho(expr, rawExpr=None)
addElif(expr, dedent=True, lineCol=None)
addElse(expr, dedent=True, lineCol=None)
addExcept(expr, dedent=True, lineCol=None)
addFilteredChunk(chunk, filterArgs=None, rawExpr=None, lineCol=None)
addFinally(expr, dedent=True, lineCol=None)
addFor(expr, lineCol=None)
addIf(expr, lineCol=None)

For a full #if ... #end if directive

addInclude(sourceExpr, includeFrom, isRaw)
addIndentingDirective(expr, lineCol=None)
addMethComment(comm)
addMethDocString(line)
addOneLineIf(expr, lineCol=None)

For a full #if ... #end if directive

addPSP(PSP)
addPass(expr)
addPlaceholder(expr, filterArgs, rawPlaceholder, cacheTokenParts, lineCol, silentMode=False)
addRaise(expr)
addRawText(text)
addReIndentingDirective(expr, dedent=True, lineCol=None)
addRepeat(expr, lineCol=None)
addReturn(expr)
addSet(expr, exprComponents, setStyle)
addSilent(expr)
addStrConst(strConst)
addTernaryExpr(conditionExpr, trueExpr, falseExpr, lineCol=None)

For a single-lie #if ... then .... else ... directive <condition> then <trueExpr> else <falseExpr>

addTry(expr, lineCol=None)
addUnless(expr, lineCol=None)
addWhile(expr, lineCol=None)
addWriteChunk(chunk)
addYield(expr)
appendToPrevChunk(appendage)
cleanupState()

Called by the containing class compiler instance

closeFilterBlock()
commitStrConst()

Add the code for outputting the pending strConst without chopping off any whitespace from it.

dedent()
docString()
endCacheRegion()
endCallRegion(regionTitle='CALL')
endCaptureRegion()
handleWSBeforeDirective()

Truncate the pending strCont to the beginning of the current line.

indent()
indentation()
isErrorCatcherOn()
methodBody()
methodDef()
methodName()
methodSignature()
nextCacheID()
nextCallRegionID()
nextCaptureRegionID()
nextFilterRegionID()
setCallArg(argName, lineCol)
setErrorCatcher(errorCatcherName)
setFilter(theFilter, isKlass)
setMethodName(name)
setMethodSignature(signature)
setTransform(transformer, isKlass)
setting(key)
startCacheRegion(cacheInfo, lineCol, rawPlaceholder=None)
startCallRegion(functionName, args, lineCol, regionTitle='CALL')
startCaptureRegion(assignTo, lineCol)
turnErrorCatcherOff()
turnErrorCatcherOn()
wrapCode()
class Cheetah.Compiler.ModuleCompiler(source=None, file=None, moduleName='DynamicallyCompiledCheetahTemplate', mainClassName=None, mainMethodName=None, baseclassName=None, extraImportStatements=None, settings=None)

Bases: Cheetah.SettingsManager.SettingsManager, Cheetah.Compiler.GenUtils

addAttribute(attribName, expr)
addComment(comm)
addImportStatement(impStatement)
addImportedVarNames(varNames, raw_statement=None)
addModuleDocString(line)

Adds a line to the generated module docstring.

addModuleGlobal(line)

Adds a line of global module code. It is inserted after the import statements and Cheetah default module constants.

addModuleHeader(line)

Adds a header comment to the top of the generated module.

addSpecialVar(basename, contents, includeUnderscores=True)

Adds module __specialConstant__ to the module globals.

classCompilerClass

alias of AutoClassCompiler

classDefs()
compile()
getModuleCode()
getModuleEncoding()
importStatements()
importedVarNames()
moduleConstants()
moduleDocstring()
moduleFooter()
moduleHeader()
parserClass

alias of _HighLevelParser

setBaseClass(baseClassName)
setCompilerSetting(key, valueExpr)
setCompilerSettings(keywords, settingsStr)
setModuleEncoding(encoding)
setShBang(shBang)
specialVars()
timestamp(theTime=None)
wrapModuleDef()