cmdserver.py :  » Windows » pyExcelerator » pywin32-214 » pythonwin » pywin » Demos » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » pythonwin » pywin » Demos » cmdserver.py
# cmdserver.py

# Demo code that is not Pythonwin related, but too good to throw away...

import win32api
import sys
from pywin.framework import winout

import thread, sys

import traceback

class ThreadWriter:
  "Assign an instance to sys.stdout for per-thread printing objects - Courtesy Guido!"
  def __init__(self):
    "Constructor -- initialize the table of writers"
    self.writers = {}
    self.origStdOut = None
  def register(self, writer):
    "Register the writer for the current thread"
    self.writers[thread.get_ident()] = writer
    if self.origStdOut is None:
      self.origStdOut = sys.stdout
      sys.stdout = self    

  def unregister(self):
    "Remove the writer for the current thread, if any"
    try:
      del self.writers[thread.get_ident()]
    except KeyError:
      pass
    if len(self.writers)==0:
      sys.stdout = self.origStdOut
      self.origStdOut = None

  def getwriter(self):
    "Return the current thread's writer, default sys.stdout"
    try:
      return self.writers[thread.get_ident()]
    except KeyError:
      return self.origStdOut

  def write(self, str):
    "Write to the current thread's writer, default sys.stdout"
    self.getwriter().write(str)

def Test():
  num=1
  while num<1000:
    print 'Hello there no ' + str(num)
    win32api.Sleep(50)
    num = num + 1

class flags:
  SERVER_BEST = 0
  SERVER_IMMEDIATE = 1
  SERVER_THREAD = 2
  SERVER_PROCESS = 3
  
def StartServer( cmd, title=None, bCloseOnEnd=0, serverFlags = flags.SERVER_BEST ):
  out = winout.WindowOutput( title, None, winout.flags.WQ_IDLE )
  if not title:
    title=cmd
  out.Create(title)
#  ServerThread((out, cmd, title, bCloseOnEnd))
#  out = sys.stdout
  thread.start_new_thread( ServerThread, (out, cmd, title, bCloseOnEnd) )

def ServerThread(myout, cmd, title, bCloseOnEnd):
  try:
    writer.register(myout)
    print 'Executing "%s"\n' % cmd
    bOK = 1
    try:
      import __main__
      exec (cmd+'\n', __main__.__dict__)
    except:
      bOK = 0
    if bOK:
      print "Command terminated without errors."
    else:
      t, v, tb = sys.exc_info()
      print t, ': ', v
      traceback.print_tb(tb)
      tb = None # prevent a cycle
      print "Command terminated with an unhandled exception"
    writer.unregister()
    if bOK and bCloseOnEnd:
      myout.frame.DestroyWindow()

  # Unhandled exception of any kind in a thread kills the gui!
  except:
    t, v, tb = sys.exc_info()
    print t, ': ', v
    traceback.print_tb(tb)
    tb = None
    print "Thread failed"

# assist for reloading (when debugging) - use only 1 tracer object,
# else a large chain of tracer objects will exist.
#try:
#  writer
#except NameError:
#  writer=ThreadWriter()
if __name__=='__main__':
  import demoutils
  demoutils.NotAScript()
  
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.