Add ability to filter through iterables, pkgfile.filter(), and tests

This commit is contained in:
Eric Torres 2019-03-25 21:53:27 -07:00
parent f849101d78
commit 019cf2ecb1
2 changed files with 44 additions and 16 deletions

View File

@ -19,7 +19,7 @@ syslog = logging.getLogger(__name__)
# ========== Functions ========== # ========== Functions ==========
def get(query=None, *, directory=None, signatures_only=False): def get(query=None, *, directory=None, signatures_only=False):
"""Return a list of package files in the current working directory. """Retrieve all package-related files from the filesystem.
:param query: names of package files to search for :param query: names of package files to search for
:type query: str :type query: str
@ -33,9 +33,22 @@ def get(query=None, *, directory=None, signatures_only=False):
filequery = f"*{query}*" if query else "*" filequery = f"*{query}*" if query else "*"
if signatures_only: if signatures_only:
yield from _filter_by_regex(filequery, SIGREGEX, path) yield from _filter_by_regex(SIGREGEX, path.glob(filequery))
else: else:
yield from _filter_by_regex(filequery, PKGREGEX, path) yield from _filter_by_regex(PKGREGEX, path.glob(filequery))
def filter(iterable, *, signatures_only=False):
"""Retrive package file types from a predefined iterable.
:param signatures_only: include only signature files
:type signatures_only: bool
:yields: paths of package files
"""
if signatures_only:
yield from _filter_by_regex(SIGREGEX, iterable)
else:
yield from _filter_by_regex(PKGREGEX, iterable)
def add(pkgfile, cachedir): def add(pkgfile, cachedir):
@ -60,18 +73,15 @@ def delete(pkg):
syslog.info(f"Removed {pkg}") syslog.info(f"Removed {pkg}")
def _filter_by_regex(query, regex_expression, path): def _filter_by_regex(regex_expression, iterable):
"""Filter by regular expression. """Filter by regular expression.
:param query: names of package files to search for
:type query: str
:param regex_expression: the expression to filter by :param regex_expression: the expression to filter by
:type regex_expression: str :type regex_expression: str
:param path: directory to look for files :param iterable: iterable to filter through
:type path: path-like object :type iterable: does this really need explanation?
:yields: package files from either the current working directory :yields: pathlib.Path objects to package files
or an arbitrary directory
""" """
for pkgfile in path.glob(query): for item in iterable:
if re.match(regex_expression, str(pkgfile)): if re.match(regex_expression, str(item)):
yield pkgfile yield Path(item)

View File

@ -36,7 +36,7 @@ def filter_files(query):
# ========== Unit Tests ========== # ========== Unit Tests ==========
class TestFilterPkgfiles(unittest.TestCase): class TestGetPkgfiles(unittest.TestCase):
def setUp(self): def setUp(self):
self.patched_path = patch.object(Path, "glob") self.patched_path = patch.object(Path, "glob")
self.mocked_glob = self.patched_path.start() self.mocked_glob = self.patched_path.start()
@ -63,7 +63,7 @@ class TestFilterPkgfiles(unittest.TestCase):
self.patched_path.stop() self.patched_path.stop()
class TestFilterSigfiles(unittest.TestCase): class TestGetSigfiles(unittest.TestCase):
def setUp(self): def setUp(self):
self.patched_path = patch.object(Path, "glob") self.patched_path = patch.object(Path, "glob")
self.mocked_glob = self.patched_path.start() self.mocked_glob = self.patched_path.start()
@ -90,6 +90,24 @@ class TestFilterSigfiles(unittest.TestCase):
self.patched_path.stop() self.patched_path.stop()
class TestFilterPkgfiles(unittest.TestCase):
def test_yield(self):
result = pkgfiles.filter(ALL_PKGFILES)
expected = [s for s in get_all_files() if re.match(PKGREGEX, str(s))]
self.assertListEqual(list(result), expected)
self.assertIsInstance(result, GeneratorType)
class TestFilterSigfiles(unittest.TestCase):
def test_yield(self):
result = pkgfiles.filter(ALL_PKGFILES, signatures_only=True)
expected = [s for s in get_all_files() if re.match(SIGREGEX, str(s))]
self.assertListEqual(list(result), expected)
self.assertIsInstance(result, GeneratorType)
class TestAddPkgfile(unittest.TestCase): class TestAddPkgfile(unittest.TestCase):
def setUp(self): def setUp(self):
self.patched_shutil = patch(f"{TESTING_MODULE}.shutil") self.patched_shutil = patch(f"{TESTING_MODULE}.shutil")
@ -109,7 +127,7 @@ class TestAddPkgfile(unittest.TestCase):
self.patched_shutil.stop() self.patched_shutil.stop()
class TestDeletePkgfiles(unittest.TestCase): class TestDeletePkgfile(unittest.TestCase):
def setUp(self): def setUp(self):
self.pkgfile = MagicMock("/var/cache/repo/pkgfile", spec_set=Path) self.pkgfile = MagicMock("/var/cache/repo/pkgfile", spec_set=Path)