Changeset 8 for formatflowed

Show
Ignore:
Timestamp:
09/05/05 13:20:18 (7 years ago)
Author:
mj
Message:

Switch to symbolic constants for chunk types

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • formatflowed/trunk/formatflowed.py

    r7 r8  
    1616import textwrap 
    1717 
    18 __all__ = ['FormatFlowedDecoder', 'decode', 'convertToWrapped'] 
     18__all__ = [ 
     19    'PARAGRAPH', 
     20    'FIXED', 
     21    'SIGNATURE_SEPARATOR', 
     22    'FormatFlowedDecoder',  
     23    'decode',  
     24    'convertToWrapped' 
     25] 
     26 
     27# Constants denoting the various text chunk types recognized by format=flowed 
     28PARAGRAPH, FIXED, SIGNATURE_SEPARATOR = range(3) 
    1929 
    2030class FormatFlowedDecoder: 
     
    114124        tuples. information is a dictionary with the following fields: 
    115125          type 
    116             One of 'paragraph', 'fixed', 'signature-separator' 
     126            One of PARAGRAPH, FIXED, SIGNATURE_SEPARATOR 
    117127          quotedepth 
    118128            Number of quotemarks found on the text chunk 
     
    120130        chunk is a unicode string. All text is unwrapped and without any  
    121131        quotemarks; when displaying these chunks, the appropriate quotemarks 
    122         should be added again, and chunks of type 'paragraph' should be 
    123         displayed wrapped. Chunks of type 'fixed' should be displayed  
     132        should be added again, and chunks of type PARAGRAPH should be 
     133        displayed wrapped. Chunks of type FIXED should be displayed  
    124134        unwrapped. 
    125135         
     
    141151            ... "Lewis Carroll"))) 
    142152            >>> list(result) == [ 
    143             ...   ({'quotedepth': 2, 'type': 'paragraph'},  
     153            ...   ({'quotedepth': 2, 'type': PARAGRAPH},  
    144154            ...     u"`Take some more tea,' the March Hare said to Alice, " 
    145155            ...     u"very earnestly."), 
    146             ...   ({'quotedepth': 1, 'type': 'fixed'}, u""), 
    147             ...   ({'quotedepth': 1, 'type': 'paragraph'},  
     156            ...   ({'quotedepth': 1, 'type': FIXED}, u""), 
     157            ...   ({'quotedepth': 1, 'type': PARAGRAPH},  
    148158            ...    u"`I've had nothing yet,' Alice replied in an offended " 
    149159            ...    u"tone, `so I can't take more.'"), 
    150             ...   ({'quotedepth': 0, 'type': 'fixed'}, u""), 
    151             ...   ({'quotedepth': 0, 'type': 'paragraph'},  
     160            ...   ({'quotedepth': 0, 'type': FIXED}, u""), 
     161            ...   ({'quotedepth': 0, 'type': PARAGRAPH},  
    152162            ...    u"`You mean you can't take less,' said the Hatter: `it's " 
    153163            ...    u"very easy to take more than nothing.'"), 
    154             ...   ({'quotedepth': 0, 'type': 'fixed'}, u""), 
    155             ...   ({'quotedepth': 0, 'type': 'signature-separator'}, u"-- "), 
    156             ...   ({'quotedepth': 0, 'type': 'fixed'}, u"Lewis Carroll") 
     164            ...   ({'quotedepth': 0, 'type': FIXED}, u""), 
     165            ...   ({'quotedepth': 0, 'type': SIGNATURE_SEPARATOR}, u"-- "), 
     166            ...   ({'quotedepth': 0, 'type': FIXED}, u"Lewis Carroll") 
    157167            ... ] 
    158168            True 
     
    169179            ... "Depth zero paragraph with fixed line."))) 
    170180            >>> list(result) == [ 
    171             ...   ({'quotedepth': 1, 'type': 'paragraph'}, 
     181            ...   ({'quotedepth': 1, 'type': PARAGRAPH}, 
    172182            ...    u"Depth one paragraph with flow space. "), 
    173             ...   ({'quotedepth': 2, 'type': 'paragraph'}, 
     183            ...   ({'quotedepth': 2, 'type': PARAGRAPH}, 
    174184            ...    u"Depth two paragraph with flow space. "), 
    175             ...   ({'quotedepth': 0, 'type': 'fixed'}, 
     185            ...   ({'quotedepth': 0, 'type': FIXED}, 
    176186            ...    u"Depth zero paragraph with fixed line.")] 
    177187            True 
     
    183193            ... "-- "))) 
    184194            >>> list(result) == [ 
    185             ...   ({'quotedepth': 0, 'type': 'paragraph'}, 
     195            ...   ({'quotedepth': 0, 'type': PARAGRAPH}, 
    186196            ...    u"A paragraph with flow space. "), 
    187             ...   ({'quotedepth': 0, 'type': 'signature-separator'}, u"-- ")] 
     197            ...   ({'quotedepth': 0, 'type': SIGNATURE_SEPARATOR}, u"-- ")] 
    188198            True 
    189199             
     
    193203            ... "A paragraph with flow space. ",))) 
    194204            >>> list(result) == [ 
    195             ...   ({'quotedepth': 0, 'type': 'paragraph'}, 
     205            ...   ({'quotedepth': 0, 'type': PARAGRAPH}, 
    196206            ...    u"A paragraph with flow space. ")] 
    197207            True 
     
    206216            ... "break across the paragraph."))) 
    207217            >>> list(result) == [ 
    208             ...   ({'quotedepth': 0, 'type': 'paragraph'},  
     218            ...   ({'quotedepth': 0, 'type': PARAGRAPH},  
    209219            ...    u'Contrived example with a word-break across the ' 
    210220            ...    u'paragraph.')] 
     
    222232            ... "\xf7K"))) 
    223233            >>> list(result) == [ 
    224             ...   ({'quotedepth': 1, 'type': 'paragraph'}, 
     234            ...   ({'quotedepth': 1, 'type': PARAGRAPH}, 
    225235            ...    u'This is a quoted paragraph encoded in cp037.')] 
    226236            True 
     
    228238        """ 
    229239        para = u'' 
    230         pinfo = {'type': 'paragraph'} 
     240        pinfo = {'type': PARAGRAPH} 
    231241        for line in flowed.split('\r\n'): 
    232242            line = line.decode(self.character_set) 
     
    238248                    # exception case: flowed line followed by sig-sep 
    239249                    yield (pinfo, para) 
    240                     pinfo = {'type': 'paragraph'} 
     250                    pinfo = {'type': PARAGRAPH} 
    241251                    para = u'' 
    242                 yield ({'type': 'signature-separator',  
     252                yield ({'type': SIGNATURE_SEPARATOR,  
    243253                        'quotedepth': quotedepth}, line) 
    244254                continue 
     
    248258                    # exception case: flowed line followed by quotedepth change 
    249259                    yield (pinfo, para) 
    250                     pinfo = {'type': 'paragraph'} 
     260                    pinfo = {'type': PARAGRAPH} 
    251261                    para = u'' 
    252262                para += self._stripflow(line) 
     
    259269                    # exception case: flowed line followed by quotedepth change 
    260270                    yield (pinfo, para) 
    261                     pinfo = {'type': 'paragraph'} 
     271                    pinfo = {'type': PARAGRAPH} 
    262272                    para = u'' 
    263273                else: 
    264274                    yield (pinfo, para + line) 
    265                     pinfo = {'type': 'paragraph'} 
     275                    pinfo = {'type': PARAGRAPH} 
    266276                    para = u'' 
    267277                    continue 
    268             yield ({'type': 'fixed', 'quotedepth': quotedepth}, line) 
     278            yield ({'type': FIXED, 'quotedepth': quotedepth}, line) 
    269279             
    270280        if para: 
     
    349359        if quotemarker and quote[-1] != ' ': 
    350360            quotemarker += ' ' 
    351         if type == 'fixed' and not wrap_fixed: 
     361        if type == FIXED and not wrap_fixed: 
    352362            result.append(quotemarker + chunk) 
    353         elif not chunk or type == 'signature-separator': 
     363        elif not chunk or type == SIGNATURE_SEPARATOR: 
    354364            result.append(quotemarker + chunk) 
    355365        else: