1
2 '''
3 Base generator object implementations.
4
5 @author: Michael Eddington
6 @version: $Id: Peach.generator-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $
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 import sys
38 import traceback
39
41 '''
42 Generators generate data. Examples of generators could be a static
43 string or integer, a string repeater, etc. Generators can be "incremented"
44 by calling C{next()} to produce the next varient of data. Generators can
45 be fairly complex, comainting sub-generators to build things like packets.
46
47 Generators support the interator protocol and can be used as such.
48
49 When building a Generator one should keep in mind that the value from a
50 generator could be asked for more then once per "round". Also it is
51 recommended that you use the default C{getValue()} implementation and override
52 the C{getRawValue()} method instead.
53
54 @see: L{SimpleGenerator}
55 '''
56
58 '''
59 Base constructor, please call me!
60 '''
61
62 self._group = None
63 self._transformer = None
64 self._identity = None
65 self._name = None
66
67
68
69
70
71
73 '''
74 Who are we and were do we come from?
75 '''
76
77 return self._identity
78
80 '''
81 Return iterator for Generator object. This is always the
82 Generator object itself.
83
84 @rtype: Generator
85 @return: Returns iterator, this is always self.
86 '''
87 return self
88
90 '''
91 Next value. OVERRIDE
92
93 From Python docs on next():
94
95 I{The intention of the protocol is that once an iterator's next() method
96 raises StopIteration, it will continue to do so on subsequent calls.
97 Implementations that do not obey this property are deemed broken. (This
98 constraint was added in Python 2.3; in Python 2.2, various iterators are
99 broken according to this rule.)}
100
101 For Generators, please use the GeneratorCompleted exception instead of
102 StopIteration (its a subclass).
103 '''
104
105 raise GeneratorCompleted("Peach.generator.Generator")
106
108 '''
109 Return data, passed through a transformer if set.
110
111 @rtype: string
112 @return: Returns generated data
113 '''
114
115 if self._transformer != None:
116 return self._transformer.encode(self.getRawValue())
117
118 return self.getRawValue()
119
121 '''
122 Return raw value w/o passing through transformer if set. OVERRIDE
123
124 @rtype: string
125 @return: Data before transformations
126 '''
127
128 return None
129
131 '''
132 Get group this Generator belongs to. Groups are used to increment sets
133 of Generators.
134
135 @rtype: Group
136 @return: Returns Group this generator belongs to
137 '''
138 return self._group
139
141 '''
142 Set group this Generator belongs to. This function will automaticly add
143 the Generator into the Group.
144
145 Groups are used to increment sets
146 of Generators.
147
148 @type group: Group
149 @param group: Group this generator belongs to
150 '''
151 self._group = group
152 if self._group != None:
153 self._group.addGenerator(self)
154
164
177
179 '''
180 Called to reset the generator to its initial state. OVERRIDE
181 '''
182 pass
183
185 '''
186 Get the name of this generator. Usefull for debugging.
187 '''
188 return self._name
189
191 '''
192 Set the name of this generator. Usefull for debugging complex
193 data generators. Stacktraces may end up in a generator creation
194 statement giving limited feedback on which generator in an array
195 might be causing the problem.
196
197 @type name: string
198 @param name: Name of generator
199 '''
200 self._name = name
201
202
204 '''
205 A simple generator contains another, possibly complex generator statement.
206 Usefull when breaking things apart for reuse.
207
208 To use, simply create a class that contains a _generator:
209
210 class MySimpleGenerator(SimpleGenerator):
211 def __init__(self, group = None):
212 SimpleGenerator.__init__(self, group)
213 self._generator = GeneratorList(None, [
214 Static('AAA'),
215 Repeater(None, Static('A'), 1, 100)
216 ])
217
218 NOTE: Do not set group on you generators unless they will not be incremented
219 by self._generator.next().
220
221 @see: L{Generator}
222 '''
223
233
236
239
242
243
245 '''
246 Exception indicating that the generator has completed all
247 permutations of its data
248 '''
249 pass
250
251
252
253
254