OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 import functools | |
Ami GONE FROM CHROMIUM
2012/01/25 17:58:48
missing newline
DaleCurtis
2012/01/25 23:59:25
Done.
| |
6 | |
7 | |
8 def FindMediaFiles(matrix, criteria=None, **kwargs): | |
9 """Given a loosely structured test matrix, finds media matching criteria. | |
Ami GONE FROM CHROMIUM
2012/01/25 17:58:48
This whole thing seems like crazy overkill to me.
DaleCurtis
2012/01/25 23:59:25
You should have seen the generic version :) In an
Ami GONE FROM CHROMIUM
2012/01/26 00:07:23
WDYT about keeping this matrix business in your ba
| |
10 | |
11 The only requirements for the test matrix structure are a top level element | |
12 and a 'files' list element under it containing dicts with at least a 'file' | |
13 tag, indicating the file name, in them. For example: | |
14 | |
15 { | |
16 "<test name>": { | |
17 <...any tags...> | |
18 | |
19 'files': [ | |
20 {'file': '<filename>', <...any tags...>} | |
21 ] | |
22 } | |
23 } | |
24 | |
25 Any kind of metadata can be added in the any tags sections. All fields can be | |
26 filtered on using the criteria mechanism. For example, to match only files | |
27 with the name 'test.mp4' you would write: | |
28 | |
29 FindMediaFiles(<matrix>, file='test.mp4') | |
30 | |
31 If you wanted to ensure that a metadata named 'duration' was < 5.0 you can use | |
32 the criteria system: | |
33 | |
34 FindMediaFiles(<matrix>, criteria={'duration': lambda v: v < 5.0}) | |
35 | |
36 Arguments: | |
37 matrix: Dictionary object as described above. | |
38 criteria: Dictionary of tags to boolean functions. Each file entry in the | |
39 matrix will be required to have the specified tags and pass the associated | |
40 function. The function will be called with the value associated with the | |
41 tag. | |
42 **kwargs: Generally a simple equality check is all that is required to find | |
43 the required tags. So by simply specifying the key and value here, you | |
44 can avoid typing out a full criteria entry. Entries here will override | |
45 those in |criteria|. | |
46 """ | |
47 results = [] | |
48 | |
49 # Automatically covert each kwarg into a simple equality check. | |
50 criteria = criteria or {} | |
51 for arg in kwargs: | |
52 criteria[arg] = functools.partial(lambda x, y: x == y, kwargs[arg]) | |
53 | |
54 for entry in matrix: | |
55 # Make a shallow copy so we don't destroy the main data structure. | |
56 test = matrix[entry].copy() | |
57 | |
58 # Insert the entry name into the dictionary so it can be filtered on. | |
59 test.setdefault('name', entry) | |
60 | |
61 # Remove the files key from the top level test structure. | |
62 for f in test.pop('files'): | |
63 # Setup default values for files. | |
64 f.setdefault('audio', True) | |
65 f.setdefault('video', True) | |
66 | |
67 # Copy in the top level tags from test into the file dictionary. | |
68 f.update(test) | |
69 | |
70 # Ensure that all the file tags pass all the criteria. | |
71 success = True | |
72 for tag, fn in criteria.iteritems(): | |
73 success = success and tag in f and fn(f[tag]) | |
74 | |
75 if success: | |
76 results.append(f['file']) | |
77 | |
78 return results | |
OLD | NEW |