PageRenderTime 154ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/grit/grit/node/io.py

https://github.com/bluebellzhy/chromium
Python | 106 lines | 81 code | 17 blank | 8 comment | 2 complexity | f8893be2d99283630a81f718f845007c MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  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. '''The <output> and <file> elements.
  6. '''
  7. import os
  8. import re
  9. import grit.format.rc_header
  10. from grit.node import base
  11. from grit import exception
  12. from grit import util
  13. from grit import xtb_reader
  14. class FileNode(base.Node):
  15. '''A <file> element.'''
  16. def __init__(self):
  17. super(type(self), self).__init__()
  18. self.re = None
  19. self.should_load_ = True
  20. def IsTranslation(self):
  21. return True
  22. def GetLang(self):
  23. return self.attrs['lang']
  24. def DisableLoading(self):
  25. self.should_load_ = False
  26. def MandatoryAttributes(self):
  27. return ['path', 'lang']
  28. def RunGatherers(self, recursive=False, debug=False):
  29. if not self.should_load_:
  30. return
  31. xtb_file = file(self.GetFilePath())
  32. try:
  33. lang = xtb_reader.Parse(xtb_file,
  34. self.UberClique().GenerateXtbParserCallback(
  35. self.attrs['lang'], debug=debug))
  36. except:
  37. print "Exception during parsing of %s" % self.GetFilePath()
  38. raise
  39. assert (lang == self.attrs['lang'], 'The XTB file you '
  40. 'reference must contain messages in the language specified\n'
  41. 'by the \'lang\' attribute.')
  42. def GetFilePath(self):
  43. return self.ToRealPath(os.path.expandvars(self.attrs['path']))
  44. class OutputNode(base.Node):
  45. '''An <output> element.'''
  46. def MandatoryAttributes(self):
  47. return ['filename', 'type']
  48. def DefaultAttributes(self):
  49. return { 'lang' : '', # empty lang indicates all languages
  50. 'language_section' : 'neutral' # defines a language neutral section
  51. }
  52. def GetType(self):
  53. return self.attrs['type']
  54. def GetLanguage(self):
  55. '''Returns the language ID, default 'en'.'''
  56. return self.attrs['lang']
  57. def GetFilename(self):
  58. return self.attrs['filename']
  59. def GetOutputFilename(self):
  60. if hasattr(self, 'output_filename'):
  61. return self.output_filename
  62. else:
  63. return self.attrs['filename']
  64. def _IsValidChild(self, child):
  65. return isinstance(child, EmitNode)
  66. class EmitNode(base.ContentNode):
  67. ''' An <emit> element.'''
  68. def DefaultAttributes(self):
  69. return { 'emit_type' : 'prepend'}
  70. def GetEmitType(self):
  71. '''Returns the emit_type for this node. Default is 'append'.'''
  72. return self.attrs['emit_type']
  73. def ItemFormatter(self, t):
  74. if t == 'rc_header':
  75. return grit.format.rc_header.EmitAppender()
  76. else:
  77. return super(type(self), self).ItemFormatter(t)