~ubuntu-branches/ubuntu/karmic/python-docutils/karmic

« back to all changes in this revision

Viewing changes to docutils/statemachine.py

  • Committer: Bazaar Package Importer
  • Author(s): martin f. krafft
  • Date: 2006-07-10 11:45:05 UTC
  • mfrom: (2.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20060710114505-otkhqcslevewxmz5
Tags: 0.4-3
Added build dependency on python-central (closes: #377580).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Author: David Goodger
2
2
# Contact: goodger@users.sourceforge.net
3
 
# Revision: $Revision: 1.15 $
4
 
# Date: $Date: 2003/07/05 19:34:37 $
 
3
# Revision: $Revision: 4152 $
 
4
# Date: $Date: 2005-12-08 00:46:30 +0100 (Thu, 08 Dec 2005) $
5
5
# Copyright: This module has been placed in the public domain.
6
6
 
7
7
"""
110
110
 
111
111
import sys
112
112
import re
113
 
from types import SliceType as _SliceType
 
113
import types
 
114
import unicodedata
114
115
 
115
116
 
116
117
class StateMachine:
344
345
        finally:
345
346
            self.notify_observers()
346
347
 
 
348
    def get_source(self, line_offset):
 
349
        """Return source of line at absolute line offset `line_offset`."""
 
350
        return self.input_lines.source(line_offset - self.input_offset)
 
351
 
347
352
    def abs_line_offset(self):
348
353
        """Return line offset of current line, from beginning of file."""
349
354
        return self.line_offset + self.input_offset
1112
1117
    # indexing a list with a slice object; they just work).
1113
1118
 
1114
1119
    def __getitem__(self, i):
1115
 
        if isinstance(i, _SliceType):
 
1120
        if isinstance(i, types.SliceType):
1116
1121
            assert i.step in (None, 1),  'cannot handle slice with stride'
1117
1122
            return self.__class__(self.data[i.start:i.stop],
1118
1123
                                  items=self.items[i.start:i.stop],
1121
1126
            return self.data[i]
1122
1127
 
1123
1128
    def __setitem__(self, i, item):
1124
 
        if isinstance(i, _SliceType):
 
1129
        if isinstance(i, types.SliceType):
1125
1130
            assert i.step in (None, 1), 'cannot handle slice with stride'
1126
1131
            if not isinstance(item, ViewList):
1127
1132
                raise TypeError('assigning non-ViewList to ViewList slice')
1396
1401
            block.data = [line[indent:] for line in block.data]
1397
1402
        return block
1398
1403
 
 
1404
    def pad_double_width(self, pad_char):
 
1405
        """
 
1406
        Pad all double-width characters in self by appending `pad_char` to each.
 
1407
        For East Asian language support.
 
1408
        """
 
1409
        if hasattr(unicodedata, 'east_asian_width'):
 
1410
            east_asian_width = unicodedata.east_asian_width
 
1411
        else:
 
1412
            return                      # new in Python 2.4
 
1413
        for i in range(len(self.data)):
 
1414
            line = self.data[i]
 
1415
            if isinstance(line, types.UnicodeType):
 
1416
                new = []
 
1417
                for char in line:
 
1418
                    new.append(char)
 
1419
                    if east_asian_width(char) in 'WF': # 'W'ide & 'F'ull-width
 
1420
                        new.append(pad_char)
 
1421
                self.data[i] = ''.join(new)
 
1422
 
 
1423
    def replace(self, old, new):
 
1424
        """Replace all occurrences of substring `old` with `new`."""
 
1425
        for i in range(len(self.data)):
 
1426
            self.data[i] = self.data[i].replace(old, new)
 
1427
 
1399
1428
 
1400
1429
class StateMachineError(Exception): pass
1401
1430
class UnknownStateError(StateMachineError): pass
1429
1458
def string2lines(astring, tab_width=8, convert_whitespace=0,
1430
1459
                 whitespace=re.compile('[\v\f]')):
1431
1460
    """
1432
 
    Return a list of one-line strings with tabs expanded and no newlines.
 
1461
    Return a list of one-line strings with tabs expanded, no newlines, and
 
1462
    trailing whitespace stripped.
1433
1463
 
1434
1464
    Each tab is expanded with between 1 and `tab_width` spaces, so that the
1435
1465
    next character's index becomes a multiple of `tab_width` (8 by default).
1442
1472
    """
1443
1473
    if convert_whitespace:
1444
1474
        astring = whitespace.sub(' ', astring)
1445
 
    return [s.expandtabs(tab_width) for s in astring.splitlines()]
 
1475
    return [s.expandtabs(tab_width).rstrip() for s in astring.splitlines()]
1446
1476
 
1447
1477
def _exception_data():
1448
1478
    """