Python notes

Some notes about the Python port of StringTemplate V3.1.

StringTemplate upto V2.2 has been ported by Marq Kole. The update to V3.1 has been implemented by Benjamin Niemann.

Changes from V2.2

During the update to V3.1 I took the deliberate freedom to change some of the APIs breaking backwards compatibility. The package has been renames to stringtemplate3, so installing it will not break any code that relies on the V2.x API.

While V2.x tried to be as close to the Java API as possible, I tried to get rid of most Java-isms as possible for V3.1. There may be more differences between the Java and the Python API than documented here, but you are unlikely to stumble upon these, unless you do some really nasty hacks.

Use of keyword arguments

Some methods that make extensive use of overloading in Java use keyword arguments in Python.

StringTemplate constructor:

StringTemplate(template=None, group=None, lexer=None, attributes=None)

StringTemplateGroup constructor:

StringTemplateGroup(name=None, rootDir=None, lexer=None, file=None, errors=None, superGroup=None)

You may either use StringTemplateGroup(name=..., rootDir=...) or StringTemplateGroup(file=...) adding lexer, errors and superGroup attributes as needed.
lexer may be either a lexer class (stringtemplate3.language.DefaultTemplateLexer.Lexer, stringtemplate3.language.AngleBracketLexer.Lexer or your own implementation) or one of the strings "default" and "angle-bracket".

StringTemplateGroup.getInstanceOf:

StringTemplateGroup.getInstanceOf(name, enclosingInstance=None, attributes=None)

Deprecated use of getter/setter methods

Python is not Java (wink)
While getter and setter methods that mimic the Java API exist, they will spit out warnings when used. These warnings can be disabled with

import warnings
warnings.simplefilter('ignore', DeprecationWarning)

Although the preferred solution is of course not to use these methods, but access the attributes directly.

deprecated methods

preferred attribute

StringTemplateGroup.getSuperGroup()
StringTemplateGroup.setSuperGroup()

StringTemplateGroup.superGroup

StringTemplateGroup.getErrorListener()
StringTemplateGroup.setErrorListener()

StringTemplateGroup.errorListener

StringTemplateGroup.getName()
StringTemplateGroup.setName()

StringTemplateGroup.name

StringTemplateGroup.getRootDir()
StringTemplateGroup.setRootDir()

StringTemplateGroup.rootDir

StringTemplateGroup.getFileCharEncoding()
StringTemplateGroup.setFileCharEncoding()

StringTemplateGroup.fileCharEncoding

StringTemplateGroup.getFileCharEncoding()
StringTemplateGroup.setRefreshInterval()

StringTemplateGroup.refreshInterval

StringTemplateGroup.getTemplateLexerClass()

StringTemplateGroup.templateLexerClass

StringTemplateGroup.setAttributeRenderers()

StringTemplateGroup.attributeRenderers

StringTemplate.getArgumentContent()
StringTemplate.setArgumentContent()

StringTemplate.argumentContext

StringTemplate.getEnclosingInstance()
StringTemplate.setEnclosingInstance()

StringTemplate.enclosingInstance

StringTemplate.getArgumentAST()
StringTemplate.setArgumentAST()

StringTemplate.argumentAST

StringTemplate.getName()
StringTemplate.setName()

StringTemplate.name

StringTemplate.getGroup()
StringTemplate.setGroup()

StringTemplate.group

StringTemplate.getNativeGroup()
StringTemplate.setNativeGroup()

StringTemplate.nativeGroup

StringTemplate.getGroupFileLine()
StringTemplate.setGroupFileLine()

StringTemplate.groupFileLine

StringTemplate.getTemplate()
StringTemplate.setTemplate()

StringTemplate.template

StringTemplate.getErrorListener()
StringTemplate.setErrorListener()

StringTemplate.errorListener

StringTemplate.getTemplateID()

StringTemplate.templateID

StringTemplate.getAttributes()
StringTemplate.setAttributes()

StringTemplate.attributes

StringTemplate.getformalArguments()
StringTemplate.setformalArguments()

StringTemplate.formalArguments

StringTemplate.setAttributeRenderers()

StringTemplate.attributeRenderers

StringTemplate.getRegionDefType()
StringTemplate.setRegionDefType()

StringTemplate.regionDefType

StringTemplate.getTemplateDeclaratorString()

StringTemplate.templateDeclaratorString

StringTemplate.getEnclosingInstanceStackString()

StringTemplate.enclosingInstanceStackString

StringTemplate.getFormalArgumentKeys()

StringTemplate.formalArgumentKeys

StringTemplate.getChunks()

StringTemplate.chunks

PathGroupLoader.getFileCharEncoding()
PathGroupLoader.setFileCharEncoding()

PathGroupLoader.fileCharEncoding

StringTemplateGroupInterface.getSuperInterface()
StringTemplateGroupInterface.setSuperInterface()

StringTemplateGroupInterface.superInterface

StringTemplateGroupInterface.getName()
StringTemplateGroupInterface.setName()

StringTemplateGroupInterface.name

ASTExpr.getAST()

ASTExpr.exprTree

ChunkToken.getIndention()
ChunkToken.setIndention()

ChunkToken.indention

ConditionalExpr.getSubtemplate()
ConditionalExpr.setSubtemplate()

ConditionalExpr.subtemplate

ConditionalExpr.getElseSubtemplate()
ConditionalExpr.setElseSubtemplate()

ConditionalExpr.elseSubtemplate

Expr.getEnclosingTemplate()

Expr.enclosingTemplate

Expr.getIndention()
Expr.setIndention()

Expr.indention

Static methods

Some static methods have been turned into module level functions. Most of these are only internally used, the only user visible change should be:

V2.x

V3.1

StringTemplate.setLintMode(...)

stringtemplate3.lintMode = ...