simpletal package

Submodules

simpletal.simpleTAL module

simpleTAL Interpreter

The classes in this module implement the TAL language, expanding both XML and HTML templates.

class simpletal.simpleTAL.LexicalHandler[source]

Bases: object

Dummy lexical handdler class

class simpletal.simpleTAL.TemplateInterpreter[source]

Bases: object

tagAsText(tag_atts, singletonFlag=0)[source]

This returns a tag as text.

initialise(context, outputFile)[source]
cleanState()[source]
popProgram()[source]
pushProgram()[source]
execute(template)[source]
cmdDefine(command, args)[source]

args: [(isLocalFlag (Y/n), variableName, variablePath),…] Define variables in either the local or global context

cmdCondition(command, args)[source]

args: expression, endTagSymbol Conditionally continues with execution of all content contained by it.

cmdRepeat(command, args)[source]

args: (varName, expression, endTagSymbol) Repeats anything in the cmndList

cmdContent(command, args)[source]

args: (replaceFlag, structureFlag, expression, endTagSymbol) Expands content

cmdAttributes(command, args)[source]

args: [(attributeName, expression)] Add, leave, or remove attributes from the start tag

cmdOmitTag(command, args)[source]

args: expression Conditionally turn off tag output

cmdOutputStartTag(command, args)[source]
cmdEndTagEndScope(command, args)[source]
cmdOutput(command, args)[source]
cmdStartScope(command, args)[source]

args: (originalAttributes, currentAttributes) Pushes the current state onto the stack, and sets up the new state

cmdNoOp(command, args)[source]
cmdUseMacro(command, args)[source]

args: (macroExpression, slotParams, endTagSymbol) Evaluates the expression, if it resolves to a SubTemplate it then places the slotParams into currentSlots and then jumps to the end tag

cmdDefineSlot(command, args)[source]

args: (slotName, endTagSymbol) If the slotName is filled then that is used, otherwise the original conent is used.

class simpletal.simpleTAL.HTMLTemplateInterpreter(minimizeBooleanAtts=0)[source]

Bases: simpletal.simpleTAL.TemplateInterpreter

tagAsTextMinimizeAtts(tag_atts, singletonFlag=0)[source]

This returns a tag as text.

class simpletal.simpleTAL.Template(commands, macros, symbols, doctype=None)[source]

Bases: object

expand(context, outputFile, outputEncoding=None, interpreter=None)[source]

This method will write to the outputFile, using the encoding specified, the expanded version of this template. The context passed in is used to resolve all expressions with the template.

expandInline(context, outputFile, interpreter=None)[source]

Internally used when expanding a template that is part of a context.

getProgram()[source]

Returns a tuple of (commandList, startPoint, endPoint, symbolTable)

class simpletal.simpleTAL.SubTemplate(startRange, endRangeSymbol)[source]

Bases: simpletal.simpleTAL.Template

A SubTemplate is part of another template, and is used for the METAL implementation. The two uses for this class are:

1 - metal:define-macro results in a SubTemplate that is the macro 2 - metal:fill-slot results in a SubTemplate that is a parameter to metal:use-macro
setParentTemplate(parentTemplate)[source]
getProgram()[source]

Returns a tuple of (commandList, startPoint, endPoint, symbolTable)

class simpletal.simpleTAL.HTMLTemplate(commands, macros, symbols, doctype=None, minimizeBooleanAtts=0)[source]

Bases: simpletal.simpleTAL.Template

A specialised form of a template that knows how to output HTML

expand(context, outputFile, outputEncoding=u'utf-8', interpreter=None)[source]

This method will write to the outputFile, using the encoding specified, the expanded version of this template. The context passed in is used to resolve all expressions with the template.

expandInline(context, outputFile, interpreter=None)[source]

Ensure we use the HTMLTemplateInterpreter

class simpletal.simpleTAL.XMLTemplate(commands, macros, symbols, doctype=None)[source]

Bases: simpletal.simpleTAL.Template

A specialised form of a template that knows how to output XML

expand(context, outputFile, outputEncoding=u'utf-8', docType=None, suppressXMLDeclaration=0, interpreter=None)[source]

This method will write to the outputFile, using the encoding specified, the expanded version of this template. The context passed in is used to resolve all expressions with the template.

class simpletal.simpleTAL.TemplateCompiler[source]

Bases: object

setTALPrefix(prefix)[source]
setMETALPrefix(prefix)[source]
popTALNamespace()[source]
popMETALNamespace()[source]
tagAsText(tag_atts, singletonFlag=0)[source]

