[Twisted-web] [Nevow] JavaScript code
Werner Thie
wthie at thiengineering.ch
Mon Jul 31 03:05:36 CDT 2006
Hi all
May I chime in with a question as a newbie?
I get the feeling from this particular thread that I am completely on the
wrong course with my experiments ( http://telinfo.thie.ch ) on hooking
together my Swiss telephonebook library with a 'live' app in the browser
window. Mainly because I am using nevow 0.8, MochiKit 1.3.1 and athena for
the implementation (Safari was never considered to work with this code,
although it would be nice to have it on board).
If I am wrong in mixing these components, could someone please advise what
I am supposed to change in my code for not getting some nasty surprises in
the future when there are updates to nevow?
I like the TechnoTurkeys adventures a lot and would like to provide some
help although I doubt that I have enough knowledge to help out with more
than some light testing.
Thxs,
Werner Thie
Experience is what you get, when you expected something else.
Server side code of telinfo.thie.ch attached for reference
---------------------------------------------------------------
# telinfoweb.py - web service functionality test of the PYTHON telinfo
package
#
# author : wthie, wth
# last edit: 22.05.2005
# modhistory:
# 28.04.2005 - wth, created
# 22.05.2005 - wth, fiddled with result and status delivery, could do a
whole
# lot more, but we leave it at that for the moment
import sys
import os
import simple_json
if '2.3' in sys.version:
path = os.path.abspath('./python23/telinfo')
if '2.4' in sys.version:
path = os.path.abspath('./python24/telinfo')
sys.path.append(path)
import telinfo
tcdpath = 'C:\\Program Files\\telcd\\teldata\\data'
verz = telinfo.SM_TELVERZ | telinfo.SM_PRIVAT | telinfo.SM_PROF
from zope.interface import implements, Interface
from twisted.python.components import registerAdapter
from nevow import athena, inevow, loaders, tags, util, static
class ITelinfoweb(Interface):
def Search():
pass
def SearchExtended():
pass
class Telinfoweb(object):
def __init__(self):
self.limit = 15
self.result = ''
def DoSearchPattern(self, pb, pattern):
pat = pattern.replace(u'\xf6', 'o')
pat = pat.replace(u'\xfc', 'u')
pat = pat.replace(u'\xe4', 'a')
pat = pat.replace(u'\xef', 'i')
pat = pat.replace(u'\xe7', 'c')
pat = pat.replace(u'\xe8', 'e')
pat = pat.replace(u'\xe9', 'e')
srchpat = pat.split('%')
for i in range(0, len(srchpat)-1, 2):
tagname = srchpat[i]
spat = srchpat[i+1]
if (len(spat) > 0):
tag = telinfo.ILLEGAL_SEARCH
if (tagname == 'name'):
tag = telinfo.NAMESEARCH
elif (tagname == 'vorname'):
tag = telinfo.FNAMESEARCH
elif (tagname == 'strasse'):
tag = telinfo.STREETSEARCH
elif (tagname == 'ort'):
tag = telinfo.ORTSEARCH
elif (tagname == 'beruf'):
tag = telinfo.BERUFSEARCH
elif (tagname == 'ledig'):
tag = telinfo.LEDIGSEARCH
elif (tagname == 'zusatz'):
tag = telinfo.ZUSATZSEARCH
if (tag != telinfo.ILLEGAL_SEARCH):
pb.Search(tag, spat)
return
def BuildResult(self, pb):
cnt = pb.GetElemCount()
d = {}
d['count'] = str(cnt)
d['limit'] = str(self.limit)
d['columns'] = ['rvorname', 'rname', 'rstrasse', 'rnr', 'rtelno',
'rplz', 'rort' ]
d['rows'] = []
if cnt > 0:
if cnt > self.limit: #limited to some value
cnt = self.limit
i = 0
while (i < cnt):
row = []
row.append(unicode(pb.RetrieveField(i, telinfo.VORNAMETAG),
'iso-8859-1'))
row.append(unicode(pb.RetrieveField(i, telinfo.NAMETAG),
'iso-8859-1'))
row.append(unicode(pb.RetrieveField(i, telinfo.STRASSETAG),
'iso-8859-1'))
row.append(unicode(pb.RetrieveField(i, telinfo.HAUSNUMMERTAG),
'iso-8859-1'))
row.append(unicode(pb.RetrieveField(i, telinfo.RUFNUMMERTAG),
'iso-8859-1'))
row.append(unicode(pb.RetrieveField(i, telinfo.PLZTAG), 'iso-8859-1'))
row.append(unicode(pb.RetrieveField(i, telinfo.ORTTAG), 'iso-8859-1'))
d['rows'].append(row)
i = i + 1
pb.DropAll()
return unicode(simple_json.dumps(d))
def Search(self, pattern):
pb = telinfo.pypbdb(tcdpath, tcdpath, 20, 0)
pb.OpenSearchSimple(0, verz, '')
self.DoSearchPattern(pb, pattern)
pb.CloseSearch()
return self.BuildResult(pb)
def SearchExtended(self, pattern):
pb = telinfo.pypbdb(tcdpath, tcdpath, 20, 0)
pb.OpenSearchSimple(1, verz, '')
self.DoSearchPattern(pb, pattern)
pb.CloseSearch()
return self.BuildResult(pb)
class TelinfowebResource(athena.LivePage):
"""
telinfo in its web incarnation
All buttons presses in the browser are sent to the server. The server
evaluates the expression and sets the output in the browser.
"""
addSlash = True
#child_html = static.File('html') #this trick did not work out with
topstyles preview feature
#will contact the author about
that problem
child_images = static.File('images')
child_scripts = static.File('scripts')
child_css = static.File('css')
docFactory = loaders.xmlfile(util.resource_filename('telinfoweb',
'html/telinfoweb.html'))
if __name__ == '__main__':
if True:
from twisted.internet import reactor
from twisted.python import log
from nevow import appserver
def TelinfowebResourceFactory(original):
return TelinfowebResource(ITelinfoweb, original)
registerAdapter(TelinfowebResourceFactory, Telinfoweb, inevow.IResource)
log.startLogging(sys.stdout)
site = appserver.NevowSite(Telinfoweb())
reactor.listenTCP(80, site)
reactor.run()
else:
tstpb = Telinfoweb()
tstpb.SearchExtended('name%Thie%vorname%W*%')
More information about the Twisted-web
mailing list