-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag_finder.py
72 lines (53 loc) · 2.09 KB
/
tag_finder.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
from argparse import ArgumentParser
from importlib import import_module
import logging
import yaml
from tagfinder.output import Output
from tagfinder.tag_detection import TagDetector
logger = logging.getLogger("tag_finder")
logger.setLevel(logging.INFO)
def transform_config(config):
new_config = {}
for key, value in config.items():
key = key.replace('-', '_')
if isinstance(value, dict):
value = transform_config(value)
new_config[key] = value
return new_config
def main(config_file : str):
with open(config_file, 'r') as cfg_file:
try:
config = yaml.safe_load(cfg_file)
except yaml.YAMLError as exc:
logger.error(f'could not read yaml file {config_file}: {exc}')
config = transform_config(config)
outputs : "list[Output]" = []
logger.setLevel(config['logging']['log_level'].upper())
if 'file_name' in config['logging'] and config['logging']['file_name']:
logger.addHandler(logging.FileHandler(config['logging']['file_name']))
else:
logger.addHandler(logging.StreamHandler())
if 'output' not in config:
config['output'] = []
for out_yml in config['output']:
t = out_yml['type'].replace('-', '_')
class_name = ''.join([x.title() for x in t.split('_')])
driver = getattr(import_module('tagfinder.output.' + t), class_name)
if not issubclass(driver, Output):
logger.fatal(f"{t} is not a valid output type")
return
outputs.append(driver(**out_yml))
if not outputs:
logger.warning('No output was configured, maybe you have a typo in the config?')
tag_finder = TagDetector(**config['tag_detection'])
try:
tag_finder.run(outputs, data_file=args.data_file)
finally:
for output in outputs:
output.close()
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--config', default='config.yml')
parser.add_argument('--data_file', default=None)
args = parser.parse_args()
main(args.config)