Skip to content

Commit

Permalink
Merge pull request #176 from mariob0y/feat/detect_format_reader
Browse files Browse the repository at this point in the history
feat: added custom reader support for `detect_format`
  • Loading branch information
jpmckinney committed Jun 16, 2021
2 parents be377c3 + 970887c commit df5c5ff
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Expand Up @@ -7,3 +7,4 @@ recursive-include tests *.json
recursive-include tests *.jsonl
recursive-include tests *.py
recursive-include tests *.yaml
recursive-include tests *.gz
10 changes: 9 additions & 1 deletion docs/changelog.rst
@@ -1,6 +1,14 @@
Changelog
=========

1.0.1 (Unreleased)
------------------

Added
~~~~~

- :meth:`ocdskit.util.detect_format` accepts a ``reader`` keyword argument: for example, ``gzip.open`` instead of ``open``.

1.0.0 (2021-05-19)
------------------

Expand Down Expand Up @@ -32,7 +40,7 @@ Fixed
Fixed
~~~~~

- :ref:`schema-strict`: Don't add ``"uniqueItems": true`` to coordinates fields.
- :ref:`schema-strict`: Don't add ``"uniqueItems": true`` to coordinates fields.

0.2.21 (2021-04-10)
-------------------
Expand Down
4 changes: 2 additions & 2 deletions ocdskit/util.py
Expand Up @@ -188,7 +188,7 @@ def _empty_package(uri, publisher, published_date, version):
}


def detect_format(path, root_path=''):
def detect_format(path, root_path='', reader=open):
"""
Returns the format of OCDS data, and whether the OCDS data is concatenated or in an array.
Expand All @@ -200,7 +200,7 @@ def detect_format(path, root_path=''):
:rtype: tuple
:raises UnknownFormatError: if the format cannot be detected
"""
with open(path, 'rb') as f:
with reader(path, 'rb') as f:
events = iter(ijson.parse(f, multiple_values=True))

while True:
Expand Down
Binary file added tests/fixtures/ocds-sample-data.json.gz
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/test_util.py
@@ -1,3 +1,4 @@
import gzip
import json

import pytest
Expand Down Expand Up @@ -142,3 +143,12 @@ def test_detect_format(filename, expected):
result = detect_format(path(filename))

assert result == expected


@pytest.mark.parametrize('filename,expected', [
('ocds-sample-data.json.gz', ('release package', False, False))
])
def test_detect_format_gz(filename, expected):
result = detect_format(path(filename), reader=gzip.open)

assert result == expected

0 comments on commit df5c5ff

Please sign in to comment.