Added ref to Misc/NEWS file; added hint to run regen script on Linux.
[python/dscho.git] / Mac / Demo / example1 / InterslipControl-1.py
blob14fc59c86c94f90f9ac335314540c336af0227e6
1 """Sample program handling InterSLIP control and showing off EasyDialogs,
2 Res and Dlg in the process"""
4 import EasyDialogs
5 import Res
6 import Dlg
7 import sys
8 import interslip
10 # Definitions for our resources
11 ID_MAIN=512
13 ITEM_CONNECT=1
14 ITEM_DISCONNECT=2
15 ITEM_UPDATE=3
16 ITEM_QUIT=4
17 ITEM_STATUS=5
18 ITEM_MESSAGE=6
20 status2text = ["<idle>", "<wait-modem>", "<dialling>", "<logging in>",
21 "<connected>", "<disconnecting>"]
24 def main():
25 """Main routine: open resourcefile, open interslip, call dialog handler"""
26 try:
27 Res.OpenResFile("InterslipControl-1.rsrc")
28 except Res.Error, arg:
29 EasyDialogs.Message("Cannot open resource file InterslipControl-1.rsrc: "+
30 arg[1])
31 sys.exit(1)
32 try:
33 interslip.open()
34 except interslip.error, arg:
35 EasyDialogs.Message("Cannot open interslip: "+arg[1])
36 sys.exit(1)
37 do_dialog()
39 def do_dialog():
40 """Post dialog and handle user interaction until quit"""
41 my_dlg = Dlg.GetNewDialog(ID_MAIN, -1)
42 while 1:
43 n = Dlg.ModalDialog(None)
44 if n == ITEM_CONNECT:
45 do_connect()
46 elif n == ITEM_DISCONNECT:
47 do_disconnect()
48 elif n == ITEM_UPDATE:
49 status, msg = do_status()
51 # Convert status number to a text string
52 try:
53 txt = status2text[status]
54 except IndexError:
55 txt = "<unknown state %d>"%status
57 # Set the status text field
58 tp, h, rect = my_dlg.GetDialogItem(ITEM_STATUS)
59 Dlg.SetDialogItemText(h, txt)
61 # Set the message text field
62 tp, h, rect = my_dlg.GetDialogItem(ITEM_MESSAGE)
63 Dlg.SetDialogItemText(h, msg)
64 elif n == ITEM_QUIT:
65 break
67 def do_connect():
68 """Connect, posting error message in case of failure"""
69 try:
70 interslip.connect()
71 except interslip.error, arg:
72 EasyDialogs.Message("Cannot connect: "+arg[1])
74 def do_disconnect():
75 """Disconnect, posting error message in case of failure"""
76 try:
77 interslip.disconnect()
78 except interslip.error, arg:
79 EasyDialogs.Message("Cannot disconnect: "+arg[1])
81 def do_status():
82 """Get status as (state_index, message),
83 posting error message in case of failure"""
84 try:
85 status, msgnum, msg = interslip.status()
86 except interslip.error, arg:
87 EasyDialogs.Message("Cannot get status: "+arg[1])
88 return 0, ''
89 return status, msg
92 main()