Source

webstring / webstring / text.py

  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
# -*- coding: utf-8 -*-
'''
Text templates.
'''

import re
from copy import deepcopy

from stuf.six import u

from webstring.base import _Template, _Field, _Group, _exceptions, _checkname

__all__ = ('TextTemplate', 'texttemplate')

# default variable and group delimiter
MARK = GROUPMARK = '$'
# variable and group discovery regex
PATTERN = r'%s{2}(.+?)%s{2}|%s{1}(\w+?)%s{1}'


def getpattern(mark=MARK, group=GROUPMARK):
    '''
    Get regular expression pattern with current delimiters for a template set.

    :keyword str mark: variable delimiter
    :keyword str group: group delimiter
    '''
    # escape delimiters
    mark, group = re.escape(mark), re.escape(group)
    # arrange in tuple
    marks = (group, group, mark, mark)
    return re.compile(PATTERN % marks, re.DOTALL | re.UNICODE)


# default regex pattern
_match = getpattern()


class _NonRoot(object):

    '''
    Base for non-root text templates.
    '''

    def append(self, data):
        '''
        Appends string or template to this template.

        :argument data: template or string
        '''
        return self.__iadd__(data)

    # create property that returns the current state
    current = property(lambda self: deepcopy(self))


class _TextField(_Field, _NonRoot):

    '''
    Text field base.
    '''

    # default variable delimiter
    _mark = MARK

    def __init__(self, name, auto=True, maxlen=25, **kw):
        '''
        :argument str name: field name
        :keyword bool auto: turns automagic on and off
        :keyword int maxlen: maximum number of times a field can repeat
        '''
        super(_TextField, self).__init__(auto, maxlen, **kw)
        self.__name__ = name
        # set text to empty unicode string
        self.text = self._btext = u('')

    def __iadd__(self, data):
        '''
        Inserts string or template after current content.
        '''
        # process strings
        if isinstance(data, basestring):
            # insert into sibling tracking list
            self._siblings.append(data)
        # process Templates
        elif hasattr(data, 'mark'):
            # append rendered string of other Templates
            self._siblings.append(data.render())
        else:
            raise TypeError(_exceptions[2])
        return self

    def render(self, info=None, format='text', encoding='utf-8'):
        '''
        Get string version of field.

        :keyword info: data to substitute into template
        :keyword str format: text format
        :keyword str encoding: encoding of output
        '''
        if info is not None:
            self.__imod__(info)
        # join internal text with any sibling text
        self.text = u''.join([self.text, u''.join(self._siblings)])
        # return with encoding
        return self.text.encode(encoding)

    def reset(self, **kw):
        '''
        Return template to original version.
        '''
        self.__init__(self.__name__, self._auto, self.max)

    # sets the delimiter for template variables
    mark = property(lambda self: self._mark, _Field._setmark)
    # create property that returns the default Template state
    default = property(
        lambda self: _TextField(self.__name__, self._auto, self.max)
    )


class _TextMany(object):

    '''
    Text root and group template base.
    '''

    # variable and group delimiters
    _mark, _groupmark = MARK, GROUPMARK

    def __init__(self, auto, maxlen, **kw):
        super(_TextMany, self).__init__(auto, maxlen, **kw)
        # assign match object to internal reference
        self._match = _match

    def __delattr__(self, attr):
        try:
            # try removing field
            try:
                # delete from internal field dictionary
                obj = self._fielddict.pop(attr)
                # get object's index
                index = self._fields.index(obj)
                # remove from internal field list
                self._fields.remove(obj)
                splits, cnt = self._template.split('%s'), 0
                # remove '%s' from template string
                for idx, item in enumerate(splits):
                    # find a split point
                    if item.rstrip() == u'':
                        # find the corresponding split point
                        if cnt == index:
                            # remove the split point and break
                            del splits[idx]
                            break
                        # increment separate counter
                        cnt += 1
                # recreate internal template
                self._template = '%s'.join(splits)
            except KeyError:
                pass
        # always delete object attribute if set
        finally:
            if hasattr(self, attr):
                object.__delattr__(self, attr)

    def _addfield(self, name):
        # adds a field from an element
        if name not in self._filter:
            # add to filter list if unprocessed
            self._filter.add(name)
            # add child as field
            self._setfield(name, _TextField(name, self._auto, self._max))

    def render(self, info=None, format='text', encoding='utf-8'):
        '''
        Get string rendering of template.

        :keyword info: data to substitute into template
        :keyword str format: format of document
        :keyword str encoding: encoding type for output
        '''
        if info is not None:
            self.__imod__(info)
        # render internal fields and store in tuple
        content = tuple(i.render(None, format, encoding) for i in self._fields)
        # interpolate into template
        self._text = self._template % content
        # output with correct encoding
        return self._text.encode(encoding)

    def reset(self, **kw):
        '''
        Returns template to original version.
        '''
        self.__init__(self._btext, self._auto, self._max)