This returns a tag as text.

getTemplate()[source]
addCommand(command)[source]
addTag(tag, tagProperties={})[source]

Used to add a tag to the stack. Various properties can be passed in the dictionary as being information required by the tag. Currently supported properties are:

‘command’ - The (command,args) tuple associated with this command ‘originalAtts’ - The original attributes that include any metal/tal attributes ‘endTagSymbol’ - The symbol associated with the end tag for this element ‘popFunctionList’ - A list of functions to execute when this tag is popped

‘singletonTag’ - A boolean to indicate that this is a singleton flag
popTag(tag, omitTagFlag=0)[source]

omitTagFlag is used to control whether the end tag should be included in the output or not. In HTML 4.01 there are several tags which should never have end tags, this flag allows the template compiler to specify that these should not be output.

parseStartTag(tag, attributes, singletonElement=0)[source]
parseEndTag(tag)[source]

Just pop the tag and related commands off the stack.

parseData(data)[source]
compileCmdDefine(argument)[source]
compileCmdCondition(argument)[source]
compileCmdRepeat(argument)[source]
compileCmdContent(argument, replaceFlag=0)[source]
compileCmdReplace(argument)[source]
compileCmdAttributes(argument)[source]
compileCmdOmitTag(argument)[source]
compileMetalUseMacro(argument)[source]
compileMetalDefineMacro(argument)[source]
compileMetalFillSlot(argument)[source]
compileMetalDefineSlot(argument)[source]
exception simpletal.simpleTAL.TemplateParseException(location, errorDescription)[source]

Bases: exceptions.Exception

class simpletal.simpleTAL.HTMLTemplateCompiler[source]

Bases: simpletal.simpleTAL.TemplateCompiler, HTMLParser.HTMLParser

parseTemplate(file, encoding=u'UTF-8-SIG', minimizeBooleanAtts=0)[source]
tagAsText(tag_atts, singletonFlag=0)[source]

This returns a tag as text.

handle_startendtag(tag, attributes)[source]
handle_starttag(tag, attributes)[source]
handle_endtag(tag)[source]
handle_data(data)[source]
handle_charref(ref)[source]
handle_entityref(ref)[source]
handle_decl(data)[source]
handle_comment(data)[source]
handle_pi(data)[source]
report_unbalanced(tag)[source]
getTemplate()[source]
class simpletal.simpleTAL.XMLTemplateCompiler[source]

Bases: simpletal.simpleTAL.TemplateCompiler, xml.sax.handler.ContentHandler, xml.sax.handler.DTDHandler, simpletal.simpleTAL.LexicalHandler

parseTemplate(file)[source]
parseDOM(dom)[source]
startDTD(name, public_id, system_id)[source]
startElement(tag, attributes)[source]
endElement(tag)[source]
skippedEntity(name)[source]
characters(data)[source]
processingInstruction(target, data)[source]
comment(data)[source]
getTemplate()[source]
simpletal.simpleTAL.compileHTMLTemplate(template, inputEncoding=u'UTF-8-SIG', minimizeBooleanAtts=0)[source]

Reads the templateFile and produces a compiled template. To use the resulting template object call:

template.expand (context, outputFile)
simpletal.simpleTAL.compileXMLTemplate(template)[source]

Reads the templateFile and produces a compiled template. To use the resulting template object call:

template.expand (context, outputFile)
simpletal.simpleTAL.compileDOMTemplate(template)[source]

Traverses the DOM and produces a compiled template. To use the resulting template object call:

template.expand (context, outputFile)

simpletal.simpleTALConstants module

simpletal.simpleTALConstants.METAL_NAME_URI = u'http://xml.zope.org/namespaces/metal'

METAL namespace URI

simpletal.simpleTALConstants.TAL_NAME_URI = u'http://xml.zope.org/namespaces/tal'

TAL namespace URI

simpletal.simpleTALConstants.TAL_DEFINE = 1

Argument – [(isLocalFlag (Y/n), variableName, variablePath),…]

simpletal.simpleTALConstants.TAL_CONDITION = 2

Argument – expression, endTagSymbol

simpletal.simpleTALConstants.TAL_REPEAT = 3

Argument – (varname, expression, endTagSymbol)

simpletal.simpleTALConstants.TAL_CONTENT = 4

Argument – (replaceFlag, type, expression)

simpletal.simpleTALConstants.TAL_REPLACE = 5

Not used in byte code, only ordering.

simpletal.simpleTALConstants.TAL_ATTRIBUTES = 6

Argument – [(attributeName, expression)]

