![]() |
Helper class to re-map PyYAML events to method calls.
Inherits From: expected_type
google.appengine.api.yaml_listener.EventListener(
event_handler
)
By default, PyYAML generates its events via a Python generator. This class is a helper that iterates over the events from the PyYAML parser and forwards them to a handle class in the form of method calls. For simplicity, the underlying event is forwarded to the handler as a parameter to the call.
This object does not produce iterable objects itself, but is instead a mapping to a given handler instance.
Example use:
```python
class PrintDocumentHandler(object):
def DocumentStart(event):
print "A new document has been started"
EventListener(PrintDocumentHandler()).Parse('''
key1: value1
---
key2: value2
'''
```
>>> A new document has been started
A new document has been started
```
```
In the example above, the implemented handler class (PrintDocumentHandler
)
has a single method that reports each time a new document is started within a
YAML file. It is not necessary to subclass the EventListener
, since only it
receives a PrintDocumentHandler
instance. Every time a new document begins,
PrintDocumentHandler.DocumentStart
is called with the PyYAML event passed in
as its parameter.
The constructor initializes the PyYAML event listener and constructs internal mapping directly from event type to method on the actual handler. This prevents reflection being used during the actual parse time.
Args | |
---|---|
event_handler
|
Event handler that will receive mapped events. Must
implement at least one appropriate handler method named from
the values of the _EVENT_METHOD_MAP .
|
Raises | |
---|---|
ListenerConfigurationError if event_handler is not an EventHandler .
|
Methods
HandleEvent
HandleEvent(
event, loader=None
)
Handles individual PyYAML event.
Args | |
---|---|
event
|
Event to forward to method call in method call. |
Raises | |
---|---|
IllegalEvent when receives an unrecognized or unsupported event type.
|
Parse
Parse(
stream, loader_class=yaml.loader.SafeLoader, **loader_args
)
Calls YAML parser to generate and handle all events.
Calls PyYAML parser and sends resulting generator to handle_event
method
for processing.
Args | |
---|---|
stream
|
String document or open file object to process as per the
yaml.parse method. Any object that implements a read() method which
returns a string document will work with the YAML parser.
|
loader_class
|
Used for dependency injection. |
**loader_args
|
Pass to the loader on construction. |