1
<?xml version="1.0" encoding="ascii"?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3
"DTD/xhtml1-transitional.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>genshi.core.Stream</title>
7
<link rel="stylesheet" href="epydoc.css" type="text/css" />
8
<script type="text/javascript" src="epydoc.js"></script>
11
<body bgcolor="white" text="black" link="blue" vlink="#204080"
13
<!-- ==================== NAVIGATION BAR ==================== -->
14
<table class="navbar" border="0" width="100%" cellpadding="0"
15
bgcolor="#a0c0ff" cellspacing="0">
18
<th> <a
19
href="genshi-module.html">Home</a> </th>
22
<th> <a
23
href="module-tree.html">Trees</a> </th>
26
<th> <a
27
href="identifier-index.html">Indices</a> </th>
30
<th> <a
31
href="help.html">Help</a> </th>
33
<!-- Project homepage -->
34
<th class="navbar" align="right" width="100%">
35
<table border="0" cellpadding="0" cellspacing="0">
36
<tr><th class="navbar" align="center"
37
><a class="navbar" target="_top" href="../index.html">Documentation Index</a></th>
41
<table width="100%" cellpadding="0" cellspacing="0">
44
<span class="breadcrumbs">
45
<a href="genshi-module.html">Package genshi</a> ::
46
<a href="genshi.core-module.html">Module core</a> ::
51
<table cellpadding="0" cellspacing="0">
52
<!-- hide/show private -->
57
<!-- ==================== CLASS DESCRIPTION ==================== -->
58
<h1 class="epydoc">Class Stream</h1><br /><br />
59
<pre class="base-tree">
62
<strong class="uidshort">Stream</strong>
66
<p>Represents a stream of markup events.</p>
67
<p>This class is basically an iterator over the events.</p>
68
<p>Stream events are tuples of the form:</p>
69
<pre class="rst-literal-block">
70
(kind, data, position)
72
<p>where <tt class="rst-docutils literal"><span class="pre">kind</span></tt> is the event kind (such as <a href="genshi.core.Stream-class.html#START" class="link">START</a>, <a href="genshi.core.Stream-class.html#END" class="link">END</a>, <a href="genshi.core.Stream-class.html#TEXT" class="link">TEXT</a>, etc),
73
<tt class="rst-docutils literal"><span class="pre">data</span></tt> depends on the kind of event, and <tt class="rst-docutils literal"><span class="pre">position</span></tt> is a
74
<tt class="rst-docutils literal"><span class="pre">(filename,</span> <span class="pre">line,</span> <span class="pre">offset)</span></tt> tuple that contains the location of the
75
original element or text in the input. If the original location is unknown,
76
<tt class="rst-docutils literal"><span class="pre">position</span></tt> is <tt class="rst-docutils literal"><span class="pre">(None,</span> <span class="pre">-1,</span> <span class="pre">-1)</span></tt>.</p>
77
<p>Also provided are ways to serialize the stream to text. The <a href="genshi.core.Stream-class.html#serialize" class="link">serialize()</a>
78
method will return an iterator over generated strings, while <a href="genshi.core.Stream-class.html#render" class="link">render()</a>
79
returns the complete generated text at once. Both accept various parameters
80
that impact the way the stream is serialized.</p><br /><br />
82
<!-- ==================== INSTANCE METHODS ==================== -->
83
<a name="section-InstanceMethods"></a>
84
<table class="summary" border="1" cellpadding="3"
85
cellspacing="0" width="100%" bgcolor="white">
86
<tr bgcolor="#70b0f0" class="table-header">
87
<td align="left" colspan="2" class="table-header">
88
<span class="table-header">Instance Methods</span></td>
91
<td width="15%" align="right" valign="top" class="summary">
92
<span class="summary-type"> </span>
93
</td><td class="summary">
94
<table width="100%" cellpadding="0" cellspacing="0" border="0">
96
<td><span class="summary-sig"><a href="genshi.core.Stream-class.html#__init__" class="summary-sig-name">__init__</a>(<span class="summary-sig-arg">self</span>,
97
<span class="summary-sig-arg">events</span>)</span><br />
98
Initialize the stream with a sequence of markup events.</td>
99
<td align="right" valign="top">
109
<td width="15%" align="right" valign="top" class="summary">
110
<span class="summary-type"> </span>
111
</td><td class="summary">
112
<table width="100%" cellpadding="0" cellspacing="0" border="0">
114
<td><span class="summary-sig"><a name="__iter__"></a><span class="summary-sig-name">__iter__</span>(<span class="summary-sig-arg">self</span>)</span></td>
115
<td align="right" valign="top">
125
<td width="15%" align="right" valign="top" class="summary">
126
<span class="summary-type"> </span>
127
</td><td class="summary">
128
<table width="100%" cellpadding="0" cellspacing="0" border="0">
130
<td><span class="summary-sig"><a href="genshi.core.Stream-class.html#__or__" class="summary-sig-name">__or__</a>(<span class="summary-sig-arg">self</span>,
131
<span class="summary-sig-arg">function</span>)</span><br />
132
Override the "bitwise or" operator to apply filters or serializers
133
to the stream, providing a syntax similar to pipes on Unix shells.</td>
134
<td align="right" valign="top">
144
<td width="15%" align="right" valign="top" class="summary">
145
<span class="summary-type"> </span>
146
</td><td class="summary">
147
<table width="100%" cellpadding="0" cellspacing="0" border="0">
149
<td><span class="summary-sig"><a href="genshi.core.Stream-class.html#filter" class="summary-sig-name">filter</a>(<span class="summary-sig-arg">self</span>,
150
<span class="summary-sig-arg">*filters</span>)</span><br />
151
Apply filters to the stream.</td>
152
<td align="right" valign="top">
162
<td width="15%" align="right" valign="top" class="summary">
163
<span class="summary-type"> </span>
164
</td><td class="summary">
165
<table width="100%" cellpadding="0" cellspacing="0" border="0">
167
<td><span class="summary-sig"><a href="genshi.core.Stream-class.html#render" class="summary-sig-name">render</a>(<span class="summary-sig-arg">self</span>,
168
<span class="summary-sig-arg">method</span>=<span class="summary-sig-default"><code class="variable-quote">'</code><code class="variable-string">xml</code><code class="variable-quote">'</code></span>,
169
<span class="summary-sig-arg">encoding</span>=<span class="summary-sig-default"><code class="variable-quote">'</code><code class="variable-string">utf-8</code><code class="variable-quote">'</code></span>,
170
<span class="summary-sig-arg">**kwargs</span>)</span><br />
171
Return a string representation of the stream.</td>
172
<td align="right" valign="top">
182
<td width="15%" align="right" valign="top" class="summary">
183
<span class="summary-type"> </span>
184
</td><td class="summary">
185
<table width="100%" cellpadding="0" cellspacing="0" border="0">
187
<td><span class="summary-sig"><a href="genshi.core.Stream-class.html#select" class="summary-sig-name">select</a>(<span class="summary-sig-arg">self</span>,
188
<span class="summary-sig-arg">path</span>,
189
<span class="summary-sig-arg">namespaces</span>=<span class="summary-sig-default">None</span>,
190
<span class="summary-sig-arg">variables</span>=<span class="summary-sig-default">None</span>)</span><br />
191
Return a new stream that contains the events matching the given
192
XPath expression.</td>
193
<td align="right" valign="top">
203
<td width="15%" align="right" valign="top" class="summary">
204
<span class="summary-type"> </span>
205
</td><td class="summary">
206
<table width="100%" cellpadding="0" cellspacing="0" border="0">
208
<td><span class="summary-sig"><a href="genshi.core.Stream-class.html#serialize" class="summary-sig-name">serialize</a>(<span class="summary-sig-arg">self</span>,
209
<span class="summary-sig-arg">method</span>=<span class="summary-sig-default"><code class="variable-quote">'</code><code class="variable-string">xml</code><code class="variable-quote">'</code></span>,
210
<span class="summary-sig-arg">**kwargs</span>)</span><br />
211
Generate strings corresponding to a specific serialization of the
213
<td align="right" valign="top">
223
<td width="15%" align="right" valign="top" class="summary">
224
<span class="summary-type"> </span>
225
</td><td class="summary">
226
<table width="100%" cellpadding="0" cellspacing="0" border="0">
228
<td><span class="summary-sig"><a href="genshi.core.Stream-class.html#__str__" class="summary-sig-name">__str__</a>(<span class="summary-sig-arg">self</span>)</span><br />
230
<td align="right" valign="top">
240
<td width="15%" align="right" valign="top" class="summary">
241
<span class="summary-type"> </span>
242
</td><td class="summary">
243
<table width="100%" cellpadding="0" cellspacing="0" border="0">
245
<td><span class="summary-sig"><a name="__unicode__"></a><span class="summary-sig-name">__unicode__</span>(<span class="summary-sig-arg">self</span>)</span></td>
246
<td align="right" valign="top">
256
<td colspan="2" class="summary">
257
<p class="indent-wrapped-lines"><b>Inherited from <code>object</code></b>:
258
<code>__delattr__</code>,
259
<code>__getattribute__</code>,
260
<code>__hash__</code>,
261
<code>__new__</code>,
262
<code>__reduce__</code>,
263
<code>__reduce_ex__</code>,
264
<code>__repr__</code>,
265
<code>__setattr__</code>
270
<!-- ==================== CLASS VARIABLES ==================== -->
271
<a name="section-ClassVariables"></a>
272
<table class="summary" border="1" cellpadding="3"
273
cellspacing="0" width="100%" bgcolor="white">
274
<tr bgcolor="#70b0f0" class="table-header">
275
<td align="left" colspan="2" class="table-header">
276
<span class="table-header">Class Variables</span></td>
279
<td width="15%" align="right" valign="top" class="summary">
280
<span class="summary-type"> </span>
281
</td><td class="summary">
282
<a name="START"></a><span class="summary-name">START</span> = <code title="'START'">'START'</code><br />
287
<td width="15%" align="right" valign="top" class="summary">
288
<span class="summary-type"> </span>
289
</td><td class="summary">
290
<a name="END"></a><span class="summary-name">END</span> = <code title="'END'">'END'</code><br />
295
<td width="15%" align="right" valign="top" class="summary">
296
<span class="summary-type"> </span>
297
</td><td class="summary">
298
<a name="TEXT"></a><span class="summary-name">TEXT</span> = <code title="'TEXT'">'TEXT'</code><br />
303
<td width="15%" align="right" valign="top" class="summary">
304
<span class="summary-type"> </span>
305
</td><td class="summary">
306
<a name="DOCTYPE"></a><span class="summary-name">DOCTYPE</span> = <code title="'DOCTYPE'">'DOCTYPE'</code><br />
311
<td width="15%" align="right" valign="top" class="summary">
312
<span class="summary-type"> </span>
313
</td><td class="summary">
314
<a name="START_NS"></a><span class="summary-name">START_NS</span> = <code title="'START_NS'">'START_NS'</code><br />
315
start namespace mapping
319
<td width="15%" align="right" valign="top" class="summary">
320
<span class="summary-type"> </span>
321
</td><td class="summary">
322
<a name="END_NS"></a><span class="summary-name">END_NS</span> = <code title="'END_NS'">'END_NS'</code><br />
323
end namespace mapping
327
<td width="15%" align="right" valign="top" class="summary">
328
<span class="summary-type"> </span>
329
</td><td class="summary">
330
<a name="START_CDATA"></a><span class="summary-name">START_CDATA</span> = <code title="'START_CDATA'">'START_CDATA'</code><br />
335
<td width="15%" align="right" valign="top" class="summary">
336
<span class="summary-type"> </span>
337
</td><td class="summary">
338
<a name="END_CDATA"></a><span class="summary-name">END_CDATA</span> = <code title="'END_CDATA'">'END_CDATA'</code><br />
343
<td width="15%" align="right" valign="top" class="summary">
344
<span class="summary-type"> </span>
345
</td><td class="summary">
346
<a name="PI"></a><span class="summary-name">PI</span> = <code title="'PI'">'PI'</code><br />
347
processing instruction
351
<td width="15%" align="right" valign="top" class="summary">
352
<span class="summary-type"> </span>
353
</td><td class="summary">
354
<a name="COMMENT"></a><span class="summary-name">COMMENT</span> = <code title="'COMMENT'">'COMMENT'</code><br />
359
<!-- ==================== PROPERTIES ==================== -->
360
<a name="section-Properties"></a>
361
<table class="summary" border="1" cellpadding="3"
362
cellspacing="0" width="100%" bgcolor="white">
363
<tr bgcolor="#70b0f0" class="table-header">
364
<td align="left" colspan="2" class="table-header">
365
<span class="table-header">Properties</span></td>
368
<td width="15%" align="right" valign="top" class="summary">
369
<span class="summary-type"> </span>
370
</td><td class="summary">
371
<a name="events"></a><span class="summary-name">events</span>
375
<td colspan="2" class="summary">
376
<p class="indent-wrapped-lines"><b>Inherited from <code>object</code></b>:
377
<code>__class__</code>
382
<!-- ==================== METHOD DETAILS ==================== -->
383
<a name="section-MethodDetails"></a>
384
<table class="details" border="1" cellpadding="3"
385
cellspacing="0" width="100%" bgcolor="white">
386
<tr bgcolor="#70b0f0" class="table-header">
387
<td align="left" colspan="2" class="table-header">
388
<span class="table-header">Method Details</span></td>
391
<a name="__init__"></a>
393
<table class="details" border="1" cellpadding="3"
394
cellspacing="0" width="100%" bgcolor="white">
396
<table width="100%" cellpadding="0" cellspacing="0" border="0">
397
<tr valign="top"><td>
398
<h3 class="epydoc"><span class="sig"><span class="sig-name">__init__</span>(<span class="sig-arg">self</span>,
399
<span class="sig-arg">events</span>)</span>
400
<br /><em class="fname">(Constructor)</em>
402
</td><td align="right" valign="top"
407
Initialize the stream with a sequence of markup events.
410
<dd><ul class="nomargin-top">
411
<li><strong class="pname"><code>events</code></strong> - a sequence or iterable providing the events</li>
419
<a name="__or__"></a>
421
<table class="details" border="1" cellpadding="3"
422
cellspacing="0" width="100%" bgcolor="white">
424
<table width="100%" cellpadding="0" cellspacing="0" border="0">
425
<tr valign="top"><td>
426
<h3 class="epydoc"><span class="sig"><span class="sig-name">__or__</span>(<span class="sig-arg">self</span>,
427
<span class="sig-arg">function</span>)</span>
428
<br /><em class="fname">(Or operator)</em>
430
</td><td align="right" valign="top"
435
<p>Override the "bitwise or" operator to apply filters or serializers
436
to the stream, providing a syntax similar to pipes on Unix shells.</p>
437
<p>Assume the following stream produced by the <a href="genshi.input-module.html#HTML" class="link">HTML</a> function:</p>
438
<pre class="py-doctest">
439
<span class="py-prompt">>>> </span><span class="py-keyword">from</span> genshi.input <span class="py-keyword">import</span> HTML
440
<span class="py-prompt">>>> </span>html = HTML(<span class="py-string">'''<p onclick="alert('Whoa')">Hello, world!</p>'''</span>)
441
<span class="py-prompt">>>> </span><span class="py-keyword">print</span> html
442
<span class="py-output"><p onclick="alert('Whoa')">Hello, world!</p></span></pre>
443
<p>A filter such as the HTML sanitizer can be applied to that stream using
444
the pipe notation as follows:</p>
445
<pre class="py-doctest">
446
<span class="py-prompt">>>> </span><span class="py-keyword">from</span> genshi.filters <span class="py-keyword">import</span> HTMLSanitizer
447
<span class="py-prompt">>>> </span>sanitizer = HTMLSanitizer()
448
<span class="py-prompt">>>> </span><span class="py-keyword">print</span> html | sanitizer
449
<span class="py-output"><p>Hello, world!</p></span></pre>
450
<p>Filters can be any function that accepts and produces a stream (where
451
a stream is anything that iterates over events):</p>
452
<pre class="py-doctest">
453
<span class="py-prompt">>>> </span><span class="py-keyword">def</span> <span class="py-defname">uppercase</span>(stream):
454
<span class="py-more">... </span> <span class="py-keyword">for</span> kind, data, pos <span class="py-keyword">in</span> stream:
455
<span class="py-more">... </span> <span class="py-keyword">if</span> kind <span class="py-keyword">is</span> TEXT:
456
<span class="py-more">... </span> data = data.upper()
457
<span class="py-more">... </span> yield kind, data, pos
458
<span class="py-prompt">>>> </span><span class="py-keyword">print</span> html | sanitizer | uppercase
459
<span class="py-output"><p>HELLO, WORLD!</p></span></pre>
460
<p>Serializers can also be used with this notation:</p>
461
<pre class="py-doctest">
462
<span class="py-prompt">>>> </span><span class="py-keyword">from</span> genshi.output <span class="py-keyword">import</span> TextSerializer
463
<span class="py-prompt">>>> </span>output = TextSerializer()
464
<span class="py-prompt">>>> </span><span class="py-keyword">print</span> html | sanitizer | uppercase | output
465
<span class="py-output">HELLO, WORLD!</span></pre>
466
<p>Commonly, serializers should be used at the end of the "pipeline";
467
using them somewhere in the middle may produce unexpected results.</p>
472
<a name="filter"></a>
474
<table class="details" border="1" cellpadding="3"
475
cellspacing="0" width="100%" bgcolor="white">
477
<table width="100%" cellpadding="0" cellspacing="0" border="0">
478
<tr valign="top"><td>
479
<h3 class="epydoc"><span class="sig"><span class="sig-name">filter</span>(<span class="sig-arg">self</span>,
480
<span class="sig-arg">*filters</span>)</span>
482
</td><td align="right" valign="top"
487
<p>Apply filters to the stream.</p>
488
<p>This method returns a new stream with the given filters applied. The
489
filters must be callables that accept the stream object as parameter,
490
and return the filtered stream.</p>
492
<pre class="rst-literal-block">
493
stream.filter(filter1, filter2)
495
<p>is equivalent to:</p>
496
<pre class="rst-literal-block">
497
stream | filter1 | filter2
503
<a name="render"></a>
505
<table class="details" border="1" cellpadding="3"
506
cellspacing="0" width="100%" bgcolor="white">
508
<table width="100%" cellpadding="0" cellspacing="0" border="0">
509
<tr valign="top"><td>
510
<h3 class="epydoc"><span class="sig"><span class="sig-name">render</span>(<span class="sig-arg">self</span>,
511
<span class="sig-arg">method</span>=<span class="sig-default"><code class="variable-quote">'</code><code class="variable-string">xml</code><code class="variable-quote">'</code></span>,
512
<span class="sig-arg">encoding</span>=<span class="sig-default"><code class="variable-quote">'</code><code class="variable-string">utf-8</code><code class="variable-quote">'</code></span>,
513
<span class="sig-arg">**kwargs</span>)</span>
515
</td><td align="right" valign="top"
520
<p>Return a string representation of the stream.</p>
521
<p>Any additional keyword arguments are passed to the serializer, and thus
522
depend on the <code class="link">method</code> parameter value.</p>
525
<dd><ul class="nomargin-top">
526
<li><strong class="pname"><code>method</code></strong> - determines how the stream is serialized; can be either
527
"xml", "xhtml", "html", "text", or a custom serializer
529
<li><strong class="pname"><code>encoding</code></strong> - how the output string should be encoded; if set to
530
<code class="link">None</code>, this method returns a <code class="link">unicode</code> object</li>
533
<div class="fields"> <p><strong>See Also:</strong>
534
XMLSerializer.__init__, XHTMLSerializer.__init__,
535
HTMLSerializer.__init__, TextSerializer.__init__
537
</div></td></tr></table>
539
<a name="select"></a>
541
<table class="details" border="1" cellpadding="3"
542
cellspacing="0" width="100%" bgcolor="white">
544
<table width="100%" cellpadding="0" cellspacing="0" border="0">
545
<tr valign="top"><td>
546
<h3 class="epydoc"><span class="sig"><span class="sig-name">select</span>(<span class="sig-arg">self</span>,
547
<span class="sig-arg">path</span>,
548
<span class="sig-arg">namespaces</span>=<span class="sig-default">None</span>,
549
<span class="sig-arg">variables</span>=<span class="sig-default">None</span>)</span>
551
</td><td align="right" valign="top"
556
Return a new stream that contains the events matching the given
560
<dd><ul class="nomargin-top">
561
<li><strong class="pname"><code>path</code></strong> - a string containing the XPath expression</li>
562
<li><strong class="pname"><code>namespaces</code></strong> - mapping of namespace prefixes used in the path</li>
563
<li><strong class="pname"><code>variables</code></strong> - mapping of variable names to values</li>
566
<dd>the selected substream</dd>
568
<dd><ul class="nomargin-top">
569
<li><code><strong class='fraise'><a href="genshi.path.PathSyntaxError-class.html">PathSyntaxError</a></strong></code> - if the given path expression is invalid or not
575
<a name="serialize"></a>
577
<table class="details" border="1" cellpadding="3"
578
cellspacing="0" width="100%" bgcolor="white">
580
<table width="100%" cellpadding="0" cellspacing="0" border="0">
581
<tr valign="top"><td>
582
<h3 class="epydoc"><span class="sig"><span class="sig-name">serialize</span>(<span class="sig-arg">self</span>,
583
<span class="sig-arg">method</span>=<span class="sig-default"><code class="variable-quote">'</code><code class="variable-string">xml</code><code class="variable-quote">'</code></span>,
584
<span class="sig-arg">**kwargs</span>)</span>
586
</td><td align="right" valign="top"
591
<p>Generate strings corresponding to a specific serialization of the
593
<p>Unlike the <a href="genshi.core.Stream-class.html#render" class="link">render()</a> method, this method is a generator that returns
594
the serialized output incrementally, as opposed to returning a single
596
<p>Any additional keyword arguments are passed to the serializer, and thus
597
depend on the <code class="link">method</code> parameter value.</p>
600
<dd><ul class="nomargin-top">
601
<li><strong class="pname"><code>method</code></strong> - determines how the stream is serialized; can be either
602
"xml", "xhtml", "html", "text", or a custom serializer
606
<div class="fields"> <p><strong>See Also:</strong>
607
XMLSerializer.__init__, XHTMLSerializer.__init__,
608
HTMLSerializer.__init__, TextSerializer.__init__
610
</div></td></tr></table>
612
<a name="__str__"></a>
614
<table class="details" border="1" cellpadding="3"
615
cellspacing="0" width="100%" bgcolor="white">
617
<table width="100%" cellpadding="0" cellspacing="0" border="0">
618
<tr valign="top"><td>
619
<h3 class="epydoc"><span class="sig"><span class="sig-name">__str__</span>(<span class="sig-arg">self</span>)</span>
620
<br /><em class="fname">(Informal representation operator)</em>
622
</td><td align="right" valign="top"
631
<dd><em class="note">(inherited documentation)</em></dd>
637
<!-- ==================== NAVIGATION BAR ==================== -->
638
<table class="navbar" border="0" width="100%" cellpadding="0"
639
bgcolor="#a0c0ff" cellspacing="0">
642
<th> <a
643
href="genshi-module.html">Home</a> </th>
646
<th> <a
647
href="module-tree.html">Trees</a> </th>
650
<th> <a
651
href="identifier-index.html">Indices</a> </th>
654
<th> <a
655
href="help.html">Help</a> </th>
657
<!-- Project homepage -->
658
<th class="navbar" align="right" width="100%">
659
<table border="0" cellpadding="0" cellspacing="0">
660
<tr><th class="navbar" align="center"
661
><a class="navbar" target="_top" href="../index.html">Documentation Index</a></th>
665
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
667
<td align="left" class="footer">
668
Generated by Epydoc 3.0beta1 on Mon Apr 16 16:02:13 2007
670
<td align="right" class="footer">
671
<a href="http://epydoc.sourceforge.net">http://epydoc.sourceforge.net</a>
676
<script type="text/javascript">
678
// Private objects are initially displayed (because if
679
// javascript is turned off then we want them to be
680
// visible); but by default, we want to hide them. So hide
681
// them unless we have a cookie that says to show them.