def dump_query(manager, prefix, name, q):
    uids = set()
    entities = []
    jsons = []
    file_base = os.path.join(prefix, name)
    meta = {
        'csv': [{
            'label': 'Entities',
            'file': name + '.entities.csv'
        }],
        'xlsx': name + '.xlsx',
        'json': name + '.json'
    }

    for entity in manager._entities.find(**q):
        uids.add(entity.get('uid'))
        json_repr = entity.pop('json', None)
        if json_repr is not None:
            jsons.append(json.loads(json_repr))
        entities.append(entity)
    entities = sorted(entities, key=lambda e: e.get('name'))
    csv_generate(file_base + '.entities.csv', entities)

    sheets = [('Entities', entities)]
    tables = {
        ('Aliases', 'other_names'): manager._other_names,
        ('Documents', 'identities'): manager._identities,
        ('Addresses', 'addresses'): manager._addresses
    }
    for (label, section), table in tables.items():
        q = table.table.select().where(table.table.c.uid.in_(uids))
        rows = list(manager.engine.query(q))
        rows = sorted(rows, key=lambda r: r.get('name'))
        csv_generate('%s.%s.csv' % (file_base, section), rows)
        sheets.append((label, rows))
        meta['csv'].append({
            'label': label,
            'file': '%s.%s.csv' % (name, section)
        })

    xlsx_generate(file_base + '.xlsx', sheets)
    json_generate(file_base + '.json', jsons)
    return meta
dataset/ETHPy150Open pudo/opennames/pepparser/dump.py/dump_query/original