class _TextGroup(_TextMany, _Group, _NonRoot):

    '''
    Text group template.
    '''

    def __init__(self, src=None, auto=True, maxlen=25, **kw):
        '''
        :keyword src: template string
        :keyword bool auto: turns automagic on and off
        :keyword int maxlen: maximum number of times a group can repeat
        '''
        super(_TextGroup, self).__init__(auto, maxlen, **kw)
        # internal temp field tracker and temp template
        self._tempfields, self._ttemplate = list(), ''
        if src is not None:
            self._settemplate(src)

    def __iadd__(self, data):
        '''
        Inserts string or template after current content.
        '''
        # process strings
        if isinstance(data, basestring):
            # append string onto internal template
            self._template = u''.join([self._template, data])
        # process templates
        elif hasattr(data, 'mark'):
            if hasattr(data, 'groupmark'):
                # add group-like template fields to _tempfield tracker
                self._tempfields.extend(data._fields)
                # add group-like template's template to temporary template
                self._ttemplate = u('').join((self._ttemplate, data._template))
            else:
                # add fields to _tempfield tracker
                self._tempfields.append(data)
                # add delimiter onto temp template
                self._ttemplate = u('').join((self._ttemplate, u('%s')))
        else:
            raise TypeError(_exceptions[2])
        return self

    def __deepcopy__(self, memo):
        # Python 2.4 deepcopy copies regexes while Python 2.5 does not
        idict = self.__dict__
        # remove _match regex if present
        try:
            match = idict.pop('_match')
        # use global if object does not have _match attribute
        except KeyError:
            match = _match
        # deep copy original object's __dict__
        ndict = deepcopy(idict)
        # re-add _match object
        ndict['_match'] = match
        # create blank group template
        cls = _TextGroup()
        # update with self's dictionary
        cls.__dict__.update(ndict)
        return cls

    def _changematch(self):
        # changes the delimiter regex pattern
        match = self._match = getpattern(self._mark, self._groupmark)
        # change delimiter on children
        for field in self._fields:
            if hasattr(field, 'groupmark'):
                field._match = match

    def _setgmark(self, mark):
        # sets the group delimiter for the template and its children
        super(_TextGroup, self)._setgmark(mark)
        self._changematch()

    def _setmark(self, mark):
        # sets the variable delimiter for the template and its children
        super(_TextGroup, self)._setmark(mark)
        self._changematch()

    def _settemplate(self, instr):
        # sets the internal group template
        addfield = self._addfield
        # iterate over any found fields
        for mo in self._match.finditer(instr):
            _, second = mo.groups()
            # extract field if found
            if second is not None:
                addfield(second)
        # check if field is empty
        if self._fields:
            # create internal template
            self._template = self._match.sub('%s', instr)
            # create text stubs and backup text
            self._text, self._btext = u'', instr

    def render(self, info=None, format='text', encoding='utf-8'):
        '''
        Returns the string rendering of current template.

        :keyword info: data substituted into template
        :keyword str format: format of document
        keyword str encoding: encoding for outpu
        '''
        # run superclass render
        text = super(_TextGroup, self).render(info, format, encoding)
        # render any tempfield siblings and store in tuple
        content = tuple(
            i.render(None, format, encoding) for i in self._tempfields
        )
        # join existing content with temporary content
        self._text = u''.join([text, self._ttemplate % content])
        # return with correct encoding
        return self._text.encode(encoding)

    # sets the delimiter for template variables
    mark = property(lambda self: self._mark, _setmark)
    # sets the delimiter for template groups
    groupmark = property(lambda self: self._groupmark, _setgmark)
    # create property that returns the default template state
    default = property(
        lambda self: _TextGroup(self._btext, self._auto, self._max)
    )