simpletal.simpleTALConstants.TAL_OMITTAG = 7

Argument – expression

simpletal.simpleTALConstants.TAL_START_SCOPE = 8

Argument – (originalAttributeList, currentAttributeList)

simpletal.simpleTALConstants.TAL_OUTPUT = 9

Argument – String to output

simpletal.simpleTALConstants.TAL_STARTTAG = 10

Argument – None

simpletal.simpleTALConstants.TAL_ENDTAG_ENDSCOPE = 11

Argument – Tag, omitTagFlag

simpletal.simpleTALConstants.TAL_NOOP = 13

Argument – None

simpletal.simpleTALConstants.METAL_USE_MACRO = 14

Argument – expression, slotParams, endTagSymbol

simpletal.simpleTALConstants.METAL_DEFINE_SLOT = 15

Argument – macroName, endTagSymbol

simpletal.simpleTALConstants.METAL_FILL_SLOT = 16

Only used for parsing

simpletal.simpleTALConstants.METAL_DEFINE_MACRO = 17

Only used for parsing

simpletal.simpleTALConstants.HTML4_VOID_ELEMENTS = frozenset([u'IMG', u'AREA', u'BASEFONT', u'FRAME', u'ISINDEX', u'META', u'PARAM', u'HR', u'BASE', u'LINK', u'BR', u'INPUT', u'COL'])

The set of elements in HTML4 that can not have end tags

Source: http://www.w3.org/TR/html401/index/elements.html

simpletal.simpleTALConstants.HTML5_VOID_ELEMENTS = frozenset([u'WBR', u'IMG', u'AREA', u'HR', u'META', u'LINK', u'KEYGEN', u'SOURCE', u'BASE', u'COMMAND', u'PARAM', u'BR', u'INPUT', u'EMBED', u'TRACK', u'COL'])

The set of elements in HTML5 that can not have end tags

Source: http://www.w3.org/TR/html-markup/syntax.html#void-element

simpletal.simpleTALConstants.HTML_FORBIDDEN_ENDTAG = frozenset([u'WBR', u'IMG', u'BASEFONT', u'ISINDEX', u'PARAM', u'BASE', u'LINK', u'SOURCE', u'BR', u'INPUT', u'EMBED', u'COL', u'AREA', u'TRACK', u'FRAME', u'KEYGEN', u'HR', u'META', u'COMMAND'])

The set of elements in HTML5 that can not have end tags

simpletal.simpleTALConstants.HTML_BOOLEAN_ATTS = frozenset([(u'SCRIPT', u'DEFER'), (u'INPUT', u'DISABLED'), (u'TEXTAREA', u'READONLY'), (u'OPTION', u'DISABLED'), (u'INPUT', u'CHECKED'), (u'OPTION', u'SELECTED'), (u'INPUT', u'READONLY'), (u'BUTTON', u'DISABLED'), (u'OPTGROUP', u'DISABLED'), (u'OBJECT', u'DECLARE'), (u'AREA', u'NOHREF'), (u'SELECT', u'DISABLED'), (u'INPUT', u'ISMAP'), (u'SELECT', u'MULTIPLE'), (u'IMG', u'ISMAP'), (u'TEXTAREA', u'DISABLED')])

Set of element – attribute pairs that can use minimized form in HTML

class simpletal.simpleTALConstants.SignalValue(info)[source]

Bases: object

Helper class to make unique values with a useful __str__

simpletal.simpleTALES module

simpleTALES Implementation

The classes in this module implement the TALES specification, used by the simpleTAL module.

exception simpletal.simpleTALES.PathNotFoundException[source]

Bases: exceptions.Exception

exception simpletal.simpleTALES.ContextContentException[source]

Bases: exceptions.Exception

This is raised when invalid content has been placed into the Context object. For example using non-ascii characters instead of Unicode strings.

exception simpletal.simpleTALES.ContextVariable(value=None)[source]

Bases: exceptions.Exception

value(currentPath=None)[source]
rawValue()[source]
exception simpletal.simpleTALES.RepeatVariable(sequence)[source]

Bases: simpletal.simpleTALES.ContextVariable

To be written

value(currentPath=None)[source]
rawValue()[source]
getCurrentValue()[source]
increment()[source]
createMap()[source]
getIndex()[source]
getNumber()[source]
getEven()[source]
getOdd()[source]
getStart()[source]
getEnd()[source]
getLowerLetter()[source]
getUpperLetter()[source]
getLowerRoman()[source]
getUpperRoman()[source]
exception simpletal.simpleTALES.IteratorRepeatVariable(sequence)[source]

