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

ARROW-2600: [Python] Add additional LocalFileSystem filesystem methods #2060

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions python/pyarrow/filesystem.py
Expand Up @@ -17,6 +17,7 @@

from os.path import join as pjoin
import os
import sys
import posixpath

from pyarrow.util import implements
Expand Down Expand Up @@ -182,6 +183,12 @@ def open(self, path, mode='rb'):
"""
raise NotImplementedError

def head(self, files, line_count=10):
"""
Take a file or list of files and print out the first n lines of the
file.
"""

@property
def pathsep(self):
return '/'
Expand Down Expand Up @@ -231,6 +238,29 @@ def open(self, path, mode='rb'):
"""
return open(path, mode=mode)

def _head_stdout(self, file_name, line_count):
with open(file_name, mode='r') as f:
for i, line in enumerate(f):
if i > line_count:
break
else:
sys.stdout.write(line)
sys.stdout.flush()

@implements(FileSystem.head)
def head(self, files, line_count=10):
"""
Take a file or list of files and print out the first n lines of the
file.
"""
if isinstance(files, str):
self._head_stdout(files, line_count - 1)
else:
for file in files:
print("==> %s <==" % file)
self._head_stdout(file, line_count - 1)
print()

@property
def pathsep(self):
return os.path.sep
Expand Down
109 changes: 109 additions & 0 deletions python/pyarrow/tests/test_localfilesystem.py
@@ -0,0 +1,109 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import pyarrow as pa
import pytest
import os


@pytest.fixture(scope="module")
def head_files():
with open("test_apache_license.txt", "w+") as license:
license.write("""# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
""")

with open("test_setup.py", "w+") as license:
license.write("""import contextlib
import glob
import os
import os.path as osp
import re
import shlex
import shutil
import sys

from Cython.Distutils import build_ext as _build_ext
import Cython


import pkg_resources
from setuptools import setup, Extension, Distribution

from os.path import join as pjoin

""")
yield
os.remove("test_apache_license.txt")
os.remove("test_setup.py")


def test_lfs_single_file_head(head_files, capsys):
head = """# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
"""
pa.LocalFileSystem().head("test_apache_license.txt", 4)
captured = capsys.readouterr()
assert captured.out == head


def test_lfs_file_list_head(head_files, capsys):
files = ["test_apache_license.txt", "test_setup.py"]
head = """==> test_apache_license.txt <==
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#

==> test_setup.py <==
import contextlib
import glob
import os
import os.path as osp
import re
import shlex
import shutil
import sys

from Cython.Distutils import build_ext as _build_ext

"""

pa.LocalFileSystem().head(files)
captured = capsys.readouterr()
assert captured.out == head