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

https://github.com/akesling/chromium · Python · 95 lines · 63 code · 16 blank · 16 comment · 5 complexity · 13096f088d5136698755bdaa0e3c723a 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.rc_header
  9. import grit.format.rc
  10. from grit.node import base
  11. from grit import util
  12. class IncludeNode(base.Node):
  13. '''An <include> element.'''
  14. def _IsValidChild(self, child):
  15. return False
  16. def MandatoryAttributes(self):
  17. return ['name', 'type', 'file']
  18. def DefaultAttributes(self):
  19. return {'translateable' : 'true',
  20. 'generateid': 'true',
  21. 'filenameonly': 'false',
  22. 'flattenhtml': 'false',
  23. 'relativepath': 'false',
  24. }
  25. def ItemFormatter(self, t):
  26. if t == 'rc_header':
  27. return grit.format.rc_header.Item()
  28. elif (t in ['rc_all', 'rc_translateable', 'rc_nontranslateable'] and
  29. self.SatisfiesOutputCondition()):
  30. return grit.format.rc.RcInclude(self.attrs['type'].upper(),
  31. self.attrs['filenameonly'] == 'true',
  32. self.attrs['relativepath'] == 'true',
  33. self.attrs['flattenhtml'] == 'true')
  34. elif t == 'resource_map_source':
  35. from grit.format import resource_map
  36. return resource_map.SourceInclude()
  37. else:
  38. return super(type(self), self).ItemFormatter(t)
  39. def FileForLanguage(self, lang, output_dir):
  40. '''Returns the file for the specified language. This allows us to return
  41. different files for different language variants of the include file.
  42. '''
  43. return self.FilenameToOpen()
  44. def GetDataPackPair(self, output_dir, lang):
  45. '''Returns a (id, string) pair that represents the resource id and raw
  46. bytes of the data. This is used to generate the data pack data file.
  47. '''
  48. from grit.format import rc_header
  49. id_map = rc_header.Item.tids_
  50. id = id_map[self.GetTextualIds()[0]]
  51. filename = self.FilenameToOpen()
  52. if self.attrs['flattenhtml'] == 'true':
  53. # If the file was flattened, the flattened file is in the output dir.
  54. filename = os.path.join(output_dir, os.path.split(filename)[1])
  55. file = open(filename, 'rb')
  56. data = file.read()
  57. file.close()
  58. return id, data
  59. # static method
  60. def Construct(parent, name, type, file, translateable=True,
  61. filenameonly=False, relativepath=False):
  62. '''Creates a new node which is a child of 'parent', with attributes set
  63. by parameters of the same name.
  64. '''
  65. # Convert types to appropriate strings
  66. translateable = util.BoolToString(translateable)
  67. filenameonly = util.BoolToString(filenameonly)
  68. relativepath = util.BoolToString(relativepath)
  69. node = IncludeNode()
  70. node.StartParsing('include', parent)
  71. node.HandleAttribute('name', name)
  72. node.HandleAttribute('type', type)
  73. node.HandleAttribute('file', file)
  74. node.HandleAttribute('translateable', translateable)
  75. node.HandleAttribute('filenameonly', filenameonly)
  76. node.HandleAttribute('relativepath', relativepath)
  77. node.EndParsing()
  78. return node
  79. Construct = staticmethod(Construct)