PythonSlave.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Mac » Contrib » BBPy » 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 » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Mac » Contrib » BBPy » PythonSlave.py
"""PythonSlave.py
An application that responds to three types of apple event: 
  'pyth'/'EXEC':   execute direct parameter as Python
  'aevt', 'quit':  quit
  'aevt', 'odoc':  perform python scripts

Copyright  1996, Just van Rossum, Letterror
"""

__version__ = "0.1.3"

import FrameWork
import sys
import traceback
import aetools
import string
import AE
import EasyDialogs
import os
import Qd
from Types import *
from Events import charCodeMask,cmdKey
import MacOS
import Evt

def dummyfunc(): pass

modulefilename = dummyfunc.func_code.co_filename

def Interact(timeout = 50000000):      # timeout after 10 days...
  AE.AEInteractWithUser(timeout)


class PythonSlave(FrameWork.Application):
  def __init__(self):
    FrameWork.Application.__init__(self)
    AE.AEInstallEventHandler('pyth', 'EXEC', ExecHandler)
    AE.AEInstallEventHandler('aevt', 'quit', QuitHandler)
    AE.AEInstallEventHandler('aevt', 'odoc', OpenDocumentHandler)
  
  def makeusermenus(self):
    self.filemenu = m = FrameWork.Menu(self.menubar, "File")
    self._quititem = FrameWork.MenuItem(m, "Quit", "Q", self._quit)
  
  def do_kHighLevelEvent(self, event):
    (what, message, when, where, modifiers) = event
    try:
      AE.AEProcessAppleEvent(event)
    except AE.Error, detail:
      print "Apple Event was not handled, error:", detail
  
  def do_key(self, event):
    (what, message, when, where, modifiers) = event
    c = chr(message & charCodeMask)
    if modifiers & cmdKey and c == '.':
      return
    FrameWork.Application.do_key(self, event)
  
  def idle(self, event):
    Qd.InitCursor()
  
  def quit(self, *args):
    raise self
  
  def getabouttext(self):
    return "About PythonSlave"
  
  def do_about(self, id, item, window, event):
    EasyDialogs.Message("PythonSlave " + __version__ + "\rCopyright  1996, Letterror, JvR")
  

def ExecHandler(theAppleEvent, theReply):
  parameters, args = aetools.unpackevent(theAppleEvent)
  if parameters.has_key('----'):
    if parameters.has_key('NAME'):
      print '--- executing "' + parameters['NAME'] + '" ---'
    else:
      print '--- executing "<unknown>" ---'
    stuff = parameters['----']
    MyExec(stuff + "\n")      # execute input
    print '--- done ---'
  return 0

def MyExec(stuff):
  stuff = string.splitfields(stuff, '\r')  # convert return chars
  stuff = string.joinfields(stuff, '\n')  # to newline chars
  Interact()
  saveyield = MacOS.EnableAppswitch(1)
  try:
    exec stuff in {}
  except:
    MacOS.EnableAppswitch(saveyield)
    traceback.print_exc()
  MacOS.EnableAppswitch(saveyield)

def OpenDocumentHandler(theAppleEvent, theReply):
  parameters, args = aetools.unpackevent(theAppleEvent)
  docs = parameters['----']
  if type(docs) <> ListType:
    docs = [docs]
  for doc in docs:
    fss, a = doc.Resolve()
    path = fss.as_pathname()
    if path <> modulefilename:
      MyExecFile(path)
  return 0

def MyExecFile(path):
  saveyield = MacOS.EnableAppswitch(1)
  savewd = os.getcwd()
  os.chdir(os.path.split(path)[0])
  print '--- Executing file "' + os.path.split(path)[1] + '"'
  try:
    execfile(path, {"__name__": "__main__"})
  except:
    traceback.print_exc()
    MacOS.EnableAppswitch(saveyield)
  MacOS.EnableAppswitch(saveyield)
  os.chdir(savewd)
  print "--- done ---"

def QuitHandler(theAppleEvent, theReply):
  slave.quit()
  return 0


slave = PythonSlave()
print "PythonSlave", __version__, "ready."
slave.mainloop()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.