• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

4dn-dcic / dcicwrangling / 13682489429

05 Mar 2025 06:03PM UTC coverage: 33.8%. Remained the same
13682489429

push

github

web-flow
Merge pull request #117 from 4dn-dcic/ajs_upd_dependencies

Ajs upd dependencies

2 of 2 new or added lines in 1 file covered. (100.0%)

22 existing lines in 14 files now uncovered.

1256 of 3716 relevant lines covered (33.8%)

2.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/scripts/tagger.py
1
import sys
×
2
import argparse
×
3
from dcicutils.ff_utils import get_metadata, patch_metadata
×
4
from functions import script_utils as scu
×
UNCOV
5
""" Script to add arbitrary tags to items that can be tagged
6
"""
7

8
def make_tag_patch(item, tag):
×
9
    if item.get('tags'):
×
10
        tags = item['tags']
×
11
        tags.append(tag)
×
12
    else:
13
        tags = [tag]
×
14
    return {'tags': tags}
×
15

16

17
def get_args():  # pragma: no cover
18
    parser = argparse.ArgumentParser(
19
        description='Add a tag to provided items (and optionally their children)',
20
        parents=[scu.create_input_arg_parser(), scu.create_ff_arg_parser()],
21
        formatter_class=argparse.RawDescriptionHelpFormatter,
22
    )
23
    parser.add_argument('tag',
24
                        help="The tag you want to add - eg. '4DN Standard'")
25
    parser.add_argument('--taglinked',
26
                        default=False,
27
                        action='store_true',
28
                        help='Tag items linked to items that are input')
29
    parser.add_argument('--types2exclude',
30
                        nargs='+',
31
                        help="List of Item Types to Explicitly Exclude Tagging - \
32
                        you may have some linked items that can get tags but may \
33
                        not want to tag them with this tag")
34
    args = parser.parse_args()
35
    return args
36

37

38
def main():  # pragma: no cover
39
    args = get_args()
40
    auth = scu.authenticate(key=args.key, keyfile=args.keyfile, env=args.env)
41
    itemids = scu.get_item_ids_from_args(args.input, auth, args.search)
42
    taggable = scu.get_types_that_can_have_field(auth, 'tags')
43
    if args.types2exclude is not None:
44
        # remove explicitly provide types not to tag
45
        taggable = [t for t in taggable if t not in args.types2exclude]
46

47
    seen = []   # only need to add tag once so this keeps track of what's been seen
48
    to_patch = {}   # keep track of those to patch
49
    # main loop through the top level item ids
50
    for itemid in itemids:
51
        items2tag = {}
52
        if args.taglinked:
53
            # need to get linked items and tag them
54
            linked = scu.get_linked_items(auth, itemid, {})
55
            items2tag = scu.filter_dict_by_value(linked, taggable, include=True)
56
        else:
57
            # only want to tag provided items
58
            itype = scu.get_item_type(auth, itemid)
59
            if itype in taggable:
60
                items2tag = {itemid: itype}
61
        for i, t in items2tag.items():
62
            if i not in seen:
63
                seen.append(i)
64
                item = get_metadata(i, auth)
65
                if not scu.has_field_value(item, 'tags', args.tag):
66
                    # not already tagged with this tag so make a patch and add 2 dict
67
                    to_patch[i] = make_tag_patch(item, args.tag)
68

69
    # now do the patching or reporting
70
    for pid, patch in to_patch.items():
71
        if args.dbupdate:
72
            pres = patch_metadata(patch, pid, auth)
73
            print(pres['status'])
74
        else:
75
            print("DRY RUN: patch ", pid, " with ", patch)
76

77

78
if __name__ == '__main__':  # pragma: no cover
79
    main()
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc