示例#1
0
    def _fresh_content_events(self, content_iterable):
        # Header events
        yield yaml.StreamStartEvent()
        yield yaml.DocumentStartEvent()
        yield yaml.MappingStartEvent(None, None, True, flow_style=False)

        # Content events
        for key, value in content_iterable:
            yield yaml.ScalarEvent(None, None, (True, False), key)
            if isinstance(value, dict):
                yield from _yaml_content_events(
                    dict((k, v) for k, v in value.items()
                         if k not in self.CASE_PRIMARY_KEYS))
            elif callable(getattr(value, 'case_data_events')):
                yield yaml.MappingStartEvent(None,
                                             None,
                                             True,
                                             flow_style=False)
                yield from value.case_data_events()
                yield yaml.MappingEndEvent()
            else:
                yield yaml.MappingStartEvent(None,
                                             None,
                                             True,
                                             flow_style=False)
                yield from value
                yield yaml.MappingEndEvent()

        # Tail events
        yield yaml.MappingEndEvent()
        yield yaml.DocumentEndEvent()
        yield yaml.StreamEndEvent()
示例#2
0
 def parse_document(self):
     node = None
     if self.check_token(yaml.DirectiveToken):
         self.get_token(yaml.DirectiveToken)
     self.get_token(yaml.DocumentStartToken)
     self.events.append(yaml.DocumentStartEvent(None, None))
     self.parse_node()
     self.events.append(yaml.DocumentEndEvent(None, None))
示例#3
0
    def _case_yaml_events(self, content_events):
        yield yaml.StreamStartEvent()
        yield yaml.DocumentStartEvent(explicit=True)
        yield yaml.SequenceStartEvent(None,
                                      None,
                                      implicit=True,
                                      flow_style=False)

        yield from content_events

        yield yaml.SequenceEndEvent()
        yield yaml.DocumentEndEvent()
        yield yaml.StreamEndEvent()
示例#4
0
    def _StoreYAMLToStream(self, stream):
        events = [
            yaml.StreamStartEvent(encoding='ascii'),
            yaml.DocumentStartEvent(explicit=True),
            yaml.SequenceStartEvent(anchor=None,
                                    tag=None,
                                    implicit=True,
                                    flow_style=False),
        ]

        # Serialize all elements in order
        for e in self.__elements:
            events.extend(e._GetYAMLEvents())

        events.extend([
            yaml.SequenceEndEvent(),
            yaml.DocumentEndEvent(explicit=True),
            yaml.StreamEndEvent(),
        ])

        yaml.emit(events, stream)
示例#5
0
def value_from_event_stream(content_events, *, safe_loading=True):
    """Convert an iterable of YAML events to a Pythonic value
    
    The *content_events* MUST NOT include stream or document events.
    """
    content_events = iter(content_events)
    events = [yaml.StreamStartEvent(), yaml.DocumentStartEvent()]
    depth = 0
    while True:
        events.append(next(content_events))
        if isinstance(events[-1], yaml.CollectionStartEvent):
            depth += 1
        elif isinstance(events[-1], yaml.CollectionEndEvent):
            depth -= 1

        if depth == 0:
            break
    events.extend([yaml.DocumentEndEvent(), yaml.StreamEndEvent()])
    node = yaml.compose(events, Loader=EventsToNodes)
    node_constructor = (yaml.constructor.SafeConstructor
                        if safe_loading else yaml.constructor.Constructor)()
    return node_constructor.construct_object(node, True)
