Skip to content

Commit

Permalink
Make encoding a property of TabbyLoader
Browse files Browse the repository at this point in the history
Because load functions are used recursively (when load statements are
found in a tabby file), it would be too much hassle to pass the
encoding parameter around - better use `self._encoding`.
  • Loading branch information
mslw committed Nov 21, 2023
1 parent 070937a commit 0b755d4
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions datalad_tabby/io/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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 @@ -50,11 +51,14 @@ def load_tabby(
With the ``jsonld`` flag, a declared or default JSON-LD context is
loaded and inserted into the record.
Encoding used when reading tsv files can be specified as ``encoding``.
"""
ldr = _TabbyLoader(
jsonld=jsonld,
recursive=recursive,
cpaths=cpaths,
encoding=encoding,
)
return ldr(src=src, single=single)

Expand All @@ -65,29 +69,29 @@ 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

def __call__(self, src: Path, *, single: bool = True, encoding: str | None = None):
def __call__(self, src: Path, *, single: bool = True):
return (self._load_single if single else self._load_many)(
src=src,
trace=[],
encoding=encoding,
)

def _load_single(
self,
*,
src: Path,
trace: List,
encoding: str | None = None,
) -> Dict:
jfpath = self._get_corresponding_jsondata_fpath(src)
obj = json.load(jfpath.open()) if jfpath.exists() else {}
Expand All @@ -98,9 +102,8 @@ def _load_single(
src=src,
trace=trace,
)

if encoding is not None:
tsv_obj = self._parse_tsv_single(src, encoding=encoding)
if self._encoding is not None:
tsv_obj = self._parse_tsv_single(src, encoding=self._encoding)
else:
try:
tsv_obj = self._parse_tsv_single(src)
Expand Down Expand Up @@ -145,7 +148,6 @@ def _load_many(
*,
src: Path,
trace: List,
encoding: str | None = None,
) -> List[Dict]:
obj_tmpl = {}
array = list()
Expand All @@ -165,10 +167,10 @@ def _load_many(

# the table field/column names have purposefully _nothing_
# to do with any possibly loaded JSON data

if encoding is not None:
breakpoint()
if self._encoding is not None:
tsv_array = self._parse_tsv_many(
src, obj_tmpl, trace=trace, fieldnames=None, encoding=encoding
src, obj_tmpl, trace=trace, fieldnames=None, encoding=self._encoding
)
else:
try:
Expand Down

0 comments on commit 0b755d4

Please sign in to comment.