OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 | |
3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 import csv | |
8 import os | |
9 import random | |
10 | |
11 NOT_AVAILABLE_STRING = 'not available' | |
12 MAX_VIDEO_INDEX = 15 | |
13 | |
14 | |
15 class MediaTestMatrix: | |
16 """This class reads video test matrix file and stores them. | |
17 | |
18 Video test matrix contains different video titles as column and different | |
19 video codec (such as "webm" as columns. The matrix entry contains video | |
20 specification such as "320x240 50 fps VP8 / no audio 1 MBit/s PSNR 36.70" | |
21 or "not available". It also provides methods to generate information based | |
22 on the client's needs. For example, "video" tag or "audio" tag is returned | |
23 based on what kinds of media it is. This matrix is for managing different | |
24 kinds of media files in media testing. | |
25 """ | |
26 # Video extensions such as "webm". | |
27 __exts = [] | |
28 # Video specification dictionary or video descriptions. | |
29 # The key is extentions. | |
30 __specs = {} | |
31 # Video titles such as "bear". | |
32 __video_titles = [] | |
33 | |
34 def ReadData(self, csv_file): | |
35 """Reads CSV file and stores in list specs and extensions. | |
36 | |
37 CSV file should have the following format: | |
38 "ext","title1","title2",.... | |
39 "0.webm", "description1","descrption2", "not available" (when it is not | |
40 available). | |
41 | |
42 Video files are stored less than and equal to 15th row. | |
43 Audio files are stored more then 15th row. | |
dennis_jeffrey
2011/03/18 17:47:50
"more then" --> "more than"
imasaki1
2011/03/18 21:54:25
Removed since we use video/audio tag.
| |
44 Args: | |
dennis_jeffrey
2011/03/18 17:47:50
Put a blank line right before the "Args:" line.
imasaki1
2011/03/18 21:54:25
Done.
| |
45 csv_file : CSV file name contains video matrix information described | |
46 above. | |
dennis_jeffrey
2011/03/18 17:47:50
Since this function raises csv.Error in line 68 be
imasaki1
2011/03/18 21:54:25
Done.
| |
47 """ | |
48 # Clean up all data. | |
49 self.__exts = [] | |
50 self.__specs = {} | |
51 self.__video_titles = [] | |
52 file = open(csv_file, 'rb') | |
53 reader = csv.reader(file) | |
54 for counter, row in enumerate(reader): | |
55 if counter == 0: | |
56 # First row is comment | |
57 pass | |
58 if counter == 1: | |
dennis_jeffrey
2011/03/18 17:47:50
"if" --> "elif"
imasaki1
2011/03/18 21:54:25
Done.
| |
59 # Second row is for header (video titles). | |
60 for title in row[1:]: | |
61 self.__video_titles.append(title) | |
62 self.__specs[title] = [] | |
63 else: | |
64 # Error checking is done here based on the number of titles | |
65 if len(self.__video_titles) != len(row) - 1: | |
66 print "Row %d should have %d columns but have %d columns" % (counter, | |
dennis_jeffrey
2011/03/18 17:47:50
"but have" --> "but has"
imasaki1
2011/03/18 21:54:25
Done.
| |
67 len(self.__video_titles), len(row)) | |
68 raise csv.Error | |
69 # First column should contain extension. | |
70 self.__exts.append(row[0]) | |
71 for i in range(len(row)-1): | |
72 self.__specs[self.__video_titles[i]].append(row[i + 1]) | |
73 file.close() | |
74 | |
75 def __GenerateMediaInfo(self, sub_type, name, media_test_matrix_home_url): | |
76 """Generate media information from matrix generated by CSV file. | |
77 | |
78 Args: | |
79 sub_type: index of the extensions (rows in the CSV file). | |
80 name: the name of the video file (column name in the CSV file). | |
81 media_test_matrix_home_url: url of the matrix home that used | |
82 to generate link. | |
83 | |
84 Returns: | |
85 a tuple of (info, url, link, tag, is_video, nickname). | |
86 info: description of the video (an entry in the matrix). | |
87 url: URL of the video or audio. | |
88 link: href tag for video or audio. | |
89 is_video: True if the file is video; False otherwise. | |
90 tag: a tag string can be used to display video or audio. | |
dennis_jeffrey
2011/03/18 17:47:50
Put the line for "tag" above the line for "is_vide
imasaki1
2011/03/18 21:54:25
Done.
| |
91 nickname: nickname of the video for presentation. | |
92 """ | |
93 if name is "none": | |
dennis_jeffrey
2011/03/18 17:47:50
Use single quotes instead of double quotes here.
imasaki1
2011/03/18 21:54:25
Done.
| |
94 return | |
95 cur_video = name | |
96 # All videos are placed up to the rows (below 15th row). | |
dennis_jeffrey
2011/03/18 17:47:50
# All videos are placed up to row MAX_VIDEO_INDEX.
imasaki1
2011/03/18 21:54:25
Removed since we are not using MAX_VIDEO_INDEX any
| |
97 is_video = sub_type <= self.__GetMaxValue(True) | |
98 file = os.path.join(cur_video, cur_video + self.__exts[sub_type]) | |
99 # Specs were generated from CSV file in readFile(). | |
100 info = self.__specs[cur_video][sub_type] | |
101 url = media_test_matrix_home_url + file | |
102 link = '<b><a href="%s">%s</a> %s</b>' % (url, file, info) | |
103 type = ['audio', 'video'][is_video] | |
104 tag_attr = "id='v' controls autoplay playbackRate=1 loop valign=top" | |
dennis_jeffrey
2011/03/18 17:47:50
Please use single quotes to surround the outer str
imasaki1
2011/03/18 21:54:25
Done.
| |
105 tag = "<%s %s src='%s'>" % (type, tag_attr, file) | |
dennis_jeffrey
2011/03/18 17:47:50
Same comment as line 104 above.
imasaki1
2011/03/18 21:54:25
Done.
| |
106 nickname = cur_video + self.__exts[sub_type] | |
107 return (info, url, link, tag, is_video, nickname) | |
108 | |
109 def __GetMaxValue(self, video_only): | |
110 """Get maximum index value of exts for video or audio. | |
111 | |
112 Args: | |
113 video_only: True if the max value is for video. | |
114 | |
115 Returns: | |
116 the max index value of exts for video or audio. | |
117 """ | |
118 return [len(self.__exts) - 1, MAX_VIDEO_INDEX][video_only] | |
119 | |
120 def GenerateRandomMediaInfo(self, number_of_tries=10, video_only=False, | |
121 video_matrix_home_url=''): | |
122 """Generate random video info that can be used for playing this media. | |
123 | |
124 Args: | |
125 video_only: True if generate random video only. | |
126 media_test_matrix_home_url: url of the matrix home that used | |
127 to generate link. | |
128 | |
129 Returns: | |
130 a list of a tuples (info, url, link, tag, is_video, nickname). | |
131 info: description of the video (an entry in the matrix). | |
132 url: URL of the video/audio. | |
133 link: href tag for video or audio. | |
134 tag: a tag string can be used to display video or audio. | |
135 is_video: True if the file is video; False otherwise. | |
136 nickname: nickname of the video for presentation. | |
137 """ | |
138 # Try number_of_tries times to find avaialble video/audio. | |
dennis_jeffrey
2011/03/18 17:47:50
"avaialble" --> "available"
imasaki1
2011/03/18 21:54:25
Done.
| |
139 for i in range(number_of_tries): | |
140 sub_type = random.randint(0, self.__GetMaxValue(video_only)) | |
141 name = self.__video_titles[random.randint(0, len(self.__video_titles)-1)] | |
142 (info, url, link, tag, | |
143 is_video, nickname) = self.__GenerateMediaInfo(sub_type, | |
144 name, | |
145 video_matrix_home_url) | |
dennis_jeffrey
2011/03/18 17:47:50
I think lines 143-145 might look better like this:
imasaki1
2011/03/18 21:54:25
Changed as well as other places.
| |
146 if NOT_AVAILABLE_STRING not in info: | |
147 return (info, url, link, tag, is_video, nickname) | |
dennis_jeffrey
2011/03/18 17:47:50
Remove one of the extra spaces after the word "ret
imasaki1
2011/03/18 21:54:25
Done.
| |
148 # Gives up after that (very small chance) | |
dennis_jeffrey
2011/03/18 17:47:50
Put period at end of sentence.
imasaki1
2011/03/18 21:54:25
Done.
| |
149 return None | |
150 | |
151 def GenerateAllMediaInfos(self, video_only, media_matrix_home_url=''): | |
152 """Generate all video infos that can be used for playing this media. | |
153 | |
154 Args: | |
155 video_only: True if generate random video only (not audio). | |
156 | |
157 Returns: | |
158 a list of a tuples (info, url, link, tag, is_video, nickname). | |
159 info: description of the video (an entry in the matrix). | |
160 url: URL of the video/audio. | |
161 is_video: True if the file is video; False otherwise. | |
dennis_jeffrey
2011/03/18 17:47:50
What about the "link" and "tag" tuple items?
imasaki1
2011/03/18 21:54:25
Done.
| |
162 nickname: nickname of the video for presentation (such as bear.webm). | |
163 """ | |
164 video_infos = [] | |
165 | |
166 for i in range(0, len(self.__video_titles)-1): | |
167 name = self.__video_titles[i] | |
168 for sub_type in range(self.__GetMaxValue(video_only)): | |
169 (info, url, link, tag, | |
170 is_video, nickname) = self.__GenerateMediaInfo(sub_type, | |
171 name, | |
172 media_matrix_home_url) | |
dennis_jeffrey
2011/03/18 17:47:50
Similar comment as line 145 above.
imasaki1
2011/03/18 21:54:25
Done.
| |
173 if NOT_AVAILABLE_STRING not in info: | |
174 video_infos.append([info, url, link, tag, is_video, nickname]) | |
175 return video_infos | |
176 | |
177 def GenerateAllMediaInfosInCompactForm(self, video_only, | |
178 media_matrix_home_url=''): | |
179 """Generate all media information in compact form. | |
180 | |
181 Compact form contains only url, nickname and tag. | |
182 | |
183 Args: | |
184 video_only: True if generate random video only. | |
185 | |
186 Returns: | |
187 a list of a tuples (url, nickname, tag). | |
188 url: URL of the video/audio. | |
189 nickname: nickname of the video/audio for presentation | |
190 (such as bear.webm). | |
191 tag: HTML5 tag for the video/audio. | |
192 """ | |
193 video_infos = [] | |
194 for i in range(0, len(self.__video_titles)-1): | |
195 name = self.__video_titles[i] | |
196 for sub_type in range(self.__GetMaxValue(video_only)): | |
197 (info, url, link, tag, | |
198 is_video, nickname) = self.__GenerateMediaInfo(sub_type, | |
199 name, | |
200 media_matrix_home_url) | |
dennis_jeffrey
2011/03/18 17:47:50
Similar comment as line 145 above.
imasaki1
2011/03/18 21:54:25
Done.
| |
201 tag = ['audio', 'video'][is_video] | |
202 if NOT_AVAILABLE_STRING not in info: | |
203 video_infos.append([url, nickname, tag]) | |
204 return video_infos | |
205 | |
dennis_jeffrey
2011/03/18 17:47:50
Functions GenerateAllMediaInfosInCompactForm, Gene
imasaki1
2011/03/18 21:54:25
I agree. I will add TODO
| |
206 @staticmethod | |
207 def LookForMediaInfoByNickName(compact_list, target): | |
208 """Look for video by its nickname in the compact_list. | |
209 | |
210 Args: | |
211 compact_list: a list generated by GenerateAllMediaInfosInCompactForm. | |
212 target: a target nickname string to look for. | |
213 | |
214 Returns: | |
215 A tuple (info, url, link, tag, is_video, nickname) where nickname. | |
dennis_jeffrey
2011/03/18 17:47:50
I don't understand what you mean by "where nicknam
imasaki1
2011/03/18 21:54:25
Done.
| |
216 url: URL of the video/audio. | |
217 tag: HTML5 tag for the video/audio. | |
218 nickname: nickname of the video/audio for presentation | |
219 (such as bear.webm). | |
dennis_jeffrey
2011/03/18 17:47:50
What about the "info", "link", and "is_video" tupl
imasaki1
2011/03/18 21:54:25
It is using compact form where we do not need thes
| |
220 """ | |
221 for url, nickname, tag in compact_list: | |
222 if target == nickname: | |
223 return (url, nickname, tag) | |
OLD | NEW |