def gff2yamlevents(data, header, str_labels=False):
    if str_labels:
        sfa = dict()

    yield yaml.StreamStartEvent(encoding='utf-8')
    yield yaml.DocumentStartEvent()
    yield MSE(anchor=None, tag=u'!Header', implicit=False, flow_style=False)
    yield SE(value=u'platform')
    yield SE(value=unicode(header.platform))
    yield SE(value=u'file_type')
    yield SE(value=unicode(header.file_type))
    yield SE(value=u'file_version')
    yield SE(value=unicode(header.file_version))
    yield SE(value=u'structs')
    yield SSE()
    for struct_i, struct in enumerate(header.structs):
        yield MSE(anchor=None,
                  tag=u'!Structure',
                  implicit=True,
                  flow_style=False)
        yield SE(value='name')
        yield SE(value=struct.fourcc)
        yield SE(value='fields')
        yield SSE()
        fa = dict()
        if str_labels:
            fa = generate_aliases([field.label for field in struct.fields])
            sfa[struct_i] = fa

        for field in struct.fields:
            if isinstance(field, Field):
                yield MSE(anchor=None,
                          tag=u'!Field',
                          implicit=True,
                          flow_style=True)
                yield SE(value='label')
                yield SE(value=unicode(field.label))
                if str_labels and field.label in fa:
                    yield SE(value='alias')
                    yield SE(value=fa[field.label])
                ftype = field.type
                indirect = field.indirect
                if issubclass(ftype, List):
                    yield SE(value='is_list')
                    yield SE(value='Yes')
                    indirect = ftype.indirect
                    ftype = ftype.elem_type
                if indirect:
                    yield SE(value='is_ref')
                    yield SE(value='Yes')
                if issubclass(ftype, Structure):
                    yield SE(value='is_struct')
                    yield SE(value='Yes')
                    yield SE(value='type')
                    yield SE(value=unicode(header.structs.index(ftype)))
                elif ftype is not None:
                    yield SE(value='type')
                    yield SE(value=unicode(ftype.id))
                yield MEE()
        yield SEE()
        yield MEE()
    yield SEE()
    yield MEE()
    yield yaml.DocumentEndEvent()

    def emit_value(value, vtype, indirect):
        if issubclass(vtype, List):
            return emit_list(value, vtype.elem_type, vtype.indirect)
        elif indirect:
            return emit_reference(value, vtype)
        elif issubclass(vtype, Structure):
            return emit_struct(value)
        elif value is None:
            return [SE(tag='!!null', value='')]
        else:
            return emit_primitive(value)

    def emit_primitive(value, implicit=True):
        tag = '!' + type(value).name
        if isinstance(value, tuple):
            yield SSE(tag=tag, implicit=implicit, flow_style=True)
            for v in value:
                if v is None:
                    yield SE(tag='!!null', value='')
                else:
                    yield SE(value=unicode(v))
            yield SEE()
        else:
            yield SE(tag=tag,
                     value=unicode(value),
                     implicit=(implicit, implicit))

    def emit_list(value, vtype, indirect):
        if value is not None:
            if indirect:
                yield SSE()
                if vtype is None:
                    for v in value:
                        if v is None:
                            yield SE(tag='!!null', value='')
                        elif isinstance(v, Structure):
                            for evt in emit_struct(v, False):
                                yield evt
                        else:
                            for evt in emit_primitive(v, False):
                                yield evt
                else:
                    for v in value:
                        if v is None:
                            yield SE(tag='!!null', value='')
                        else:
                            emit_value(v, vtype, True)
                yield SEE()
            elif issubclass(vtype, Structure):
                yield SSE()
                for v in value:
                    for evt in emit_struct(v):
                        yield evt
                yield SEE()
            elif isinstance(value, str):
                yield SE(tag='!binary',
                         value=encodestring(value),
                         style='|',
                         implicit=(False, False))
            else:
                yield SSE()
                for v in value:
                    for evt in emit_primitive(v):
                        yield evt
                yield SEE()
        else:
            yield SE(tag='!!null', value='')

    def emit_struct(struct, implicit=True):
        def simplekind(field):
            return issubclass(field.type, (int, long, float))

        simple = all(simplekind(field) for field in struct.fields)
        yield MSE(anchor=None,
                  tag='!' + struct.fourcc,
                  implicit=implicit,
                  flow_style=simple)
        if str_labels:
            fa = sfa[header.structs.index(type(struct))]
        for label in struct:
            if str_labels and label in fa:
                yield SE(value=fa[label])
            else:
                yield SE(value=unicode(label))
            field = struct.getfieldbylabel(label)
            #print type(struct), field
            for evt in emit_value(struct[label], field.type, field.indirect):
                yield evt
        yield MEE()

    def emit_reference(value, vtype):
        if vtype is None:
            if value is None:
                yield SE(tag='!!null', value='')
            elif isinstance(value, Structure):
                for evt in emit_struct(value, False):
                    yield evt
            else:
                for evt in emit_primitive(value, False):
                    yield evt
        else:
            if value is None:
                yield SE(tag='!!null', value='')
            else:
                for evt in emit_value(value, vtype, False):
                    yield evt

    yield yaml.DocumentStartEvent()
    for evt in emit_struct(data):
        yield evt
    yield yaml.DocumentEndEvent()

    yield yaml.StreamEndEvent()