blob: 2b1963718a4ab0fd0af51af2673725de0a22c500 [file] [log] [blame]
Guido van Rossum74b3f8a1993-10-28 09:53:13 +00001# Interface to the interactive CWI library catalog.
2
3import sys
4import stdwin
5from stdwinevents import *
6import select
7import telnetlib
8import vt100win
9from form import Form
10
11
12# Main program
13
14def main():
Guido van Rossum74901d51993-11-01 14:50:59 +000015 vt = vt100win.VT100win()
16 #
17 host = 'biefstuk.cwi.nl'
18 tn = telnetlib.Telnet(host, 0)
19 #
20 try:
Guido van Rossum4117e541998-09-14 16:44:15 +000021 vt.send(tn.read_until('login: ', 10))
22 tn.write('cwilib\r')
23 #
24 vt.send(tn.read_until('Hit <RETURN> to continue...', 10))
25 tn.write('\r')
26 #
27 vt.send(tn.read_until('QUIT', 20))
Guido van Rossum74901d51993-11-01 14:50:59 +000028 except EOFError:
Guido van Rossum4117e541998-09-14 16:44:15 +000029 sys.stderr.write('Connection closed prematurely\n')
30 sys.exit(1)
Guido van Rossum74901d51993-11-01 14:50:59 +000031 #
32 define_screens(vt)
33 matches = vt.which_screens()
34 if 'menu' not in matches:
Guido van Rossum4117e541998-09-14 16:44:15 +000035 sys.stderr.write('Main menu does not appear\n')
36 sys.exit(1)
Guido van Rossum74901d51993-11-01 14:50:59 +000037 #
38 tn.write('\r\r')
39 vt.open('Progress -- CWI Library')
40 vt.set_debuglevel(0)
41 ui = UserInterface()
42 #
43 while 1:
Guido van Rossum4117e541998-09-14 16:44:15 +000044 try:
45 data = tn.read_very_eager()
46 except EOFError:
47 stdwin.message('Connection closed--goodbye')
48 break
49 if data:
50 print 'send...'
51 vt.send(data)
52 print 'send...done'
53 continue
54 event = stdwin.pollevent()
55 if event:
56 type, window, detail = event
57 if window == None and type == WE_LOST_SEL:
58 window = ui.queryform.window
59 event = type, window, detail
60 if type == WE_CLOSE:
61 break
62 if window in ui.windows:
63 ui.dispatch(type, window, detail)
64 elif window == vt.window:
65 if type == WE_NULL:
66 pass
67 elif type == WE_COMMAND:
68 if detail == WC_RETURN:
69 tn.write('\r')
70 elif detail == WC_BACKSPACE:
71 tn.write('\b')
72 elif detail == WC_TAB:
73 tn.write('\t')
74 elif detail == WC_UP:
75 tn.write('\033[A')
76 elif detail == WC_DOWN:
77 tn.write('\033[B')
78 elif detail == WC_RIGHT:
79 tn.write('\033[C')
80 elif detail == WC_LEFT:
81 tn.write('\033[D')
82 else:
83 print '*** Command:', detail
84 elif type == WE_CHAR:
85 tn.write(detail)
86 elif type == WE_DRAW:
87 vt.draw(detail)
88 elif type in (WE_ACTIVATE, WE_DEACTIVATE):
89 pass
90 else:
91 print '*** VT100 event:', type, detail
92 else:
93 print '*** Alien event:', type, window, detail
94 continue
95 rfd, wfd, xfd = select.select([tn, stdwin], [], [])
Guido van Rossum74b3f8a1993-10-28 09:53:13 +000096
97
98# Subroutine to define our screen recognition patterns
99
100def define_screens(vt):
Guido van Rossum74901d51993-11-01 14:50:59 +0000101 vt.define_screen('menu', {
Guido van Rossum4117e541998-09-14 16:44:15 +0000102 'title': ('search', 0, 0, 80,
103 ' SEARCH FUNCTIONS +OTHER FUNCTIONS '),
104 })
Guido van Rossum74901d51993-11-01 14:50:59 +0000105 vt.define_screen('search', {
Guido van Rossum4117e541998-09-14 16:44:15 +0000106 'title': ('search', 0, 0, 80, ' Search '),
107 })
Guido van Rossum74901d51993-11-01 14:50:59 +0000108 vt.define_screen('shortlist', {'title': ('search', 0, 0, 80,
Guido van Rossum4117e541998-09-14 16:44:15 +0000109 ' Short-list')})
Guido van Rossum74901d51993-11-01 14:50:59 +0000110 vt.define_screen('showrecord', {
Guido van Rossum4117e541998-09-14 16:44:15 +0000111 'title': ('search', 0, 0, 80, ' Show record '),
112 })
Guido van Rossum74901d51993-11-01 14:50:59 +0000113 vt.define_screen('timelimit', {
Guido van Rossum4117e541998-09-14 16:44:15 +0000114 'limit': ('search', 12, 0, 80, ' TIME LIMIT '),
115 })
Guido van Rossum74901d51993-11-01 14:50:59 +0000116 vt.define_screen('attention', {
Guido van Rossum4117e541998-09-14 16:44:15 +0000117 'BASE': ('copy', 0, 0, 0, 'search'),
118 'title': ('search', 10, 0, 80, ' ATTENTION ')})
Guido van Rossum74901d51993-11-01 14:50:59 +0000119 vt.define_screen('syntaxerror', {
Guido van Rossum4117e541998-09-14 16:44:15 +0000120 'BASE': ('copy', 0, 0, 0, 'attention'),
121 'message': ('search', 12, 0, 80, ' Syntax error'),
122 })
Guido van Rossum74901d51993-11-01 14:50:59 +0000123 vt.define_screen('emptyerror', {
Guido van Rossum4117e541998-09-14 16:44:15 +0000124 'BASE': ('copy', 0, 0, 0, 'attention'),
125 'message': ('search', 12, 0, 80,
126 ' Check your input. Search at least one term'),
127 })
Guido van Rossum74901d51993-11-01 14:50:59 +0000128 vt.define_screen('unsortedwarning', {
Guido van Rossum4117e541998-09-14 16:44:15 +0000129 'BASE': ('copy', 0, 0, 0, 'attention'),
130 'message': ('search', 12, 0, 80,
131 ' Number of records exceeds sort limit'),
132 })
Guido van Rossum74901d51993-11-01 14:50:59 +0000133 vt.define_screen('thereismore', {
Guido van Rossum4117e541998-09-14 16:44:15 +0000134 'BASE': ('copy', 0, 0, 0, 'showrecord'),
135 'message': ('search', 15, 0, 80,
136 'There is more within this record. Use the arrow keys'),
137 })
Guido van Rossum74901d51993-11-01 14:50:59 +0000138 vt.define_screen('nofurther', {
Guido van Rossum4117e541998-09-14 16:44:15 +0000139 'BASE': ('copy', 0, 0, 0, 'showrecord'),
140 'message': ('search', 17, 0, 80, 'You cannot go further\.'),
141 })
Guido van Rossum74901d51993-11-01 14:50:59 +0000142 vt.define_screen('nofurtherback', {
Guido van Rossum4117e541998-09-14 16:44:15 +0000143 'BASE': ('copy', 0, 0, 0, 'showrecord'),
144 'message': ('search', 17, 0, 80,
145 'You cannot go further back'),
146 })
Guido van Rossum74b3f8a1993-10-28 09:53:13 +0000147
148
149# Class to implement our user interface.
150
151class UserInterface:
152
Guido van Rossum74901d51993-11-01 14:50:59 +0000153 def __init__(self):
Guido van Rossum4117e541998-09-14 16:44:15 +0000154 stdwin.setfont('7x14')
155 self.queryform = QueryForm()
156 self.listform = ListForm()
157 self.recordform = RecordForm()
158 self.forms = [self.queryform, self.listform, self.recordform]
159 define_query_fields(self.queryform)
160 self.windows = []
161 for form in self.forms:
162 if form.formheight > 0:
163 form.open()
164 self.windows.append(form.window)
Guido van Rossum74b3f8a1993-10-28 09:53:13 +0000165
Guido van Rossum74901d51993-11-01 14:50:59 +0000166 def __del__(self):
Guido van Rossum4117e541998-09-14 16:44:15 +0000167 self.close()
Guido van Rossum74b3f8a1993-10-28 09:53:13 +0000168
Guido van Rossum74901d51993-11-01 14:50:59 +0000169 def close(self):
Guido van Rossum4117e541998-09-14 16:44:15 +0000170 for form in self.forms:
171 form.close()
Guido van Rossum74b3f8a1993-10-28 09:53:13 +0000172
Guido van Rossum74901d51993-11-01 14:50:59 +0000173 def dispatch(self, type, window, detail):
Guido van Rossum4117e541998-09-14 16:44:15 +0000174 for form in self.forms:
175 if window == form.window:
176 form.dispatch(type, detail)
Guido van Rossum74b3f8a1993-10-28 09:53:13 +0000177
178
179def define_query_fields(f):
Guido van Rossum74901d51993-11-01 14:50:59 +0000180 f.define_field('name', 'Name auth./ed.', 1, 60)
181 f.define_field('title', 'Title', 4, 60)
182 f.define_field('shelfmark', 'Shelf mark', 1, 60)
183 f.define_field('class', 'Prim. classif.', 1, 60)
184 f.define_field('series', 'Series', 1, 60)
185 f.define_field('congress', 'Congr. pl./year', 1, 60)
186 f.define_field('type', 'Type', 1, 60)
Guido van Rossum74b3f8a1993-10-28 09:53:13 +0000187
188
189class QueryForm(Form):
190
Guido van Rossum74901d51993-11-01 14:50:59 +0000191 def __init__(self):
Guido van Rossum4117e541998-09-14 16:44:15 +0000192 Form.__init__(self, 'Query form -- CWI Library')
Guido van Rossum74b3f8a1993-10-28 09:53:13 +0000193
Guido van Rossum74901d51993-11-01 14:50:59 +0000194 def dispatch(self, type, detail):
Guido van Rossum4117e541998-09-14 16:44:15 +0000195 if type == WE_COMMAND and detail == WC_RETURN:
196 print '*** SUBMIT ***'
197 else:
198 Form.dispatch(self, type, detail)
Guido van Rossum74b3f8a1993-10-28 09:53:13 +0000199
200
201class ListForm(Form):
202
Guido van Rossum74901d51993-11-01 14:50:59 +0000203 def __init__(self):
Guido van Rossum4117e541998-09-14 16:44:15 +0000204 Form.__init__(self, 'Short list -- CWI Library')
Guido van Rossum74b3f8a1993-10-28 09:53:13 +0000205
206
207class RecordForm(Form):
208
Guido van Rossum74901d51993-11-01 14:50:59 +0000209 def __init__(self):
Guido van Rossum4117e541998-09-14 16:44:15 +0000210 Form.__init__(self, 'Record detail -- CWI Library')
Guido van Rossum74b3f8a1993-10-28 09:53:13 +0000211
212
213main()