Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: grit/gather/chrome_scaled_image.py

Issue 1194063002: grit: Add fallback_to_default_layout attribute to <output> (Closed) Base URL: https://chromium.googlesource.com/external/grit-i18n.git@master
Patch Set: proof of concept test Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 '''Gatherer for <structure type="chrome_scaled_image">. 6 '''Gatherer for <structure type="chrome_scaled_image">.
7 ''' 7 '''
8 8
9 import os 9 import os
10 import struct 10 import struct
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 def _FindInputFile(self): 93 def _FindInputFile(self):
94 output_context = self.grd_node.GetRoot().output_context 94 output_context = self.grd_node.GetRoot().output_context
95 match = self.split_context_re_.match(output_context) 95 match = self.split_context_re_.match(output_context)
96 if not match: 96 if not match:
97 raise exception.MissingMandatoryAttribute( 97 raise exception.MissingMandatoryAttribute(
98 'All <output> nodes must have an appropriate context attribute' 98 'All <output> nodes must have an appropriate context attribute'
99 ' (e.g. context="touch_200_percent")') 99 ' (e.g. context="touch_200_percent")')
100 req_layout, req_scale = match.group(1), int(match.group(2)) 100 req_layout, req_scale = match.group(1), int(match.group(2))
101 101
102 layouts = [req_layout] 102 layouts = [req_layout]
103 if 'default' not in layouts: 103 try_default_layout = self.grd_node.GetRoot().fallback_to_default_layout
flackr 2015/06/23 13:33:33 Hmm, can't we do the same thing as for try_low_res
tdanderson 2015/06/23 14:22:54 I considered that, but the corresponding <output>
104 if try_default_layout and 'default' not in layouts:
104 layouts.append('default') 105 layouts.append('default')
106
107 # TODO(tdanderson): Search in descending order of all image scales
108 # instead of immediately falling back to 100. crbug.com/x
105 scales = [req_scale] 109 scales = [req_scale]
106 try_low_res = self.grd_node.FindBooleanAttribute( 110 try_low_res = self.grd_node.FindBooleanAttribute(
107 'fallback_to_low_resolution', default=False, skip_self=False) 111 'fallback_to_low_resolution', default=False, skip_self=False)
108 if try_low_res and 100 not in scales: 112 if try_low_res and 100 not in scales:
109 scales.append(100) 113 scales.append(100)
114
110 for layout in layouts: 115 for layout in layouts:
111 for scale in scales: 116 for scale in scales:
112 dir = '%s_%s_percent' % (layout, scale) 117 dir = '%s_%s_percent' % (layout, scale)
113 path = os.path.join(dir, self.rc_file) 118 path = os.path.join(dir, self.rc_file)
114 if os.path.exists(self.grd_node.ToRealPath(path)): 119 if os.path.exists(self.grd_node.ToRealPath(path)):
115 return path, scale, req_scale 120 return path, scale, req_scale
116 # If we get here then the file is missing, so fail. 121
122 if not try_default_layout:
123 # The file was not found in the specified output context and it was
124 # explicitly indicated that the default context should not be searched
125 # as a fallback, so return an empty path.
126 return None, 100, req_scale
127
128 # The file was found in neither the specified context nor the default
129 # context, so raise an exception.
117 dir = "%s_%s_percent" % (_MakeBraceGlob(layouts), 130 dir = "%s_%s_percent" % (_MakeBraceGlob(layouts),
118 _MakeBraceGlob(map(str, scales))) 131 _MakeBraceGlob(map(str, scales)))
119 raise exception.FileNotFound( 132 raise exception.FileNotFound(
120 'Tried ' + self.grd_node.ToRealPath(os.path.join(dir, self.rc_file))) 133 'Tried ' + self.grd_node.ToRealPath(os.path.join(dir, self.rc_file)))
121 134
122 def GetInputPath(self): 135 def GetInputPath(self):
123 path, scale, req_scale = self._FindInputFile() 136 path, scale, req_scale = self._FindInputFile()
124 return path 137 return path
125 138
126 def Parse(self): 139 def Parse(self):
127 pass 140 pass
128 141
129 def GetTextualIds(self): 142 def GetTextualIds(self):
130 return [self.extkey] 143 return [self.extkey]
131 144
132 def GetData(self, *args): 145 def GetData(self, *args):
133 path, scale, req_scale = self._FindInputFile() 146 path, scale, req_scale = self._FindInputFile()
147 if path is None:
148 return None
149
134 data = util.ReadFile(self.grd_node.ToRealPath(path), util.BINARY) 150 data = util.ReadFile(self.grd_node.ToRealPath(path), util.BINARY)
135 data = _RescaleImage(data, scale, req_scale) 151 data = _RescaleImage(data, scale, req_scale)
136 data = _MoveSpecialChunksToFront(data) 152 data = _MoveSpecialChunksToFront(data)
137 return data 153 return data
138 154
139 def Translate(self, *args, **kwargs): 155 def Translate(self, *args, **kwargs):
140 return self.GetData() 156 return self.GetData()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698