The latest version of this document can be found at www.broad.ology.org.uk/amiga/proaction/01_Skeleton_Application.html
1: #!python 2: # 3: ################################################################ 4: # 5: # Anatomy of a ProAction Application. 6: # 7: # 01_Skeleton_Application.py 8: # 9: # Introducing our main functions. 10: # Opening our Window 11: # Handling Basic Input 12: # 13: ################################################################ 14: # 15: # 16: 17: import sys 18: import os 19: import arexx 20: 21: class Application: 22: 23: def __init__(self): 24: 25: self.PORTNAME = "TestScript" 26: self.UNIQUE = True 27: 28: # 29: # HandleInput() receives commands at the ARexx port and acts accordingly. 30: # They may be commands from ProAction (the CLOSE command in this example) 31: # Or they maybe commands from other ARexx based scripts etc. 32: # 33: 34: def HandleInput(pyport,guikey): 35: 36: global app 37: 38: # Loop until die becomes non zero 39: die = 0 40: while die == 0: 41: # Wait for a message to arrive at our Port 42: pyport.wait() 43: # Get the first message (but there may be more than one) 44: msg = pyport.getmsg() 45: # Loop until all queued messages are processed 46: while msg: 47: cmd = msg.msg 48: msg.reply() 49: if cmd == "QUIT": 50: die = 1 51: break 52: elif cmd[:5] == "CLOSE": 53: # Window Close Button Pressed 54: die = 1 55: break 56: else: 57: print cmd 58: msg = pyport.getmsg() 59: 60: 61: def DoGUI(pubscreen): 62: 63: global app 64: 65: 66: # Our script needs an incoming ARexx port to receive messages from 67: # ProAction. We create this using the arexx modules Port method 68: # Which returns a Port object 69: 70: 71: 72: pyport = arexx.Port(app.PORTNAME) 73: if pyport: 74: if app.UNIQUE: 75: if pyport.name != app.PORTNAME: 76: ErrorExit("ARexx Port " + app.PORTNAME + " already Exists!") 77: 78: # First we'll build our window.class tagslist 79: # Python can't use send StemVars at the moment so we'll 80: # use the TAGSTRING tecnique throughout. 81: 82: wintags = "" 83: wintags += "WA_Width,300," 84: wintags += "WA_Height,100," 85: wintags += "WA_DragBar,1," 86: wintags += "WA_DepthGadget,1," 87: wintags += "WA_SizeGadget,1," 88: wintags += "WA_CloseGadget,1," 89: wintags += "WA_Title,"+ sys.argv[0] + "," 90: wintags += "WA_PubScreenFallBack,1," 91: wintags += "WA_PubScreenName," + pubscreen + "," 92: wintags += "WINDOW_Position,WPOS_CENTERSCREEN," 93: wintags += "WA_Activate,1," 94: wintags += "TAG_DONE" 95: 96: (rc,rc2,guikey) = arexx.dorexx("PROACTION","CREATEGUI PORTNAME " + pyport.name + " TAGSTRING "" + wintags + """) 97: if rc == 0: 98: # later we will build our GUI here; 99: 100: # Open our Window. 101: 102: (rc,rc2,result) = arexx.dorexx("PROACTION","OPENGUIWINDOW GUIID " + guikey) 103: 104: # Handle events at the ARexx port. 105: HandleInput(pyport,guikey) 106: 107: # All done now shutdown the GUI 108: 109: arexx.dorexx("PROACTION","DESTROYGUI GUIID " + guikey) 110: else: # pyport 111: ErrorExit("Couldn't create our ARexx port") 112: 113: # 114: # ErrorExit() throws up an RequestChoice notification to indicate 115: # a critical error. 116: # 117: 118: def ErrorExit(msg): 119: # Build our command as a string. 120: # We can use sys.argv[0] for the script name to make thsi more reusable. 121: command = "RequestChoice TYPE "ERROR" "" + sys.argv[0] + "" "" + msg + "" "OK" >NIL:" 122: # Call the command via the os.system() 123: os.system(command) 124: exit() 125: 126: # 127: # The support GetPorts() returns a list of public MsgPorts 128: # as a string 129: # 130: 131: 132: def GetPorts(): 133: # Depending on the version of python we may have a useful 134: # function that retuns just what we need, so we use the try 135: # except syntax to test. 136: 137: try: 138: ports = os.getports() 139: ports = " ".join(ports) 140: except AttributeError: 141: # The getports function wasn't present so we call the REXX 142: # equivalent. 143: (rc,rc2,ports) = arexx.dorexx("REXX","return show('P')") 144: if rc != 0: 145: # We couldn't even call REXX somwthing serioiusly up here 146: # Bail out with a requester. 147: ErrorExit("Couldn't Find ARexx!") 148: return ports 149: 150: 151: # Now we need to check that ProAction is present an available to us, 152: # So first get a list of active ports 153: 154: ports = GetPorts() 155: 156: # Then check for ProActions port which will be "PROACTION". 157: # Port names are case sensitive. 158: 159: if -1 == ports.find("PROACTION"): 160: # No ProAction start it 161: os.system("RUN >"T:proactionpid" *>NIL: APPDIR:PROACTION") 162: os.system("C:WaitForPort PROACTION") 163: # Now check again 164: ports = GetPorts() 165: if -1 == ports.find("PROACTION"): 166: # Still not there :-( 167: ErrorExit("Unable to start or find ProAction GUIServer") 168: 169: 170: # Okay we should be good to go now 171: # Initialise our aplication 172: 173: app = Application(); 174: 175: DoGUI("Workbench") 176: