2 # Simple Apple-event driven Python interpreter
5 import os
, sys
, traceback
6 from cStringIO
import StringIO
7 from MiniAEFrame
import AEServer
, MiniApplication
9 class PythonServer(AEServer
, MiniApplication
):
12 MiniApplication
.__init
__(self
)
13 AEServer
.__init
__(self
)
14 self
.installaehandler('aevt', 'oapp', ignore
)
15 self
.installaehandler('aevt', 'quit', quit
)
16 self
.installaehandler('misc', 'dosc', doscript
)
25 def doscript(args
, **kwds
):
26 print "doscript:", repr(args
) ###
30 #print "Normalising args" ###
31 if type(args
) == type(""):
33 #print "Setting sys.argv" ###
35 #print "Finding script directory and module file" ###
36 dir = os
.path
.dirname(args
[0])
37 dir = os
.path
.join(start_dir
, dir)
38 pyfile
= os
.path
.basename(args
[0])
39 mod
= os
.path
.splitext(pyfile
)[0]
40 #print "dir:", repr(dir) ###
41 #print "mod:", repr(mod) ###
43 sys
.path
= start_path
[:]
45 #print "path:", sys.path ###
47 sys
.stdout
= StringIO()
48 sys
.stderr
= StringIO()
50 #sys.__stdout__.write("Path: %s\n" % sys.path) ###
51 #sys.__stdout__.write("Importing: %s\n" % mod) ###
54 except KeyboardInterrupt:
56 except SystemExit, exc
:
57 #sys.__stdout__.write("Caught a SystemExit\n") ###
62 #sys.__stdout__.write("stat = %s\n" % stat) ###
66 #sys.__stdout__.write("Done the import\n") ###
68 output
= sys
.stdout
.getvalue()
69 #sys.__stdout__.write("Output:\n%s" % output) ###
70 errput
= sys
.stderr
.getvalue()
72 sys
.stdout
= sys
.__stdout
__
73 sys
.stderr
= sys
.__stdout
__
75 return [stat
, output
, errput
]
77 start_dir
= os
.getcwd()
78 start_path
= sys
.path
[:]
79 server
= PythonServer()
80 #print "Open for business"