cmdserver.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # cmdserver.py
  2. # Demo code that is not Pythonwin related, but too good to throw away...
  3. import win32api
  4. import sys
  5. from pywin.framework import winout
  6. import thread, sys
  7. import traceback
  8. class ThreadWriter:
  9. "Assign an instance to sys.stdout for per-thread printing objects - Courtesy Guido!"
  10. def __init__(self):
  11. "Constructor -- initialize the table of writers"
  12. self.writers = {}
  13. self.origStdOut = None
  14. def register(self, writer):
  15. "Register the writer for the current thread"
  16. self.writers[thread.get_ident()] = writer
  17. if self.origStdOut is None:
  18. self.origStdOut = sys.stdout
  19. sys.stdout = self
  20. def unregister(self):
  21. "Remove the writer for the current thread, if any"
  22. try:
  23. del self.writers[thread.get_ident()]
  24. except KeyError:
  25. pass
  26. if len(self.writers)==0:
  27. sys.stdout = self.origStdOut
  28. self.origStdOut = None
  29. def getwriter(self):
  30. "Return the current thread's writer, default sys.stdout"
  31. try:
  32. return self.writers[thread.get_ident()]
  33. except KeyError:
  34. return self.origStdOut
  35. def write(self, str):
  36. "Write to the current thread's writer, default sys.stdout"
  37. self.getwriter().write(str)
  38. def Test():
  39. num=1
  40. while num<1000:
  41. print 'Hello there no ' + str(num)
  42. win32api.Sleep(50)
  43. num = num + 1
  44. class flags:
  45. SERVER_BEST = 0
  46. SERVER_IMMEDIATE = 1
  47. SERVER_THREAD = 2
  48. SERVER_PROCESS = 3
  49. def StartServer( cmd, title=None, bCloseOnEnd=0, serverFlags = flags.SERVER_BEST ):
  50. out = winout.WindowOutput( title, None, winout.flags.WQ_IDLE )
  51. if not title:
  52. title=cmd
  53. out.Create(title)
  54. # ServerThread((out, cmd, title, bCloseOnEnd))
  55. # out = sys.stdout
  56. thread.start_new_thread( ServerThread, (out, cmd, title, bCloseOnEnd) )
  57. def ServerThread(myout, cmd, title, bCloseOnEnd):
  58. try:
  59. writer.register(myout)
  60. print 'Executing "%s"\n' % cmd
  61. bOK = 1
  62. try:
  63. import __main__
  64. exec (cmd+'\n', __main__.__dict__)
  65. except:
  66. bOK = 0
  67. if bOK:
  68. print "Command terminated without errors."
  69. else:
  70. t, v, tb = sys.exc_info()
  71. print t, ': ', v
  72. traceback.print_tb(tb)
  73. tb = None # prevent a cycle
  74. print "Command terminated with an unhandled exception"
  75. writer.unregister()
  76. if bOK and bCloseOnEnd:
  77. myout.frame.DestroyWindow()
  78. # Unhandled exception of any kind in a thread kills the gui!
  79. except:
  80. t, v, tb = sys.exc_info()
  81. print t, ': ', v
  82. traceback.print_tb(tb)
  83. tb = None
  84. print "Thread failed"
  85. # assist for reloading (when debugging) - use only 1 tracer object,
  86. # else a large chain of tracer objects will exist.
  87. #try:
  88. # writer
  89. #except NameError:
  90. # writer=ThreadWriter()
  91. if __name__=='__main__':
  92. import demoutils
  93. demoutils.NotAScript()