Bases: simpletal.simpleTALES.RepeatVariable

getCurrentValue()[source]
increment()[source]
createMap()[source]
getLength()[source]
getEnd()[source]
exception simpletal.simpleTALES.PathFunctionVariable(func)[source]

Bases: simpletal.simpleTALES.ContextVariable

This class wraps a callable object (e.g. function) so that it can receive part of a TAL path as it’s argument.

To use this simply create a new instance of the PathFunctionVariable and then place this into the Context (see above). The path passed to the function is that part of the path not already used. For example if the function “helloFunc” is placed in the Context the path “helloFunc/an/example” results in the string “an/example” being passed to the function.

value(currentPath=None)[source]
exception simpletal.simpleTALES.CachedFuncResult(value=None)[source]

Bases: simpletal.simpleTALES.ContextVariable

This class wraps a callable object (e.g. function) so that the callable is only called once.

In normal SimpleTAL operation any function placed into a Context might be called multiple times during template expansion. To ensure that it is only called once simply wrap in the CachedFuncResult object first.

value(currentPath=None)[source]
clearCache()[source]

Clears the cache.

Use this to clear the cache between multiple template expansions if the callable should be executed once per template expansion.

class simpletal.simpleTALES.PythonPathFunctions(context)[source]

Bases: object

path(expr)[source]
string(expr)[source]
exists(expr)[source]
nocall(expr)[source]
test(*arguments)[source]
class simpletal.simpleTALES.Context(options=None, allowPythonPath=False)[source]

Bases: object

addRepeat(name, var, initialValue)[source]
removeRepeat(name)[source]
addGlobal(name, value)[source]

Adds the value to the context under name.

Value can either be a fundamental python data type or a callable object.

pushLocals()[source]
setLocal(name, value)[source]
popLocals()[source]
evaluate(expr, originalAtts=None)[source]
evaluatePython(expr)[source]
evaluatePath(expr)[source]
evaluateExists(expr)[source]
evaluateNoCall(expr)[source]
evaluateNot(expr)[source]
evaluateString(expr)[source]
traversePath(expr, canCall=1)[source]
populateDefaultVariables(options)[source]

simpletal.simpleTALUtils module

simpleTALUtils

This module is holds utilities that make using SimpleTAL easier. Initially this is just the HTMLStructureCleaner class, used to clean up HTML that can then be used as ‘structure’ content.

class simpletal.simpleTALUtils.TemplateCache[source]

Bases: object

A TemplateCache is a multi-thread safe object that caches compiled templates. This cache only works with file based templates, the mtime of the file is checked on each hit, if the file has changed the template is re-compiled.

isHTML(name)[source]
getTemplate(name, inputEncoding='UTF-8-SIG')[source]

Name should be the path of a template file. If self.isHTML(name) it is treated as an HTML Template, otherwise it’s treated as an XML Template. If the template file has changed since the last cache it will be re-compiled.

inputEncoding is only used for HTML templates, and should be the encoding that the template is stored in.

getXMLTemplate(name)[source]

Name should be the path of an XML template file.

class simpletal.simpleTALUtils.TemplateRoot(rootPath, loadFunc, templateExt='.html')[source]

Bases: object

Simple-to-use templating. Interface not yet fully stable.

expand(templatePath, options=None, addGlobals={})[source]
expandMacros(templatePath, options=None, addGlobals={})[source]
get(templatePath)[source]
resolvePath(subpath)[source]
getForContext(subpath=None)[source]
class simpletal.simpleTALUtils.MacroExpansionInterpreter[source]

Bases: simpletal.simpleTAL.TemplateInterpreter

A MacroExpansionInterpreter only expands METAL macros but does not touch TAL.

popProgram()[source]
pushProgram()[source]
cmdOutputStartTag(command, args)[source]
cmdUseMacro(command, args)[source]
cmdEndTagEndScope(command, args)[source]
simpletal.simpleTALUtils.expandMacros(context, template, outputFile, outputEncoding='utf-8')[source]

This function can be used to expand a template which contains METAL macros, while leaving in place all the TAL and METAL commands.

Doing this makes editing a template which uses METAL macros easier, because the results of the macro can be seen immediately. The macros referred to by the passed in template must be present in the context so that their contents can be referenced. The outputEncoding determines the encoding of the returned string, which will contain the expanded macro.

simpletal.simpleTALUtils.ExpandMacros(context, template, outputEncoding='utf-8')[source]

This legacy function does the same as expandMacros but returns a string instead of writing to a file.

Module contents