Skip to content

Commit

Permalink
Add client list method.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtth committed Apr 7, 2015
1 parent e709f89 commit 44aacf6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion hdfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"""HdfsCLI."""

__version__ = '0.5.1'
__version__ = '0.5.2'

import logging as lg
try:
Expand Down
21 changes: 21 additions & 0 deletions hdfs/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,27 @@ def rename(self, hdfs_src_path, hdfs_dst_path):
self.resolve(hdfs_src_path), hdfs_dst_path
)

def list(self, hdfs_path):
"""Return status of files contained in a remote folder.
:param hdfs_path: Remote path to a directory. If `hdfs_path` doesn't exist
or points to a normal file, an :class:`HdfsError` will be raised.
This method returns a list of tuples `(path, status)` where `path` is the
absolute path to a file or directory, and `status` is its corresponding
JSON FileStatus_ object.
"""
hdfs_path = self.resolve(hdfs_path)
statuses = self._list_status(hdfs_path).json()['FileStatuses']['FileStatus']
if len(statuses) == 1 and not statuses[0]['pathSuffix']:
# This is a normal file.
raise HdfsError('%r is not a directory.', hdfs_path)
return [
(osp.join(hdfs_path, status['pathSuffix']), status)
for status in statuses
]

def walk(self, hdfs_path, depth=0):
"""Depth-first walk of remote folder statuses.
Expand Down
24 changes: 24 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,30 @@ def test_missing(self):
self.client.content('foo')


class TestList(_TestSession):

@raises(HdfsError)
def test_file(self):
self.client.write('foo', 'hello, world!')
self.client.list('foo')

@raises(HdfsError)
def test_missing(self):
self.client.list('foo')

def test_empty_dir(self):
self.client._mkdirs('foo')
eq_(self.client.list('foo'), [])

def test_dir(self):
self.client.write('foo/bar', 'hello, world!')
statuses = self.client.list('foo')
eq_(len(statuses), 1)
status = self.client.status('foo/bar')
status['pathSuffix'] = 'bar'
eq_(statuses[0], (osp.join(self.client.root, 'foo', 'bar'), status))


class TestWalk(_TestSession):

def test_file(self):
Expand Down

0 comments on commit 44aacf6

Please sign in to comment.