Skip to content

Commit

Permalink
Support lazy files with pyjson5
Browse files Browse the repository at this point in the history
In order for pyjson5 to be satisfied, it must be passed the real file
descriptor (or something satisfying `io.IOBase`). For simplicity, pass
it the underlying open() result.

Also improve the file close behavior using a `finally` block.
  • Loading branch information
sirosen committed Dec 6, 2023
1 parent 59a7816 commit 71accf7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/check_jsonschema/cli/param_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def convert(
return t.cast(t.Type[jsonschema.protocols.Validator], result)


class _CustomLazyFile(click.utils.LazyFile):
class CustomLazyFile(click.utils.LazyFile):
def __init__(
self,
filename: str | os.PathLike[str],
Expand Down Expand Up @@ -150,7 +150,7 @@ def convert(

value_: str | os.PathLike[str] = t.cast("str | os.PathLike[str]", value)

lf = _CustomLazyFile(value_, mode="rb")
lf = CustomLazyFile(value_, mode="rb")
if ctx is not None:
ctx.call_on_close(lf.close_intelligently)
return t.cast(t.IO[bytes], lf)
25 changes: 17 additions & 8 deletions src/check_jsonschema/instance_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io
import typing as t

from check_jsonschema.cli.param_types import CustomLazyFile

from .parsers import ParseError, ParserSet
from .transforms import Transform

Expand Down Expand Up @@ -35,14 +37,21 @@ def iter_files(self) -> t.Iterator[tuple[str, ParseError | t.Any]]:
name = "<stdin>"
else:
raise ValueError(f"File {file} has no name attribute")

try:
data: t.Any = self._parsers.parse_data_with_path(
file, name, self._default_filetype
)
except ParseError as err:
data = err
else:
data = self._data_transform(data)
if isinstance(file, CustomLazyFile):
stream = file.open()
else:
stream = file

file.close()
try:
data: t.Any = self._parsers.parse_data_with_path(
stream, name, self._default_filetype
)
except ParseError as err:
data = err
else:
data = self._data_transform(data)
finally:
file.close()
yield (name, data)

0 comments on commit 71accf7

Please sign in to comment.