summaryrefslogtreecommitdiff
path: root/src/PlaceholderProcessor.py
blob: 7d4b34312755e1713e7820077fc7163452576808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python
# $Id: PlaceholderProcessor.py,v 1.16 2001/08/03 17:20:10 tavis_rudd Exp $
"""Provides utilities for processing $placeholders in Cheetah templates


Meta-Data
================================================================================
Author: Tavis Rudd <tavis@calrudd.com>,
License: This software is released for unlimited distribution under the
         terms of the Python license.
Version: $Revision: 1.16 $
Start Date: 2001/03/30
Last Revision Date: $Date: 2001/08/03 17:20:10 $
"""
__author__ = "Tavis Rudd <tavis@calrudd.com>"
__version__ = "$Revision: 1.16 $"[11:-2]


##################################################
## DEPENDENCIES ##

import re
import sys, string
from types import StringType
from tokenize import tokenprog

#intra-package dependencies ...
from TagProcessor import TagProcessor
from Components import Component
import NameMapper
from Utilities import lineNumFromPos
#import Template                    # imported below to avoid circ. imports
##################################################
## GLOBALS AND CONSTANTS ##

True = (1==1)
False = (1==0)

placeholderTagsRE = re.compile(r'(?:(?<=\A)|(?<!\\))\$(?=[A-Za-z_\*\{])')

# cacheType's for $placeholders
NO_CACHE = 0
STATIC_CACHE = 1
TIMED_REFRESH_CACHE = 2

##################################################
## FUNCTIONS ##

def matchTokenOrfail(text, pos):
    match = tokenprog.match(text, pos)
    if match is None:
        raise SyntaxError(text, pos)
    return match, match.end()

##################################################
## CLASSES ##

class SyntaxError(ValueError):
    def __init__(self, text, pos):
        self.text = text
        self.pos = pos
    def __str__(self):
        lineNum = lineNumFromPos(self.text, self.pos)
        return "unfinished expression on line %d (char %d) in: \n%s " % (
            lineNum, self.pos, self.text)
        # @@ augment this to give the line number and show a normal version of the txt

