-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.py
40 lines (34 loc) · 1.29 KB
/
transform.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
from cifs_transformer.csv2cifs import Csv2Cifs
from cifs_transformer.datexII2cifs import DatexII2CifsTransformer
from cifs_transformer.cifs2cifs import Cifs2CifsTransformer
import argparse
import datetime
import logging
import json
import os
def main(config_file, outfile):
with open(config_file) as input_file:
config = json.load(input_file)
incidents = []
for source in config['sources']:
try:
if source['type'] == 'DATEXII':
source_incidents = DatexII2CifsTransformer(source['reference']).transform(source['url'])
elif source['type'] == 'CIFS':
source_incidents = Cifs2CifsTransformer(source['reference']).transform(source['url'])
else:
source_incidents = Csv2Cifs().transform(source['url'])
incidents.extend(source_incidents['incidents'])
except:
logging.error("Could not parse %s", source)
result = {
"incidents": incidents,
"timestamp": datetime.datetime.now().isoformat()
}
json.dump(result, outfile)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', dest='config', default=os.environ.get('CIFS_CONFIG', 'config.json'))
parser.add_argument('-o', dest='outfile', required=False, default='-', nargs='?', type=argparse.FileType('w', encoding='utf-8'))
args = parser.parse_args()
main(args.config, args.outfile)