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