Repository navigation
Expand file tree
/
Copy pathrun_result_assembly.py
More file actions
157 lines (109 loc) · 4.5 KB
/
Copy pathrun_result_assembly.py
File metadata and controls
157 lines (109 loc) · 4.5 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
import os
import sys
import re
import json
from vmaf.core.result import Result
__copyright__ = "Copyright 2016-2019, Netflix, Inc."
__license__ = "Apache, Version 2.0"
def print_usage():
print("usage: python " + os.path.basename(sys.argv[0]) + " input_file\n")
print("input_file contains a list of files for assembly (can be xml or json)")
class FileAssembler:
SUPPORTED_FILE_TYPES = ['xml', 'json']
def __init__(self, to_assemble_input):
self.to_assemble_input = to_assemble_input
@staticmethod
def create_assembly_file_list(to_assemble_input):
to_assemble_list = []
if isinstance(to_assemble_input, list):
to_assemble_list = to_assemble_input
else:
with open(to_assemble_input, "rt") as input_file:
for line in input_file.readlines():
# match comment
mo = re.match(r"^#", line)
if mo:
print("Skip commented line: {}".format(line))
continue
# match whitespace
mo = re.match(r"[\s]+", line)
if mo:
continue
mo = re.match(r"([\S]+)", line)
if not mo:
print("Invalid file: {}".format(line))
print_usage()
return 1
to_assemble_list.append(line.strip())
return to_assemble_list
def _create_result_list(self, to_assemble_list):
pass
def assemble(self):
"""
Main file assembly logic
"""
to_assemble_list = self.create_assembly_file_list(self.to_assemble_input)
self._assert(to_assemble_list)
results = self._create_result_list(to_assemble_list)
combined_result = Result.combine_result(results)
return combined_result
def _assert(self, to_assemble_list):
"""
Perform necessary assertions before parsing any of the files.
"""
# check that the number of files is greater than 0
assert len(to_assemble_list) > 0
# check that the file formats match
assemble_format_list = [os.path.splitext(f)[1].split(".")[1] for f in to_assemble_list]
assert len(set(assemble_format_list)) == 1, "The file formats for assembly do not much."
# check that the file format is supported for assembly
assert assemble_format_list[0] in self.SUPPORTED_FILE_TYPES, \
"The assembly format is not consistent, use any of {fmts}".format(fmts=str(self.SUPPORTED_FILE_TYPES))
class XmlAssembler(FileAssembler):
@staticmethod
def _parse_files(to_assemble_list):
to_assemble_xml_strings = []
for to_assemble_xml in to_assemble_list:
with open(to_assemble_xml, 'r') as f:
to_assemble_xml_strings.append(f.read())
return to_assemble_xml_strings
def _create_result_list(self, to_assemble_list):
to_assemble_xml_strings = self._parse_files(to_assemble_list)
results = []
for to_assemble_xml_string in to_assemble_xml_strings:
results.append(Result.from_xml(to_assemble_xml_string))
return results
class JsonAssembler(FileAssembler):
@staticmethod
def _parse_files(to_assemble_list):
to_assemble_json_strings = []
for json_file in to_assemble_list:
with open(json_file, 'r') as f:
to_assemble_json_strings.append(json.load(f))
return to_assemble_json_strings
def _create_result_list(self, to_assemble_list):
to_assemble_jsons = self._parse_files(to_assemble_list)
results = []
for to_assemble_json in to_assemble_jsons:
to_assemble_json_string = json.dumps(to_assemble_json)
results.append(Result.from_json(to_assemble_json_string))
return results
def main():
if len(sys.argv) != 2:
print_usage()
return 2
files_to_assemble_list = sys.argv[1]
desired_file_list = FileAssembler.create_assembly_file_list(files_to_assemble_list)
if ".xml" in desired_file_list[0]:
xml_output = XmlAssembler(files_to_assemble_list).assemble().to_xml()
print(xml_output)
elif ".json" in desired_file_list[0]:
json_output = JsonAssembler(files_to_assemble_list).assemble().to_json()
print(json_output)
else:
print_usage()
return 2
if __name__ == "__main__":
ret = main()
exit(ret)