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

Accepting CSV's fmtparams on import_from_csv #209

Open
wants to merge 2 commits into
base: develop
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
5 changes: 3 additions & 2 deletions rows/plugins/plugin_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def discover_dialect(sample, encoding):


def import_from_csv(filename_or_fobj, encoding='utf-8', dialect=None,
sample_size=8192, *args, **kwargs):
sample_size=8192, fmtparams=None, *args, **kwargs):
'''Import data from a CSV file

If a file-like object is provided it MUST be in binary mode, like in
Expand All @@ -63,7 +63,8 @@ def import_from_csv(filename_or_fobj, encoding='utf-8', dialect=None,
dialect = discover_dialect(fobj.read(sample_size), encoding)
fobj.seek(cursor)

reader = unicodecsv.reader(fobj, encoding=encoding, dialect=dialect)
fmtparams = fmtparams or {}
reader = unicodecsv.reader(fobj, encoding=encoding, dialect=dialect, **fmtparams)

meta = {'imported_from': 'csv',
'filename': filename,
Expand Down
17 changes: 17 additions & 0 deletions tests/tests_plugin_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ def test_import_from_csv_force_dialect(self, mocked_create_table):
call_args = mocked_create_table.call_args_list[0]
self.assertEqual(data, list(call_args[0][0]))

@mock.patch('rows.plugins.plugin_csv.create_table', mock.Mock())
@mock.patch('rows.plugins.plugin_csv.unicodecsv')
@mock.patch('rows.plugins.plugin_csv.get_filename_and_fobj', mock.Mock(return_value=('f_name', 'f_obj')))
def test_import_from_csv_forcing_fmtparams(self, mocked_unicodecsv):
data, lines = make_csv_data(quote_char="'",
field_delimiter=";",
line_delimiter="\r\n")
fobj = BytesIO()
fobj.write(lines.encode('utf-8'))
fobj.seek(0)

rows.import_from_csv(fobj, dialect='excel', fmtparams={'delimiter': ';'})

mocked_unicodecsv.assert_called_once_with(
'f_obj', encoding='utf-8', dialect='excel', delimiter=';'
)

def test_detect_dialect_more_data(self):
temp = tempfile.NamedTemporaryFile(delete=False)
filename = '{}.{}'.format(temp.name, self.file_extension)
Expand Down