Permalink
Browse files

fix method POST/GET, pythonic tweaks

  • Loading branch information...
1 parent b56bce2 commit 6d8e0e4a56d8ddaeb2908f1e38aa2b9d576f4399 @tamiel tamiel committed May 18, 2011
View
@@ -40,8 +40,7 @@ if __name__ == '__main__':
elif options.daemon:
daemon = True
else:
- parser.print_help()
- sys.exit(0)
+ daemon = False
if not os.path.isfile(configfile):
print "Config file %s not found !" % configfile
sys.exit(1)
View
@@ -40,8 +40,7 @@ if __name__ == '__main__':
elif options.daemon:
daemon = True
else:
- parser.print_help()
- sys.exit(0)
+ daemon = False
if not os.path.isfile(configfile):
print "Config file %s not found !" % configfile
sys.exit(1)
@@ -429,7 +429,7 @@ def run(self, outbound_socket):
elif isinstance(child_instance, Speak):
text = child_instance.text
loop = child_instance.loop_times
- child_type = child_instance.type
+ child_type = child_instance.item_type
method = child_instance.method
if child_type and method:
language = child_instance.language
@@ -769,7 +769,7 @@ def __init__(self):
def parse_grammar(self, element, uri=None):
Grammar.parse_grammar(self, element, uri)
method = self.extract_attribute_value("method")
- if method != 'GET' and method != 'POST':
+ if not method in ('GET', 'POST'):
raise RESTAttributeException("Method must be 'GET' or 'POST'")
self.method = method
url = element.text.strip()
@@ -816,7 +816,7 @@ class Speak(Grammar):
text: text to say
voice: voice to be used based on engine
language: language to use
- loop: number of times to say this text
+ loop: number of times to say this text (0 for unlimited)
engine: voice engine to be used for Speak (flite, cepstral)
Extended params - Currently uses Callie (Female) Voice
@@ -840,50 +840,51 @@ class Speak(Grammar):
def __init__(self):
Grammar.__init__(self)
self.loop_times = 1
- self.language = ""
+ self.language = "en"
self.sound_file_path = ""
self.engine = ""
self.voice = ""
- self.method = ""
- self.type = ""
+ self.method = "POST"
+ self.item_type = ""
def parse_grammar(self, element, uri=None):
Grammar.parse_grammar(self, element, uri)
- loop = int(self.extract_attribute_value("loop"))
- if loop < 0:
- raise RESTFormatException("Speak loop must be a positive integer")
+ try:
+ loop = int(self.extract_attribute_value("loop"))
+ except ValueError:
+ loop = 1
+ if loop <= 0:
+ self.loop_times = 999
+ else:
+ self.loop_times = loop
self.engine = self.extract_attribute_value("engine")
self.language = self.extract_attribute_value("language")
self.voice = self.extract_attribute_value("voice")
item_type = self.extract_attribute_value("type")
if item_type and (item_type in self.valid_types):
- self.type = item_type
-
+ self.item_type = item_type
method = self.extract_attribute_value("method")
if method and (method in self.valid_methods):
self.method = method
- if loop == 0:
- self.loop_times = 999
- else:
- self.loop_times = loop
-
def run(self, outbound_socket):
- if self.type and self.method:
+ if self.item_type and self.method:
say_args = "%s %s %s %s" \
- % (self.language, self.type, self.method, self.text)
+ % (self.language, self.item_type,
+ self.method, self.text)
else:
- say_args = "%s|%s|%s" %(self.engine, self.voice, self.text)
+ say_args = "%s|%s|%s" % (self.engine, self.voice, self.text)
for i in range(self.loop_times):
- if self.type and self.method:
+ if self.item_type and self.method:
outbound_socket.say(say_args)
else:
outbound_socket.speak(say_args)
event = outbound_socket._action_queue.get()
# Log Speak execute response
outbound_socket.log.info("Speak finished %s times - (%s)" \
% ((i+1), str(event.get_header('Application-Response'))))
+ outbound_socket.log.info("Speak finished")
class ScheduleHangup(Grammar):
@@ -118,7 +118,7 @@ def __init__(self, auth_id='', auth_token=''):
self.opener = None
def _build_get_uri(self, uri, params):
- if params and len(params) > 0:
+ if params:
if uri.find('?') > 0:
if uri[-1] != '&':
uri += '&'
@@ -145,9 +145,8 @@ def _prepare_http_request(self, uri, params, method='POST'):
# append the POST variables sorted by key to the uri
s = uri
- if len(params) > 0:
- for k, v in sorted(params.items()):
- s += k + v
+ for k, v in sorted(params.items()):
+ s += k + v
# compute signature and compare signatures
signature = base64.encodestring(hmac.new(self.auth_token, s, sha1).\
@@ -165,10 +164,11 @@ def fetch_response(self, uri, params, method='POST'):
for arg in args:
try:
k, v = arg.split('=')
- params.update({k:v})
+ params[k] = v
except ValueError:
pass
request = self._prepare_http_request(uri, params, method)
response = urllib2.urlopen(request).read()
return response
+
@@ -229,12 +229,13 @@ def process_call(self):
This will fetch the XML, validate the response
Parse the XML and Execute it
"""
+ fetch_method = 'POST'
for x in range(MAX_REDIRECT):
try:
if self.has_hangup():
self.log.warn("Channel has hung up, breaking Processing Call")
return
- self.fetch_xml(method='POST')
+ self.fetch_xml(method=fetch_method)
if not self.xml_response:
self.log.warn("No XML Response")
return
@@ -247,6 +248,8 @@ def process_call(self):
# Set Answer URL to Redirect URL
self.answer_url = redirect.get_url()
fetch_method = redirect.get_method()
+ if not fetch_method:
+ fetch_method = 'POST'
# Reset all the previous response and grammar
self.xml_response = ""
self.parsed_grammar = []
@@ -256,21 +259,23 @@ def process_call(self):
gevent.sleep(0.010)
continue
except Exception, e:
+ self.log.error("Processing Call Failure !")
# If error occurs during xml parsing
# log exception and break
self.log.error(str(e))
[ self.log.error(line) for line in \
traceback.format_exc().splitlines() ]
- self.log.error("XML error")
return
- self.log.warn("Max Redirect reached !")
+ self.log.warn("Max Redirect Reached !")
def fetch_xml(self, method='POST'):
"""
This method will retrieve the xml from the answer_url
The url result expected is an XML content which will be stored in
xml_response
"""
+ self.log.info("Fetching %s RESTXML from %s with %s" \
+ % (method, self.answer_url, self.params))
http_obj = HTTPRequest(self.auth_id, self.auth_token)
self.xml_response = http_obj.fetch_response(self.answer_url,
self.params, method)

0 comments on commit 6d8e0e4

Please sign in to comment.