Tayra

Template. Script. Distribute

runtime – Stack machine runtime

Run-time engine, a stack machine interpreting instructions generated by tayra.codegen.InstrGen. Instructions are nothing but methods on the stack machine object.

References automatically made available in a template script.

_m,
Reference to StackMachine instance used to generate the final HTML text.
this,
Every template script can be viewed as an object instance which can be referenced using this. In case of template scripts making use of inheritance feature, this will always refer to the template script at the end of the inheritance chain.
local,
For non-inheriting template scripts this and local refer to the same object. In case of template scripts using inheritance feature, unlike this symbol which refers to the template script at the end of the inheritance chain, local will always refer to the template script object in which it is used.
parent,
In case of inheriting scripts, parent will refer to the base template from which local template script derives.
next,
In case of inheriting scripts, next will refer to the deriving template script.

All names this, local, parent, next refer to the same type of object, template-module. Having a refence to template-module allows developers to access global variables and functions defined in the module.

In case if a template script is part of an inheritance chain, then, attribute references on the template-module will propogate towards the top of the chain until an attribute is found in one of the base module.

_compiler,
Refers to tayra.compiler.TTLCompiler plugin instance.
_context,
Refers to the context dictionary.
_ttlhash,
Refers to the hash value generated from template text.
_ttlfile,
File name of template script.
__compiler,
Temporary variable that is optionally created for compiling and importing library templates. Refers to a tayra.compiler.TTLCompiler plugin instance.
__ttlcode,
Temporary variable that is optionally created while importing library templates.
<Implemented-interfaces>,
When a template script implements an interface, interface name and plugin name will also be made available in the global context.

Other than this every template script will have the following standard imports

1
2
3
4
5
import imp
from   io                   import StringIO
from   pluggdapps.plugin    import Plugin, implements
import pluggdapps.utils     as h
from   tayra                import BaseTTLPlugin

Module contents

class tayra.runtime.StackMachine(compiler)[source]

Bases: builtins.object

Stack machine instruction interpreterr. Intructions are method calls on this object. Supported instructions are,:

indent, append, extend, pushbuf, popbuf, popbuftext, popobject,
handletag, evalexprs, inherit

Initializing a stack machine can be a costly operation, so to avoid creating a stack machine object everytime, create it once and call _init() before interpreting a new template-module.

append(value)[source]

Append value string to recently pushed buffer in the buffer stack.

downindent(down='')[source]

Decrease the indentation level for subsequent HTML text by down spaces.

evalexprs(name, text, filters, globals_, locals_)[source]

Evaluate expression text in globals_ and locals_ context using plugin prefix in text or using the TTLCompiler[‘expression.default’] plugin. Convert the resulting object to string, pipe them to the list of filters specified by filters. filters is comma separated value of escape filters to be applied on the output string.

extend(value)[source]

Extend recently pushed buffer in the buffer stack using value list.

handletag(contents, tagbegin, **kwargs)[source]

Parse tagbegin for tokens, style and attribute. Along with them add content to pass them to tag-plugin handlers. append() the html text returned by the handler into the buffer stack.

There are two types of pruning, inner-prunning (specified by <... !>) and outer-prunning (specified by <! ...>). Pruning modifiers are detected by the AST and passed as part of modifier dictionary.

optional key-word arguments,

indent,
Boolean to keep indentation in output html.
nl,
String.
iprune,
Boolean, if True, should remove all whitespaces before and after the content enclosed by this tag.
oprune,
Boolean, if True, should remove all leading and trailining whitespaces around this tag element.
indent()[source]

Add indentation to output HTML text.

inherit(ttlloc, childglobals)[source]

Special instruction used by @inherit directive.

popbuf()[source]

Pop the recently pushed buffer in the buffer stack and return the same. Return type is a list of strings.

popbuftext()[source]

Same as popbuf() and additionaly join all the string elements in the buffer and return the same. Return type is string.

popobject()[source]

Return the first element from recently pushed buffer in the buffer stack. Discard the remaining buffer. If recently pushed buffer is empty, return None.

pushbuf(buf=None)[source]

Push an empty buffer into the buffer stack.

upindent(up='')[source]

Increase the indentation level for subsequent HTML text by up spaces.

class tayra.runtime.Namespace(parentnm, localmod)[source]

Bases: builtins.object

Namespace wrapper for template-modules to traverse the interitance chain for referred attributes.