Skip to content

Commit ea1f1d6

Browse files
committed
bin: enable reading of EntitySets per partes
Some services limit size of GET EntitySet response to a certain number of Entities (20, 1000, ...). This patch adds the possibility to specify number of entities retrieved in a request. The executable utility will try to get all entities in one request and will not check number of returned entities by default. If the user adds '--chunk-size 5', the utility will be sending GET requests until it read the entity set completely.
1 parent 4c128e9 commit ea1f1d6

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

bin/pyodata

+29-8
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,39 @@ def print_out_metadata_info(args, client):
5757

5858
def print_out_entity_set(args, client):
5959
typ = client.schema.entity_set(args.name).entity_type
60-
request = getattr(client.entity_sets, args.name).get_entities()
61-
entities = request.execute()
60+
entity_set = getattr(client.entity_sets, args.name)
61+
62+
count = entity_set.get_entities().count().execute()
63+
count_width = len(str(count))
64+
print('[Size of entity set:', count, ']')
65+
66+
skip = 0
67+
top = args.chunk_size
68+
start = 0
6269

6370
keys = {kp.name for kp in typ.key_proprties}
6471
members = {mp.name for mp in typ.proprties() if mp.name not in keys}
6572

66-
for entity in entities:
67-
for prop in keys:
68-
print(f'K {prop} = {getattr(entity, prop)}')
73+
while skip < count:
74+
if args.chunk_size < 1:
75+
entities = entity_set.get_entities().execute()
76+
skip = count # stop iteration after the first run
77+
else:
78+
entities = entity_set.get_entities().skip(skip).top(top).execute()
79+
80+
for seq_no, entity in enumerate(entities, start=start):
81+
seq_no_str = str(seq_no)
82+
print('-- {0:-03}'.format(seq_no), '-' * (76 - len(seq_no_str)))
83+
for prop in keys:
84+
print(f'K {prop} = {getattr(entity, prop)}')
85+
86+
for prop in members:
87+
print(f'+ {prop} = {getattr(entity, prop)}')
6988

70-
for prop in members:
71-
print(f'+ {prop} = {getattr(entity, prop)}')
89+
print('-' * 80)
7290

73-
print('-' * 80)
91+
skip += top
92+
start += len(entities)
7493

7594

7695
def print_out_function_import(args, client):
@@ -93,6 +112,8 @@ def _parse_args(argv):
93112
help='Specify metadata parser custom error handlers in the form: TARGET=POLICY')
94113
parser.add_argument('-v', '--verbose', dest='verbose_count', action='count', default=0,
95114
help='make verbose output')
115+
parser.add_argument('--chunk-size', type=int, default=0,
116+
help='amount of fetched entities in one GET EntitySet Request; 0 means all')
96117

97118
parser.set_defaults(func=print_out_metadata_info)
98119

0 commit comments

Comments
 (0)