PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/grit/grit/gather/txt.py

https://github.com/akesling/chromium
Python | 52 lines | 28 code | 13 blank | 11 comment | 1 complexity | 7887eb51324383df0f8092476f2aeff9 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. '''Supports making amessage from a text file.
  6. '''
  7. import types
  8. from grit.gather import interface
  9. from grit import tclib
  10. from grit import util
  11. class TxtFile(interface.GathererBase):
  12. '''A text file gatherer. Very simple, all text from the file becomes a
  13. single clique.
  14. '''
  15. def __init__(self, contents):
  16. super(type(self), self).__init__()
  17. self.text_ = contents
  18. self.clique_ = None
  19. def Parse(self):
  20. self.clique_ = self.uberclique.MakeClique(tclib.Message(text=self.text_))
  21. pass
  22. def GetText(self):
  23. '''Returns the text of what is being gathered.'''
  24. return self.text_
  25. def GetTextualIds(self):
  26. return []
  27. def GetCliques(self):
  28. '''Returns the MessageClique objects for all translateable portions.'''
  29. return [self.clique_]
  30. def Translate(self, lang, pseudo_if_not_available=True,
  31. skeleton_gatherer=None, fallback_to_english=False):
  32. return self.clique_.MessageForLanguage(lang,
  33. pseudo_if_not_available,
  34. fallback_to_english).GetRealContent()
  35. def FromFile(filename_or_stream, extkey=None, encoding = 'cp1252'):
  36. if isinstance(filename_or_stream, types.StringTypes):
  37. filename_or_stream = util.WrapInputStream(file(filename_or_stream, 'rb'), encoding)
  38. return TxtFile(filename_or_stream.read())
  39. FromFile = staticmethod(FromFile)