summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-03-25 17:33:49 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-03-25 17:40:01 +0100
commite74398cda112543aa6f19ed615001976ee89536f (patch)
treec2f71ebfe79346755629eb565b7a64e384ce0cec
parentf556ea9ad72af80c17a456bcc8248e57f2afe0e5 (diff)
New command: get-values
-rw-r--r--lesana/command.py56
-rwxr-xr-xscripts/lesana1
-rw-r--r--tests/test_commands.py13
3 files changed, 70 insertions, 0 deletions
diff --git a/lesana/command.py b/lesana/command.py
index 0054be7..9ff3533 100644
--- a/lesana/command.py
+++ b/lesana/command.py
@@ -321,6 +321,62 @@ class Search(Command):
print("{entry}".format(entry=entry,))
+class GetValues(Command):
+ """
+ List all values for one field, with entry counts.
+ """
+ arguments = [
+ (
+ ['--collection', '-c'],
+ {
+ 'help': 'The collection to work on (default .)'
+ },
+ ),
+ (
+ ['--field', '-f'],
+ {
+ 'help': 'Name of the field',
+ 'required': True,
+ },
+ ),
+ (
+ ['--template', '-t'],
+ {
+ 'help': 'Template to use when displaying results',
+ },
+ ),
+ (
+ ['query'],
+ {
+ 'help': 'Xapian query to limit the count search " \
+ + "in the collection',
+ 'nargs': '*',
+ 'default': '*'
+ },
+ ),
+ ]
+
+ def main(self):
+ collection = self.collection_class(self.args.collection)
+ counts = collection.get_field_values(
+ self.args.field,
+ ' '.join(self.args.query)
+ )
+ if self.args.template:
+ try:
+ template = collection.get_template(self.args.template)
+ print(template.render(counts=counts))
+ except TemplatingError as e:
+ logging.error("{}".format(e))
+ sys.exit(1)
+ else:
+ for v in counts:
+ print("{value}: {count}".format(
+ value=v['value'],
+ count=v['frequency']
+ ))
+
+
class Export(Command):
"""
Export entries to a different collection
diff --git a/scripts/lesana b/scripts/lesana
index 8e34179..a9778ab 100755
--- a/scripts/lesana
+++ b/scripts/lesana
@@ -19,6 +19,7 @@ class Lesana(lesana.command.MainCommand):
("show", lesana.command.Show()),
("index", lesana.command.Index()),
("search", lesana.command.Search()),
+ ("get-values", lesana.command.GetValues()),
("update", lesana.command.Update()),
("export", lesana.command.Export()),
("init", lesana.command.Init()),
diff --git a/tests/test_commands.py b/tests/test_commands.py
index 92b603f..99cc649 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -121,6 +121,19 @@ class testCommands(unittest.TestCase):
)
self.assertEqual(streams['stderr'].getvalue(), '')
+ def test_get_values(self):
+ args = {
+ 'collection': self.tmpdir.name,
+ 'git': True,
+ 'template': False,
+ 'query': '*',
+ 'field': 'position',
+ }
+ streams = self._run_command(command.GetValues(), args)
+ print(streams['stdout'].getvalue())
+ self.assertIn('somewhere: 2', streams['stdout'].getvalue())
+ self.assertEqual(streams['stderr'].getvalue(), '')
+
def test_export(self):
dest_tmpdir = tempfile.TemporaryDirectory()
utils.copytree(