-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_tags.py
executable file
·72 lines (57 loc) · 1.85 KB
/
gen_tags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Module to re(generate) tags.json database.
"""
from __future__ import absolute_import
import argparse
from argparse import RawDescriptionHelpFormatter as RawDescriptionHelp
import json
import os
ROOT = os.path.abspath(os.path.dirname(__file__))
if os.path.dirname(__file__) == '':
ROOT = os.getcwd()
def create_args_parser():
"""
Create argument parser.
"""
prog_name = os.path.basename(__file__)[0:-3]
mesg = """
Generate the tags.json database that maps
tags onto plugin names.
Do NOT edit the generated file. Just regenerate it.
""".format(prog_name.capitalize(), prog_name)
mesg = mesg[0:-5]
parser = argparse.ArgumentParser(prog=prog_name, description=mesg,
formatter_class=RawDescriptionHelp)
parser.add_argument('input', default=os.path.join(ROOT, 'db.json'),
help='input json file to transform into tags')
parser.add_argument('output', nargs='?',
help='output json, by default tags.json')
return parser
def add_tags(tags, plugin, plugin_tags):
"""
Add plugin name to all relevant tags.
"""
for tag in plugin_tags:
if tag not in tags:
tags[tag] = []
tags[tag].append(plugin)
def main():
"""
Main entry point.
"""
parser = create_args_parser()
args = parser.parse_args()
if args.output is None:
args.output = os.path.join(os.path.dirname(args.input), 'tags.json')
with open(args.input, 'r') as fin:
plugs = json.load(fin)
tags = {}
for plug in plugs:
add_tags(tags, plug, plugs[plug]['tags'])
with open(args.output, 'w') as fout:
json.dump(tags, fout, sort_keys=True,
indent=2, separators=(',', ': '))
if __name__ == "__main__":
main()