<s> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> from error import MarkedYAMLError <EOL> from events import StreamEndEvent , StreamStartEvent , AliasEvent , SequenceEndEvent , SequenceStartEvent , MappingEndEvent , MappingStartEvent , ScalarEvent <EOL> from nodes import MappingNode , ScalarNode , SequenceNode <EOL> class ComposerError ( MarkedYAMLError ) : <EOL> pass <EOL> class Composer ( object ) : <EOL> def __init__ ( self ) : <EOL> self . anchors = { } <EOL> def check_node ( self ) : <EOL> if self . check_event ( StreamStartEvent ) : <EOL> self . get_event ( ) <EOL> return not self . check_event ( StreamEndEvent ) <EOL> def get_node ( self ) : <EOL> if not self . check_event ( StreamEndEvent ) : <EOL> return self . compose_document ( ) <EOL> def get_single_node ( self ) : <EOL> self . get_event ( ) <EOL> document = None <EOL> if not self . check_event ( StreamEndEvent ) : <EOL> document = self . compose_document ( ) <EOL> if not self . check_event ( StreamEndEvent ) : <EOL> event = self . get_event ( ) <EOL> raise ComposerError ( "<STR_LIT>" , <EOL> document . start_mark , "<STR_LIT>" , <EOL> event . start_mark ) <EOL> self . get_event ( ) <EOL> return document <EOL> def compose_document ( self ) : <EOL> self . get_event ( ) <EOL> node = self . compose_node ( None , None ) <EOL> self . get_event ( ) <EOL> self . anchors = { } <EOL> return node <EOL> def compose_node ( self , parent , index ) : <EOL> if self . check_event ( AliasEvent ) : <EOL> event = self . get_event ( ) <EOL> anchor = event . anchor <EOL> if anchor not in self . anchors : <EOL> raise ComposerError ( None , None , "<STR_LIT>" <EOL> % anchor . encode ( '<STR_LIT:utf-8>' ) , event . start_mark ) <EOL> return self . anchors [ anchor ] <EOL> event = self . peek_event ( ) <EOL> anchor = event . anchor <EOL> if anchor is not None and anchor in self . anchors : <EOL> raise ComposerError ( "<STR_LIT>" <EOL> % anchor . encode ( '<STR_LIT:utf-8>' ) , self . anchors [ anchor ] . start_mark , <EOL> "<STR_LIT>" , event . start_mark ) <EOL> self . descend_resolver ( parent , index ) <EOL> if self . check_event ( ScalarEvent ) : <EOL> node = self . compose_scalar_node ( anchor ) <EOL> elif self . check_event ( SequenceStartEvent ) : <EOL> node = self . compose_sequence_node ( anchor ) <EOL> elif self . check_event ( MappingStartEvent ) : <EOL> node = self . compose_mapping_node ( anchor ) <EOL> self . ascend_resolver ( ) <EOL> return node <EOL> def compose_scalar_node ( self , anchor ) : <EOL> event = self . get_event ( ) <EOL> tag = event . tag <EOL> if tag is None or tag == u'<STR_LIT:!>' : <EOL> tag = self . resolve ( ScalarNode , event . value , event . implicit ) <EOL> node = ScalarNode ( tag , event . value , <EOL> event . start_mark , event . end_mark , style = event . style ) <EOL> if anchor is not None : <EOL> self . anchors [ anchor ] = node <EOL> return node <EOL> def compose_sequence_node ( self , anchor ) : <EOL> start_event = self . get_event ( ) <EOL> tag = start_event . tag <EOL> if tag is None or tag == u'<STR_LIT:!>' : <EOL> tag = self . resolve ( SequenceNode , None , start_event . implicit ) <EOL> node = SequenceNode ( tag , [ ] , <EOL> start_event . start_mark , None , <EOL> flow_style = start_event . flow_style ) <EOL> if anchor is not None : <EOL> self . anchors [ anchor ] = node <EOL> index = <NUM_LIT:0> <EOL> while not self . check_event ( SequenceEndEvent ) : <EOL> node . value . append ( self . compose_node ( node , index ) ) <EOL> index += <NUM_LIT:1> <EOL> end_event = self . get_event ( ) <EOL> node . end_mark = end_event . end_mark <EOL> return node <EOL> def compose_mapping_node ( self , anchor ) : <EOL> start_event = self . get_event ( ) <EOL> tag = start_event . tag <EOL> if tag is None or tag == u'<STR_LIT:!>' : <EOL> tag = self . resolve ( MappingNode , None , start_event . implicit ) <EOL> node = MappingNode ( tag , [ ] , <EOL> start_event . start_mark , None , <EOL> flow_style = start_event . flow_style ) <EOL> if anchor is not None : <EOL> self . anchors [ anchor ] = node <EOL> while not self . check_event ( MappingEndEvent ) : <EOL> item_key = self . compose_node ( node , None ) <EOL> item_value = self . compose_node ( node , item_key ) <EOL> node . value . append ( ( item_key , item_value ) ) <EOL> end_event = self . get_event ( ) <EOL> node . end_mark = end_event . end_mark <EOL> return node </s>
