Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an encoding parameter to io.load_tabby #116

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions datalad_tabby/io/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def load_tabby(
jsonld: bool = True,
recursive: bool = True,
cpaths: List | None = None,
encoding: str | None = None,
) -> Dict | List:
"""Load a tabby (TSV) record as structured (JSON(-LD)) data

Expand All @@ -48,11 +49,16 @@ def load_tabby(

With the ``jsonld`` flag, a declared or default JSON-LD context is
loaded and inserted into the record.

Tsv file encoding used when reading can be specified with the
``encoding`` parameter.

"""
ldr = _TabbyLoader(
jsonld=jsonld,
recursive=recursive,
cpaths=cpaths,
encoding=encoding,
)
return ldr(src=src, single=single)

Expand All @@ -63,13 +69,15 @@ def __init__(
jsonld: bool = True,
recursive: bool = True,
cpaths: List[Path] | None = None,
encoding: str | None = None,
):
std_convention_path = Path(__file__).parent / 'conventions'
if cpaths is None:
cpaths = [std_convention_path]
else:
cpaths.append(std_convention_path)
self._cpaths = cpaths
self._encoding = encoding
self._jsonld = jsonld
self._recursive = recursive

Expand All @@ -95,7 +103,7 @@ def _load_single(
trace=trace,
)

with src.open(newline='') as tsvfile:
with src.open(newline='', encoding=self._encoding) as tsvfile:
reader = csv.reader(tsvfile, delimiter='\t')
# row_id is useful for error reporting
for row_id, row in enumerate(reader):
Expand Down Expand Up @@ -146,7 +154,7 @@ def _load_many(
# to do with any possibly loaded JSON data
fieldnames = None

with src.open(newline='') as tsvfile:
with src.open(newline='', encoding=self._encoding) as tsvfile:
# we cannot use DictReader -- we need to support identically named
# columns
reader = csv.reader(tsvfile, delimiter='\t')
Expand Down
2 changes: 1 addition & 1 deletion docs/source/best-practices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ covers the entire document, including content inserted from other tables.
When individual tables require a different context specification, it can be
declared in the respective ``<prefix>_<table-name>.ctx.jsonld`` side-car files.
Such a context is inserted in each metadata object read from the respective
table. Standard JSON-LD rules for context scoping and propgation apply to the
table. Standard JSON-LD rules for context scoping and propagation apply to the
semantics of such a declaration.

A third approach to context specification is a record-global
Expand Down