class TextTemplate(_TextMany, _Template):

    '''
    Text root template.
    '''

    __name__ = 'root'
    # pattern to split group name from group template
    _groupbr = re.compile('(\w+)(\W.+)', re.DOTALL | re.UNICODE)

    def __init__(self, src=None, auto=True, maxlen=25, **kw):
        '''
        :keyword src: file path or string source
        :keyword bool auto: turns automagic on or off
        :keyword int maxlen: maximum number of times a template can repeat
        '''
        super(TextTemplate, self).__init__(auto, maxlen, **kw)
        # check if source exists
        if src is not None:
            # try reading text source from a file
            try:
                self.fromfile(src)
            except IOError:
                # try reading from  string
                try:
                    self.fromstring(src)
                except SyntaxError:
                    raise IOError(_exceptions[1])

    def __iadd__(self, data):
        '''
        Inserts string or template after current content.
        '''
        # process strings
        if isinstance(data, basestring):
            # append string to internal template
            self._template = u''.join([self._template, data])
        # process templates
        elif hasattr(data, 'mark'):
            if hasattr(data, 'groupmark'):
                # extend internal fields with other template's fields
                self._fields.extend(data._fields)
                # append other template
                self._template = u''.join([self._template, data._template])
            else:
                # append field to internal field list
                self._fields.append(data)
                # append delimiter to internal template
                self._template = u''.join([self._template, '%s'])
        else:
            raise TypeError(_exceptions[2])
        return self

    def __deepcopy__(self, memo):
        # python 2.4 deepcopy copies regex objects while Python 2.5 does not
        idict = self.__dict__
        # remove _match regex if present
        try:
            match = idict.pop('_match')
        # use global if object does not have _match attribute
        except KeyError:
            match = _match
        # deep copy original object's __dict__
        ndict = deepcopy(idict)
        # re-add _match object
        ndict['_match'] = match
        # create blank group template
        cls = _TextGroup()
        # update with deepcopied dictionary
        cls.__dict__.update(ndict)
        return cls

    def _addgroup(self, group):
        # create group templates
        realname, template = self._groupbr.match(group).groups()
        # separate group name and template
        name = _checkname(realname)
        # check if group already processed
        if name not in self._filter:
            # mark as processed
            self._filter.add(name)
            # make new group template without passing child
            node = _TextGroup(template, self._auto, self._max)
            # name group
            node.__name__ = name
            # set field
            self._setfield(name, node)

    def _setgmark(self, mark):
        # set group delimiter for template and its children.
        super(TextTemplate, self)._setgmark(mark)
        self._changematch()

    def _setmark(self, mark):
        # set variable delimiter for template and its children.
        super(TextTemplate, self)._setmark(mark)
        self._changematch()

    def fromfile(self, path):
        '''
        Create element from file source.

        :argument str path: source file
        '''
        self.fromstring(open(path, 'rb').read())

    def fromstring(self, instr):
        '''
        Create template from source string.

        :argument str instr: template source
        '''
        # extract fields, groups from source
        addgroup = self._addgroup
        addfield = self._addfield
        for mo in self._match.finditer(instr):
            first, second = mo.groups()
            # add groups
            if first is not None:
                addgroup(first)
            # add fields
            elif second is not None:
                addfield(second)
        # pnly initialize templates with fields
        if self._fields:
            # internal template
            self._template = self._match.sub(u'%s', instr)
            # text stub
            self._text = u('')
            # backup text
            self._btext = instr

    # sets the delimiter for template variables
    mark = property(lambda self: self._mark, _setmark)
    # sets the delimiter for template variables
    groupmark = property(lambda self: self._groupmark, _setgmark)
    # create property that returns the current template state
    current = property(lambda self: deepcopy(self))
    # create property that returns the default template state
    default = property(
        lambda self: TextTemplate(self._btext, self._auto, self._max)
    )