Added utils.find_in_path + unittest
This commit is contained in:
parent
ccedb7cbb1
commit
6f970a41e5
@ -16,6 +16,8 @@
|
|||||||
#
|
#
|
||||||
# ##### END GPL LICENSE BLOCK #####
|
# ##### END GPL LICENSE BLOCK #####
|
||||||
|
|
||||||
|
import pathlib
|
||||||
|
|
||||||
|
|
||||||
def sizeof_fmt(num: int, suffix='B') -> str:
|
def sizeof_fmt(num: int, suffix='B') -> str:
|
||||||
"""Returns a human-readable size.
|
"""Returns a human-readable size.
|
||||||
@ -29,3 +31,34 @@ def sizeof_fmt(num: int, suffix='B') -> str:
|
|||||||
num /= 1024
|
num /= 1024
|
||||||
|
|
||||||
return '%.1f Yi%s' % (num, suffix)
|
return '%.1f Yi%s' % (num, suffix)
|
||||||
|
|
||||||
|
|
||||||
|
def find_in_path(path: pathlib.Path, filename: str) -> pathlib.Path:
|
||||||
|
"""Performs a breadth-first search for the filename.
|
||||||
|
|
||||||
|
Returns the path that contains the file, or None if not found.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import collections
|
||||||
|
|
||||||
|
# Be lenient on our input type.
|
||||||
|
if isinstance(path, str):
|
||||||
|
path = pathlib.Path(path)
|
||||||
|
|
||||||
|
if not path.exists():
|
||||||
|
return None
|
||||||
|
assert path.is_dir()
|
||||||
|
|
||||||
|
to_visit = collections.deque([path])
|
||||||
|
while to_visit:
|
||||||
|
this_path = to_visit.popleft()
|
||||||
|
|
||||||
|
for subpath in this_path.iterdir():
|
||||||
|
if subpath.is_dir():
|
||||||
|
to_visit.append(subpath)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if subpath.name == filename:
|
||||||
|
return subpath
|
||||||
|
|
||||||
|
return None
|
||||||
|
8
requirements-dev.txt
Normal file
8
requirements-dev.txt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
-r requirements.txt
|
||||||
|
|
||||||
|
# Primary requirements
|
||||||
|
pytest==3.0.3
|
||||||
|
|
||||||
|
# Secondary requirements
|
||||||
|
py==1.4.31
|
||||||
|
|
25
tests/test_utils.py
Normal file
25
tests/test_utils.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
"""Unittests for blender_cloud.utils."""
|
||||||
|
|
||||||
|
import pathlib
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from blender_cloud import utils
|
||||||
|
|
||||||
|
|
||||||
|
class FindInPathTest(unittest.TestCase):
|
||||||
|
def test_nonexistant_path(self):
|
||||||
|
path = pathlib.Path('/doesnotexistreally')
|
||||||
|
self.assertFalse(path.exists())
|
||||||
|
self.assertIsNone(utils.find_in_path(path, 'jemoeder.blend'))
|
||||||
|
|
||||||
|
def test_really_breadth_first(self):
|
||||||
|
"""A depth-first test might find dir_a1/dir_a2/dir_a3/find_me.txt first."""
|
||||||
|
|
||||||
|
path = pathlib.Path(__file__).parent / 'test_really_breadth_first'
|
||||||
|
found = utils.find_in_path(path, 'find_me.txt')
|
||||||
|
self.assertEqual(path / 'dir_b1' / 'dir_b2' / 'find_me.txt', found)
|
||||||
|
|
||||||
|
def test_nonexistant_file(self):
|
||||||
|
path = pathlib.Path(__file__).parent / 'test_really_breadth_first'
|
||||||
|
found = utils.find_in_path(path, 'do_not_find_me.txt')
|
||||||
|
self.assertEqual(None, found)
|
Reference in New Issue
Block a user