class PlaceholderProcessor(TagProcessor):
    """A class for processing $placeholders in strings."""

    def __init__(self, tagRE = placeholderTagsRE, marker=' placeholderTag.',
           markerEscaped = ' placeholderTag\.',
           markerLookBehind=r'(?:(?<= placeholderTag\.)|(?<= placeholderTag\.\{))'):
        """Setup the regexs used by this class

        All $placeholders are translated into valid Python code by swapping $
        for the self._marker.  This marker is then used to find the start of
        each placeholder and allows $vars in function arg lists to be parsed
        correctly.  '$x()' becomes  ' placeholderTag.x()' when it's marked.

        The marker starts with a space to allow $var$var to be parsed correctly.
        $a$b is translated to --placeholderTag.a placeholderTag.b-- instead of
        --placeholderTag.aplaceholderTag.b--, which the parser would mistake for
        a single $placeholder The extra space is removed by the parser."""

        nameCharLookForward = r'(?=[A-Za-z_])'
        cachedTags = re.compile(markerLookBehind + r'\*' + nameCharLookForward)
        refreshTags = re.compile(markerLookBehind +
                                 r'\s*\*([0-9\.]+?)\*' +
                                 nameCharLookForward)

        self._tagRE = tagRE
        self._marker = marker
        self._markerEscaped = markerEscaped
        self._markerLookBehind = markerLookBehind
        self._cachedTags = cachedTags
        self._refreshTags = refreshTags
        self._nameRE = re.compile(
            marker + r'(?:CACHED\.|REFRESH_[0-9]+(?:_[0-9]+){0,1}\.){0,1}([A-Za-z_0-9\.]+)')


    def initializeTemplateObj(self, templateObj):
        """Initialize the templateObj so that all the necessary attributes are
        in place for the tag-processing stage"""

        TagProcessor.initializeTemplateObj(self, templateObj)
        
        if not templateObj._perResponseSetupCodeChunks.has_key('placeholders'):
            ## setup the code to be included at the beginning of each response ##
            indent = templateObj._settings['indentationStep']
            baseInd = indent  * \
                   templateObj._settings['initialIndentLevel']

            templateObj._perResponseSetupCodeChunks['placeholders'] = \
                      baseInd + "if self._checkForCacheRefreshes:\n"\
                      + baseInd + indent + "timedRefreshCache = self._timedRefreshCache\n" \
                      + baseInd + indent + "currTime = currentTime()\n"\
                      + baseInd + indent + "self._timedRefreshList.sort()\n"\
                      + baseInd + indent + "if currTime >= self._timedRefreshList[0][0]:\n"\
                      + baseInd + indent * 2 +  " self._timedRefresh(currTime)\n"\
                      + baseInd + indent + "                                   \n" \
                      + baseInd + "nestedTemplates = self._nestedTemplatesCache\n" \
                      + baseInd + "components = self._componentsDict\n"

            ## initialize the caches, the localVarsList, and the timedRefreshList
            templateObj._timedRefreshCache = {} # caching timedRefresh vars
            templateObj._nestedTemplatesCache = {} # caching references to nested templates
            templateObj._componentsDict = {}       # you get the idea...
            templateObj._timedRefreshList = []
            templateObj._checkForCacheRefreshes = False

    def mark(self, txt):
        """Swap the $'s for a marker that can be parsed as valid python code.
        Default is 'placeholder.'

        Also mark whether the placeholder is to be statically cached or
        timed-refresh cached"""
        
        txt = self._tagRE.sub(self._marker, txt)
        txt = self._cachedTags.sub('CACHED.', txt)
        def refreshSubber(match):
            return 'REFRESH_' + match.group(1).replace('.','_') + '.'
        txt = self._refreshTags.sub(refreshSubber, txt)
        return txt

    def splitTxt(self, txt):
        
        """Split a text string containing marked placeholders
        (e.g. self.mark(txt)) into a list of plain text VS placeholders.

        This is the core of the placeholder parsing!
        """
        
        namechars = "abcdefghijklmnopqrstuvwxyz" \
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
        chunks = []
        pos = 0

        MARKER = self._marker
        MARKER_LENGTH = len(MARKER)

        while 1:
            markerPos = string.find(txt, MARKER, pos)
            if markerPos < 0:
                break
            nextchar = txt[markerPos + MARKER_LENGTH]

            if nextchar == "{":
                chunks.append((0, txt[pos:markerPos]))
                pos = markerPos + MARKER_LENGTH + 1
                level = 1
                while level:
                    match, pos = matchTokenOrfail(txt, pos)
                    tstart, tend = match.regs[3]
                    token = txt[tstart:tend]

                    if token == "{":
                        level = level+1
                    elif token == "}":
                        level = level-1
                chunks.append((1, txt[markerPos + MARKER_LENGTH + 1 : pos-1]))

            elif nextchar in namechars:
                chunks.append((0, txt[pos:markerPos]))
                match, pos = matchTokenOrfail(txt, markerPos + MARKER_LENGTH)

                while pos < len(txt):
                    if txt[pos] == "." and \
                        pos+1 < len(txt) and txt[pos+1] in namechars:

                        match, pos = matchTokenOrfail(txt, pos+1)
                    elif txt[pos] in "([":
                        pos, level = pos+1, 1
                        while level:
                            match, pos = matchTokenOrfail(txt, pos)
                            tstart, tend = match.regs[3]
                            token = txt[tstart:tend]
                            if token[0] in "([":
                                level = level+1
                            elif token[0] in ")]":
                                level = level-1
                    else:
                        break
                chunks.append((1, txt[markerPos + MARKER_LENGTH:pos]))

            else:
                raise SyntaxError(txt[pos:markerPos+MARKER_LENGTH], pos)
                ## @@ we shouldn't have gotten here

        if pos < len(txt):
            chunks.append((0, txt[pos:]))

        return chunks

    def wrapPlaceholders(self, txt, before='<Cheetah>placeholders__@__',
                         after='</Cheetah>'):
        
        """Wrap all marked placeholders in a template definition in the internal
        Cheetah tags so that they will be picked up by the tag processor."""
        
        result = []
        resAppend = result.append
        for live, chunk in self.splitTxt(txt):
            if live:
                resAppend( before + chunk + after )
            else:
                resAppend(chunk)

        return string.join(result, "")

    def preProcess(self, templateObj, templateDef):
        """Do the preProcessing stuff for stage 1 of the Template class'
        code-generator"""
        return self.wrapPlaceholders(self.mark(templateDef))


    def translatePlaceholderString(self, txt, searchList, templateObj,
                                   prefix='searchList', executeCallables=True):
        """Translate a marked placeholder string into valid Python code."""
        
        def translateName(name, prefix=prefix, searchList=searchList,
                            templateObj=templateObj,
                            executeCallables=executeCallables):
            
            import Template                         # import it here to avoid circ. imports

            ## get rid of the 'cache-type' tokens
            # - these are handled by the tag-processor instead
            nameChunks = name.split('.')
            if nameChunks[0] == 'CACHED':
                    del nameChunks[0]
            if nameChunks[0].startswith('REFRESH'):
                del nameChunks[0]
            name = '.'.join(nameChunks)

            ## split the name into a part that NameMapper can handle and the rest
            firstSpecialChar = re.search(r'\(|\[', name)
            if firstSpecialChar:         # NameMapper can't handle [] or ()
                firstSpecialChar = firstSpecialChar.start()
                nameMapperPartOfName, remainderOfName = \
                                      name[0:firstSpecialChar], name[firstSpecialChar:]
                remainderOfName = remainderOfName
            else:
                nameMapperPartOfName = name
                remainderOfName = ''

            ## only do autocalling on names that have no () in them
            if name.find('(') == -1 and templateObj.setting('useAutocalling'):
                safeToAutoCall = True
            else:
                safeToAutoCall = False
            
            ## deal with local vars from #set and #for directives
            if name in templateObj._localVarsList:
                return name
            elif nameChunks[0] in templateObj._localVarsList:
                translatedName = 'valueForName(' + nameChunks[0] + ',"""' + \
                           '.'.join(nameChunks[1:]) + '""", executeCallables=True)' + \
                           remainderOfName
                return translatedName


            ## Translate the NameMapper part of the Name
            try:
                translatedName = prefix + searchList.translateName(
                    nameMapperPartOfName, executeCallables=safeToAutoCall) + \
                    remainderOfName
            except NameMapper.NotFound:
                if nameMapperPartOfName in templateObj._localVarsList:
                    return name
                elif templateObj and nameChunks[0] in templateObj._localVarsList:
                    name = 'valueForName(' + nameChunks[0] + ',"""' + \
                       '.'.join(nameChunks[1:]) + '""", executeCallables=True)'
                    return name
                else:
                    raise NameMapper.NotFound, name


            ## Deal with Cheetah 'Template' and 'Component' objects
            # but only if the tag has no ()'s in it 
            if safeToAutoCall:
                value = eval(translatedName)
                if isinstance(value, Component):
                    templateObj._componentsDict[name] = value
                    return 'components["""' + \
                           name + '"""](trans, templateObj=self)'
                elif isinstance(value, Template.Template):
                    templateObj._nestedTemplatesCache[name] = value.respond
                    return 'nestedTemplates["""' + \
                           name + '"""](trans, iAmNested=True)'

            return translatedName


        ##########################
        resultList = []
        for live, chunk in self.splitTxt(txt):
            if live:
                if self._nameRE.search(chunk):
                    chunk = self.translatePlaceholderString(chunk,
                                                            searchList, templateObj)
                resultList.append( translateName(chunk) ) # using the function from above
            else:
                resultList.append(chunk)

        return string.join(resultList, "")
    

    def translateRawPlaceholderString(self, txt, searchList, templateObj=None,
                                      prefix='searchList', executeCallables=True):
        """Translate raw $placeholders in a string directly into valid Python code.

        This method is used for handling $placeholders in #directives
        """
        
        return self.translatePlaceholderString(
            self.mark(txt), searchList, prefix=prefix, templateObj=templateObj,
            executeCallables=executeCallables)

    def getValueAtRuntime(self, templateObj, tag):
        searchList = templateObj.searchList()
        try:
            translatedTag = self.translatePlaceholderString(self._marker + tag, searchList, templateObj)
            value = eval(translatedTag)
            if callable(value):
                value = value()
            return value
        except NameMapper.NotFound:
            return templateObj._settings['varNotFound_handler'](templateObj, tag)

        
    def processTag(self, templateObj, tag):

        """This method is called by the Template class for every $placeholder
        tag in the template definition

        It is a wrapper around self.translatePlaceholderString that deals with caching"""

        ## find out what cacheType the tag has
        if not templateObj._codeGeneratorState['defaultCacheType'] == None:
            cacheType = templateObj._codeGeneratorState['defaultCacheType']
            if cacheType == TIMED_REFRESH_CACHE:
                cacheRefreshInterval = \
                        templateObj._codeGeneratorState['cacheRefreshInterval']
        else:
            cacheType = NO_CACHE

        ## examine the namechunks for the caching keywords
        nameChunks = tag.split('.')
        if nameChunks[0] == 'CACHED':
            del nameChunks[0]
            cacheType = STATIC_CACHE
        if nameChunks[0].startswith('REFRESH'):
            cacheType = TIMED_REFRESH_CACHE
            cacheRefreshInterval = float('.'.join(nameChunks[0].split('_')[1:]))
            del nameChunks[0]
        tag = '.'.join(nameChunks)


        ## translate the tag into Python code using self.translatePlaceholderString
        searchList = templateObj.searchList()
        try:
            translatedTag = self.translatePlaceholderString(
                self._marker + tag, searchList, templateObj)

        except NameMapper.NotFound:
            return self.wrapEvalTag(
                templateObj,
                'self.placeholderProcessor.getValueAtRuntime(self, r"""' + \
                tag + '""")')


        ## deal with the caching and return the proper code to the code-generator
        if cacheType == STATIC_CACHE:
            return str(eval(translatedTag)).replace("'''",r"\'\'\'")
        elif cacheType == TIMED_REFRESH_CACHE:
            templateObj._setTimedRefresh(translatedTag, cacheRefreshInterval)
            return self.wrapEvalTag(
                templateObj,
                'timedRefreshCache["""' + translatedTag + '"""]')
        else:
            return self.wrapEvalTag(templateObj, "str(" + translatedTag + ")")