Skip to content

Commit

Permalink
csv: add allowed delimiters parameter to sniffer
Browse files Browse the repository at this point in the history
  • Loading branch information
slint authored and Diego committed Jul 24, 2019
1 parent 72ec8db commit 608eb76
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions invenio_previewer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
PREVIEWER_CSV_VALIDATION_BYTES = 1024
"""Number of bytes read by CSV previewer to validate the file."""

PREVIEWER_CSV_SNIFFER_ALLOWED_DELIMITERS = None
"""Allowed delimiter characters passed to the ``csv.Sniffer.sniff`` method."""

PREVIEWER_CHARDET_BYTES = 1024
"""Number of bytes to read for character encoding detection by `cchardet`."""

Expand Down
6 changes: 5 additions & 1 deletion invenio_previewer/extensions/csv_dthreejs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ def validate_csv(file):
encoding = detect_encoding(fp, default='utf-8')
sample = fp.read(
current_app.config.get('PREVIEWER_CSV_VALIDATION_BYTES', 1024))
delimiter = csv.Sniffer().sniff(sample.decode(encoding)).delimiter
allowed_delimiters = current_app.config.get(
'PREVIEWER_CSV_SNIFFER_ALLOWED_DELIMITERS', None)
delimiter = csv.Sniffer().sniff(
sample=sample.decode(encoding),
delimiters=allowed_delimiters).delimiter
is_valid = True
except Exception as e:
current_app.logger.debug(
Expand Down

0 comments on commit 608eb76

Please sign in to comment.