@@ -127,26 +127,26 @@ without any '.' or '/' characters.
127
127
Hooks
128
128
-----
129
129
Hooks are an advanced feature that allow arbitrary code to be executed during
130
- the parsing and serialization process. A `Hooks ` object is associated with a
131
- processor and contains two functions: `after_parse ` and `before_serialize `.
130
+ the parsing and serialization process. A `` Hooks ` ` object is associated with a
131
+ processor and contains two functions: `` after_parse `` and `` before_serialize ` `.
132
132
133
133
Both of these functions (which can be any callable object) are provided two
134
134
parameters and should return a single value. The first parameter provided to
135
- both functions is a `ProcessorStateView ` object which contains information
135
+ both functions is a `` ProcessorStateView ` ` object which contains information
136
136
about the current state of the processor when the function is invoked.
137
137
138
- The `after_parse ` function is invoked after a processor has parsed a value
139
- from the XML data. The second parameter provided to the `after_parse ` function
140
- is the value parsed by the processor from the XML data. The `after_parse `
138
+ The `` after_parse ` ` function is invoked after a processor has parsed a value
139
+ from the XML data. The second parameter provided to the `` after_parse ` ` function
140
+ is the value parsed by the processor from the XML data. The `` after_parse ` `
141
141
function must return a single value which will be used by the processor as its
142
- parse result. The value returned by `after_parse ` replaces the value parsed
142
+ parse result. The value returned by `` after_parse ` ` replaces the value parsed
143
143
from the XML data as the processor's parse result.
144
144
145
- The `before_serialize ` function is invoked before a processor serializes a
146
- value to XML. The second parameter provided to the `before_serialize ` function
147
- is the value to be serialized by the processor to XML. The `before_serialize `
145
+ The `` before_serialize ` ` function is invoked before a processor serializes a
146
+ value to XML. The second parameter provided to the `` before_serialize ` ` function
147
+ is the value to be serialized by the processor to XML. The `` before_serialize ` `
148
148
function must return a single value which the processor will serialize to XML.
149
- The value returned by `before_serialize ` replaces the value provided to the
149
+ The value returned by `` before_serialize ` ` replaces the value provided to the
150
150
processor to serialize to XML.
151
151
152
152
There are three intended use cases for hooks (though since hooks can be any
@@ -163,9 +163,9 @@ parsing into a shape more convenient for the application to use and transform
163
163
values during serialization back into shapes that better fit the XML structure.
164
164
165
165
Hooks can be used to achieve this by simply returning the transformed value
166
- from the `after_parse ` and `before_serialize ` functions. This works because
166
+ from the `` after_parse `` and `` before_serialize ` ` functions. This works because
167
167
whatever value a processor was going to use for parsing or serialization is
168
- replaced by the value returned by `after_parse ` or `before_serialize `.
168
+ replaced by the value returned by `` after_parse `` or `` before_serialize ` `.
169
169
170
170
As a basic example, if we want to make sure all strings read from an XML
171
171
document are uppercase when used in our application and lowercase when written
@@ -201,7 +201,7 @@ to XML, we could use hooks to perform value transformations.
201
201
</data>
202
202
203
203
When using hooks to perform value transformations, it is a good idea to ensure
204
- that the transformations performed by `after_parse ` and `before_serialize ` are
204
+ that the transformations performed by `` after_parse `` and `` before_serialize ` ` are
205
205
inverse operations of each other so that parsing and serialization work
206
206
correctly when using transformed values. This is particularly important when
207
207
values are transformed into different types.
@@ -212,9 +212,9 @@ By default, declxml only performs a very basic level of validation by ensuring
212
212
that required values are present and that they are of the correct type. Hooks
213
213
provide the ability to perform additional, application-specific validation.
214
214
215
- When performing validation, we can use the `ProcessorStateView ` object provided
216
- as the first parameter to the `after_parse ` and `before_serialize ` functions.
217
- The `ProcessorStateView ` object provides a useful method, `raise_error `, for
215
+ When performing validation, we can use the `` ProcessorStateView ` ` object provided
216
+ as the first parameter to the `` after_parse `` and `` before_serialize ` ` functions.
217
+ The `` ProcessorStateView `` object provides a useful method, `` raise_error ` `, for
218
218
reporting errors. This method will raise an application-provided exception
219
219
with a custom error message and will include information about the current
220
220
state of the processor in the error message.
@@ -263,15 +263,15 @@ we could use hooks to perform the validation.
263
263
RuntimeError: Invalid value -90 at data/value
264
264
265
265
When using hooks for validation, it is important to remember to return the
266
- value from the `before_parse ` and `after_serialize ` functions since the
266
+ value from the `` before_parse `` and `` after_serialize ` ` functions since the
267
267
processor will used the value returned by those functions as its parsing result
268
268
and the value to serialize to XML, respectively.
269
269
270
270
Debugging
271
271
"""""""""
272
272
Hooks can also be used to debug processors. We can use the
273
- `ProcessorStateView ` object provided to the `before_parse ` and
274
- `after_serialize ` functions to include information about which values are
273
+ `` ProcessorStateView `` object provided to the `` before_parse ` ` and
274
+ `` after_serialize ` ` functions to include information about which values are
275
275
received in which locations in the XML document.
276
276
277
277
.. sourcecode :: py
0 commit comments