Skip to content

Commit

Permalink
category filter for add_actor, add owner as possible header field.
Browse files Browse the repository at this point in the history
  • Loading branch information
soad003 committed Feb 28, 2023
1 parent a30cef5 commit 63a868e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
12 changes: 11 additions & 1 deletion src/tagpack/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ def add_actors_to_tagpack(args):
tagpack_files = collect_tagpack_files(args.path)

schema = TagPackSchema()
user_choice_cache = {}

for headerfile_dir, files in tagpack_files.items():
for tagpack_file in files:
Expand All @@ -373,7 +374,11 @@ def get_label(actor_row):

return [(x["id"], get_label(x)) for x in res]

updated = tagpack.add_actors(find_actor_candidates)
updated = tagpack.add_actors(
find_actor_candidates,
only_categories=strip_empty(args.only_categories.split(",")),
user_choice_cache=user_choice_cache,
)

if updated:
updated_file = (
Expand Down Expand Up @@ -1097,6 +1102,11 @@ def print_help_subparser(subparser, args):
default=5,
help="Limits the number of results",
)
ptp_add_actor.add_argument(
"--only_categories",
default="",
help="Only edit tags of a certain category (multiple possible with semi-colon)",
)
ptp_add_actor.add_argument(
"--inplace",
action="store_true",
Expand Down
4 changes: 4 additions & 0 deletions src/tagpack/conf/tagpack_schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ header:
description:
type: text
mandatory: false
owner:
type: text
mandatory: false
description: Field specifies the owner of the tagpack.
is_public:
type: boolean
mandatory: false
Expand Down
40 changes: 29 additions & 11 deletions src/tagpack/tagpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,9 @@ def verify_addresses(self):
for a in sorted(addrs):
print_warn(f"\t{a}")

def add_actors(self, find_actor_candidates) -> bool:
def add_actors(
self, find_actor_candidates, only_categories=None, user_choice_cache={}
) -> bool:
"""Suggest actors for labels that have no actors assigned
Args:
Expand All @@ -373,25 +375,27 @@ def add_actors(self, find_actor_candidates) -> bool:
or as a list[tuple[str,str]] where the first entry is a id
and the second a human readable label of the entry.
only_categories (None, optional): List of tag-categories to edit.
Returns:
bool: true if suggestions where found
"""

suggestions_found = False
user_choice_cache = {}
labels_with_no_actors = set()

def get_user_choice_cached(hl, candidates, cache):
def get_user_choice_cached(hl, cache):
# normalize label to allow for better matching
hl = hl.replace("_", " ").replace("-", " ").replace(".", " ")
hl = hl.replace("_", " ").replace("-", " ").replace(".", " ").lower()
if hl in cache:
return user_choice_cache[hl]
return cache[hl]
else:
candidates = find_actor_candidates(hl)
if len(candidates) == 0:
choice = None
else:
magic_choice = 1

newhl = hl
while True:
new_candidates = candidates + [
Expand All @@ -410,10 +414,17 @@ def get_user_choice_cached(hl, candidates, cache):
cache[hl] = choice
return choice

if "label" in self.all_header_fields and "actor" not in self.all_header_fields:
if (
"label" in self.all_header_fields
and "actor" not in self.all_header_fields
and (
only_categories is not None
and self.all_header_fields.get("category") in only_categories
)
):
hl = self.all_header_fields.get("label")
candidates = find_actor_candidates(hl)
actor = get_user_choice_cached(hl, candidates, user_choice_cache)
# candidates = find_actor_candidates(hl)
actor = get_user_choice_cached(hl, user_choice_cache)

if actor:
self.contents["actor"] = actor
Expand All @@ -429,10 +440,17 @@ def get_user_choice_cached(hl, candidates, cache):
actors = set()
all_tags_carry_actor = True
for tag in self.get_unique_tags():
# Continue if tag is not of a selected category
if (
only_categories is not None
and tag.all_fields.get("category") in only_categories
):
continue

if "label" in tag.explicit_fields and "actor" not in tag.explicit_fields:
tl = tag.explicit_fields.get("label")
candidates = find_actor_candidates(tl)
actor = get_user_choice_cached(tl, candidates, user_choice_cache)
# candidates = find_actor_candidates(tl)
actor = get_user_choice_cached(tl, user_choice_cache)
if actor:
tag.contents["actor"] = actor
actors.add(actor)
Expand Down

0 comments on commit 63a868e

Please sign in to comment.