src/reportlab/lib/sequencer.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4668 f8d6dc8f07fc
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 339
diff changeset
     2
#see license.txt for license details
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3902
diff changeset
     3
__version__='3.3.0'
3029
eded59f94021 adding docstrings to lib
andy
parents: 3028
diff changeset
     4
__doc__="""A Sequencer class counts things. It aids numbering and formatting lists."""
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
     5
__all__='''Sequencer getSequencer setSequencer'''.split()
297
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
     6
#
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
     7
# roman numbers conversion thanks to
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
     8
#
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
     9
# fredrik lundh, november 1996 (based on a C hack from 1984)
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    10
#
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    11
# fredrik@pythonware.com
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    12
# http://www.pythonware.com
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 494
diff changeset
    13
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    14
_RN_TEMPLATES = [ 0, 0o1, 0o11, 0o111, 0o12, 0o2, 0o21, 0o211, 0o2111, 0o13 ]
297
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    15
_RN_LETTERS = "IVXLCDM"
3822
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
    16
from reportlab import isPy3
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 494
diff changeset
    17
297
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    18
def _format_I(value):
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    19
    if value < 0 or value > 3999:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    20
        raise ValueError("illegal value")
297
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    21
    str = ""
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    22
    base = -1
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    23
    while value:
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    24
        value, index = divmod(value, 10)
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    25
        tmp = _RN_TEMPLATES[index]
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    26
        while tmp:
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    27
            tmp, index = divmod(tmp, 8)
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    28
            str = _RN_LETTERS[index+base] + str
3822
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
    29
        base += 2
297
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    30
    return str
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    31
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
    32
def _format_i(num):
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
    33
    return _format_I(num).lower()
262
2acc8ccdaa4c Doc strings, sequencer
andy_robinson
parents: 253
diff changeset
    34
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
    35
def _format_123(num):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    36
    """The simplest formatter"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    37
    return str(num)
262
2acc8ccdaa4c Doc strings, sequencer
andy_robinson
parents: 253
diff changeset
    38
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
    39
def _format_ABC(num):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    40
    """Uppercase.  Wraps around at 26."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    41
    n = (num -1) % 26
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    42
    return chr(n+65)
262
2acc8ccdaa4c Doc strings, sequencer
andy_robinson
parents: 253
diff changeset
    43
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
    44
