OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python -tt | |
2 # Copyright (c) 2011 The Chromium OS 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 | |
6 """A BmpBlock class""" | |
7 | |
8 import os | |
9 import types | |
10 import yaml | |
11 | |
12 class BmpBlock(object): | |
13 """A wrapper for the config.yaml file. | |
14 It has a few special attributes to specify which part we're focusing on. | |
15 """ | |
16 | |
17 def __init__(self, filename=None): | |
18 self.yaml = None | |
19 self.filename = None | |
20 self.current_screen = None | |
21 self.filename = filename # always set, so we can reload | |
22 if filename: | |
23 self.LoadFile(filename) | |
24 | |
25 def LoadFile(self, filename): | |
26 """Load the specified yaml file and verify that it's a valid BmpBlock""" | |
27 print "Loading", filename | |
28 with open(filename, 'rb') as f: | |
29 stuff = yaml.safe_load(f) | |
30 # FIXME: This is pretty lame. We should be able to find images using a | |
31 # default directory path instead of using chdir. | |
32 if os.path.dirname(filename): | |
33 os.chdir(os.path.dirname(filename)) | |
34 if self.IsValidSyntax(stuff): | |
35 self.yaml = stuff | |
36 self.current_screen = sorted(self.yaml["screens"].keys())[0] | |
37 | |
38 def Reload(self): | |
39 tmp = self.current_screen | |
40 self.LoadFile(self.filename) | |
41 if tmp in self.yaml["screens"]: | |
42 self.current_screen = tmp | |
43 | |
44 def IsValidSyntax(self, thing): | |
45 """Raise an error if the specified dict is not a valid BmpBlock structure""" | |
46 | |
47 assert isinstance(thing, dict) | |
48 assert thing["bmpblock"] == 1.0 | |
49 | |
50 seen_images = {} | |
51 seen_screens = {} | |
52 | |
53 images = thing["images"] | |
54 assert isinstance(images, dict) | |
55 assert len(images) > 0 | |
56 # image values should all be filenames (ie, strings) | |
57 for val in images.values(): | |
58 assert val and isinstance(val, types.StringTypes) and len(val) > 0 | |
Randall Spangler
2011/03/01 19:23:32
don't need 'and len' now
| |
59 | |
60 screens = thing["screens"] | |
61 assert isinstance(screens, dict) | |
62 assert screens | |
63 # screen values should all be lists of 3-tuples | |
64 for scrname, imglist in screens.items(): | |
65 assert len(imglist) <= 8 | |
66 for img in imglist: | |
67 assert 3 == len(img) | |
68 # must have defined all referenced bitmaps | |
69 x,y,i = img | |
70 assert i in images | |
71 seen_images[i] = True | |
72 | |
73 localizations = thing["localizations"] | |
74 assert hasattr(localizations, '__iter__') | |
75 assert localizations | |
76 # localizations should all be lists with the same number of screens | |
77 len0 = len(localizations[0]) | |
78 assert len0 | |
79 for elt in localizations: | |
80 assert len0 == len(elt) | |
81 # we must have defined all referenced screens | |
82 for scr in elt: | |
83 assert scr in screens | |
84 seen_screens[scr] = True | |
85 | |
86 for unused_img in [x for x in images if x not in seen_images]: | |
87 print " Unused image:", unused_img | |
88 for unused_scr in [x for x in screens if x not in seen_screens]: | |
89 print " Unused screen:", unused_scr | |
90 | |
91 return True | |
92 | |
93 def RegisterScreenDisplayObject(self, displayer): | |
94 """Register an object with a .Redisplay() function to display updates.""" | |
95 self.displayer = displayer | |
96 | |
97 | |
98 def Redisplay(self): | |
99 """Redisplay contents.""" | |
100 if self.displayer: | |
101 if self.current_screen: | |
102 sc = self.yaml['screens'][self.current_screen] | |
103 slist = [(x,y,self.yaml['images'][z]) for x,y,z in sc] | |
104 self.displayer.DisplayScreen(self.current_screen, slist) | |
OLD | NEW |