-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecordToolInfo.py
executable file
·69 lines (51 loc) · 1.92 KB
/
recordToolInfo.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
#! /usr/bin/env python3
import json
import sys
import click
@click.group(invoke_without_command=True)
def cli():
pass
mapping_file = "./data.json"
with open(mapping_file) as f:
mappings = json.loads(f.read())
@cli.command()
def add():
"""Interactively add a tool that will convert THIS to THAT."""
from_format = [x.strip() for x in input("Source format: ").strip().split(',')]
if len(from_format) > 1:
print("info: found multiple source formats")
to_format = [x.strip() for x in input("Target format: ").strip().split(',')]
if len(to_format) > 1:
print("info: found multiple to formats")
tool = input("Tool name: ").strip()
source = input("Package/source?: ").strip()
url = input("Url?: ").strip()
snippet = input("Snippet (optional)?: ").strip()
for iter_source in from_format:
if iter_source not in mappings:
mappings[iter_source] = {}
for iter_target in to_format:
print(f"info: okay adding {iter_source} - {iter_target}")
if iter_target not in mappings.get(iter_source):
mappings[iter_source][iter_target] = []
else:
print(f"info: found pre-existing target formats: {','.join(mappings[iter_source].keys())}")
if tool in [x["tool"] for x in mappings[iter_source][iter_target]]:
print("thanks, but entry already exists!")
continue
mappings[iter_source][iter_target].append({
"tool": tool,
"source": source,
"url": url,
"snippet": snippet
})
with open(mapping_file, 'w') as f:
f.write(json.dumps(mappings, indent=4))
@cli.command()
@click.argument("this")
@click.argument("that")
def get(this, that):
"""Get tools that will convert THIS to THAT. """
print(mappings.get(this, {}).get(that, {}))
if __name__ == '__main__':
cli()