forked from sync-lab/ETCetera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetc2traffic.py
86 lines (68 loc) · 3.13 KB
/
etc2traffic.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
73
74
75
76
77
78
79
80
81
82
83
84
85
import re
import sys
import argparse
import os
from ETCetera.exceptions.sentient_base_exception import SentientBaseException
if __name__ == "__main__":
# main(sys.argv)
parser = argparse.ArgumentParser(prog='etc2pta', usage='python %(prog)s system_type input_file [options]')
parser.add_argument('system_type', help='System type. Possible values: linear, nonlinear')
parser.add_argument('input_file', help='Path to input file')
parser.add_argument('-o', '--output_file', help='Output file, default is out.json if nothing specified. Tries to extract file type from the name if possible.', nargs='?', const='out.json')
parser.add_argument('--output_type', nargs=1, help='Output file type. Will overwrite type generated from -o option')
parser.add_argument('-v', '--verbose', help='Increase verbosity', action='count', default=0)
args = parser.parse_args()
# print(args)
system_type = args.system_type.lower()
input_file = args.input_file
if system_type not in {'linear', 'non-linear', 'nonlinear', 'general'}:
print(f'{system_type} is not a valid system type.')
parser.print_help()
sys.exit(os.EX_IOERR)
if not os.path.exists(input_file):
print(f'{input_file} is not a valid file.')
parser.print_help()
sys.exit(os.EX_IOERR)
# if args.verbose:
import logging
lv = max(0, 40-10*args.verbose)
logger = logging.getLogger()
logger.setLevel(lv)
ch = logging.StreamHandler()
ch.setLevel(lv)
ch.setFormatter(
logging.Formatter('[%(levelname)s - %(filename)s:%(lineno)s - '
'%(funcName)s()] %(message)s'))
if len(logger.handlers) <= 0:
logger.addHandler(ch)
# Process output file before the costly computation
if args.output_file:
re_string = '\.(json|pickle)$'
temp = re.findall(re_string, args.output_file)
if len(temp) > 1 or len(temp) == 0 and not args.output_type:
print('Please specify a correct output file type.')
out_type = temp[0]
if args.output_type:
# Replace the extension type
args.output_file = args.output_file[0:-len(temp[0])] + args.output_type[0]
out_type = args.output_type[0]
try:
if system_type == 'linear':
from ETCetera.util.construct_from_file_linearPETC import \
construct_linearPETC_traffic_from_file
traffic = construct_linearPETC_traffic_from_file(input_file)
if system_type in ['general', 'nonlinear', 'non-linear']:
from ETCetera.util.construct_from_file_nonlinearETC import \
construct_nonlinearETC_traffic_from_file
traffic = construct_nonlinearETC_traffic_from_file(input_file)
traffic.visualize()
# More to follow...
except SentientBaseException as e:
print(str(e))
sys.exit()
else:
if args.output_file:
from config import save_path
if not args.output_file.startswith(save_path):
args.output_file = os.path.join(save_path, args.output_file)
traffic.export(args.output_file, out_type)