Skip to content

Commit

Permalink
Merge 3c41e59 into a9bc75d
Browse files Browse the repository at this point in the history
  • Loading branch information
jalavik committed Jun 8, 2016
2 parents a9bc75d + 3c41e59 commit 360150f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions invenio_oaiharvester/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,15 @@ def write_to_dir(records, output_dir, max_records=1000, encoding='utf-8'):
:return: paths to files created, total number of records
"""
if not records:
return [], 0

output_path = check_or_create_dir(output_dir)

files_created = [create_file_name(output_path)]
total = 0 # total number of records processed
f = codecs.open(files_created[0], 'w+', encoding=encoding)
f.write('<ListRecords>')
for record in records:
total += 1
if total > 1 and total % max_records == 0:
Expand All @@ -204,5 +208,6 @@ def write_to_dir(records, output_dir, max_records=1000, encoding='utf-8'):
files_created.append(create_file_name(output_path))
f = codecs.open(files_created[-1], 'w+', encoding=encoding)
f.write(record.raw)
f.write('</ListRecords>')
f.close()
return files_created, total
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ def test_write_to_dir(app, tmpdir):
assert len(files) == 2
assert total == 2

files, total = write_to_dir([], tmpdir.dirname, max_records=1)
assert len(files) == 0
assert total == 0


def test_create_file_name(tmpdir):
"""oaiharvest - testing dir creation."""
Expand Down

0 comments on commit 360150f

Please sign in to comment.