Updated find files to ignore hidden files and folders.
This commit is contained in:
parent
355ff811af
commit
7ceb530064
@ -47,21 +47,31 @@ def split_path(path):
|
|||||||
break
|
break
|
||||||
return parts
|
return parts
|
||||||
|
|
||||||
# pylint: disable = W0612
|
|
||||||
# Unused variable 'dirnames'
|
|
||||||
def find_files(path):
|
def find_files(path):
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
if not isinstance(path, unicode):
|
if not isinstance(path, unicode):
|
||||||
path = path.decode('utf-8')
|
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:
|
||||||
|
if dirname.startswith('.'):
|
||||||
|
dirnames.remove(dirname)
|
||||||
|
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
|
# Skip hidden files.
|
||||||
|
if filename.startswith('.'):
|
||||||
|
continue
|
||||||
|
|
||||||
filename = os.path.join(dirpath, filename)
|
filename = os.path.join(dirpath, filename)
|
||||||
if not isinstance(filename, unicode):
|
if not isinstance(filename, unicode):
|
||||||
|
try:
|
||||||
filename = filename.decode('utf-8')
|
filename = filename.decode('utf-8')
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
filename = filename.decode('latin1')
|
||||||
yield filename
|
yield filename
|
||||||
# pylint: enable = W0612
|
|
||||||
|
|
||||||
# FIXME replace with mock usage in tests.
|
# FIXME replace with mock usage in tests.
|
||||||
class Mtime(object):
|
class Mtime(object):
|
||||||
|
|||||||
BIN
tests/data/.blank.mp3
Normal file
BIN
tests/data/.blank.mp3
Normal file
Binary file not shown.
0
tests/data/.hidden/.gitignore
vendored
Normal file
0
tests/data/.hidden/.gitignore
vendored
Normal file
@ -156,6 +156,12 @@ class FindFilesTest(unittest.TestCase):
|
|||||||
self.assert_(is_unicode(name),
|
self.assert_(is_unicode(name),
|
||||||
'%s is not unicode object' % repr(name))
|
'%s is not unicode object' % repr(name))
|
||||||
|
|
||||||
|
def test_ignores_hidden_folders(self):
|
||||||
|
self.assertEqual(self.find('.hidden'), [])
|
||||||
|
|
||||||
|
def test_ignores_hidden_files(self):
|
||||||
|
self.assertEqual(self.find('.blank.mp3'), [])
|
||||||
|
|
||||||
|
|
||||||
class MtimeTest(unittest.TestCase):
|
class MtimeTest(unittest.TestCase):
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user