summaryrefslogtreecommitdiff
path: root/src/zope/tales/tales.py
blob: fd3295ede1a55198fc1600e983e885e90938c5ab (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""TALES

An implementation of a TAL expression engine
"""
__docformat__ = "reStructuredText"
import re

from zope.interface import implementer
import six

try:
    from zope.tal.interfaces import ITALExpressionEngine
    from zope.tal.interfaces import ITALExpressionCompiler
    from zope.tal.interfaces import ITALExpressionErrorInfo
except ImportError:
    from zope.interface import Interface
    class ITALExpressionEngine(Interface):
        pass
    class ITALExpressionCompiler(Interface):
        pass
    class ITALExpressionErrorInfo(Interface):
        pass

from zope.tales.interfaces import ITALESIterator

NAME_RE = r"[a-zA-Z][a-zA-Z0-9_]*"
_parse_expr = re.compile(r"(%s):" % NAME_RE).match
_valid_name = re.compile('%s$' % NAME_RE).match


class TALESError(Exception):
    """Error during TALES evaluation"""

class Undefined(TALESError):
    '''Exception raised on traversal of an undefined path'''

class CompilerError(Exception):
    '''TALES Compiler Error'''

class RegistrationError(Exception):
    '''Expression type or base name registration Error'''


_default = object()

@implementer(ITALESIterator)
class Iterator(object):
    """TALES Iterator
    """

    def __init__(self, name, seq, context):
        """Construct an iterator

        Iterators are defined for a name, a sequence, or an iterator and a
        context, where a context simply has a setLocal method:

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)

        A local variable is not set until the iterator is used:

        >>> int("foo" in context.vars)
        0

        We can create an iterator on an empty sequence:

        >>> it = Iterator('foo', (), context)

        An iterator works as well:

        >>> it = Iterator('foo', {"apple":1, "pear":1, "orange":1}, context)
        >>> it.next()
        True

        >>> it = Iterator('foo', {}, context)
        >>> it.next()
        False

        >>> it = Iterator('foo', iter((1, 2, 3)), context)
        >>> it.next()
        True
        >>> it.next()
        True

        """

        self._seq = seq
        self._iter = i = iter(seq)
        self._nextIndex = 0
        self._name = name
        self._setLocal = context.setLocal

        # This is tricky. We want to know if we are on the last item,
        # but we can't know that without trying to get it. :(
        self._last = False
        try:
            self._next = six.advance_iterator(i)
        except StopIteration:
            self._done = True
        else:
            self._done = False

    def __next__(self):
        """Advance the iterator, if possible.

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> bool(it.next())
        True
        >>> context.vars['foo']
        'apple'
        >>> bool(it.next())
        True
        >>> context.vars['foo']
        'pear'
        >>> bool(it.next())
        True
        >>> context.vars['foo']
        'orange'
        >>> bool(it.next())
        False

        >>> it = Iterator('foo', {"apple":1, "pear":1, "orange":1}, context)
        >>> bool(it.next())
        True
        >>> bool(it.next())
        True
        >>> bool(it.next())
        True
        >>> bool(it.next())
        False

        >>> it = Iterator('foo', (), context)
        >>> bool(it.next())
        False

        >>> it = Iterator('foo', {}, context)
        >>> bool(it.next())
        False


        If we can advance, set a local variable to the new value.
        """
        # Note that these are *NOT* Python iterators!
        if self._done:
            return False
        self._item = v = self._next
        try:
            self._next = six.advance_iterator(self._iter)
        except StopIteration:
            self._done = True
            self._last = True

        self._nextIndex += 1
        self._setLocal(self._name, v)
        return True

    next = __next__  # Python 2 compatibility

    def index(self):
        """Get the iterator index

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> int(bool(it.next()))
        1
        >>> it.index()
        0
        >>> int(bool(it.next()))
        1
        >>> it.index()
        1
        >>> int(bool(it.next()))
        1
        >>> it.index()
        2
        """
        index = self._nextIndex - 1
        if index < 0:
            raise TypeError("No iteration position")
        return index

    def number(self):
        """Get the iterator position

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> int(bool(it.next()))
        1
        >>> it.number()
        1
        >>> int(bool(it.next()))
        1
        >>> it.number()
        2
        >>> int(bool(it.next()))
        1
        >>> it.number()
        3
        """
        return self._nextIndex

    def even(self):
        """Test whether the position is even

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> it.next()
        True
        >>> it.even()
        True
        >>> it.next()
        True
        >>> it.even()
        False
        >>> it.next()
        True
        >>> it.even()
        True
        """
        return not ((self._nextIndex - 1) % 2)

    def odd(self):
        """Test whether the position is odd

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> it.next()
        True
        >>> it.odd()
        False
        >>> it.next()
        True
        >>> it.odd()
        True
        >>> it.next()
        True
        >>> it.odd()
        False
        """
        return bool((self._nextIndex - 1) % 2)

    def parity(self):
        """Return 'odd' or 'even' depending on the position's parity

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> it.next()
        True
        >>> it.parity()
        'odd'
        >>> it.next()
        True
        >>> it.parity()
        'even'
        >>> it.next()
        True
        >>> it.parity()
        'odd'
        """
        if self._nextIndex % 2:
            return 'odd'
        return 'even'

    def letter(self, base=ord('a'), radix=26):
        """Get the iterator position as a lower-case letter

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> it.next()
        True
        >>> it.letter()
        'a'
        >>> it.next()
        True
        >>> it.letter()
        'b'
        >>> it.next()
        True
        >>> it.letter()
        'c'
        """
        index = self._nextIndex - 1
        if index < 0:
            raise TypeError("No iteration position")
        s = ''
        while 1:
            index, off = divmod(index, radix)
            s = chr(base + off) + s
            if not index: return s

    def Letter(self):
        """Get the iterator position as an upper-case letter

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> it.next()
        True
        >>> it.Letter()
        'A'
        >>> it.next()
        True
        >>> it.Letter()
        'B'
        >>> it.next()
        True
        >>> it.Letter()
        'C'
        """
        return self.letter(base=ord('A'))

    def Roman(self, rnvalues=(
                    (1000,'M'),(900,'CM'),(500,'D'),(400,'CD'),
                    (100,'C'),(90,'XC'),(50,'L'),(40,'XL'),
                    (10,'X'),(9,'IX'),(5,'V'),(4,'IV'),(1,'I')) ):
        """Get the iterator position as an upper-case roman numeral

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> it.next()
        True
        >>> it.Roman()
        'I'
        >>> it.next()
        True
        >>> it.Roman()
        'II'
        >>> it.next()
        True
        >>> it.Roman()
        'III'
        """
        n = self._nextIndex
        s = ''
        for v, r in rnvalues:
            rct, n = divmod(n, v)
            s = s + r * rct
        return s

    def roman(self):
        """Get the iterator position as a lower-case roman numeral

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> it.next()
        True
        >>> it.roman()
        'i'
        >>> it.next()
        True
        >>> it.roman()
        'ii'
        >>> it.next()
        True
        >>> it.roman()
        'iii'
        """
        return self.Roman().lower()

    def start(self):
        """Test whether the position is the first position

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> it.next()
        True
        >>> it.start()
        True
        >>> it.next()
        True
        >>> it.start()
        False
        >>> it.next()
        True
        >>> it.start()
        False

        >>> it = Iterator('foo', {}, context)
        >>> it.start()
        False
        >>> it.next()
        False
        >>> it.start()
        False
        """
        return self._nextIndex == 1

    def end(self):
        """Test whether the position is the last position

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> it.next()
        True
        >>> it.end()
        False
        >>> it.next()
        True
        >>> it.end()
        False
        >>> it.next()
        True
        >>> it.end()
        True

        >>> it = Iterator('foo', {}, context)
        >>> it.end()
        False
        >>> it.next()
        False
        >>> it.end()
        False
        """
        return self._last

    def item(self):
        """Get the iterator value

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> it.next()
        True
        >>> it.item()
        'apple'
        >>> it.next()
        True
        >>> it.item()
        'pear'
        >>> it.next()
        True
        >>> it.item()
        'orange'

        >>> it = Iterator('foo', {1:2}, context)
        >>> it.next()
        True
        >>> it.item()
        1

        """
        if self._nextIndex == 0:
            raise TypeError("No iteration position")
        return self._item

    def length(self):
        """Get the length of the iterator sequence

        >>> context = Context(ExpressionEngine(), {})
        >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
        >>> it.length()
        3

        You can even get the length of a mapping:

        >>> it = Iterator('foo', {"apple":1, "pear":2, "orange":3}, context)
        >>> it.length()
        3

        But you can't get the length of an iterable which doesn't
        support len():

        >>> class MyIter(object):
        ...     def __init__(self, seq):
        ...         self._iter = iter(seq)
        ...     def __iter__(self):
        ...         return self
        ...     def __next__(self):
        ...         return next(self._iter)
        ...     next = __next__
        >>> it = Iterator('foo', MyIter({"apple":1, "pear":2}), context)
        >>> try:
        ...     it.length()
        ... except TypeError:
        ...     pass
        ... else:
        ...     print('Expected TypeError')
        """
        return len(self._seq)


@implementer(ITALExpressionErrorInfo)
class ErrorInfo(object):
    """Information about an exception passed to an on-error handler."""

    def __init__(self, err, position=(None, None)):
        if isinstance(err, Exception):
            self.type = err.__class__
            self.value = err
        else:
            self.type = err
            self.value = None
        self.lineno = position[0]
        self.offset = position[1]


@implementer(ITALExpressionCompiler)
class ExpressionEngine(object):
    '''Expression Engine

    An instance of this class keeps a mutable collection of expression
    type handlers.  It can compile expression strings by delegating to
    these handlers.  It can provide an expression Context, which is
    capable of holding state and evaluating compiled expressions.
    '''

    def __init__(self):
        self.types = {}
        self.base_names = {}
        self.namespaces = {}
        self.iteratorFactory = Iterator

    def registerFunctionNamespace(self, namespacename, namespacecallable):
        """Register a function namespace

        namespace - a string containing the name of the namespace to
                    be registered

        namespacecallable - a callable object which takes the following
                            parameter:

                            context - the object on which the functions
                                      provided by this namespace will
                                      be called

                            This callable should return an object which
                            can be traversed to get the functions provided
                            by the this namespace.

        example:

           class stringFuncs(object):

              def __init__(self,context):
                 self.context = str(context)

              def upper(self):
                 return self.context.upper()

              def lower(self):
                 return self.context.lower()

            engine.registerFunctionNamespace('string',stringFuncs)
        """
        self.namespaces[namespacename] = namespacecallable


    def getFunctionNamespace(self, namespacename):
        """ Returns the function namespace """
        return self.namespaces[namespacename]

    def registerType(self, name, handler):
        if not _valid_name(name):
            raise RegistrationError('Invalid expression type name "%s".' % name)
        types = self.types
        if name in types:
            raise RegistrationError(
                'Multiple registrations for Expression type "%s".' % name)
        types[name] = handler

    def getTypes(self):
        return self.types

    def registerBaseName(self, name, object):
        if not _valid_name(name):
            raise RegistrationError('Invalid base name "%s".' % name)
        base_names = self.base_names
        if name in base_names:
            raise RegistrationError(
                'Multiple registrations for base name "%s".' % name)
        base_names[name] = object

    def getBaseNames(self):
        return self.base_names

    def compile(self, expression):
        m = _parse_expr(expression)
        if m:
            type = m.group(1)
            expr = expression[m.end():]
        else:
            type = "standard"
            expr = expression
        try:
            handler = self.types[type]
        except KeyError:
            raise CompilerError('Unrecognized expression type "%s".' % type)
        return handler(type, expr, self)

    def getContext(self, contexts=None, **kwcontexts):
        if contexts is not None:
            if kwcontexts:
                kwcontexts.update(contexts)
            else:
                kwcontexts = contexts
        return Context(self, kwcontexts)

    def getCompilerError(self):
        return CompilerError


@implementer(ITALExpressionEngine)
class Context(object):
    '''Expression Context

    An instance of this class holds context information that it can
    use to evaluate compiled expressions.
    '''
    position = (None, None)
    source_file = None

    def __init__(self, engine, contexts):
        self._engine = engine
        self.contexts = contexts
        self.setContext('nothing', None)
        self.setContext('default', _default)

        self.repeat_vars = rv = {}
        # Wrap this, as it is visible to restricted code
        self.setContext('repeat', rv)
        self.setContext('loop', rv) # alias

        self.vars = vars = contexts.copy()
        self._vars_stack = [vars]

        # Keep track of what needs to be popped as each scope ends.
        self._scope_stack = []

    def setContext(self, name, value):
        # Hook to allow subclasses to do things like adding security proxies
        self.contexts[name] = value

    def beginScope(self):
        self.vars = vars = self.vars.copy()
        self._vars_stack.append(vars)
        self._scope_stack.append([])

    def endScope(self):
        self._vars_stack.pop()
        self.vars = self._vars_stack[-1]

        scope = self._scope_stack.pop()
        # Pop repeat variables, if any
        i = len(scope)
        while i:
            i = i - 1
            name, value = scope[i]
            if value is None:
                del self.repeat_vars[name]
            else:
                self.repeat_vars[name] = value

    def setLocal(self, name, value):
        self.vars[name] = value

    def setGlobal(self, name, value):
        for vars in self._vars_stack:
            vars[name] = value

    def getValue(self, name, default=None):
        value = default
        for vars in self._vars_stack:
            value = vars.get(name, default)
            if value is not default:
                break
        return value

    def setRepeat(self, name, expr):
        expr = self.evaluate(expr)
        if not expr:
            return self._engine.iteratorFactory(name, (), self)
        it = self._engine.iteratorFactory(name, expr, self)
        old_value = self.repeat_vars.get(name)
        self._scope_stack[-1].append((name, old_value))
        self.repeat_vars[name] = it
        return it

    def evaluate(self, expression):
        if isinstance(expression, str):
            expression = self._engine.compile(expression)
        __traceback_supplement__ = (
           TALESTracebackSupplement, self, expression)
        return expression(self)

    evaluateValue = evaluate

    def evaluateBoolean(self, expr):
        return not not self.evaluate(expr)

    def evaluateText(self, expr):
        text = self.evaluate(expr)
        if text is self.getDefault() or text is None:
            return text
        if isinstance(text, six.string_types):
            # text could already be something text-ish, e.g. a Message object
            return text
        return six.text_type(text)

    def evaluateStructure(self, expr):
        return self.evaluate(expr)
    evaluateStructure = evaluate

    def evaluateMacro(self, expr):
        # TODO: Should return None or a macro definition
        return self.evaluate(expr)
    evaluateMacro = evaluate

    def createErrorInfo(self, err, position):
        return ErrorInfo(err, position)

    def getDefault(self):
        return _default

    def setSourceFile(self, source_file):
        self.source_file = source_file

    def setPosition(self, position):
        self.position = position

    def translate(self, msgid, domain=None, mapping=None, default=None):
        # custom Context implementations are supposed to customize
        # this to call whichever translation routine they want to use
        return six.text_type(msgid)


class TALESTracebackSupplement(object):
    """Implementation of zope.exceptions.ITracebackSupplement"""

    def __init__(self, context, expression):
        self.context = context
        self.source_url = context.source_file
        self.line = context.position[0]
        self.column = context.position[1]
        self.expression = repr(expression)

    def getInfo(self, as_html=0):
        import pprint
        data = self.context.contexts.copy()
        if 'modules' in data:
            del data['modules']     # the list is really long and boring
        s = pprint.pformat(data)
        if not as_html:
            return '   - Names:\n      %s' % s.replace('\n', '\n      ')
        else:
            from cgi import escape
            return '<b>Names:</b><pre>%s</pre>' % (escape(s))
        return None