def _format_abc(num):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    45
    """Lowercase.  Wraps around at 26."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    46
    n = (num -1) % 26
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    47
    return chr(n+97)
262
2acc8ccdaa4c Doc strings, sequencer
andy_robinson
parents: 253
diff changeset
    48
3479
9c29cc835739 support for ordered/bulleted lists
rgbecker
parents: 3029
diff changeset
    49
_type2formatter = {
9c29cc835739 support for ordered/bulleted lists
rgbecker
parents: 3029
diff changeset
    50
        'I':_format_I,
9c29cc835739 support for ordered/bulleted lists
rgbecker
parents: 3029
diff changeset
    51
        'i':_format_i,
9c29cc835739 support for ordered/bulleted lists
rgbecker
parents: 3029
diff changeset
    52
        '1':_format_123,
9c29cc835739 support for ordered/bulleted lists
rgbecker
parents: 3029
diff changeset
    53
        'A':_format_ABC,
9c29cc835739 support for ordered/bulleted lists
rgbecker
parents: 3029
diff changeset
    54
        'a':_format_abc,
9c29cc835739 support for ordered/bulleted lists
rgbecker
parents: 3029
diff changeset
    55
        }
9c29cc835739 support for ordered/bulleted lists
rgbecker
parents: 3029
diff changeset
    56
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
    57
class _Counter:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    58
    """Private class used by Sequencer.  Each counter
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    59
    knows its format, and the IDs of anything it
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    60
    resets, as well as its value. Starts at zero
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    61
    and increments just before you get the new value,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    62
    so that it is still 'Chapter 5' and not 'Chapter 6'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    63
    when you print 'Figure 5.1'"""
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 494
diff changeset
    64
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    65
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    66
        self._base = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    67
        self._value = self._base
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    68
        self._formatter = _format_123
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    69
        self._resets = []
262
2acc8ccdaa4c Doc strings, sequencer
andy_robinson
parents: 253
diff changeset
    70
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    71
    def setFormatter(self, formatFunc):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    72
        self._formatter = formatFunc
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
    73
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    74
    def reset(self, value=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    75
        if value:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    76
            self._value = value
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    77
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    78
            self._value = self._base
262
2acc8ccdaa4c Doc strings, sequencer
andy_robinson
parents: 253
diff changeset
    79
3886
af36456f03d8 sequencer.py: fix for python2
robin
parents: 3884
diff changeset
    80
    def next(self):
3822
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
    81
        self._value += 1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    82
        v = self._value
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    83
        for counter in self._resets:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    84
            counter.reset()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    85
        return v
3902
70800e74a6e7 sequencer.py: fix missing method
robin
parents: 3886
diff changeset
    86
    __next__ = next
262
2acc8ccdaa4c Doc strings, sequencer
andy_robinson
parents: 253
diff changeset
    87
2180
ede738001655 renamed methid this to _this - java keyword
dragan1
parents: 2102
diff changeset
    88
    def _this(self):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
    89
        return self._value
262
2acc8ccdaa4c Doc strings, sequencer
andy_robinson
parents: 253
diff changeset
    90
3886
af36456f03d8 sequencer.py: fix for python2
robin
parents: 3884
diff changeset
    91
    if isPy3:
af36456f03d8 sequencer.py: fix for python2
robin
parents: 3884
diff changeset
    92
        def nextf(self):
af36456f03d8 sequencer.py: fix for python2
robin
parents: 3884
diff changeset
    93
            """Returns next value formatted"""
af36456f03d8 sequencer.py: fix for python2
robin
parents: 3884
diff changeset
    94
            return self._formatter(next(self))
af36456f03d8 sequencer.py: fix for python2
robin
parents: 3884
diff changeset
    95
    else:
af36456f03d8 sequencer.py: fix for python2
robin
parents: 3884
diff changeset
    96
        def nextf(self):
af36456f03d8 sequencer.py: fix for python2
robin
parents: 3884
diff changeset
    97
            """Returns next value formatted"""
af36456f03d8 sequencer.py: fix for python2
robin
parents: 3884
diff changeset
    98
            return self._formatter(self.__next__())
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
    99
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   100
    def thisf(self):
2180
ede738001655 renamed methid this to _this - java keyword
dragan1
parents: 2102
diff changeset
   101
        return self._formatter(self._this())
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
   102
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   103
    def chain(self, otherCounter):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   104
        if not otherCounter in self._resets:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   105
            self._resets.append(otherCounter)
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
   106
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   107
class Sequencer:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   108
    """Something to make it easy to number paragraphs, sections,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   109
    images and anything else.  The features include registering
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   110
    new string formats for sequences, and 'chains' whereby
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   111
    some counters are reset when their parents.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   112
    It keeps track of a number of
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   113
    'counters', which are created on request:
3028
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   114
    Usage::
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   115
    
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   116
        >>> seq = layout.Sequencer()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   117
        >>> seq.next('Bullets')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   118
        1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   119
        >>> seq.next('Bullets')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   120
        2
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   121
        >>> seq.next('Bullets')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   122
        3
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   123
        >>> seq.reset('Bullets')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   124
        >>> seq.next('Bullets')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   125
        1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   126
        >>> seq.next('Figures')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   127
        1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   128
        >>>
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   129
    """
297
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
   130
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   131
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   132
        self._counters = {}  #map key to current number
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   133
        self._formatters = {}
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   134
        self._reset()
262
2acc8ccdaa4c Doc strings, sequencer
andy_robinson
parents: 253
diff changeset
   135
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   136
    def _reset(self):
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   137
        self._counters.clear()
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   138
        self._formatters.clear()
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   139
        self._formatters.update({
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   140
            # the formats it knows initially
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   141
            '1':_format_123,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   142
            'A':_format_ABC,
2368
791a362e9cae added seqchain/format tags
rgbecker
parents: 2332
diff changeset
   143
            'a':_format_abc,
791a362e9cae added seqchain/format tags
rgbecker
parents: 2332
diff changeset
   144
            'I':_format_I,
791a362e9cae added seqchain/format tags
rgbecker
parents: 2332
diff changeset
   145
            'i':_format_i,
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   146
            })
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   147
        d = dict(_counters=self._counters,_formatters=self._formatters)
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   148
        self.__dict__.clear()
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   149
        self.__dict__.update(d)
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   150
        self._defaultCounter = None
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
   151
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   152
    def _getCounter(self, counter=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   153
        """Creates one if not present"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   154
        try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   155
            return self._counters[counter]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   156
        except KeyError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   157
            cnt = _Counter()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   158
            self._counters[counter] = cnt
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   159
            return cnt
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
   160
