annotate genshi/core.py @ 958:6fc92535c888 experimental-performance-improvement-exploration

Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
author hodgestar
date Tue, 13 Mar 2012 03:03:02 +0000
parents 417787b9b9a7
children
rev   line source
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
1 # -*- coding: utf-8 -*-
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
2 #
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
3 # Copyright (C) 2006-2009 Edgewall Software
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
4 # All rights reserved.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
5 #
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
6 # This software is licensed as described in the file COPYING, which
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
7 # you should have received as part of this distribution. The terms
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
8 # are also available at http://genshi.edgewall.org/wiki/License.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
9 #
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
10 # This software consists of voluntary contributions made by many
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
11 # individuals. For the exact contribution history, see the revision
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
12 # history and logs, available at http://genshi.edgewall.org/log/.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
13
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
14 """Core classes for markup processing."""
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
15
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
16 try:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
17 reduce # builtin in Python < 3
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
18 except NameError:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
19 from functools import reduce
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
20 import sys
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
21 from itertools import chain
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
22 import operator
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
23
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
24 from genshi.util import plaintext, stripentities, striptags, stringrepr
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
25
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
26 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Attrs', 'Namespace',
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
27 'QName']
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
28 __docformat__ = 'restructuredtext en'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
29
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
30
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
31 class StreamEventKind(str):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
32 """A kind of event on a markup stream."""
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
33 __slots__ = []
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
34 _instances = {}
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
35
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
36 def __new__(cls, val):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
37 return cls._instances.setdefault(val, str.__new__(cls, val))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
38
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
39
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
40 class Stream(object):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
41 """Represents a stream of markup events.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
42
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
43 This class is basically an iterator over the events.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
44
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
45 Stream events are tuples of the form::
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
46
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
47 (kind, data, position)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
48
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
49 where ``kind`` is the event kind (such as `START`, `END`, `TEXT`, etc),
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
50 ``data`` depends on the kind of event, and ``position`` is a
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
51 ``(filename, line, offset)`` tuple that contains the location of the
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
52 original element or text in the input. If the original location is unknown,
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
53 ``position`` is ``(None, -1, -1)``.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
54
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
55 Also provided are ways to serialize the stream to text. The `serialize()`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
56 method will return an iterator over generated strings, while `render()`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
57 returns the complete generated text at once. Both accept various parameters
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
58 that impact the way the stream is serialized.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
59 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
60 __slots__ = ['events', 'serializer']
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
61
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
62 START = StreamEventKind('START') #: a start tag
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
63 END = StreamEventKind('END') #: an end tag
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
64 TEXT = StreamEventKind('TEXT') #: literal text
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
65 XML_DECL = StreamEventKind('XML_DECL') #: XML declaration
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
66 DOCTYPE = StreamEventKind('DOCTYPE') #: doctype declaration
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
67 START_NS = StreamEventKind('START_NS') #: start namespace mapping
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
68 END_NS = StreamEventKind('END_NS') #: end namespace mapping
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
69 START_CDATA = StreamEventKind('START_CDATA') #: start CDATA section
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
70 END_CDATA = StreamEventKind('END_CDATA') #: end CDATA section
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
71 PI = StreamEventKind('PI') #: processing instruction
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
72 COMMENT = StreamEventKind('COMMENT') #: comment
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
73
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
74 def __init__(self, events, serializer=None):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
75 """Initialize the stream with a sequence of markup events.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
76
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
77 :param events: a sequence or iterable providing the events
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
78 :param serializer: the default serialization method to use for this
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
79 stream
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
80
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
81 :note: Changed in 0.5: added the `serializer` argument
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
82 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
83 self.events = events #: The underlying iterable producing the events
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
84 self.serializer = serializer #: The default serializion method
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
85
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
86 def __iter__(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
87 return iter(self.events)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
88
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
89 def __or__(self, function):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
90 """Override the "bitwise or" operator to apply filters or serializers
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
91 to the stream, providing a syntax similar to pipes on Unix shells.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
92
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
93 Assume the following stream produced by the `HTML` function:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
94
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
95 >>> from genshi.input import HTML
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
96 >>> html = HTML('''<p onclick="alert('Whoa')">Hello, world!</p>''', encoding='utf-8')
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
97 >>> print(html)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
98 <p onclick="alert('Whoa')">Hello, world!</p>
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
99
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
100 A filter such as the HTML sanitizer can be applied to that stream using
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
101 the pipe notation as follows:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
102
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
103 >>> from genshi.filters import HTMLSanitizer
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
104 >>> sanitizer = HTMLSanitizer()
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
105 >>> print(html | sanitizer)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
106 <p>Hello, world!</p>
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
107
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
108 Filters can be any function that accepts and produces a stream (where
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
109 a stream is anything that iterates over events):
958
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
110
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
111 >>> def uppercase(stream):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
112 ... for kind, data, pos in stream:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
113 ... if kind is TEXT:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
114 ... data = data.upper()
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
115 ... yield kind, data, pos
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
116 >>> print(html | sanitizer | uppercase)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
117 <p>HELLO, WORLD!</p>
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
118
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
119 Serializers can also be used with this notation:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
120
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
121 >>> from genshi.output import TextSerializer
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
122 >>> output = TextSerializer()
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
123 >>> print(html | sanitizer | uppercase | output)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
124 HELLO, WORLD!
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
125
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
126 Commonly, serializers should be used at the end of the "pipeline";
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
127 using them somewhere in the middle may produce unexpected results.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
128
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
129 :param function: the callable object that should be applied as a filter
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
130 :return: the filtered stream
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
131 :rtype: `Stream`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
132 """
958
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
133 # TODO: this is horribly slow because is has to guess whether
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
134 # the function passed in is something that produces stream
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
135 # events or something that produces a sequence of strings.
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
136 # Sequences of strings are converted back to a sequence of
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
137 # stream events (and then back to text when rendered).
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
138 events = _possible_text_iterator_to_stream(function(self))
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
139 return Stream(events, serializer=self.serializer)
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
140
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
141 def filter(self, *filters):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
142 """Apply filters to the stream.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
143
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
144 This method returns a new stream with the given filters applied. The
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
145 filters must be callables that accept the stream object as parameter,
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
146 and return the filtered stream.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
147
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
148 The call::
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
149
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
150 stream.filter(filter1, filter2)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
151
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
152 is equivalent to::
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
153
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
154 stream | filter1 | filter2
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
155
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
156 :param filters: one or more callable objects that should be applied as
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
157 filters
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
158 :return: the filtered stream
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
159 :rtype: `Stream`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
160 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
161 return reduce(operator.or_, (self,) + filters)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
162
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
163 def render(self, method=None, encoding=None, out=None, **kwargs):
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
164 """Return a string representation of the stream.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
165
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
166 Any additional keyword arguments are passed to the serializer, and thus
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
167 depend on the `method` parameter value.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
168
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
169 :param method: determines how the stream is serialized; can be either
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
170 "xml", "xhtml", "html", "text", or a custom serializer
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
171 class; if `None`, the default serialization method of
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
172 the stream is used
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
173 :param encoding: how the output string should be encoded; if set to
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
174 `None`, this method returns a `unicode` object
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
175 :param out: a file-like object that the output should be written to
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
176 instead of being returned as one big string; note that if
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
177 this is a file or socket (or similar), the `encoding` must
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
178 not be `None` (that is, the output must be encoded)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
179 :return: a `str` or `unicode` object (depending on the `encoding`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
180 parameter), or `None` if the `out` parameter is provided
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
181 :rtype: `basestring`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
182
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
183 :see: XMLSerializer, XHTMLSerializer, HTMLSerializer, TextSerializer
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
184 :note: Changed in 0.5: added the `out` parameter
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
185 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
186 from genshi.output import encode
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
187 if method is None:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
188 method = self.serializer or 'xml'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
189 generator = self.serialize(method=method, **kwargs)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
190 return encode(generator, method=method, encoding=encoding, out=out)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
191
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
192 def select(self, path, namespaces=None, variables=None):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
193 """Return a new stream that contains the events matching the given
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
194 XPath expression.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
195
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
196 >>> from genshi import HTML
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
197 >>> stream = HTML('<doc><elem>foo</elem><elem>bar</elem></doc>', encoding='utf-8')
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
198 >>> print(stream.select('elem'))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
199 <elem>foo</elem><elem>bar</elem>
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
200 >>> print(stream.select('elem/text()'))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
201 foobar
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
202
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
203 Note that the outermost element of the stream becomes the *context
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
204 node* for the XPath test. That means that the expression "doc" would
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
205 not match anything in the example above, because it only tests against
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
206 child elements of the outermost element:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
207
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
208 >>> print(stream.select('doc'))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
209 <BLANKLINE>
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
210
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
211 You can use the "." expression to match the context node itself
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
212 (although that usually makes little sense):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
213
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
214 >>> print(stream.select('.'))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
215 <doc><elem>foo</elem><elem>bar</elem></doc>
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
216
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
217 :param path: a string containing the XPath expression
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
218 :param namespaces: mapping of namespace prefixes used in the path
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
219 :param variables: mapping of variable names to values
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
220 :return: the selected substream
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
221 :rtype: `Stream`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
222 :raises PathSyntaxError: if the given path expression is invalid or not
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
223 supported
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
224 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
225 from genshi.path import Path
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
226 return Path(path).select(self, namespaces, variables)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
227
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
228 def serialize(self, method='xml', **kwargs):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
229 """Generate strings corresponding to a specific serialization of the
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
230 stream.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
231
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
232 Unlike the `render()` method, this method is a generator that returns
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
233 the serialized output incrementally, as opposed to returning a single
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
234 string.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
235
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
236 Any additional keyword arguments are passed to the serializer, and thus
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
237 depend on the `method` parameter value.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
238
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
239 :param method: determines how the stream is serialized; can be either
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
240 "xml", "xhtml", "html", "text", or a custom serializer
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
241 class; if `None`, the default serialization method of
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
242 the stream is used
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
243 :return: an iterator over the serialization results (`Markup` or
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
244 `unicode` objects, depending on the serialization method)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
245 :rtype: ``iterator``
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
246 :see: XMLSerializer, XHTMLSerializer, HTMLSerializer, TextSerializer
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
247 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
248 from genshi.output import get_serializer
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
249 if method is None:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
250 method = self.serializer or 'xml'
958
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
251 return get_serializer(method, **kwargs)(self)
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
252
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
253 def __str__(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
254 return self.render()
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
255
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
256 def __unicode__(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
257 return self.render(encoding=None)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
258
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
259 def __html__(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
260 return self
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
261
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
262
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
263 START = Stream.START
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
264 END = Stream.END
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
265 TEXT = Stream.TEXT
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
266 XML_DECL = Stream.XML_DECL
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
267 DOCTYPE = Stream.DOCTYPE
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
268 START_NS = Stream.START_NS
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
269 END_NS = Stream.END_NS
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
270 START_CDATA = Stream.START_CDATA
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
271 END_CDATA = Stream.END_CDATA
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
272 PI = Stream.PI
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
273 COMMENT = Stream.COMMENT
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
274
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
275
958
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
276 def _text_event(text):
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
277 return (TEXT, unicode(text), (None, -1, -1))
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
278
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
279
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
280 def _text_to_stream(text):
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
281 yield _text_event(text)
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
282
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
283
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
284 def _possible_text_iterator_to_stream(textiter_or_stream):
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
285 it = iter(textiter_or_stream)
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
286 event = it.next()
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
287
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
288 # Check whether the iterable is a real markup event stream by examining the
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
289 # first item it yields; if it's not we'll need to do some conversion
958
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
290 if type(event) is not tuple:
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
291 yield TEXT, unicode(event), (None, -1, -1)
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
292 for event in it:
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
293 yield TEXT, unicode(event), (None, -1, -1)
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
294 return
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
295
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
296 # This looks like a markup event stream, so we'll just pass it through
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
297 # unchanged
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
298 yield event
958
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
299 for event in it:
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
300 yield event
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
301
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
302
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
303 class Attrs(tuple):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
304 """Immutable sequence type that stores the attributes of an element.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
305
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
306 Ordering of the attributes is preserved, while access by name is also
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
307 supported.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
308
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
309 >>> attrs = Attrs([('href', '#'), ('title', 'Foo')])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
310 >>> attrs
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
311 Attrs([('href', '#'), ('title', 'Foo')])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
312
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
313 >>> 'href' in attrs
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
314 True
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
315 >>> 'tabindex' in attrs
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
316 False
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
317 >>> attrs.get('title')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
318 'Foo'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
319
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
320 Instances may not be manipulated directly. Instead, the operators ``|`` and
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
321 ``-`` can be used to produce new instances that have specific attributes
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
322 added, replaced or removed.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
323
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
324 To remove an attribute, use the ``-`` operator. The right hand side can be
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
325 either a string or a set/sequence of strings, identifying the name(s) of
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
326 the attribute(s) to remove:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
327
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
328 >>> attrs - 'title'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
329 Attrs([('href', '#')])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
330 >>> attrs - ('title', 'href')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
331 Attrs()
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
332
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
333 The original instance is not modified, but the operator can of course be
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
334 used with an assignment:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
335
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
336 >>> attrs
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
337 Attrs([('href', '#'), ('title', 'Foo')])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
338 >>> attrs -= 'title'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
339 >>> attrs
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
340 Attrs([('href', '#')])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
341
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
342 To add a new attribute, use the ``|`` operator, where the right hand value
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
343 is a sequence of ``(name, value)`` tuples (which includes `Attrs`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
344 instances):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
345
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
346 >>> attrs | [('title', 'Bar')]
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
347 Attrs([('href', '#'), ('title', 'Bar')])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
348
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
349 If the attributes already contain an attribute with a given name, the value
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
350 of that attribute is replaced:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
351
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
352 >>> attrs | [('href', 'http://example.org/')]
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
353 Attrs([('href', 'http://example.org/')])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
354 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
355 __slots__ = []
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
356
958
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
357 ATTRS = StreamEventKind('ATTRS')
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
358
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
359 def __contains__(self, name):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
360 """Return whether the list includes an attribute with the specified
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
361 name.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
362
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
363 :return: `True` if the list includes the attribute
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
364 :rtype: `bool`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
365 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
366 for attr, _ in self:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
367 if attr == name:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
368 return True
941
417787b9b9a7 Fix genshi.core.Attrs.__contains__ so that it returns False instead of None if an attribute is not found (CPython translates this to False automatically but PyPy does not).
hodgestar
parents: 932
diff changeset
369 return False
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
370
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
371 def __getitem__(self, i):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
372 """Return an item or slice of the attributes list.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
373
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
374 >>> attrs = Attrs([('href', '#'), ('title', 'Foo')])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
375 >>> attrs[1]
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
376 ('title', 'Foo')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
377 >>> attrs[1:]
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
378 Attrs([('title', 'Foo')])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
379 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
380 items = tuple.__getitem__(self, i)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
381 if type(i) is slice:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
382 return Attrs(items)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
383 return items
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
384
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
385 def __getslice__(self, i, j):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
386 """Return a slice of the attributes list.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
387
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
388 >>> attrs = Attrs([('href', '#'), ('title', 'Foo')])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
389 >>> attrs[1:]
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
390 Attrs([('title', 'Foo')])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
391 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
392 return Attrs(tuple.__getslice__(self, i, j))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
393
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
394 def __or__(self, attrs):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
395 """Return a new instance that contains the attributes in `attrs` in
904
82b9b3afe0b8 The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents: 862
diff changeset
396 addition to any already existing attributes. Any attributes in the new
82b9b3afe0b8 The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents: 862
diff changeset
397 set that have a value of `None` are removed.
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
398
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
399 :return: a new instance with the merged attributes
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
400 :rtype: `Attrs`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
401 """
904
82b9b3afe0b8 The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents: 862
diff changeset
402 remove = set([an for an, av in attrs if av is None])
82b9b3afe0b8 The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents: 862
diff changeset
403 replace = dict([(an, av) for an, av in attrs
82b9b3afe0b8 The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents: 862
diff changeset
404 if an in self and av is not None])
82b9b3afe0b8 The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents: 862
diff changeset
405 return Attrs([(sn, replace.get(sn, sv)) for sn, sv in self
82b9b3afe0b8 The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents: 862
diff changeset
406 if sn not in remove] +
82b9b3afe0b8 The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents: 862
diff changeset
407 [(an, av) for an, av in attrs
82b9b3afe0b8 The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents: 862
diff changeset
408 if an not in self and an not in remove])
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
409
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
410 def __repr__(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
411 if not self:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
412 return 'Attrs()'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
413 return 'Attrs([%s])' % ', '.join([repr(item) for item in self])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
414
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
415 def __sub__(self, names):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
416 """Return a new instance with all attributes with a name in `names` are
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
417 removed.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
418
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
419 :param names: the names of the attributes to remove
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
420 :return: a new instance with the attribute removed
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
421 :rtype: `Attrs`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
422 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
423 if isinstance(names, basestring):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
424 names = (names,)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
425 return Attrs([(name, val) for name, val in self if name not in names])
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
426
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
427 def get(self, name, default=None):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
428 """Return the value of the attribute with the specified name, or the
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
429 value of the `default` parameter if no such attribute is found.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
430
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
431 :param name: the name of the attribute
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
432 :param default: the value to return when the attribute does not exist
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
433 :return: the attribute value, or the `default` value if that attribute
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
434 does not exist
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
435 :rtype: `object`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
436 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
437 for attr, value in self:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
438 if attr == name:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
439 return value
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
440 return default
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
441
958
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
442 def toevent(self):
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
443 """Return the attributes as a markup event.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
444
958
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
445 The returned event is an `ATTRS` event, the data is the Attr object.
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
446
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
447 >>> a = Attrs([('href', '#'), ('title', 'Foo')])
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
448 >>> a.toevent()
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
449 ('ATTRS', Attrs([('href', '#'), ('title', 'Foo')]), (None, -1, -1))
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
450
958
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
451 :return: an `ATTR` event
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
452 :rtype: `tuple`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
453 """
958
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
454 return self.ATTRS, self, (None, -1, -1)
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
455
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
456 def concatenate_values(self):
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
457 """Return the values of the attributes concatenated into a string.
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
458
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
459 >>> a = Attrs([('href', '#'), ('title', 'Foo')])
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
460 >>> a.concatenate_values()
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
461 '#Foo'
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
462
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
463 :return: the concatenated attribute values
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
464 :rtype: `str`
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
465 """
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
466 return ''.join([x[1] for x in self])
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
467
6fc92535c888 Be more careful about what is passed into streams as events and remove many uses of _ensure as a result. An ATTRS event is added for handling Attributes returned by gensh.path.select().
hodgestar
parents: 941
diff changeset
468 ATTRS = Attrs.ATTRS
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
469
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
470
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
471 class Markup(unicode):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
472 """Marks a string as being safe for inclusion in HTML/XML output without
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
473 needing to be escaped.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
474 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
475 __slots__ = []
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
476
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
477 def __add__(self, other):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
478 return Markup(unicode.__add__(self, escape(other)))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
479
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
480 def __radd__(self, other):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
481 return Markup(unicode.__add__(escape(other), self))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
482
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
483 def __mod__(self, args):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
484 if isinstance(args, dict):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
485 args = dict(zip(args.keys(), map(escape, args.values())))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
486 elif isinstance(args, (list, tuple)):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
487 args = tuple(map(escape, args))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
488 else:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
489 args = escape(args)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
490 return Markup(unicode.__mod__(self, args))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
491
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
492 def __mul__(self, num):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
493 return Markup(unicode.__mul__(self, num))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
494 __rmul__ = __mul__
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
495
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
496 def __repr__(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
497 return "<%s %s>" % (type(self).__name__, unicode.__repr__(self))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
498
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
499 def join(self, seq, escape_quotes=True):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
500 """Return a `Markup` object which is the concatenation of the strings
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
501 in the given sequence, where this `Markup` object is the separator
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
502 between the joined elements.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
503
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
504 Any element in the sequence that is not a `Markup` instance is
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
505 automatically escaped.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
506
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
507 :param seq: the sequence of strings to join
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
508 :param escape_quotes: whether double quote characters in the elements
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
509 should be escaped
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
510 :return: the joined `Markup` object
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
511 :rtype: `Markup`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
512 :see: `escape`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
513 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
514 return Markup(unicode.join(self, [escape(item, quotes=escape_quotes)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
515 for item in seq]))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
516
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
517 @classmethod
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
518 def escape(cls, text, quotes=True):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
519 """Create a Markup instance from a string and escape special characters
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
520 it may contain (<, >, & and \").
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
521
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
522 >>> escape('"1 < 2"')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
523 <Markup u'&#34;1 &lt; 2&#34;'>
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
524
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
525 If the `quotes` parameter is set to `False`, the \" character is left
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
526 as is. Escaping quotes is generally only required for strings that are
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
527 to be used in attribute values.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
528
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
529 >>> escape('"1 < 2"', quotes=False)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
530 <Markup u'"1 &lt; 2"'>
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
531
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
532 :param text: the text to escape
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
533 :param quotes: if ``True``, double quote characters are escaped in
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
534 addition to the other special characters
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
535 :return: the escaped `Markup` string
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
536 :rtype: `Markup`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
537 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
538 if not text:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
539 return cls()
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
540 if type(text) is cls:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
541 return text
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
542 if hasattr(text, '__html__'):
904
82b9b3afe0b8 The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents: 862
diff changeset
543 return cls(text.__html__())
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
544
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
545 text = text.replace('&', '&amp;') \
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
546 .replace('<', '&lt;') \
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
547 .replace('>', '&gt;')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
548 if quotes:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
549 text = text.replace('"', '&#34;')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
550 return cls(text)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
551
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
552 def unescape(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
553 """Reverse-escapes &, <, >, and \" and returns a `unicode` object.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
554
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
555 >>> Markup('1 &lt; 2').unescape()
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
556 u'1 < 2'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
557
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
558 :return: the unescaped string
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
559 :rtype: `unicode`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
560 :see: `genshi.core.unescape`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
561 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
562 if not self:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
563 return ''
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
564 return unicode(self).replace('&#34;', '"') \
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
565 .replace('&gt;', '>') \
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
566 .replace('&lt;', '<') \
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
567 .replace('&amp;', '&')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
568
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
569 def stripentities(self, keepxmlentities=False):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
570 """Return a copy of the text with any character or numeric entities
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
571 replaced by the equivalent UTF-8 characters.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
572
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
573 If the `keepxmlentities` parameter is provided and evaluates to `True`,
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
574 the core XML entities (``&amp;``, ``&apos;``, ``&gt;``, ``&lt;`` and
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
575 ``&quot;``) are not stripped.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
576
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
577 :return: a `Markup` instance with entities removed
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
578 :rtype: `Markup`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
579 :see: `genshi.util.stripentities`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
580 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
581 return Markup(stripentities(self, keepxmlentities=keepxmlentities))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
582
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
583 def striptags(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
584 """Return a copy of the text with all XML/HTML tags removed.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
585
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
586 :return: a `Markup` instance with all tags removed
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
587 :rtype: `Markup`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
588 :see: `genshi.util.striptags`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
589 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
590 return Markup(striptags(self))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
591
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
592
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
593 try:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
594 from genshi._speedups import Markup
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
595 except ImportError:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
596 pass # just use the Python implementation
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
597
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
598
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
599 escape = Markup.escape
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
600
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
601
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
602 def unescape(text):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
603 """Reverse-escapes &, <, >, and \" and returns a `unicode` object.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
604
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
605 >>> unescape(Markup('1 &lt; 2'))
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
606 u'1 < 2'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
607
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
608 If the provided `text` object is not a `Markup` instance, it is returned
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
609 unchanged.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
610
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
611 >>> unescape('1 &lt; 2')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
612 '1 &lt; 2'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
613
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
614 :param text: the text to unescape
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
615 :return: the unescsaped string
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
616 :rtype: `unicode`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
617 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
618 if not isinstance(text, Markup):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
619 return text
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
620 return text.unescape()
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
621
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
622
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
623 class Namespace(object):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
624 """Utility class creating and testing elements with a namespace.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
625
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
626 Internally, namespace URIs are encoded in the `QName` of any element or
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
627 attribute, the namespace URI being enclosed in curly braces. This class
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
628 helps create and test these strings.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
629
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
630 A `Namespace` object is instantiated with the namespace URI.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
631
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
632 >>> html = Namespace('http://www.w3.org/1999/xhtml')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
633 >>> html
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
634 Namespace('http://www.w3.org/1999/xhtml')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
635 >>> html.uri
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
636 u'http://www.w3.org/1999/xhtml'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
637
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
638 The `Namespace` object can than be used to generate `QName` objects with
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
639 that namespace:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
640
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
641 >>> html.body
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
642 QName('http://www.w3.org/1999/xhtml}body')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
643 >>> html.body.localname
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
644 u'body'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
645 >>> html.body.namespace
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
646 u'http://www.w3.org/1999/xhtml'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
647
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
648 The same works using item access notation, which is useful for element or
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
649 attribute names that are not valid Python identifiers:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
650
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
651 >>> html['body']
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
652 QName('http://www.w3.org/1999/xhtml}body')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
653
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
654 A `Namespace` object can also be used to test whether a specific `QName`
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
655 belongs to that namespace using the ``in`` operator:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
656
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
657 >>> qname = html.body
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
658 >>> qname in html
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
659 True
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
660 >>> qname in Namespace('http://www.w3.org/2002/06/xhtml2')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
661 False
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
662 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
663 def __new__(cls, uri):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
664 if type(uri) is cls:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
665 return uri
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
666 return object.__new__(cls)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
667
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
668 def __getnewargs__(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
669 return (self.uri,)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
670
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
671 def __getstate__(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
672 return self.uri
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
673
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
674 def __setstate__(self, uri):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
675 self.uri = uri
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
676
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
677 def __init__(self, uri):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
678 self.uri = unicode(uri)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
679
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
680 def __contains__(self, qname):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
681 return qname.namespace == self.uri
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
682
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
683 def __ne__(self, other):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
684 return not self == other
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
685
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
686 def __eq__(self, other):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
687 if isinstance(other, Namespace):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
688 return self.uri == other.uri
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
689 return self.uri == other
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
690
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
691 def __getitem__(self, name):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
692 return QName(self.uri + '}' + name)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
693 __getattr__ = __getitem__
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
694
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
695 def __hash__(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
696 return hash(self.uri)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
697
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
698 if sys.version_info[0] == 2:
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
699 # Only use stringrepr in python 2
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
700 def __repr__(self):
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
701 return '%s(%s)' % (type(self).__name__, stringrepr(self.uri))
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
702 else:
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
703 def __repr__(self):
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
704 return '%s(%r)' % (type(self).__name__, self.uri)
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
705
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
706 def __str__(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
707 return self.uri.encode('utf-8')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
708
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
709 def __unicode__(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
710 return self.uri
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
711
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
712
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
713 # The namespace used by attributes such as xml:lang and xml:space
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
714 XML_NAMESPACE = Namespace('http://www.w3.org/XML/1998/namespace')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
715
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
716
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
717 class QName(unicode):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
718 """A qualified element or attribute name.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
719
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
720 The unicode value of instances of this class contains the qualified name of
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
721 the element or attribute, in the form ``{namespace-uri}local-name``. The
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
722 namespace URI can be obtained through the additional `namespace` attribute,
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
723 while the local name can be accessed through the `localname` attribute.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
724
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
725 >>> qname = QName('foo')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
726 >>> qname
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
727 QName('foo')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
728 >>> qname.localname
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
729 u'foo'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
730 >>> qname.namespace
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
731
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
732 >>> qname = QName('http://www.w3.org/1999/xhtml}body')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
733 >>> qname
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
734 QName('http://www.w3.org/1999/xhtml}body')
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
735 >>> qname.localname
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
736 u'body'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
737 >>> qname.namespace
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
738 u'http://www.w3.org/1999/xhtml'
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
739 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
740 __slots__ = ['namespace', 'localname']
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
741
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
742 def __new__(cls, qname):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
743 """Create the `QName` instance.
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
744
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
745 :param qname: the qualified name as a string of the form
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
746 ``{namespace-uri}local-name``, where the leading curly
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
747 brace is optional
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
748 """
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
749 if type(qname) is cls:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
750 return qname
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
751
923
374203a70ef8 Pull up r1145 to trunk.
jruigrok
parents: 904
diff changeset
752 qname = qname.lstrip('{')
374203a70ef8 Pull up r1145 to trunk.
jruigrok
parents: 904
diff changeset
753 parts = qname.split('}', 1)
860
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
754 if len(parts) > 1:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
755 self = unicode.__new__(cls, '{%s' % qname)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
756 self.namespace, self.localname = map(unicode, parts)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
757 else:
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
758 self = unicode.__new__(cls, qname)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
759 self.namespace, self.localname = None, unicode(qname)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
760 return self
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
761
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
762 def __getnewargs__(self):
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
763 return (self.lstrip('{'),)
16d55698006a A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents: 857
diff changeset
764
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
765 if sys.version_info[0] == 2:
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
766 # Only use stringrepr in python 2
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
767 def __repr__(self):
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
768 return '%s(%s)' % (type(self).__name__, stringrepr(self.lstrip('{')))
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
769 else:
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
770 def __repr__(self):
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
771 return '%s(%r)' % (type(self).__name__, self.lstrip('{'))
Copyright (C) 2012-2017 Edgewall Software