Guido van Rossum | 74b3f8a | 1993-10-28 09:53:13 +0000 | [diff] [blame^] | 1 | # Interface to the interactive CWI library catalog. |
| 2 | |
| 3 | import sys |
| 4 | import stdwin |
| 5 | from stdwinevents import * |
| 6 | import select |
| 7 | import telnetlib |
| 8 | import vt100win |
| 9 | from form import Form |
| 10 | |
| 11 | |
| 12 | # Main program |
| 13 | |
| 14 | def main(): |
| 15 | vt = vt100win.VT100win() |
| 16 | # |
| 17 | host = 'biefstuk.cwi.nl' |
| 18 | port = 0 |
| 19 | timeout = 10.0 |
| 20 | tn = telnetlib.Telnet(host, port) |
| 21 | tn.set_timeout(timeout) |
| 22 | # |
| 23 | try: |
| 24 | vt.send(tn.read_until('login: ')) |
| 25 | tn.write('cwilib\r') |
| 26 | # |
| 27 | vt.send(tn.read_until('Hit <RETURN> to continue...')) |
| 28 | tn.write('\r') |
| 29 | # |
| 30 | vt.send(tn.read_until('QUIT')) |
| 31 | except EOFError: |
| 32 | sys.stderr.write('Connection closed prematurely\n') |
| 33 | sys.exit(1) |
| 34 | # |
| 35 | define_screens(vt) |
| 36 | matches = vt.which_screens() |
| 37 | if 'menu' not in matches: |
| 38 | sys.stderr.write('No main menu within %g seconds\n' % timeout) |
| 39 | sys.exit(1) |
| 40 | # |
| 41 | tn.set_timeout(0) |
| 42 | tn.write('\r\r') |
| 43 | vt.open('Progress -- CWI Library') |
| 44 | ui = UserInterface() |
| 45 | # |
| 46 | while 1: |
| 47 | event = stdwin.pollevent() |
| 48 | if not event: |
| 49 | rfd, wfd, xfd = select.select([stdwin, tn], [], []) |
| 50 | if stdwin in rfd: |
| 51 | event = stdwin.getevent() |
| 52 | if event: |
| 53 | type, window, detail = event |
| 54 | if window == None and type == WE_LOST_SEL: |
| 55 | window = ui.queryform.window |
| 56 | event = type, window, detail |
| 57 | if type == WE_CLOSE: |
| 58 | break |
| 59 | if window in ui.windows: |
| 60 | ui.dispatch(type, window, detail) |
| 61 | elif window == vt.window: |
| 62 | if type == WE_NULL: |
| 63 | pass |
| 64 | elif type == WE_COMMAND: |
| 65 | if detail == WC_RETURN: |
| 66 | tn.write('\r') |
| 67 | elif detail == WC_BACKSPACE: |
| 68 | tn.write('\b') |
| 69 | elif detail == WC_TAB: |
| 70 | tn.write('\t') |
| 71 | elif detail == WC_UP: |
| 72 | tn.write('\033[A') |
| 73 | elif detail == WC_DOWN: |
| 74 | tn.write('\033[B') |
| 75 | elif detail == WC_RIGHT: |
| 76 | tn.write('\033[C') |
| 77 | elif detail == WC_LEFT: |
| 78 | tn.write('\033[D') |
| 79 | else: |
| 80 | print '*** Command:', detail |
| 81 | elif type == WE_CHAR: |
| 82 | tn.write(detail) |
| 83 | elif type == WE_DRAW: |
| 84 | vt.draw(detail) |
| 85 | elif type in (WE_ACTIVATE, WE_DEACTIVATE): |
| 86 | pass |
| 87 | else: |
| 88 | print '*** VT100 event:', type, detail |
| 89 | else: |
| 90 | print '*** Alien event:', type, window, detail |
| 91 | elif tn in rfd: |
| 92 | vt.window.setwincursor('watch') |
| 93 | try: |
| 94 | data = tn.read_now() |
| 95 | except EOFError: |
| 96 | stdwin.message('Connection closed--goodbye') |
| 97 | break |
| 98 | print 'send...' |
| 99 | vt.send(data) |
| 100 | print 'send...done' |
| 101 | vt.window.setwincursor('arrow') |
| 102 | matches = vt.which_screens() |
| 103 | if 'timelimit' in matches: |
| 104 | stdwin.message('Time limit--goodbye') |
| 105 | break |
| 106 | print '*** Matches:', matches |
| 107 | else: |
| 108 | print '*** Weird return from select:', rfd, wfd, xfd |
| 109 | |
| 110 | |
| 111 | # Subroutine to define our screen recognition patterns |
| 112 | |
| 113 | def define_screens(vt): |
| 114 | vt.define_screen('menu', { |
| 115 | 'title': ('search', 0, 0, 80, |
| 116 | ' SEARCH FUNCTIONS +OTHER FUNCTIONS '), |
| 117 | }) |
| 118 | vt.define_screen('search', { |
| 119 | 'title': ('search', 0, 0, 80, ' Search '), |
| 120 | }) |
| 121 | vt.define_screen('shortlist', {'title': ('search', 0, 0, 80, |
| 122 | ' Short-list')}) |
| 123 | vt.define_screen('showrecord', { |
| 124 | 'title': ('search', 0, 0, 80, ' Show record '), |
| 125 | }) |
| 126 | vt.define_screen('timelimit', { |
| 127 | 'limit': ('search', 12, 0, 80, ' TIME LIMIT '), |
| 128 | }) |
| 129 | vt.define_screen('attention', { |
| 130 | 'BASE': ('copy', 0, 0, 0, 'search'), |
| 131 | 'title': ('search', 10, 0, 80, ' ATTENTION ')}) |
| 132 | vt.define_screen('syntaxerror', { |
| 133 | 'BASE': ('copy', 0, 0, 0, 'attention'), |
| 134 | 'message': ('search', 12, 0, 80, ' Syntax error'), |
| 135 | }) |
| 136 | vt.define_screen('emptyerror', { |
| 137 | 'BASE': ('copy', 0, 0, 0, 'attention'), |
| 138 | 'message': ('search', 12, 0, 80, |
| 139 | ' Check your input. Search at least one term'), |
| 140 | }) |
| 141 | vt.define_screen('unsortedwarning', { |
| 142 | 'BASE': ('copy', 0, 0, 0, 'attention'), |
| 143 | 'message': ('search', 12, 0, 80, |
| 144 | ' Number of records exceeds sort limit'), |
| 145 | }) |
| 146 | vt.define_screen('thereismore', { |
| 147 | 'BASE': ('copy', 0, 0, 0, 'showrecord'), |
| 148 | 'message': ('search', 15, 0, 80, |
| 149 | 'There is more within this record. Use the arrow keys'), |
| 150 | }) |
| 151 | vt.define_screen('nofurther', { |
| 152 | 'BASE': ('copy', 0, 0, 0, 'showrecord'), |
| 153 | 'message': ('search', 17, 0, 80, 'You cannot go further\.'), |
| 154 | }) |
| 155 | vt.define_screen('nofurtherback', { |
| 156 | 'BASE': ('copy', 0, 0, 0, 'showrecord'), |
| 157 | 'message': ('search', 17, 0, 80, |
| 158 | 'You cannot go further back'), |
| 159 | }) |
| 160 | |
| 161 | |
| 162 | # Class to implement our user interface. |
| 163 | |
| 164 | class UserInterface: |
| 165 | |
| 166 | def __init__(self): |
| 167 | stdwin.setfont('7x14') |
| 168 | self.queryform = QueryForm() |
| 169 | self.listform = ListForm() |
| 170 | self.recordform = RecordForm() |
| 171 | self.forms = [self.queryform, self.listform, self.recordform] |
| 172 | define_query_fields(self.queryform) |
| 173 | self.windows = [] |
| 174 | for form in self.forms: |
| 175 | if form.formheight > 0: |
| 176 | form.open() |
| 177 | self.windows.append(form.window) |
| 178 | |
| 179 | def __del__(self): |
| 180 | self.close() |
| 181 | |
| 182 | def close(self): |
| 183 | for form in self.forms: |
| 184 | form.close() |
| 185 | |
| 186 | def dispatch(self, type, window, detail): |
| 187 | for form in self.forms: |
| 188 | if window == form.window: |
| 189 | form.dispatch(type, detail) |
| 190 | |
| 191 | |
| 192 | def define_query_fields(f): |
| 193 | f.define_field('name', 'Name auth./ed.', 1, 60) |
| 194 | f.define_field('title', 'Title', 4, 60) |
| 195 | f.define_field('shelfmark', 'Shelf mark', 1, 60) |
| 196 | f.define_field('class', 'Prim. classif.', 1, 60) |
| 197 | f.define_field('series', 'Series', 1, 60) |
| 198 | f.define_field('congress', 'Congr. pl./year', 1, 60) |
| 199 | f.define_field('type', 'Type', 1, 60) |
| 200 | |
| 201 | |
| 202 | class QueryForm(Form): |
| 203 | |
| 204 | def __init__(self): |
| 205 | Form.__init__(self, 'Query form -- CWI Library') |
| 206 | |
| 207 | def dispatch(self, type, detail): |
| 208 | if type == WE_COMMAND and detail == WC_RETURN: |
| 209 | print '*** SUBMIT ***' |
| 210 | else: |
| 211 | Form.dispatch(self, type, detail) |
| 212 | |
| 213 | |
| 214 | class ListForm(Form): |
| 215 | |
| 216 | def __init__(self): |
| 217 | Form.__init__(self, 'Short list -- CWI Library') |
| 218 | |
| 219 | |
| 220 | class RecordForm(Form): |
| 221 | |
| 222 | def __init__(self): |
| 223 | Form.__init__(self, 'Record detail -- CWI Library') |
| 224 | |
| 225 | |
| 226 | main() |