/tools/grit/grit/node/include.py

https://github.com/Gitman1989/chromium · Python · 119 lines · 80 code · 20 blank · 19 comment · 7 complexity · 7541eaa7f9f7886076d9ea4d2522736b MD5 · raw file

  1. #!/usr/bin/python2.4
  2. # Copyright (c) 2006-2008 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. '''Handling of the <include> element.
  6. '''
  7. import os
  8. import grit.format.html_inline
  9. import grit.format.rc_header
  10. import grit.format.rc
  11. from grit.node import base
  12. from grit import util
  13. class IncludeNode(base.Node):
  14. '''An <include> element.'''
  15. def __init__(self):
  16. base.Node.__init__(self)
  17. # Keep track of whether we've flattened the file or not. We don't
  18. # want to flatten the same file multiple times.
  19. self._is_flattened = False
  20. def _IsValidChild(self, child):
  21. return False
  22. def MandatoryAttributes(self):
  23. return ['name', 'type', 'file']
  24. def DefaultAttributes(self):
  25. return {'translateable' : 'true',
  26. 'generateid': 'true',
  27. 'filenameonly': 'false',
  28. 'flattenhtml': 'false',
  29. 'relativepath': 'false',
  30. }
  31. def ItemFormatter(self, t):
  32. if t == 'rc_header':
  33. return grit.format.rc_header.Item()
  34. elif (t in ['rc_all', 'rc_translateable', 'rc_nontranslateable'] and
  35. self.SatisfiesOutputCondition()):
  36. return grit.format.rc.RcInclude(self.attrs['type'].upper(),
  37. self.attrs['filenameonly'] == 'true',
  38. self.attrs['relativepath'] == 'true',
  39. self.attrs['flattenhtml'] == 'true')
  40. elif t == 'resource_map_source':
  41. from grit.format import resource_map
  42. return resource_map.SourceInclude()
  43. elif t == 'resource_file_map_source':
  44. from grit.format import resource_map
  45. return resource_map.SourceFileInclude()
  46. else:
  47. return super(type(self), self).ItemFormatter(t)
  48. def FileForLanguage(self, lang, output_dir):
  49. '''Returns the file for the specified language. This allows us to return
  50. different files for different language variants of the include file.
  51. '''
  52. return self.FilenameToOpen()
  53. def GetDataPackPair(self, output_dir, lang):
  54. '''Returns a (id, string) pair that represents the resource id and raw
  55. bytes of the data. This is used to generate the data pack data file.
  56. '''
  57. from grit.format import rc_header
  58. id_map = rc_header.Item.tids_
  59. id = id_map[self.GetTextualIds()[0]]
  60. filename = self.FilenameToOpen()
  61. if self.attrs['flattenhtml'] == 'true':
  62. self.Flatten(output_dir)
  63. # The flattened file is in the output dir.
  64. filename = os.path.join(output_dir, os.path.split(filename)[1])
  65. file = open(filename, 'rb')
  66. data = file.read()
  67. file.close()
  68. return id, data
  69. def Flatten(self, output_dir):
  70. if self._is_flattened:
  71. return
  72. filename = self.FilenameToOpen()
  73. flat_filename = os.path.join(output_dir, os.path.split(filename)[1])
  74. grit.format.html_inline.InlineFile(filename, flat_filename, self)
  75. self._is_flattened = True
  76. def GetHtmlResourceFilenames(self):
  77. """Returns a set of all filenames inlined by this file."""
  78. return grit.format.html_inline.GetResourceFilenames(self.FilenameToOpen())
  79. # static method
  80. def Construct(parent, name, type, file, translateable=True,
  81. filenameonly=False, relativepath=False):
  82. '''Creates a new node which is a child of 'parent', with attributes set
  83. by parameters of the same name.
  84. '''
  85. # Convert types to appropriate strings
  86. translateable = util.BoolToString(translateable)
  87. filenameonly = util.BoolToString(filenameonly)
  88. relativepath = util.BoolToString(relativepath)
  89. node = IncludeNode()
  90. node.StartParsing('include', parent)
  91. node.HandleAttribute('name', name)
  92. node.HandleAttribute('type', type)
  93. node.HandleAttribute('file', file)
  94. node.HandleAttribute('translateable', translateable)
  95. node.HandleAttribute('filenameonly', filenameonly)
  96. node.HandleAttribute('relativepath', relativepath)
  97. node.EndParsing()
  98. return node
  99. Construct = staticmethod(Construct)