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

Primaly implementation of read content from stdin #203

Open
wants to merge 1 commit 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
1 change: 0 additions & 1 deletion rows/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def _get_field_names(field_names, table_field_names, permit_not=False):
else:
return new_field_names


@click.group()
@click.version_option(version=rows.__version__, prog_name='rows')
def cli():
Expand Down
25 changes: 20 additions & 5 deletions rows/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import mimetypes
import os
import tempfile
import sys
from io import BytesIO

try:
from urlparse import urlparse # Python 2
Expand Down Expand Up @@ -162,7 +164,10 @@ def detect_local_source(path, content, mime_type=None, encoding=None):

# TODO: may add sample_size

filename = os.path.basename(path)
if isinstance(path, BytesIO):
filename = '-'
else:
filename = os.path.basename(path)
parts = filename.split('.')
extension = parts[-1] if len(parts) > 1 else None

Expand All @@ -178,6 +183,10 @@ def detect_local_source(path, content, mime_type=None, encoding=None):
mime_type = mime_type or mimetypes.guess_type(filename)[0]

plugin_name = plugin_name_by_mime_type(mime_type, mime_name, extension)
if plugin_name == None:
plugin_name = 'csv'
path = sys.stdin

if encoding == 'binary':
encoding = None

Expand All @@ -188,9 +197,16 @@ def detect_local_source(path, content, mime_type=None, encoding=None):

def local_file(path, sample_size=1048576):

# TODO: may change sample_size
with open(path, 'rb') as fobj:
content = fobj.read(sample_size)
if path == '-':
if (sys.version_info > (3, 5)):
content = sys.stdin.buffer.read()
else:
content = sys.stdin.read()
path = BytesIO(content)
else:
# TODO: may change sample_size
with open(path, 'rb') as fobj:
content = fobj.read(sample_size)

source = detect_local_source(path, content, mime_type=None, encoding=None)

Expand Down Expand Up @@ -249,7 +265,6 @@ def detect_source(uri, verify_ssl, timeout=5):

if uri.startswith('http://') or uri.startswith('https://'):
return download_file(uri, verify_ssl=verify_ssl, timeout=timeout)

else:
return local_file(uri)

Expand Down