utils: find_files() returns bytestrings
This commit is contained in:
parent
b5d9dc10a7
commit
905ceeb72a
@ -106,30 +106,32 @@ def expand_path(path):
|
|||||||
|
|
||||||
|
|
||||||
def find_files(path):
|
def find_files(path):
|
||||||
|
"""
|
||||||
|
Finds all files within a path.
|
||||||
|
|
||||||
|
Directories and files with names starting with ``.`` is ignored.
|
||||||
|
|
||||||
|
:returns: yields the full path to files as bytestrings
|
||||||
|
"""
|
||||||
|
if isinstance(path, unicode):
|
||||||
|
path = path.encode('utf-8')
|
||||||
|
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
if not isinstance(path, unicode):
|
if not os.path.basename(path).startswith(b'.'):
|
||||||
path = path.decode('utf-8')
|
|
||||||
if not os.path.basename(path).startswith('.'):
|
|
||||||
yield path
|
yield path
|
||||||
else:
|
else:
|
||||||
for dirpath, dirnames, filenames in os.walk(path):
|
for dirpath, dirnames, filenames in os.walk(path):
|
||||||
# Filter out hidden folders by modifying dirnames in place.
|
|
||||||
for dirname in dirnames:
|
for dirname in dirnames:
|
||||||
if dirname.startswith('.'):
|
if dirname.startswith(b'.'):
|
||||||
|
# Skip hidden folders by modifying dirnames inplace
|
||||||
dirnames.remove(dirname)
|
dirnames.remove(dirname)
|
||||||
|
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
# Skip hidden files.
|
if filename.startswith(b'.'):
|
||||||
if filename.startswith('.'):
|
# Skip hidden files
|
||||||
continue
|
continue
|
||||||
|
|
||||||
filename = os.path.join(dirpath, filename)
|
yield os.path.join(dirpath, filename)
|
||||||
if not isinstance(filename, unicode):
|
|
||||||
try:
|
|
||||||
filename = filename.decode('utf-8')
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
filename = filename.decode('latin1')
|
|
||||||
yield filename
|
|
||||||
|
|
||||||
|
|
||||||
def check_file_path_is_inside_base_dir(file_path, base_path):
|
def check_file_path_is_inside_base_dir(file_path, base_path):
|
||||||
|
|||||||
@ -201,11 +201,11 @@ class FindFilesTest(unittest.TestCase):
|
|||||||
self.assertEqual(len(files), 1)
|
self.assertEqual(len(files), 1)
|
||||||
self.assert_(files[0], path_to_data_dir('blank.mp3'))
|
self.assert_(files[0], path_to_data_dir('blank.mp3'))
|
||||||
|
|
||||||
def test_names_are_unicode(self):
|
def test_names_are_bytestrings(self):
|
||||||
is_unicode = lambda f: isinstance(f, unicode)
|
is_bytes = lambda f: isinstance(f, bytes)
|
||||||
for name in self.find(''):
|
for name in self.find(''):
|
||||||
self.assert_(
|
self.assert_(
|
||||||
is_unicode(name), '%s is not unicode object' % repr(name))
|
is_bytes(name), '%s is not unicode object' % repr(name))
|
||||||
|
|
||||||
def test_ignores_hidden_folders(self):
|
def test_ignores_hidden_folders(self):
|
||||||
self.assertEqual(self.find('.hidden'), [])
|
self.assertEqual(self.find('.hidden'), [])
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user