PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/mail/Logger.py

http://pyjamas.googlecode.com/
Python | 61 lines | 60 code | 1 blank | 0 comment | 0 complexity | 7cbd12aa144a32fe627a7317c4db53e6 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. from ui import Grid
  2. class Logger(Grid):
  3. def __init__(self, target="", message=""):
  4. if message:
  5. return Logger().write(target, message)
  6. # make sure there is only one instance of this class
  7. if _logger: return _logger
  8. self.setSingleton()
  9. Grid.__init__(self)
  10. self.targets=[]
  11. self.targets.append("app")
  12. #self.targets.append("ui")
  13. self.resize(len(self.targets)+1, 2)
  14. self.setBorderWidth(1)
  15. self.counter=0
  16. self.setHTML(0, 0, "<b>Log</b>")
  17. self.setText(1, 0, "app")
  18. for i in range(len(self.targets)):
  19. target=self.targets[i]
  20. self.setText(i+1, 0, target)
  21. def setSingleton(self):
  22. JS("""
  23. _logger=this;
  24. };
  25. var _logger=null;
  26. {
  27. """)
  28. def addTarget(self, target):
  29. self.targets.append(target)
  30. self.resize(len(self.targets)+1, 2)
  31. self.setText(len(self.targets), 0, target)
  32. return self.targets.index(target)
  33. def write(self, target, message):
  34. self.counter+=1
  35. if target=='':
  36. target='app'
  37. target_idx=self.targets.index(target)
  38. # add new target
  39. if target_idx<0:
  40. target_idx=self.addTarget(target)
  41. target_row=target_idx+1
  42. old_text=self.getHTML(target_row, 1)
  43. log_line=self.counter + ": " + message
  44. if old_text=='&nbsp;':
  45. new_text=log_line
  46. else:
  47. new_text=old_text + "<br>" + log_line
  48. self.setHTML(target_row, 1, new_text)