misc of python pep8 fixes
This commit is contained in:
parent
39200cd13b
commit
b0e86e65cf
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
"""
|
||||
FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
||||
Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
|
||||
|
||||
|
@ -31,6 +31,7 @@ from twisted.internet import reactor, defer
|
|||
from twisted.internet.protocol import ClientFactory
|
||||
import freepy
|
||||
|
||||
|
||||
class FsHelper(ClientFactory):
|
||||
|
||||
def __init__(self, host=None, passwd=None, port=None):
|
||||
|
@ -39,17 +40,17 @@ class FsHelper(ClientFactory):
|
|||
if passwd:
|
||||
self.passwd = passwd
|
||||
if port:
|
||||
self.port = port
|
||||
|
||||
self.port = port
|
||||
|
||||
self.freepyd = None
|
||||
self.connection_deferred = None
|
||||
|
||||
def reset(self):
|
||||
self.freepyd = None
|
||||
self.connection_deferred = None
|
||||
|
||||
|
||||
def connect(self):
|
||||
|
||||
|
||||
if self.freepyd:
|
||||
# if we have a protocol object, we are connected (since we always
|
||||
# null it upon any disconnection)
|
||||
|
@ -58,7 +59,7 @@ class FsHelper(ClientFactory):
|
|||
if self.connection_deferred:
|
||||
# we are already connecting, return existing dfrd
|
||||
return self.connection_deferred
|
||||
|
||||
|
||||
self.connection_deferred = defer.Deferred()
|
||||
self.connection_deferred.addCallback(self.dologin)
|
||||
self.connection_deferred.addErrback(self.generalError)
|
||||
|
@ -72,29 +73,28 @@ class FsHelper(ClientFactory):
|
|||
self.connection_deferred = None
|
||||
deferred2callback.callback("Connected")
|
||||
|
||||
|
||||
def generalError(self, failure):
|
||||
print "General error: %s" % failure
|
||||
return failure
|
||||
|
||||
def startedConnecting(self, connector):
|
||||
pass
|
||||
|
||||
|
||||
def buildProtocol(self, addr):
|
||||
return freepy.FreepyDispatcher(self.conncb, self.discocb)
|
||||
|
||||
|
||||
def clientConnectionLost(self, connector, reason):
|
||||
print "clientConnectionLost! conn=%s, reason=%s" % (connector,
|
||||
reason)
|
||||
self.connection_deferred = None
|
||||
self.connection_deferred = None
|
||||
self.freepyd = None
|
||||
|
||||
|
||||
def clientConnectionFailed(self, connector, reason):
|
||||
print "clientConnectionFailed! conn=%s, reason=%s" % (connector,
|
||||
reason)
|
||||
self.freepyd = None
|
||||
deferred2callback = self.connection_deferred
|
||||
self.connection_deferred = None
|
||||
self.connection_deferred = None
|
||||
deferred2callback.errback(reason)
|
||||
|
||||
def discocb(self, reason):
|
||||
|
@ -102,7 +102,7 @@ class FsHelper(ClientFactory):
|
|||
self.freepyd = None
|
||||
|
||||
def dologin(self, connectmsg):
|
||||
return self.freepyd.login(self.passwd)
|
||||
return self.freepyd.login(self.passwd)
|
||||
|
||||
def originate(self, party2dial, dest_ext_app, bgapi=True):
|
||||
"""
|
||||
|
@ -128,9 +128,6 @@ class FsHelper(ClientFactory):
|
|||
d.addCallback(originate_inner)
|
||||
return d
|
||||
|
||||
|
||||
|
||||
|
||||
def dialconf(self, people2dial, conf_name, bgapi=True):
|
||||
"""
|
||||
conf_name - name of conf TODO: change to match db
|
||||
|
@ -162,7 +159,7 @@ class FsHelper(ClientFactory):
|
|||
|
||||
def listconf(self, conf_name):
|
||||
"""
|
||||
conf_name - name of conf
|
||||
conf_name - name of conf
|
||||
returns - a deferred that will be called back with a result
|
||||
like:
|
||||
|
||||
|
@ -215,7 +212,6 @@ class FsHelper(ClientFactory):
|
|||
d.addCallback(confdtmf_inner)
|
||||
return d
|
||||
|
||||
|
||||
def confsay(self, conf_name, text2speak, bgapi=True):
|
||||
"""
|
||||
conf_name - name of conf
|
||||
|
@ -253,7 +249,7 @@ class FsHelper(ClientFactory):
|
|||
def confstop(self, conf_name, bgapi=True):
|
||||
"""
|
||||
stop playback of all sounds
|
||||
|
||||
|
||||
conf_name - name of conf
|
||||
returns - a deferred that will be called back with a result
|
||||
like:
|
||||
|
@ -268,7 +264,6 @@ class FsHelper(ClientFactory):
|
|||
d.addCallback(confstop_inner)
|
||||
return d
|
||||
|
||||
|
||||
def showchannels(self, bgapi=True):
|
||||
|
||||
def showchannels_inner(ignored):
|
||||
|
@ -289,10 +284,9 @@ class FsHelper(ClientFactory):
|
|||
d.addCallback(killchan_inner)
|
||||
return d
|
||||
|
||||
|
||||
def broadcast(self, uuid, path, legs="both", bgapi=True):
|
||||
"""
|
||||
@legs - one of the following strings: aleg|bleg|both
|
||||
@legs - one of the following strings: aleg|bleg|both
|
||||
"""
|
||||
def broadcast_inner(ignored):
|
||||
df = self.freepyd.broadcast(uuid, path, legs, bgapi)
|
||||
|
@ -325,12 +319,10 @@ class FsHelper(ClientFactory):
|
|||
d.addCallback(sofia_profile_restart_inner)
|
||||
return d
|
||||
|
||||
|
||||
def sofia_status_profile(self, profile_name, bgapi=True):
|
||||
|
||||
def sofia_status_profile_inner(ignored):
|
||||
df = self.freepyd.sofia_status_profile(profile_name,
|
||||
bgapi)
|
||||
df = self.freepyd.sofia_status_profile(profile_name, bgapi)
|
||||
return df
|
||||
|
||||
d = self.connect()
|
||||
|
@ -340,7 +332,7 @@ class FsHelper(ClientFactory):
|
|||
|
||||
class FsHelperTest:
|
||||
def __init__(self, fshelper):
|
||||
self.fshelper=fshelper
|
||||
self.fshelper = fshelper
|
||||
pass
|
||||
|
||||
def test_dialconf(self):
|
||||
|
@ -349,10 +341,16 @@ class FsHelperTest:
|
|||
# called "freeswitch" on the local freeswitch instance.
|
||||
# one party is actually another conference, just to make
|
||||
# the example more confusing.
|
||||
people2dial = [{'name':'freeswitch',
|
||||
'number':'888@conference.freeswitch.org'},
|
||||
{'name':'mouselike',
|
||||
'number':'904@mouselike.org'}]
|
||||
people2dial = [
|
||||
{
|
||||
'name': 'freeswitch',
|
||||
'number': '888@conference.freeswitch.org'
|
||||
},
|
||||
{
|
||||
'name': 'mouselike',
|
||||
'number': ' 904@mouselike.org'
|
||||
}
|
||||
]
|
||||
d = self.fshelper.dialconf(people2dial, "freeswitch", bgapi=False)
|
||||
def failed(error):
|
||||
print "Failed to dial users!"
|
||||
|
@ -361,7 +359,7 @@ class FsHelperTest:
|
|||
d.addErrback(failed)
|
||||
def worked(*args):
|
||||
print "Worked! Dialed user result: %s" % str(args)
|
||||
d.addCallback(worked)
|
||||
d.addCallback(worked)
|
||||
return d
|
||||
|
||||
def test_listconf(self):
|
||||
|
@ -389,7 +387,7 @@ class FsHelperTest:
|
|||
def worked(*args):
|
||||
print "Kicked user from conf, result: %s" % str(args)
|
||||
d.addCallback(worked)
|
||||
|
||||
|
||||
|
||||
def test1():
|
||||
kick_everyone = False
|
||||
|
@ -409,7 +407,7 @@ def test1():
|
|||
print "failed: %s" % str(failure)
|
||||
reactor.stop()
|
||||
if kick_everyone:
|
||||
d.addCallback(kickeveryone)
|
||||
d.addCallback(kickeveryone)
|
||||
d.addErrback(failed)
|
||||
#fsht.test_confkick()
|
||||
#d = fshelper.connect()
|
||||
|
@ -422,14 +420,14 @@ def test1():
|
|||
def test2():
|
||||
fshelper = FsHelper(host="127.0.0.1",
|
||||
passwd="ClueCon",
|
||||
port=8021)
|
||||
port=8021)
|
||||
fshelper.sofia_profile_restart("mydomain.com")
|
||||
reactor.run()
|
||||
reactor.run()
|
||||
|
||||
def test3():
|
||||
fshelper = FsHelper(host="127.0.0.1",
|
||||
passwd="ClueCon",
|
||||
port=8021)
|
||||
port=8021)
|
||||
print "Calling originate.."
|
||||
party2dial="sofia/foo/600@192.168.1.202:5080"
|
||||
d = fshelper.originate(party2dial=party2dial,
|
||||
|
@ -439,25 +437,25 @@ def test3():
|
|||
def worked(result):
|
||||
print "Originate succeeded: %s" % result
|
||||
reactor.stop()
|
||||
|
||||
|
||||
def failed(failure):
|
||||
print "failed: %s" % str(failure)
|
||||
reactor.stop()
|
||||
|
||||
|
||||
d.addCallback(worked)
|
||||
d.addErrback(failed)
|
||||
reactor.run()
|
||||
reactor.run()
|
||||
|
||||
|
||||
def test4():
|
||||
fshelper = FsHelper(host="127.0.0.1",
|
||||
passwd="ClueCon",
|
||||
port=8021)
|
||||
port=8021)
|
||||
|
||||
def worked(result):
|
||||
print "Originate succeeded: %s" % result
|
||||
#reactor.stop()
|
||||
|
||||
|
||||
def failed(failure):
|
||||
print "failed: %s" % str(failure)
|
||||
#reactor.stop()
|
||||
|
@ -470,39 +468,39 @@ def test4():
|
|||
bgapi=True)
|
||||
d.addCallback(worked)
|
||||
d.addErrback(failed)
|
||||
party2dial="sofia/foo/someone@bar.com"
|
||||
party2dial="sofia/foo/someone@bar.com"
|
||||
d2 = fshelper.originate(party2dial=party2dial,
|
||||
dest_ext_app=dest_ext_app,
|
||||
bgapi=True)
|
||||
|
||||
|
||||
d2.addCallback(worked)
|
||||
d2.addErrback(failed)
|
||||
reactor.run()
|
||||
reactor.run()
|
||||
|
||||
|
||||
def test5():
|
||||
fshelper = FsHelper(host="127.0.0.1",
|
||||
passwd="ClueCon",
|
||||
port=8021)
|
||||
port=8021)
|
||||
|
||||
def worked(result):
|
||||
print "Originate succeeded: %s" % result
|
||||
#reactor.stop()
|
||||
|
||||
|
||||
def failed(failure):
|
||||
print "failed: %s" % str(failure)
|
||||
#reactor.stop()
|
||||
|
||||
for i in xrange(20):
|
||||
|
||||
party2dial="sofia/foo/600@192.168.1.202:5080"
|
||||
party2dial = "sofia/foo/600@192.168.1.202:5080"
|
||||
d = fshelper.originate(party2dial=party2dial,
|
||||
dest_ext_app="700",
|
||||
bgapi=True)
|
||||
d.addCallback(worked)
|
||||
d.addErrback(failed)
|
||||
|
||||
reactor.run()
|
||||
reactor.run()
|
||||
|
||||
|
||||
def test6():
|
||||
"""
|
||||
|
@ -514,17 +512,17 @@ def test6():
|
|||
from wikipbx import channelsutil
|
||||
def show_chanels(raw_xml):
|
||||
print raw_xml
|
||||
|
||||
|
||||
def failure(failure):
|
||||
print failure
|
||||
|
||||
|
||||
d = fshelper.showchannels(bgapi=False)
|
||||
d.addCallback(show_chanels)
|
||||
d.addErrback(failure)
|
||||
reactor.run()
|
||||
|
||||
|
||||
if __name__=="__main__":
|
||||
|
||||
if __name__ == "__main__":
|
||||
#test1()
|
||||
#test2()
|
||||
#test3()
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
"""
|
||||
"""
|
||||
FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
||||
Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
|
||||
|
||||
|
@ -23,13 +23,14 @@ the Initial Developer. All Rights Reserved.
|
|||
|
||||
Contributor(s): Traun Leyden <tleyden@branchcut.com>
|
||||
"""
|
||||
|
||||
|
||||
"""
|
||||
Data models for objects inside freeswitch
|
||||
"""
|
||||
|
||||
import re
|
||||
|
||||
|
||||
class ConfMember:
|
||||
|
||||
def __init__(self, rawstring):
|
||||
|
@ -45,7 +46,7 @@ class ConfMember:
|
|||
self.energy_level = None
|
||||
|
||||
self.parse(self.rawstring)
|
||||
|
||||
|
||||
def parse(self, rawstring):
|
||||
"""
|
||||
1;sofia/mydomain.com/user@somewhere.com;898e6552-24ab-11dc-9df7-9fccd4095451;FreeSWITCH;0000000000;hear|speak;0;0;300
|
||||
|
@ -76,6 +77,6 @@ class ConfMember:
|
|||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return "%s (%s)" % (self.member_id, self.member_uri)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
"""
|
||||
"""
|
||||
FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
||||
Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
|
||||
|
||||
|
@ -65,7 +65,7 @@ class FreepyRequest(object):
|
|||
debug("setRequestFinished called. response_content: %s " %
|
||||
self.response_content)
|
||||
self.finished = True
|
||||
|
||||
|
||||
def getDeferred(self):
|
||||
return self.deferred
|
||||
|
||||
|
@ -89,7 +89,7 @@ class FreepyRequest(object):
|
|||
if not line.strip() or len(line.strip()) == 0:
|
||||
self._fsm.BlankLine()
|
||||
return self.isRequestFinished()
|
||||
|
||||
|
||||
matchstr = re.compile("auth/request", re.I)
|
||||
result = matchstr.search(line)
|
||||
if (result != None):
|
||||
|
@ -159,7 +159,7 @@ class LoginRequest(FreepyRequest):
|
|||
"""
|
||||
Example success response
|
||||
========================
|
||||
|
||||
|
||||
lineReceived: Content-Type: auth/request
|
||||
lineReceived:
|
||||
lineReceived: Content-Type: command/reply
|
||||
|
@ -176,7 +176,7 @@ class LoginRequest(FreepyRequest):
|
|||
lineReceived:
|
||||
|
||||
"""
|
||||
|
||||
|
||||
def __init__(self):
|
||||
super(LoginRequest, self).__init__()
|
||||
import loginrequest_sm
|
||||
|
@ -190,7 +190,7 @@ class LoginRequest(FreepyRequest):
|
|||
return
|
||||
msg = "Login failed, most likely a bad password"
|
||||
self.errbackDeferred(Failure(Exception(msg)))
|
||||
|
||||
|
||||
def getReplyText(self):
|
||||
self.response_content
|
||||
|
||||
|
@ -204,7 +204,7 @@ class BgApiRequest(FreepyRequest):
|
|||
|
||||
linereceived: Content-Type: command/reply
|
||||
linereceived: Reply-Text: +OK Job-UUID: 788da080-24e0-11dc-85f6-3d7b12..
|
||||
linereceived:
|
||||
linereceived:
|
||||
|
||||
"""
|
||||
def __init__(self):
|
||||
|
@ -233,7 +233,7 @@ class ApiRequest(FreepyRequest):
|
|||
lineReceived:
|
||||
lineReceived: Call Requested: result: [SUCCESS]
|
||||
"""
|
||||
|
||||
|
||||
def __init__(self):
|
||||
super(ApiRequest, self).__init__()
|
||||
import apirequest_sm
|
||||
|
@ -255,7 +255,7 @@ class ApiRequest(FreepyRequest):
|
|||
# we need to add it back .. otherwise the Content-length
|
||||
# will be off by one
|
||||
line += "\n"
|
||||
|
||||
|
||||
self.response_content += line
|
||||
if len(self.response_content) == self.content_length:
|
||||
return True
|
||||
|
@ -272,7 +272,7 @@ class ApiRequest(FreepyRequest):
|
|||
# By default, just return accumulated string
|
||||
return self.response_content
|
||||
|
||||
|
||||
|
||||
class DialoutRequest(ApiRequest):
|
||||
"""
|
||||
Example raw dialout response
|
||||
|
@ -291,7 +291,7 @@ class DialoutRequest(ApiRequest):
|
|||
class BgDialoutRequest(BgApiRequest):
|
||||
def __init__(self):
|
||||
super(BgDialoutRequest, self).__init__()
|
||||
|
||||
|
||||
|
||||
class ConfKickRequest(ApiRequest):
|
||||
"""
|
||||
|
@ -314,13 +314,13 @@ class BgConfKickRequest(BgApiRequest):
|
|||
|
||||
def __init__(self):
|
||||
super(BgConfKickRequest, self).__init__()
|
||||
|
||||
|
||||
|
||||
class ListConfRequest(ApiRequest):
|
||||
"""
|
||||
Response to request to list conferences:
|
||||
========================================
|
||||
|
||||
|
||||
lineReceived: Content-Type: api/response
|
||||
lineReceived: Content-Length: 233
|
||||
lineReceived:
|
||||
|
@ -332,7 +332,7 @@ class ListConfRequest(ApiRequest):
|
|||
def __init__(self):
|
||||
super(ListConfRequest, self).__init__()
|
||||
self.conf_members = []
|
||||
|
||||
|
||||
def add_content(self, line):
|
||||
"""
|
||||
conf not empty example
|
||||
|
@ -352,13 +352,13 @@ class ListConfRequest(ApiRequest):
|
|||
else:
|
||||
confmember = models.ConfMember(line)
|
||||
self.conf_members.append(confmember)
|
||||
|
||||
|
||||
return super(ListConfRequest, self).add_content(line)
|
||||
|
||||
|
||||
def getResponse(self):
|
||||
|
||||
# TODO: parse this content into a meaningful
|
||||
# 'object' .. though, not sure this is really
|
||||
# necessary. wait till there's a need
|
||||
return self.conf_members
|
||||
|
||||
|
||||
|
|
|
@ -2,24 +2,24 @@
|
|||
# http://wiki.freeswitch.org/wiki/Mod_python
|
||||
# before reporting errors
|
||||
|
||||
import sys, time
|
||||
from freeswitch import *
|
||||
|
||||
|
||||
def onDTMF(input, itype, funcargs):
|
||||
console_log("1","\n\nonDTMF input: %s\n" % input)
|
||||
console_log("1", "\n\nonDTMF input: %s\n" % input)
|
||||
if input == "5":
|
||||
return "pause"
|
||||
return "pause"
|
||||
if input == "3":
|
||||
return "seek:+60000"
|
||||
return "seek:+60000"
|
||||
if input == "1":
|
||||
return "seek:-60000"
|
||||
return "seek:-60000"
|
||||
if input == "0":
|
||||
return "stop"
|
||||
return None # will make the streamfile audio stop
|
||||
return "stop"
|
||||
return None # will make the streamfile audio stop
|
||||
|
||||
|
||||
def handler(uuid):
|
||||
|
||||
console_log("1","... test from my python program\n")
|
||||
console_log("1", "... test from my python program\n")
|
||||
session = PySession(uuid)
|
||||
session.answer()
|
||||
session.setDTMFCallback(onDTMF, "")
|
||||
|
@ -27,13 +27,12 @@ def handler(uuid):
|
|||
session.playFile("/path/to/your.mp3", "")
|
||||
session.speak("Please enter telephone number with area code and press pound sign. ")
|
||||
input = session.getDigits("", 11, "*#", "#", 10000)
|
||||
console_log("1","result from get digits is %s\n" % input)
|
||||
console_log("1", "result from get digits is %s\n" % input)
|
||||
phone_number = session.playAndGetDigits(5, 11, 3, 10000, "*#",
|
||||
"/sounds/test.gsm",
|
||||
"/sounds/invalid.gsm",
|
||||
"",
|
||||
"^17771112222$");
|
||||
console_log("1","result from play_and_get_digits is %s\n" % phone_number)
|
||||
"^17771112222$")
|
||||
console_log("1", "result from play_and_get_digits is %s\n" % phone_number)
|
||||
session.transfer("1000", "XML", "default")
|
||||
session.hangup("1")
|
||||
|
||||
|
|
|
@ -2,14 +2,14 @@ from freeswitch import *
|
|||
from py_modules.speechtools import Grammar, SpeechDetect
|
||||
from py_modules.speechtools import SpeechObtainer
|
||||
|
||||
import time, os
|
||||
import os
|
||||
|
||||
VOICE_ENGINE = "cepstral"
|
||||
VOICE = "William"
|
||||
GRAMMAR_ROOT = "/usr/src/freeswitch_trunk/scripts"
|
||||
|
||||
"""
|
||||
Example speech recognition application in python.
|
||||
Example speech recognition application in python.
|
||||
|
||||
How to make this work:
|
||||
|
||||
|
@ -20,24 +20,24 @@ How to make this work:
|
|||
|
||||
"""
|
||||
|
||||
|
||||
class RecipeWizard:
|
||||
|
||||
def __init__(self, session):
|
||||
self.session=session
|
||||
self.session.set_tts_parms(VOICE_ENGINE, VOICE)
|
||||
self.session = session
|
||||
self.session.set_tts_parms(VOICE_ENGINE, VOICE)
|
||||
self.main()
|
||||
|
||||
def main(self):
|
||||
|
||||
console_log("debug", "recipe wizard main()\n")
|
||||
self.speechdetect = SpeechDetect(self.session, "openmrcp", "127.0.0.1");
|
||||
console_log("debug", "recipe wizard main()\n")
|
||||
self.speechdetect = SpeechDetect(self.session, "openmrcp", "127.0.0.1")
|
||||
self.speechobtainer = SpeechObtainer(speech_detect=self.speechdetect,
|
||||
required_phrases=1,
|
||||
wait_time=5000,
|
||||
max_tries=3)
|
||||
gfile = os.path.join(GRAMMAR_ROOT, "mainmenu.xml")
|
||||
self.grammar = Grammar("mainmenu", gfile,"input",80,90)
|
||||
self.speechobtainer.setGrammar(self.grammar);
|
||||
self.grammar = Grammar("mainmenu", gfile, "input", 80, 90)
|
||||
self.speechobtainer.setGrammar(self.grammar)
|
||||
console_log("debug", "calling speechobtainer.run()\n")
|
||||
self.speechobtainer.detectSpeech()
|
||||
self.session.speak("Hello. Welcome to the recipe wizard. Drinks or food?")
|
||||
|
@ -47,8 +47,9 @@ class RecipeWizard:
|
|||
self.session.speak("Received result. Result is: %s" % result[0])
|
||||
else:
|
||||
self.session.speak("Sorry, I did not hear you")
|
||||
|
||||
console_log("debug", "speechobtainer.run() finished\n")
|
||||
|
||||
console_log("debug", "speechobtainer.run() finished\n")
|
||||
|
||||
|
||||
def mainmenu():
|
||||
"""
|
||||
|
@ -78,10 +79,9 @@ def mainmenu():
|
|||
"""
|
||||
pass
|
||||
|
||||
|
||||
def handler(uuid):
|
||||
session = PySession(uuid)
|
||||
session.answer()
|
||||
rw = RecipeWizard(session)
|
||||
session.hangup("1")
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue