Skip to content

Commit

Permalink
Merge pull request #2412 from midichef/guess_errors
Browse files Browse the repository at this point in the history
[loaders-] handle exceptions in filetype guess
  • Loading branch information
anjakefala committed Jun 25, 2024
2 parents 5723b4b + 9fe6adc commit 8c16d46
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion visidata/loaders/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
def guess_csv(vd, p):
import csv
csv.field_size_limit(2**31-1) #288 Windows has max 32-bit
line = next(p.open())
try:
line = next(p.open())
except StopIteration:
return
if ',' in line:
dialect = csv.Sniffer().sniff(line)
r = dict(filetype='csv', _likelihood=0)
Expand Down
5 changes: 4 additions & 1 deletion visidata/loaders/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
@VisiData.api
def guess_json(vd, p):
with p.open(encoding=vd.options.encoding) as fp:
line = next(fp)
try:
line = next(fp)
except StopIteration:
return

line = line.strip()

Expand Down
10 changes: 8 additions & 2 deletions visidata/loaders/jsonla.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ def guess_jsonla(vd, p):
'''

with p.open(encoding=vd.options.encoding) as fp:
first_line = next(fp)
try:
first_line = next(fp)
except StopIteration:
return

if first_line.strip().startswith('['):
ret = json.loads(first_line)
try:
ret = json.loads(first_line)
except json.decoder.JSONDecodeError:
return
if isinstance(ret, list) and all(isinstance(v, str) for v in ret):
return dict(filetype='jsonla')

Expand Down

0 comments on commit 8c16d46

Please sign in to comment.