2023-08-25 14:26:43 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# papis-short-help: List all tags occuring in query items
|
|
|
|
#
|
|
|
|
# Takes a query and spits out a sorted list of all tags contained therein,
|
|
|
|
# nothing more.
|
|
|
|
# Can be very useful for things like picking a tag or two and listing all
|
|
|
|
# items that contain it:
|
|
|
|
# $ papis tags "*" | fzf | xargs papis show "tags:{}"
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
from papis import database
|
|
|
|
from papis.database.base import Database
|
|
|
|
|
|
|
|
from papis.document import Document
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
2023-10-02 17:16:23 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--count", "-c", help="the query to search for", action="store_true"
|
|
|
|
)
|
|
|
|
parser.add_argument("query", nargs="*", help="the query to search for", default="*")
|
2023-08-25 14:26:43 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def main(db: Database, args) -> None:
|
|
|
|
query = " ".join(args.query)
|
|
|
|
docs: list[Document] = db.query(query)
|
|
|
|
|
2023-10-02 17:16:23 +00:00
|
|
|
all_tags: dict[str, int] = {}
|
2023-08-25 14:26:43 +00:00
|
|
|
for doc in docs:
|
|
|
|
t: list[str] | str = doc.get("tags", "")
|
|
|
|
tags = (
|
|
|
|
t.replace(";", ",").replace(" ", "").split(",") if isinstance(t, str) else t
|
|
|
|
)
|
|
|
|
|
|
|
|
for tag in tags:
|
2023-10-02 17:16:23 +00:00
|
|
|
if tag == '':
|
|
|
|
continue
|
|
|
|
all_tags[tag] = all_tags.get(tag, 0) + 1
|
2023-08-25 14:26:43 +00:00
|
|
|
|
2023-10-02 17:16:23 +00:00
|
|
|
if args.count:
|
|
|
|
print_tags_and_counts(all_tags)
|
|
|
|
else:
|
|
|
|
print_tags_only(all_tags)
|
|
|
|
|
|
|
|
def print_tags_only(all_tags):
|
2023-08-25 14:26:43 +00:00
|
|
|
for tag in sorted(all_tags):
|
|
|
|
print(tag)
|
|
|
|
|
2023-10-02 17:16:23 +00:00
|
|
|
def print_tags_and_counts(all_tags):
|
|
|
|
for tag, count in sorted(all_tags.items(), key=lambda d: d[1], reverse=True):
|
|
|
|
if args.count:
|
|
|
|
print(tag, count)
|
|
|
|
else:
|
|
|
|
print(tag)
|
|
|
|
|
2023-08-25 14:26:43 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main(database.get(), args)
|