2180
ede738001655 renamed methid this to _this - java keyword
dragan1
parents: 2102
diff changeset
   161
    def _this(self, counter=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   162
        """Retrieves counter value but does not increment. For
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   163
        new counters, sets base value to 1."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   164
        if not counter:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   165
            counter = self._defaultCounter
2180
ede738001655 renamed methid this to _this - java keyword
dragan1
parents: 2102
diff changeset
   166
        return self._getCounter(counter)._this()
297
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
   167
3822
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   168
    if isPy3:
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   169
        def __next__(self):
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   170
            """Retrieves the numeric value for the given counter, then
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   171
            increments it by one.  New counters start at one."""
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   172
            return next(self._getCounter(self._defaultCounter))
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   173
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   174
        def next(self,counter=None):
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   175
            if not counter:
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   176
                return next(self)
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   177
            else:
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   178
                dc = self._defaultCounter
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   179
                try:
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   180
                    self._defaultCounter = counter
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   181
                    return next(self)
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   182
                finally:
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   183
                    self._defaultCounter = dc
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   184
    else:
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   185
        def next(self, counter=None):
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   186
            """Retrieves the numeric value for the given counter, then
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   187
            increments it by one.  New counters start at one."""
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   188
            if not counter:
63cc69aed3b4 sequencer.py: fixed for python3 magics
robin
parents: 3721
diff changeset
   189
                counter = self._defaultCounter
3886
af36456f03d8 sequencer.py: fix for python2
robin
parents: 3884
diff changeset
   190
            return self._getCounter(counter).next()
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
   191
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   192
    def thisf(self, counter=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   193
        if not counter:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   194
            counter = self._defaultCounter
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   195
        return self._getCounter(counter).thisf()
297
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
   196
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   197
    def nextf(self, counter=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   198
        """Retrieves the numeric value for the given counter, then
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   199
        increments it by one.  New counters start at one."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   200
        if not counter:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   201
            counter = self._defaultCounter
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   202
        return self._getCounter(counter).nextf()
297
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
   203
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   204
    def setDefaultCounter(self, default=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   205
        """Changes the key used for the default"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   206
        self._defaultCounter = default
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
   207
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   208
    def registerFormat(self, format, func):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   209
        """Registers a new formatting function.  The funtion
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   210
        must take a number as argument and return a string;
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   211
        fmt is a short menmonic string used to access it."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   212
        self._formatters[format] = func
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
   213
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   214
    def setFormat(self, counter, format):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   215
        """Specifies that the given counter should use
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   216
        the given format henceforth."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   217
        func = self._formatters[format]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   218
        self._getCounter(counter).setFormatter(func)
297
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
   219
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   220
    def reset(self, counter=None, base=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   221
        if not counter:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   222
            counter = self._defaultCounter
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   223
        self._getCounter(counter)._value = base
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   224
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   225
    def chain(self, parent, child):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   226
        p = self._getCounter(parent)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   227
        c = self._getCounter(child)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   228
        p.chain(c)
297
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
   229
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   230
    def __getitem__(self, key):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   231
        """Allows compact notation to support the format function.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   232
        s['key'] gets current value, s['key+'] increments."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   233
        if key[-1:] == '+':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   234
            counter = key[:-1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   235
            return self.nextf(counter)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   236
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   237
            return self.thisf(key)
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
   238
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   239
    def format(self, template):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   240
        """The crowning jewels - formats multi-level lists."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   241
        return template % self
266
081154da1a78 Added Sequencer and associated XML tags
andy_robinson
parents: 263
diff changeset
   242
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   243
    def dump(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   244
        """Write current state to stdout for diagnostics"""
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   245
        counters = list(self._counters.items())
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   246
        counters.sort()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   247
        print('Sequencer dump:')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   248
        for (key, counter) in counters:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   249
            print('    %s: value = %d, base = %d, format example = %s' % (
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   250
                key, counter._this(), counter._base, counter.thisf()))
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 266
diff changeset
   251
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 266
diff changeset
   252
"""Your story builder needs to set this to"""
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 266
diff changeset
   253
_sequencer = None
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 494
diff changeset
   254
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 266
diff changeset
   255
def getSequencer():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   256
    global _sequencer
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   257
    if _sequencer is None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   258
        _sequencer = Sequencer()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   259
    return  _sequencer
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 266
diff changeset
   260
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 266
diff changeset
   261
def setSequencer(seq):
2102
c2c775b01271 Add missing global statement
rgbecker
parents: 1683
diff changeset
   262
    global _sequencer
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   263
    s = _sequencer
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   264
    _sequencer = seq
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   265
    return s
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 266
diff changeset
   266
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   267
def _reset():
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   268
    global _sequencer
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   269
    if _sequencer:
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   270
        _sequencer._reset()
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   271
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   272
from reportlab.rl_config import register_reset
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   273
register_reset(_reset)
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   274
del register_reset
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2368
diff changeset
   275
262
2acc8ccdaa4c Doc strings, sequencer
andy_robinson
parents: 253
diff changeset
   276
def test():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   277
    s = Sequencer()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   278
    print('Counting using default sequence: %d %d %d' % (next(s),next(s), next(s)))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   279
    print('Counting Figures: Figure %d, Figure %d, Figure %d' % (
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   280
        s.next('figure'), s.next('figure'), s.next('figure')))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   281
    print('Back to default again: %d' % next(s))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   282
    s.setDefaultCounter('list1')
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   283
    print('Set default to list1: %d %d %d' % (next(s),next(s), next(s)))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   284
    s.setDefaultCounter()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   285
    print('Set default to None again: %d %d %d' % (next(s),next(s), next(s)))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   286
    print()
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   287
    print('Creating Appendix counter with format A, B, C...')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   288
    s.setFormat('Appendix', 'A')
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   289
    print('    Appendix %s, Appendix %s, Appendix %s' % (
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   290
        s.nextf('Appendix'),    s.nextf('Appendix'),s.nextf('Appendix')))
263
9259181a6ad0 Largely complete class for numbering lists, figures and chapters
andy_robinson
parents: 262
diff changeset
   291
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   292
    def format_french(num):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   293
        return ('un','deux','trois','quatre','cinq')[(num-1)%5]
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   294
    print()
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   295
    print('Defining a custom format with french words:')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   296
    s.registerFormat('french', format_french)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   297
    s.setFormat('FrenchList', 'french')
3884
3bc59a4c3c21 attempt to remove python 3 only print stuff
robin
parents: 3822
diff changeset
   298
    print('   ' +(' '.join(str(s.nextf('FrenchList')) for i in range(1,6))))
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   299
    print()
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   300
    print('Chaining H1 and H2 - H2 goes back to one when H1 increases')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 562
diff changeset
   301
    s.chain('H1','H2')
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   302
    print('    H1 = %d' % s.next('H1'))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   303
    print('      H2 = %d' % s.next('H2'))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   304
    print('      H2 = %d' % s.next('H2'))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   305
    print('      H2 = %d' % s.next('H2'))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   306
    print('    H1 = %d' % s.next('H1'))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   307
    print('      H2 = %d' % s.next('H2'))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   308
    print('      H2 = %d' % s.next('H2'))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   309
    print('      H2 = %d' % s.next('H2'))
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   310
    print()
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   311
    print('GetItem notation - append a plus to increment')
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   312
    print('    seq["Appendix"] = %s' % s["Appendix"])
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   313
    print('    seq["Appendix+"] = %s' % s["Appendix+"])
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   314
    print('    seq["Appendix+"] = %s' % s["Appendix+"])
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   315
    print('    seq["Appendix"] = %s' % s["Appendix"])
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   316
    print()
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   317
    print('Finally, string format notation for nested lists.  Cool!')
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   318
    print('The expression ("Figure %(Chapter)s.%(Figure+)s" % seq) gives:')
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   319
    print('    Figure %(Chapter)s.%(Figure+)s' % s)
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   320
    print('    Figure %(Chapter)s.%(Figure+)s' % s)
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   321
    print('    Figure %(Chapter)s.%(Figure+)s' % s)
297
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
   322
fb5927952085 Added Roman formatters
rgbecker
parents: 279
diff changeset
   323
262
2acc8ccdaa4c Doc strings, sequencer
andy_robinson
parents: 253
diff changeset
   324
if __name__=='__main__':
2102
c2c775b01271 Add missing global statement
rgbecker
parents: 1683
diff changeset
   325
    test()