Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
cmdserver.py117 linesDownload Raw Back to Demos
1# cmdserver.py2 3# Demo code that is not Pythonwin related, but too good to throw away...4 5import _thread6import sys7import traceback8 9import win32api10from pywin.framework import winout11 12 13class ThreadWriter:14    "Assign an instance to sys.stdout for per-thread printing objects - Courtesy Guido!"15 16    def __init__(self):17        "Constructor -- initialize the table of writers"18        self.writers = {}19        self.origStdOut = None20 21    def register(self, writer):22        "Register the writer for the current thread"23        self.writers[_thread.get_ident()] = writer24        if self.origStdOut is None:25            self.origStdOut = sys.stdout26            sys.stdout = self27 28    def unregister(self):29        "Remove the writer for the current thread, if any"30        try:31            del self.writers[_thread.get_ident()]32        except KeyError:33            pass34        if len(self.writers) == 0:35            sys.stdout = self.origStdOut36            self.origStdOut = None37 38    def getwriter(self):39        "Return the current thread's writer, default sys.stdout"40        try:41            return self.writers[_thread.get_ident()]42        except KeyError:43            return self.origStdOut44 45    def write(self, str):46        "Write to the current thread's writer, default sys.stdout"47        self.getwriter().write(str)48 49 50def Test():51    num = 152    while num < 1000:53        print("Hello there no " + str(num))54        win32api.Sleep(50)55        num = num + 156 57 58class flags:59    SERVER_BEST = 060    SERVER_IMMEDIATE = 161    SERVER_THREAD = 262    SERVER_PROCESS = 363 64 65def StartServer(cmd, title=None, bCloseOnEnd=0, serverFlags=flags.SERVER_BEST):66    out = winout.WindowOutput(title, None, winout.flags.WQ_IDLE)67    if not title:68        title = cmd69    out.Create(title)70    # 	ServerThread((out, cmd, title, bCloseOnEnd))71    # 	out = sys.stdout72    _thread.start_new_thread(ServerThread, (out, cmd, title, bCloseOnEnd))73 74 75def ServerThread(myout, cmd, title, bCloseOnEnd):76    try:77        writer.register(myout)78        print('Executing "%s"\n' % cmd)79        bOK = 180        try:81            import __main__82 83            exec(cmd + "\n", __main__.__dict__)84        except:85            bOK = 086        if bOK:87            print("Command terminated without errors.")88        else:89            t, v, tb = sys.exc_info()90            print(t, ": ", v)91            traceback.print_tb(tb)92            tb = None  # prevent a cycle93            print("Command terminated with an unhandled exception")94        writer.unregister()95        if bOK and bCloseOnEnd:96            myout.frame.DestroyWindow()97 98    # Unhandled exception of any kind in a thread kills the gui!99    except:100        t, v, tb = sys.exc_info()101        print(t, ": ", v)102        traceback.print_tb(tb)103        tb = None104        print("Thread failed")105 106 107# assist for reloading (when debugging) - use only 1 tracer object,108# else a large chain of tracer objects will exist.109# try:110# 	writer111# except NameError:112# 	writer=ThreadWriter()113if __name__ == "__main__":114    import demoutils115 116    demoutils.NotAScript()117 
codekingpro/portable-devtools · Team Ai