diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2017-01-14 10:30:18 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2017-01-14 10:30:18 +0100 |
commit | 1ab41af4cdce777c3f5b0e0f985e5846999b9010 (patch) | |
tree | 42b7deb2a3f8adfe94e6132b000aced95ebc0a48 | |
parent | bb451cf5ab9f3db35c91b79c0f2bbdc27a29a0e1 (diff) |
New command: remove
-rw-r--r-- | lesana/collection.py | 12 | ||||
-rw-r--r-- | lesana/command.py | 37 | ||||
-rwxr-xr-x | scripts/lesana | 3 | ||||
-rw-r--r-- | tests/test_collection.py | 24 |
4 files changed, 65 insertions, 11 deletions
diff --git a/lesana/collection.py b/lesana/collection.py index 8a4e0cc..3bc7503 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -265,6 +265,18 @@ class Collection(object): # FIXME: if more items are returned, something is wrong? return self._match_to_entry(enquire.get_mset(0, 1)[0]) + def remove_entries(self, uids): + cache = xapian.WritableDatabase( + os.path.join(self.basedir, '.lesana/xapian'), + xapian.DB_CREATE_OR_OPEN + ) + for uid in uids: + entry = self.entry_from_uid(uid) + cache.delete_document(entry.idterm) + os.remove(os.path.join(self.itemdir, entry.fname)) + cache.commit() + cache.close() + @classmethod def init(cls, directory=None, git_enabled=True, edit_file=None): """ diff --git a/lesana/command.py b/lesana/command.py index 9425bd2..2553915 100644 --- a/lesana/command.py +++ b/lesana/command.py @@ -109,19 +109,19 @@ class Edit(guacamole.Command): class Index(guacamole.Command): arguments = [ - (['--collection', '-c'], dict( - help='The collection to work on (default .)' - )), - (['files'], dict( - help='List of files to index (default: everything)', - default=None, - nargs='*' - )), ] def register_arguments(self, parser): - for arg in self.arguments: - parser.add_argument(*arg[0], **arg[1]) + parser.add_argument( + '--collection', '-c', + help='The collection to work on (default .)', + ) + parser.add_argument( + 'files', + help='List of files to index (default: everything)', + default=None, + nargs='*' + ) def invoked(self, ctx): collection = Collection(ctx.args.collection) @@ -203,3 +203,20 @@ class Init(guacamole.Command): git_enabled=ctx.args.git, edit_file=edit_file_in_external_editor ) + + +class Remove(guacamole.Command): + def register_arguments(self, parser): + parser.add_argument( + '--collection', '-c', + help='The collection to work on (default .)', + ) + parser.add_argument( + 'entries', + help='List of entries to remove', + nargs='+', + ) + + def invoked(self, ctx): + collection = Collection(ctx.args.collection) + collection.remove_entries(uids=ctx.args.entries) diff --git a/scripts/lesana b/scripts/lesana index da9b833..02403dc 100755 --- a/scripts/lesana +++ b/scripts/lesana @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import guacamole -from lesana.command import New, Edit, Index, Search, Init +from lesana.command import New, Edit, Index, Search, Init, Remove class Lesana(guacamole.Command): """ @@ -14,6 +14,7 @@ class Lesana(guacamole.Command): ('index', Index), ('search', Search), ('init', Init), + ('rm', Remove), ) if __name__ == '__main__': diff --git a/tests/test_collection.py b/tests/test_collection.py index 3959399..a37a696 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -230,6 +230,30 @@ class testCollectionCreation(unittest.TestCase): self.assertFalse(os.path.isfile(os.path.join(tmpdir, '.gitignore'))) shutil.rmtree(tmpdir) + def test_deletion(self): + tmpdir = tempfile.mkdtemp() + shutil.copy('tests/data/simple/settings.yaml', tmpdir) + shutil.copytree( + 'tests/data/simple/items', + os.path.join(tmpdir, 'items'), + ) + collection = lesana.Collection.init(tmpdir) + # We start with one item indexed with the term "another" + collection.start_search('another') + mset = collection._enquire.get_mset(0, 10) + self.assertEqual(mset.get_matches_estimated(), 1) + # Then delete it + collection.remove_entries(['11189ee47ddf4796b718a483b379f976']) + # An now we should have none + self.assertFalse(os.path.exists(os.path.join( + tmpdir, + 'items', + '11189ee47ddf4796b718a483b379f976.yaml' + ))) + collection.start_search('another') + mset = collection._enquire.get_mset(0, 10) + self.assertEqual(mset.get_matches_estimated(), 0) + if __name__ == '__main__': unittest.main() |