Skip to content

Commit e74765b

Browse files
authored
Issue 119: Change retrieve_data to be a generator
See issue josegonzalez#119.
1 parent 6db5bd7 commit e74765b

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

bin/github-backup

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,11 @@ def get_github_repo_url(args, repository):
406406
return repo_url
407407

408408

409-
def retrieve_data(args, template, query_args=None, single_request=False):
409+
def retrieve_data_gen(args, template, query_args=None, single_request=False):
410410
auth = get_auth(args)
411411
query_args = get_query_args(query_args)
412412
per_page = 100
413413
page = 0
414-
data = []
415414

416415
while True:
417416
page = page + 1
@@ -438,20 +437,21 @@ def retrieve_data(args, template, query_args=None, single_request=False):
438437
response = json.loads(r.read().decode('utf-8'))
439438
if len(errors) == 0:
440439
if type(response) == list:
441-
data.extend(response)
440+
for resp in response:
441+
yield resp
442442
if len(response) < per_page:
443443
break
444444
elif type(response) == dict and single_request:
445-
data.append(response)
445+
yield response
446446

447447
if len(errors) > 0:
448448
log_error(errors)
449449

450450
if single_request:
451451
break
452452

453-
return data
454-
453+
def retrieve_data(args, template, query_args=None, single_request=False):
454+
return list(retrieve_data_gen(args, template, query_args, single_request))
455455

456456
def get_query_args(query_args=None):
457457
if not query_args:
@@ -836,18 +836,21 @@ def backup_pulls(args, repo_cwd, repository, repos_template):
836836
pull_states = ['open', 'closed']
837837
for pull_state in pull_states:
838838
query_args['state'] = pull_state
839-
# It'd be nice to be able to apply the args.since filter here...
840-
_pulls = retrieve_data(args,
839+
_pulls = retrieve_data_gen(args,
841840
_pulls_template,
842841
query_args=query_args)
843842
for pull in _pulls:
843+
if args.since and pull['updated_at'] < args.since:
844+
break
844845
if not args.since or pull['updated_at'] >= args.since:
845846
pulls[pull['number']] = pull
846847
else:
847-
_pulls = retrieve_data(args,
848+
_pulls = retrieve_data_gen(args,
848849
_pulls_template,
849850
query_args=query_args)
850851
for pull in _pulls:
852+
if args.since and pull['updated_at'] < args.since:
853+
break
851854
if not args.since or pull['updated_at'] >= args.since:
852855
pulls[pull['number']] = retrieve_data(
853856
args,

0 commit comments

Comments
 (0)