Mercurial > cpython-lru_cache-weakref
annotate Mac/Contrib/BBPy/PythonSlave.py @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | d10653e71cfe |
| children |
| rev | line source |
|---|---|
| 9011 | 1 """PythonSlave.py |
| 2 An application that responds to three types of apple event: | |
| 3 'pyth'/'EXEC': execute direct parameter as Python | |
| 4 'aevt', 'quit': quit | |
| 5 'aevt', 'odoc': perform python scripts | |
| 6 | |
| 7 Copyright © 1996, Just van Rossum, Letterror | |
| 8 """ | |
| 9 | |
| 10 __version__ = "0.1.3" | |
| 11 | |
| 12 import FrameWork | |
| 13 import sys | |
| 14 import traceback | |
| 15 import aetools | |
| 16 import string | |
|
19488
42b2f1936254
Import the MacOS toolbox modules from the Carbon package.
Jack Jansen <jack.jansen@cwi.nl>
parents:
17845
diff
changeset
|
17 from Carbon import AE |
|
19625
d10653e71cfe
Case mismatch in "import Types". Apparently nobody has looked at this for a looooong time. Reported by Chris Smith.
Jack Jansen <jack.jansen@cwi.nl>
parents:
19488
diff
changeset
|
18 import EasyDialogs |
| 9011 | 19 import os |
|
19488
42b2f1936254
Import the MacOS toolbox modules from the Carbon package.
Jack Jansen <jack.jansen@cwi.nl>
parents:
17845
diff
changeset
|
20 from Carbon import Qd |
|
19625
d10653e71cfe
Case mismatch in "import Types". Apparently nobody has looked at this for a looooong time. Reported by Chris Smith.
Jack Jansen <jack.jansen@cwi.nl>
parents:
19488
diff
changeset
|
21 from types import * |
|
19488
42b2f1936254
Import the MacOS toolbox modules from the Carbon package.
Jack Jansen <jack.jansen@cwi.nl>
parents:
17845
diff
changeset
|
22 from Carbon.Events import charCodeMask, cmdKey |
| 9011 | 23 import MacOS |
|
19488
42b2f1936254
Import the MacOS toolbox modules from the Carbon package.
Jack Jansen <jack.jansen@cwi.nl>
parents:
17845
diff
changeset
|
24 from Carbon import Evt |
| 9011 | 25 |
| 26 def dummyfunc(): pass | |
| 27 | |
| 28 modulefilename = dummyfunc.func_code.co_filename | |
| 29 | |
| 30 def Interact(timeout = 50000000): # timeout after 10 days... | |
| 31 AE.AEInteractWithUser(timeout) | |
| 32 | |
| 33 | |
| 34 class PythonSlave(FrameWork.Application): | |
| 35 def __init__(self): | |
| 36 FrameWork.Application.__init__(self) | |
| 37 AE.AEInstallEventHandler('pyth', 'EXEC', ExecHandler) | |
| 38 AE.AEInstallEventHandler('aevt', 'quit', QuitHandler) | |
| 39 AE.AEInstallEventHandler('aevt', 'odoc', OpenDocumentHandler) | |
| 40 | |
| 41 def makeusermenus(self): | |
| 42 self.filemenu = m = FrameWork.Menu(self.menubar, "File") | |
| 43 self._quititem = FrameWork.MenuItem(m, "Quit", "Q", self._quit) | |
| 44 | |
| 45 def do_kHighLevelEvent(self, event): | |
| 46 (what, message, when, where, modifiers) = event | |
| 47 try: | |
| 48 AE.AEProcessAppleEvent(event) | |
| 49 except AE.Error, detail: | |
| 50 print "Apple Event was not handled, error:", detail | |
| 51 | |
| 52 def do_key(self, event): | |
| 53 (what, message, when, where, modifiers) = event | |
| 54 c = chr(message & charCodeMask) | |
| 55 if modifiers & cmdKey and c == '.': | |
| 56 return | |
| 57 FrameWork.Application.do_key(self, event) | |
| 58 | |
| 59 def idle(self, event): | |
| 60 Qd.InitCursor() | |
| 61 | |
| 62 def quit(self, *args): | |
| 63 raise self | |
| 64 | |
| 65 def getabouttext(self): | |
|
17845
fb31c33761c9
Fixed macroman<->latin1 conversion. Some chars don't
Jack Jansen <jack.jansen@cwi.nl>
parents:
9011
diff
changeset
|
66 return "About PythonSlave" |
| 9011 | 67 |
| 68 def do_about(self, id, item, window, event): | |
| 69 EasyDialogs.Message("PythonSlave " + __version__ + "\rCopyright © 1996, Letterror, JvR") | |
| 70 | |
| 71 | |
| 72 def ExecHandler(theAppleEvent, theReply): | |
| 73 parameters, args = aetools.unpackevent(theAppleEvent) | |
| 74 if parameters.has_key('----'): | |
| 75 if parameters.has_key('NAME'): | |
| 76 print '--- executing "' + parameters['NAME'] + '" ---' | |
| 77 else: | |
| 78 print '--- executing "<unknown>" ---' | |
| 79 stuff = parameters['----'] | |
| 80 MyExec(stuff + "\n") # execute input | |
| 81 print '--- done ---' | |
| 82 return 0 | |
| 83 | |
| 84 def MyExec(stuff): | |
| 85 stuff = string.splitfields(stuff, '\r') # convert return chars | |
| 86 stuff = string.joinfields(stuff, '\n') # to newline chars | |
| 87 Interact() | |
| 88 saveyield = MacOS.EnableAppswitch(1) | |
| 89 try: | |
| 90 exec stuff in {} | |
| 91 except: | |
| 92 MacOS.EnableAppswitch(saveyield) | |
| 93 traceback.print_exc() | |
| 94 MacOS.EnableAppswitch(saveyield) | |
| 95 | |
| 96 def OpenDocumentHandler(theAppleEvent, theReply): | |
| 97 parameters, args = aetools.unpackevent(theAppleEvent) | |
| 98 docs = parameters['----'] | |
| 99 if type(docs) <> ListType: | |
| 100 docs = [docs] | |
| 101 for doc in docs: | |
| 102 fss, a = doc.Resolve() | |
| 103 path = fss.as_pathname() | |
| 104 if path <> modulefilename: | |
| 105 MyExecFile(path) | |
| 106 return 0 | |
| 107 | |
| 108 def MyExecFile(path): | |
| 109 saveyield = MacOS.EnableAppswitch(1) | |
| 110 savewd = os.getcwd() | |
| 111 os.chdir(os.path.split(path)[0]) | |
| 112 print '--- Executing file "' + os.path.split(path)[1] + '"' | |
| 113 try: | |
| 114 execfile(path, {"__name__": "__main__"}) | |
| 115 except: | |
| 116 traceback.print_exc() | |
| 117 MacOS.EnableAppswitch(saveyield) | |
| 118 MacOS.EnableAppswitch(saveyield) | |
| 119 os.chdir(savewd) | |
| 120 print "--- done ---" | |
| 121 | |
| 122 def QuitHandler(theAppleEvent, theReply): | |
| 123 slave.quit() | |
| 124 return 0 | |
| 125 | |
| 126 | |
| 127 slave = PythonSlave() | |
| 128 print "PythonSlave", __version__, "ready." | |
| 129 slave.mainloop() |
