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

Skip to content

Commit 74b3f8a

Browse files
committed
Initial revision
1 parent dc4b93d commit 74b3f8a

5 files changed

Lines changed: 971 additions & 0 deletions

File tree

Demo/cwilib/cwilib.py

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
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()

Demo/cwilib/form.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# Fill-out form window
2+
3+
import stdwin
4+
from stdwinevents import *
5+
6+
7+
class Form:
8+
9+
def __init__(self, title):
10+
self.title = title
11+
self.window = None
12+
self.fields = {}
13+
self.fieldnames = []
14+
self.formwidth = self.formheight = 0
15+
self.focusname = None
16+
self.tefocus = None
17+
18+
def define_field(self, name, label, lines, chars):
19+
self.fieldnames.append(name)
20+
lh = stdwin.lineheight()
21+
cw = stdwin.textwidth('m')
22+
left = 20*cw
23+
top = self.formheight + 4
24+
right = left + chars*cw
25+
bottom = top + lines*lh
26+
te = None
27+
self.fields[name] = (label, left, top, right, bottom, te)
28+
self.formheight = bottom + 2
29+
self.formwidth = max(self.formwidth, right + 4)
30+
31+
def open(self):
32+
if self.window: return
33+
self.formwidth = max(100, self.formwidth)
34+
self.formheight = max(50, self.formheight)
35+
stdwin.setdefwinsize(self.formwidth, self.formheight)
36+
stdwin.setdefscrollbars(0, 0)
37+
self.window = stdwin.open(self.title)
38+
self.window.setdocsize(self.formwidth, self.formheight)
39+
for name in self.fieldnames:
40+
label, left, top, right, bottom, te = \
41+
self.fields[name]
42+
rect = (left, top), (right, bottom)
43+
te = self.window.textcreate(rect)
44+
te.setactive(0)
45+
te.setview(rect)
46+
self.fields[name] = \
47+
label, left, top, right, bottom, te
48+
if self.fieldnames:
49+
self.setfocus(self.fieldnames[0])
50+
51+
def setfocus(self, name):
52+
if name <> self.focusname and self.tefocus:
53+
self.tefocus.setactive(0)
54+
self.focusname = name
55+
if self.focusname:
56+
self.tefocus = self.fields[self.focusname][-1]
57+
self.tefocus.setactive(1)
58+
else:
59+
self.tefocus = None
60+
61+
def dispatch(self, type, detail):
62+
event = type, self.window, detail
63+
if type == WE_NULL:
64+
pass
65+
elif type == WE_DRAW:
66+
self.draw(detail)
67+
elif type == WE_MOUSE_DOWN:
68+
x, y = detail[0]
69+
for name in self.fieldnames:
70+
label, left, top, right, bottom, te = \
71+
self.fields[name]
72+
if left <= x < right and \
73+
top <= y < bottom:
74+
self.setfocus(name)
75+
break
76+
else:
77+
stdwin.fleep()
78+
return
79+
if self.tefocus:
80+
(left, top), (right, bottom) = \
81+
self.tefocus.getrect()
82+
if x < left: x = left
83+
if x >= right: x = right-1
84+
if y < top: y = top
85+
if y >= bottom:
86+
y = bottom-1
87+
x = right-1
88+
event = type, self.window, ((x,y),)+detail[1:]
89+
if not self.tefocus.event(event):
90+
stdwin.fleep()
91+
elif type in (WE_MOUSE_MOVE, WE_MOUSE_UP, WE_CHAR):
92+
if not self.tefocus or not self.tefocus.event(event):
93+
stdwin.fleep()
94+
elif type == WE_MOUSE_UP:
95+
button = detail[2]
96+
if button == 2:
97+
self.paste_selection()
98+
else:
99+
self.make_selection()
100+
elif type == WE_COMMAND:
101+
if detail in (WC_BACKSPACE, WC_UP, WC_DOWN,
102+
WC_LEFT, WC_RIGHT):
103+
if not self.tefocus or \
104+
not self.tefocus.event(event):
105+
stdwin.fleep()
106+
elif detail == WC_RETURN:
107+
print '*** Submit query'
108+
elif detail == WC_TAB:
109+
if not self.fields:
110+
stdwin.fleep()
111+
return
112+
if not self.focusname:
113+
i = 0
114+
else:
115+
i = self.fieldnames.index(
116+
self.focusname)
117+
i = (i+1) % len(self.fieldnames)
118+
self.setfocus(self.fieldnames[i])
119+
self.tefocus.setfocus(0, 0x7fff)
120+
self.make_selection()
121+
elif type in (WE_ACTIVATE, WE_DEACTIVATE):
122+
pass
123+
elif type == WE_LOST_SEL:
124+
if self.tefocus:
125+
a, b = self.tefocus.getfocus()
126+
self.tefocus.setfocus(a, a)
127+
else:
128+
print 'Form.dispatch(%d, %s)' % (type, `detail`)
129+
130+
def draw(self, detail):
131+
d = self.window.begindrawing()
132+
d.cliprect(detail)
133+
d.erase(detail)
134+
self.drawform(d, detail)
135+
d.noclip()
136+
d.close()
137+
# Stupid textedit objects can't draw with open draw object...
138+
self.drawtextedit(detail)
139+
140+
def drawform(self, d, detail):
141+
for name in self.fieldnames:
142+
label, left, top, right, bottom, te = self.fields[name]
143+
d.text((0, top), label)
144+
d.box((left-3, top-2), (right+4, bottom+2))
145+
146+
def drawtextedit(self, detail):
147+
for name in self.fieldnames:
148+
label, left, top, right, bottom, te = self.fields[name]
149+
te.draw(detail)
150+
151+
def make_selection(self):
152+
s = self.tefocus.getfocustext()
153+
if not s:
154+
return
155+
stdwin.rotatecutbuffers(1)
156+
stdwin.setcutbuffer(0, s)
157+
if not self.window.setselection(WS_PRIMARY, s):
158+
stdwin.fleep()
159+
160+
def paste_selection(self):
161+
if not self.tefocus:
162+
stdwin.fleep()
163+
return
164+
s = stdwin.getselection(WS_PRIMARY)
165+
if not s:
166+
s = stdwin.getcutbuffer(0)
167+
if not s:
168+
stdwin.fleep()
169+
return
170+
self.tefocus.replace(s)

0 commit comments

Comments
 (0)