OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 # Copyright (c) 2011 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 |
| 6 '''Handling of the <include> element. |
| 7 ''' |
| 8 |
| 9 import os |
| 10 |
| 11 import grit.format.html_inline |
| 12 import grit.format.rc_header |
| 13 import grit.format.rc |
| 14 |
| 15 from grit.node import base |
| 16 from grit import util |
| 17 |
| 18 class IncludeNode(base.Node): |
| 19 '''An <include> element.''' |
| 20 def __init__(self): |
| 21 base.Node.__init__(self) |
| 22 |
| 23 # Cache flattened data so that we don't flatten the same file |
| 24 # multiple times. |
| 25 self._flattened_data = None |
| 26 # Also keep track of the last filename we flattened to, so we can |
| 27 # avoid doing it more than once. |
| 28 self._last_flat_filename = None |
| 29 |
| 30 def _IsValidChild(self, child): |
| 31 return False |
| 32 |
| 33 def _GetFlattenedData(self, allow_external_script=False): |
| 34 if not self._flattened_data: |
| 35 filename = self.FilenameToOpen() |
| 36 self._flattened_data = ( |
| 37 grit.format.html_inline.InlineToString(filename, self, |
| 38 allow_external_script=allow_external_script)) |
| 39 return self._flattened_data |
| 40 |
| 41 def MandatoryAttributes(self): |
| 42 return ['name', 'type', 'file'] |
| 43 |
| 44 def DefaultAttributes(self): |
| 45 return {'translateable' : 'true', |
| 46 'generateid': 'true', |
| 47 'filenameonly': 'false', |
| 48 'flattenhtml': 'false', |
| 49 'allowexternalscript': 'false', |
| 50 'relativepath': 'false', |
| 51 } |
| 52 |
| 53 def ItemFormatter(self, t): |
| 54 if t == 'rc_header': |
| 55 return grit.format.rc_header.Item() |
| 56 elif (t in ['rc_all', 'rc_translateable', 'rc_nontranslateable'] and |
| 57 self.SatisfiesOutputCondition()): |
| 58 return grit.format.rc.RcInclude(self.attrs['type'].upper(), |
| 59 self.attrs['filenameonly'] == 'true', |
| 60 self.attrs['relativepath'] == 'true', |
| 61 self.attrs['flattenhtml'] == 'true') |
| 62 elif t == 'resource_map_source': |
| 63 from grit.format import resource_map |
| 64 return resource_map.SourceInclude() |
| 65 elif t == 'resource_file_map_source': |
| 66 from grit.format import resource_map |
| 67 return resource_map.SourceFileInclude() |
| 68 else: |
| 69 return super(type(self), self).ItemFormatter(t) |
| 70 |
| 71 def FileForLanguage(self, lang, output_dir): |
| 72 '''Returns the file for the specified language. This allows us to return |
| 73 different files for different language variants of the include file. |
| 74 ''' |
| 75 return self.FilenameToOpen() |
| 76 |
| 77 def GetDataPackPair(self, lang, encoding): |
| 78 '''Returns a (id, string) pair that represents the resource id and raw |
| 79 bytes of the data. This is used to generate the data pack data file. |
| 80 ''' |
| 81 from grit.format import rc_header |
| 82 id_map = rc_header.Item.tids_ |
| 83 id = id_map[self.GetTextualIds()[0]] |
| 84 if self.attrs['flattenhtml'] == 'true': |
| 85 allow_external_script = self.attrs['allowexternalscript'] == 'true' |
| 86 data = self._GetFlattenedData(allow_external_script=allow_external_script) |
| 87 else: |
| 88 filename = self.FilenameToOpen() |
| 89 infile = open(filename, 'rb') |
| 90 data = infile.read() |
| 91 infile.close() |
| 92 |
| 93 # Include does not care about the encoding, because it only returns binary |
| 94 # data. |
| 95 return id, data |
| 96 |
| 97 def Flatten(self, output_dir): |
| 98 '''Rewrite file references to be base64 encoded data URLs. The new file |
| 99 will be written to output_dir and the name of the new file is returned.''' |
| 100 filename = self.FilenameToOpen() |
| 101 flat_filename = os.path.join(output_dir, |
| 102 self.attrs['name'] + '_' + os.path.basename(filename)) |
| 103 |
| 104 if self._last_flat_filename == flat_filename: |
| 105 return |
| 106 |
| 107 outfile = open(flat_filename, 'wb') |
| 108 outfile.write(self._GetFlattenedData()) |
| 109 outfile.close() |
| 110 |
| 111 self._last_flat_filename = flat_filename |
| 112 return os.path.basename(flat_filename) |
| 113 |
| 114 |
| 115 def GetHtmlResourceFilenames(self): |
| 116 """Returns a set of all filenames inlined by this file.""" |
| 117 return grit.format.html_inline.GetResourceFilenames(self.FilenameToOpen()) |
| 118 |
| 119 # static method |
| 120 def Construct(parent, name, type, file, translateable=True, |
| 121 filenameonly=False, relativepath=False): |
| 122 '''Creates a new node which is a child of 'parent', with attributes set |
| 123 by parameters of the same name. |
| 124 ''' |
| 125 # Convert types to appropriate strings |
| 126 translateable = util.BoolToString(translateable) |
| 127 filenameonly = util.BoolToString(filenameonly) |
| 128 relativepath = util.BoolToString(relativepath) |
| 129 |
| 130 node = IncludeNode() |
| 131 node.StartParsing('include', parent) |
| 132 node.HandleAttribute('name', name) |
| 133 node.HandleAttribute('type', type) |
| 134 node.HandleAttribute('file', file) |
| 135 node.HandleAttribute('translateable', translateable) |
| 136 node.HandleAttribute('filenameonly', filenameonly) |
| 137 node.HandleAttribute('relativepath', relativepath) |
| 138 node.EndParsing() |
| 139 return node |
| 140 Construct = staticmethod(Construct) |
OLD | NEW |