Skip to content

Commit 13c6e4d

Browse files
Oddant1cherman2
andauthored
IMP: Make multiple key removal a thing (#360)
Co-authored-by: Chloe Herman <60228108+cherman2@users.noreply.github.com>
1 parent 941e1af commit 13c6e4d

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

q2cli/builtin/tools.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -715,21 +715,34 @@ def cache_create(cache):
715715
type=click.Path(exists=True, file_okay=False, dir_okay=True,
716716
readable=True),
717717
help='Path to an existing cache to remove the key from.')
718-
@click.option('--key', required=True,
719-
help='The key to remove from the cache.')
718+
@click.option('--key', required=True, multiple=True,
719+
help='The key to remove from the cache. Pass `key` parameter '
720+
'multiple times to remove multiple keys.')
720721
def cache_remove(cache, key):
721722
from qiime2.core.cache import Cache
722723
from q2cli.core.config import CONFIG
723724

725+
# I don't want to instantiate this every time in the loop
724726
try:
725727
_cache = Cache(cache)
726-
_cache.remove(key)
727728
except Exception as e:
728-
header = "There was a problem removing the key '%s' from the " \
729-
"cache '%s':" % (key, cache)
729+
header = f"The path '{cache}' is not a valid QIIME 2 cache."
730730
q2cli.util.exit_with_error(e, header=header, traceback=None)
731731

732-
success = "Removed key '%s' from cache '%s'" % (key, cache)
732+
for _key in key:
733+
try:
734+
# TODO: This will run gc after every key removal. Don't need to do
735+
# that
736+
_cache.remove(_key)
737+
except Exception as e:
738+
header = f"There was a problem removing the key '{_key}' from "\
739+
f"the cache '{cache}'"
740+
q2cli.util.exit_with_error(e, header=header, traceback=None)
741+
else:
742+
success = f"Removed key '{_key}' from cache '{cache}'"
743+
click.echo(CONFIG.cfg_style('success', success))
744+
745+
success = f"Removed key(s) '{key}' from cache '{cache}'"
733746
click.echo(CONFIG.cfg_style('success', success))
734747

735748

q2cli/tests/test_tools.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,39 @@ def test_cache_remove(self):
578578
tools,
579579
['cache-remove', '--cache', str(self.cache.path), '--key', 'key'])
580580

581-
success = "Removed key 'key' from cache '%s'\n" % self.cache.path
581+
success = \
582+
f"Removed key 'key' from cache '{str(self.cache.path)}'\n" \
583+
f"Removed key(s) '('key',)' from cache '{str(self.cache.path)}'\n"
582584
self.assertEqual(success, result.output)
583585
self.assertFalse('key' in self.cache.get_keys())
584586

587+
def test_cache_remove_multiple(self):
588+
self.cache.save(self.art1, 'key1')
589+
self.cache.save(self.art1, 'key2')
590+
self.cache.save(self.art1, 'key3')
591+
592+
keys = self.cache.get_keys()
593+
self.assertEqual(set(['key1', 'key2', 'key3']), keys)
594+
595+
result = self.runner.invoke(
596+
tools,
597+
['cache-remove', '--cache', str(self.cache.path), '--key', 'key1',
598+
'--key', 'key2']
599+
)
600+
601+
success = \
602+
f"Removed key 'key1' from cache '{str(self.cache.path)}'\n" \
603+
f"Removed key 'key2' from cache '{str(self.cache.path)}'\n" \
604+
"Removed key(s) '('key1', 'key2')' from cache " \
605+
f"'{str(self.cache.path)}'\n"
606+
607+
new_keys = self.cache.get_keys()
608+
609+
self.assertEqual(success, result.output)
610+
self.assertFalse('key1' in new_keys)
611+
self.assertFalse('key2' in new_keys)
612+
self.assertTrue('key3' in new_keys)
613+
585614
def test_cache_garbage_collection(self):
586615
# Data referenced directly by key
587616
self.cache.save(self.art1, 'foo')

0 commit comments

Comments
 (0)