예제 #1
0
def to_proto(traversal, indexer, node_interface=default_node_interface):

    tree = proto.AnnotatedTree()
    trans = {'count': 0}

    for parent, pid, attr_name, child, cid, depth in traversal:
        if parent is None or child is None:
            continue

        parent_interface = node_interface(parent)
        child_interface = node_interface(child)

        reindex_child = not _match_line(parent_interface, child_interface)
        cid = _add_proto_node(tree,
                              child_interface,
                              cid,
                              depth,
                              trans,
                              indexer,
                              use_row=reindex_child)
        pid = _add_proto_node(tree, parent_interface, pid, depth - 1, trans,
                              indexer)
        attr_id = indexer.index('attr_names', attr_name)

        tree.from_node.append(cid)
        tree.assignment.append(pid)
        tree.assign_attr.append(attr_id)

    return tree
예제 #2
0
 def __init__(self, name, indexer=None):
     self.proto = proto.AnnotatedTree()
     self.proto.name = name
     self._fields = set(
         [field.name for field in self.proto.DESCRIPTOR.fields])
     self._field_access_pattern = None
     self._indexer = indexer
예제 #3
0
def stream_lmdb(root, decode=False):
    env_in = lmdb.open(root,
                       max_readers=1,
                       readonly=True,
                       lock=False,
                       readahead=False,
                       meminit=False,
                       max_dbs=1)

    in_db = env_in.open_db(key='cpg-data'.encode('utf-8'), create=False)

    with env_in.begin(write=False, db=in_db) as txn1:
        cursor = txn1.cursor()

        for idx, data in tqdm(enumerate(cursor.iternext()),
                              total=txn1.stat()['entries']):

            _, data = data

            if decode:
                tree = pb.AnnotatedTree()
                tree.ParseFromString(data)
                data = tree

            yield idx, data

        cursor.close()
예제 #4
0
def to_proto(data):
    tree = proto.AnnotatedTree()
    tree.ParseFromString(data)
    return tree