Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 74901d5

Browse files
committed
Restructured main loop. Etc.
1 parent e36f735 commit 74901d5

1 file changed

Lines changed: 161 additions & 174 deletions

File tree

Demo/cwilib/cwilib.py

Lines changed: 161 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -12,215 +12,202 @@
1212
# Main program
1313

1414
def main():
15-
vt = vt100win.VT100win()
15+
vt = vt100win.VT100win()
16+
#
17+
host = 'biefstuk.cwi.nl'
18+
tn = telnetlib.Telnet(host, 0)
19+
#
20+
try:
21+
vt.send(tn.read_until('login: ', 10))
22+
tn.write('cwilib\r')
1623
#
17-
host = 'biefstuk.cwi.nl'
18-
port = 0
19-
timeout = 10.0
20-
tn = telnetlib.Telnet(host, port)
21-
tn.set_timeout(timeout)
24+
vt.send(tn.read_until('Hit <RETURN> to continue...', 10))
25+
tn.write('\r')
2226
#
27+
vt.send(tn.read_until('QUIT', 20))
28+
except EOFError:
29+
sys.stderr.write('Connection closed prematurely\n')
30+
sys.exit(1)
31+
#
32+
define_screens(vt)
33+
matches = vt.which_screens()
34+
if 'menu' not in matches:
35+
sys.stderr.write('Main menu does not appear\n')
36+
sys.exit(1)
37+
#
38+
tn.write('\r\r')
39+
vt.open('Progress -- CWI Library')
40+
vt.set_debuglevel(0)
41+
ui = UserInterface()
42+
#
43+
while 1:
2344
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'))
45+
data = tn.read_very_eager()
3146
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
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
10790
else:
108-
print '*** Weird return from select:', rfd, wfd, xfd
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], [], [])
10996

11097

11198
# Subroutine to define our screen recognition patterns
11299

113100
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-
})
101+
vt.define_screen('menu', {
102+
'title': ('search', 0, 0, 80,
103+
' SEARCH FUNCTIONS +OTHER FUNCTIONS '),
104+
})
105+
vt.define_screen('search', {
106+
'title': ('search', 0, 0, 80, ' Search '),
107+
})
108+
vt.define_screen('shortlist', {'title': ('search', 0, 0, 80,
109+
' Short-list')})
110+
vt.define_screen('showrecord', {
111+
'title': ('search', 0, 0, 80, ' Show record '),
112+
})
113+
vt.define_screen('timelimit', {
114+
'limit': ('search', 12, 0, 80, ' TIME LIMIT '),
115+
})
116+
vt.define_screen('attention', {
117+
'BASE': ('copy', 0, 0, 0, 'search'),
118+
'title': ('search', 10, 0, 80, ' ATTENTION ')})
119+
vt.define_screen('syntaxerror', {
120+
'BASE': ('copy', 0, 0, 0, 'attention'),
121+
'message': ('search', 12, 0, 80, ' Syntax error'),
122+
})
123+
vt.define_screen('emptyerror', {
124+
'BASE': ('copy', 0, 0, 0, 'attention'),
125+
'message': ('search', 12, 0, 80,
126+
' Check your input. Search at least one term'),
127+
})
128+
vt.define_screen('unsortedwarning', {
129+
'BASE': ('copy', 0, 0, 0, 'attention'),
130+
'message': ('search', 12, 0, 80,
131+
' Number of records exceeds sort limit'),
132+
})
133+
vt.define_screen('thereismore', {
134+
'BASE': ('copy', 0, 0, 0, 'showrecord'),
135+
'message': ('search', 15, 0, 80,
136+
'There is more within this record. Use the arrow keys'),
137+
})
138+
vt.define_screen('nofurther', {
139+
'BASE': ('copy', 0, 0, 0, 'showrecord'),
140+
'message': ('search', 17, 0, 80, 'You cannot go further\.'),
141+
})
142+
vt.define_screen('nofurtherback', {
143+
'BASE': ('copy', 0, 0, 0, 'showrecord'),
144+
'message': ('search', 17, 0, 80,
145+
'You cannot go further back'),
146+
})
160147

161148

162149
# Class to implement our user interface.
163150

164151
class UserInterface:
165152

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)
153+
def __init__(self):
154+
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)
178165

179-
def __del__(self):
180-
self.close()
166+
def __del__(self):
167+
self.close()
181168

182-
def close(self):
183-
for form in self.forms:
184-
form.close()
169+
def close(self):
170+
for form in self.forms:
171+
form.close()
185172

186-
def dispatch(self, type, window, detail):
187-
for form in self.forms:
188-
if window == form.window:
189-
form.dispatch(type, detail)
173+
def dispatch(self, type, window, detail):
174+
for form in self.forms:
175+
if window == form.window:
176+
form.dispatch(type, detail)
190177

191178

192179
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)
180+
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)
200187

201188

202189
class QueryForm(Form):
203190

204-
def __init__(self):
205-
Form.__init__(self, 'Query form -- CWI Library')
191+
def __init__(self):
192+
Form.__init__(self, 'Query form -- CWI Library')
206193

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)
194+
def dispatch(self, type, detail):
195+
if type == WE_COMMAND and detail == WC_RETURN:
196+
print '*** SUBMIT ***'
197+
else:
198+
Form.dispatch(self, type, detail)
212199

213200

214201
class ListForm(Form):
215202

216-
def __init__(self):
217-
Form.__init__(self, 'Short list -- CWI Library')
203+
def __init__(self):
204+
Form.__init__(self, 'Short list -- CWI Library')
218205

219206

220207
class RecordForm(Form):
221208

222-
def __init__(self):
223-
Form.__init__(self, 'Record detail -- CWI Library')
209+
def __init__(self):
210+
Form.__init__(self, 'Record detail -- CWI Library')
224211

225212

226213
main()

0 commit comments

Comments
 (0)