[ previous ] [ next ] [ threads ]
 To :  yate@v...
 From :  Maciek Kaminski <maciejka@t...>
 Subject :  yaypm
 Date :  Mon, 04 Jul 2005 10:29:31 +0200
YAYPM - Yet Another Yate Python Module.

It is based on Twisted asynchronous framework, allows to program in 
"Twisted style" with defereds and as a bonus, with little help of python 
generators, to "flatten" message loops efficiently. With twisted 
asynchronous implementation of network protocols it makes powerful 
prototyping tool. As an example here is a code sniplet that reads your 
email:

def attachAndWaitForNotify(yate, targetid, source):

    yield yate.dispatch(Message(
        type = "message", name = "chan.attach",
        attrs = {"userData": targetid,
                 "notify": targetid,
                 "source": source}))
    getResult()

    yield yate.onmsg("chan.notify", lambda m : m.attrs["targetid"] == 
targetid)
    yate.ret(getResult(), True)

def handle(yate, callid, targetid):

    logger.debug("imap.py")

    yield go(attachAndWaitForNotify(yate, targetid, 
"tts/voice_cstr_pl_em_diphone/Witaj w czytniku poczty elektronicznej"))
    getResult()   

    yield go(connect("mail.tiger.com.pl", "maciejka", "xxxxx"))
    imap = getResult()

    yield imap.examine('INBOX')
    getResult()

    yield go(getNewEnvelope(imap))
    envelopes = getResult()

    if(not envelopes):
        yield go(attachAndWaitForNotify(yate, targetid, 
"tts/voice_cstr_pl_em_diphone/Nie masz nowych wiadomos~ci"))
        getResult()
    else:
        yield go(attachAndWaitForNotify(yate, targetid, 
"tts/voice_cstr_pl_em_diphone/Liczba nowych wiadomos~ci %d" % 
len(envelopes)))
        getResult()
        no = 1
        for _, value in envelopes.iteritems():
            msg = value['ENVELOPE']

            x= "tts/voice_cstr_pl_em_diphone/Wiadomos~c~ numer %d 
Nadawca to %s Temat to %s" % ( no, msg[2][0][0], msg[1])

            yield defer.DeferredList([go(attachAndWaitForNotify(yate, 
targetid, x)),
                                     yate.onmsg("chan.dtmf", lambda m : 
m.attrs["id"] == callid and m.attrs["text"] == "#", autoreturn = True)],
                                     fireOnOneCallback = True)
            getResult()
            no = no + 1
       
    yield imap.logout()
    getResult()
             
    yield yate.dispatch(Message(type = "message", name = "call.drop", 
attrs = {"id": callid}))
    getResult()

Works both with rmodule and hardly with extmodule (twisted is a large 
library and it takes my laptop 1-2seconds to load it, so doing it on 
every call.execute is noticeable).

P.S.: It definitely needs more documentation. I promise to write it as 
soon as the code gets into cvs.
mk




‹