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

BUG: fix multiple issues with directory and json types #574

Open
wants to merge 1 commit into
base: master
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: 8 additions & 4 deletions odo/backends/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def discover_jsonlines(j, n=10, encoding='utf-8', **kwargs):

@convert.register(list, (JSON, Temp(JSON)))
def json_to_list(j, dshape=None, **kwargs):
return json_load(j.path, **kwargs)
result = json_load(j.path, **kwargs)
if not isinstance(result, list):
result = [result]
return result


@convert.register(Iterator, (JSONLines, Temp(JSONLines)))
Expand Down Expand Up @@ -138,9 +141,10 @@ def json_load(path, encoding='utf-8', **kwargs):
f = open(path)
s = f.read()

data = json.loads(s)

f.close()
try:
data = json.loads(s)
finally:
f.close()

return data

Expand Down
5 changes: 5 additions & 0 deletions odo/backends/tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def test_convert_json_list():
j = JSON(fn)
assert convert(list, j) == dat

non_list_data ={'a': 1}
with json_file(non_list_data) as fn:
j = JSON(fn)
assert convert(list, j) == [non_list_data]


def test_convert_jsonlines():
with jsonlines_file(dat) as fn:
Expand Down
40 changes: 28 additions & 12 deletions odo/directory.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from __future__ import absolute_import, division, print_function

from glob import glob
import os
import re

from datashape import discover, var
from toolz import memoize, first

from .chunks import Chunks, chunks
from .resource import resource
from .utils import copydoc
from toolz import memoize, first
from datashape import discover, var
import os


class _Directory(Chunks):
Expand All @@ -25,29 +28,42 @@ class _Directory(Chunks):


"""
@staticmethod
def container(uri):
raise NotImplementedError('overridden by Directory(cls) types')

def __init__(self, path, **kwargs):
self.path = path
self.kwargs = kwargs

def __iter__(self):
return (resource(os.path.join(self.path, fn), **self.kwargs)
for fn in sorted(os.listdir(self.path)))
return (
self.container(
os.path.join(self.path, fn),
**self.kwargs
)
for fn in sorted(os.listdir(self.path))
)


@memoize
@copydoc(_Directory)
def Directory(cls):
""" Parametrized DirectoryClass """
return type('Directory(%s)' % cls.__name__, (_Directory, chunks(cls)), {})
"""Parametrized Directory class.
"""
return type(
'Directory(%s)' % cls.__name__,
(_Directory, chunks(cls)),
{'container': cls},
)


re_path_sep = re.escape(os.path.sep)

re_path_sep = os.path.sep
if re_path_sep == '\\':
re_path_sep = '\\\\'

@discover.register(_Directory)
def discover_Directory(c, **kwargs):
return var * discover(first(c)).subshape[0]
def discover_directory(c, **kwargs):
return var * discover(first(c)).measure


@resource.register('.+' + re_path_sep + '\*\..+', priority=15)
Expand Down