diff options
-rw-r--r-- | lesana/collection.py | 12 | ||||
-rw-r--r-- | lesana/command.py | 26 | ||||
-rwxr-xr-x | scripts/lesana | 1 | ||||
-rw-r--r-- | tests/test_collection.py | 32 |
4 files changed, 71 insertions, 0 deletions
diff --git a/lesana/collection.py b/lesana/collection.py index 4bec594..25b86e5 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -452,6 +452,18 @@ class Collection(object): repo.index.remove([f_path]) os.remove(f_path) + def update_field(self, query, field, value): + self.start_search(query) + changed = [] + for e in self.get_all_search_results(): + e.data[field] = value + changed.append(e) + self.save_entries(changed) + self.git_add_files([ + os.path.join(self.itemdir, e.fname) for e in changed + ]) + self.update_cache([e.fname for e in changed]) + def get_template(self, template_fname, searchpath='.'): env = jinja2.Environment( loader=jinja2.FileSystemLoader( diff --git a/lesana/command.py b/lesana/command.py index f2f3e56..c2bdf28 100644 --- a/lesana/command.py +++ b/lesana/command.py @@ -331,3 +331,29 @@ class Remove(Command): def main(self): collection = self.collection_class(self.args.collection) collection.remove_entries(eids=self.args.entries) + + +class Update(Command): + arguments = [ + (['--collection', '-c'], dict( + help='The collection to work on (default .)', + )), + (['--field', '-f'], dict( + help='The field to change', + )), + (['--value', '-t'], dict( + help='The value to set', + )), + (['query'], dict( + help='Xapian query to search in the collection', + nargs='+' + )), + ] + + def main(self): + collection = self.collection_class(self.args.collection) + collection.update_field( + ' '.join(self.args.query), + field=self.args.field, + value=self.args.value + ) diff --git a/scripts/lesana b/scripts/lesana index 408aa2c..5591b3b 100755 --- a/scripts/lesana +++ b/scripts/lesana @@ -15,6 +15,7 @@ class Lesana(): ('show', lesana.command.Show()), ('index', lesana.command.Index()), ('search', lesana.command.Search()), + ('update', lesana.command.Update()), ('export', lesana.command.Export()), ('init', lesana.command.Init()), ('rm', lesana.command.Remove()), diff --git a/tests/test_collection.py b/tests/test_collection.py index e97e912..eb57fb0 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -154,6 +154,38 @@ class testCollection(unittest.TestCase): self.assertIn('11189ee4: Another item', res) +class testSimpleCollection(unittest.TestCase): + def setUp(self): + self.tmpdir = tempfile.mkdtemp() + shutil.copytree('tests/data/simple', self.tmpdir, dirs_exist_ok=True) + self.collection = lesana.Collection(self.tmpdir) + + def tearDown(self): + shutil.rmtree(self.tmpdir) + + def test_update(self): + self.collection.update_field('Item', field="position", value="new_pos") + with open(os.path.join( + self.collection.basedir, + 'items', + '11189ee47ddf4796b718a483b379f976.yaml' + )) as fp: + self.assertIn("new_pos", fp.read()) + pass + self.assertEqual( + self.collection.entry_from_eid( + "11189ee47ddf4796b718a483b379f976" + ).data['position'], + "new_pos" + ) + + self.assertIsNone( + self.collection.entry_from_eid( + "8b69b063b2a64db7b5714294a69255c7" + ).data['position'] + ) + + class testEntries(unittest.TestCase): def setUp(self): self.collection = lesana.Collection('tests/data/simple') |