Skip to content

Commit

Permalink
fix: Don't error on piped data 1db7277 #779 https://til.simonwillison…
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Apr 28, 2024
1 parent 7734845 commit 3dc81c1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ jobs:
PYTHONIOENCODING: utf-8
PYTHONUTF8: 1
run: pytest --cov agate
- name: Read from stdin
run: python -c 'import sys; import agate; agate.Table.from_csv(sys.stdin, sniff_limit=1)' < examples/test.csv
- name: Read from pipe
run: printf 'a,b,c\n1,2,3' | python -c 'import sys; import agate; agate.Table.from_csv(sys.stdin, sniff_limit=1)'
- run: python charts.py
- env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Unreleased
----------

- fix: Version 1.10.0 errors on piped data.

1.10.1 - April 28, 2024
-----------------------

Expand Down
20 changes: 13 additions & 7 deletions agate/table/from_csv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
import itertools
from io import StringIO
import sys


@classmethod
Expand Down Expand Up @@ -63,14 +64,19 @@ def from_csv(cls, path, column_names=None, column_types=None, row_names=None, sk

if sniff_limit is None:
# Reads to the end of the tile, but avoid reading the file twice.
handle = StringIO(f.read())
kwargs['dialect'] = csv.Sniffer().sniff(handle.getvalue())
handle = io.StringIO(f.read())
sample = handle.getvalue()
elif sniff_limit > 0:
offset = f.tell()
if f == sys.stdin:
buffered = io.BufferedReader(sys.stdin.buffer)
handle = io.TextIOWrapper(buffered, encoding=encoding)
sample = buffered.peek(sniff_limit).decode(encoding) # reads *bytes*
else:
offset = f.tell()
sample = f.read(sniff_limit) # reads *characters*
f.seek(offset) # can't do f.seek(-sniff_limit, os.SEEK_CUR) unless file was opened in binary mode

# Reads only the start of the file.
kwargs['dialect'] = csv.Sniffer().sniff(f.read(sniff_limit))
f.seek(offset)
kwargs['dialect'] = csv.Sniffer().sniff(sample)

reader = csv.reader(handle, header=header, **kwargs)

Expand Down

0 comments on commit 3dc81c1

Please sign in to comment.