Skip to content

Commit

Permalink
Add support for specifying @staticmethod and @classmethod decorators …
Browse files Browse the repository at this point in the history
…on template methods

The classmethod and staticmethod keyword arguments will become the searchList as far as the
method body is concerned

Signed-off-by: R. Tyler Ballance <tyler@slide.com>
  • Loading branch information
R. Tyler Ballance committed Apr 19, 2009
1 parent fe7acdb commit ece695c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/Compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,8 @@ def nextCallRegionID(self):
return self.nextCacheID()

def startCallRegion(self, functionName, args, lineCol, regionTitle='CALL'):
class CallDetails: pass
class CallDetails(object):
pass
callDetails = CallDetails()
callDetails.ID = ID = self.nextCallRegionID()
callDetails.functionName = functionName
Expand Down Expand Up @@ -982,13 +983,25 @@ def _setupState(self):
MethodCompiler._setupState(self)
self._argStringList = [ ("self",None) ]
self._streamingEnabled = True
self._isClassMethod = None
self._isStaticMethod = None

def _useKWsDictArgForPassingTrans(self):
alreadyHasTransArg = [argname for argname,defval in self._argStringList
if argname=='trans']
return (self.methodName()!='respond'
and not alreadyHasTransArg
and self.setting('useKWsDictArgForPassingTrans'))

def isClassMethod(self):
if self._isClassMethod is None:
self._isClassMethod = '@classmethod' in self._decorators
return self._isClassMethod

def isStaticMethod(self):
if self._isStaticMethod is None:
self._isStaticMethod = '@staticmethod' in self._decorators
return self._isStaticMethod

def cleanupState(self):
MethodCompiler.cleanupState(self)
Expand Down Expand Up @@ -1030,7 +1043,7 @@ def _addAutoSetupCode(self):
if self._initialMethodComment:
self.addChunk(self._initialMethodComment)

if self._streamingEnabled:
if self._streamingEnabled and not self.isClassMethod() and not self.isStaticMethod():
if self._useKWsDictArgForPassingTrans() and self._kwargsName:
self.addChunk('trans = %s.get("trans")'%self._kwargsName)
self.addChunk('if (not trans and not self._CHEETAH__isBuffering'
Expand Down Expand Up @@ -1058,9 +1071,11 @@ def _addAutoSetupCode(self):
pass
elif allowSearchListAsMethArg and 'searchList' in argNames:
self.addChunk('SL = searchList')
else:
elif not self.isClassMethod() and not self.isStaticMethod():
self.addChunk('SL = self._CHEETAH__searchList')
if self.setting('useFilters'):
else:
self.addChunk('SL = [KWS]')
if self.setting('useFilters') and not self.isClassMethod() and not self.isStaticMethod():
self.addChunk('_filter = self._CHEETAH__currentFilter')
self.addChunk('')
self.addChunk("#" *40)
Expand All @@ -1087,6 +1102,11 @@ def methodSignature(self):
argStringChunks = []
for arg in self._argStringList:
chunk = arg[0]
if chunk == 'self' and self.isClassMethod():
chunk = 'cls'
if chunk == 'self' and self.isStaticMethod():
# Skip the "self" method for @staticmethod decorators
continue
if not arg[1] == None:
chunk += '=' + arg[1]
argStringChunks.append(chunk)
Expand Down
30 changes: 30 additions & 0 deletions src/Tests/Template.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,36 @@ def test_FailCase(self):
klass = Template.compile(source=source, compilerSettings={'useLegacyImportMode' : False})
t = klass(namespaces={'foo' : 1234})

class ClassMethodSupport(TemplateTest):
def test_BasicDecorator(self):
template = '''
#@classmethod
#def myClassMethod()
#return '$foo = %s' % $foo
#end def
'''
template = Template.compile(source=template)
try:
rc = template.myClassMethod(foo='bar')
assert rc == '$foo = bar', (rc, 'Template class method didn\'t return what I expected')
except AttributeError, ex:
self.fail(ex)

class StaticMethodSupport(TemplateTest):
def test_BasicDecorator(self):
template = '''
#@staticmethod
#def myStaticMethod()
#return '$foo = %s' % $foo
#end def
'''
template = Template.compile(source=template)
try:
rc = template.myStaticMethod(foo='bar')
assert rc == '$foo = bar', (rc, 'Template class method didn\'t return what I expected')
except AttributeError, ex:
self.fail(ex)




Expand Down

0 comments on commit ece695c

Please sign in to comment.