|
From: <jpm...@gm...> - 2007-09-16 19:41:16
|
hi,
i'm a newbie to python but i've been working with asterisk since quite
some time. today i've written some functions for the Manager class to
implement database access. maybe you're interested...
def DBGet(self, family, key):
response = self.command('database get %s %s' % (family, key))
if response.has_header('Value'):
return response['Value']
else:
raise ManagerException('Database entry not found')
def DBPut(self, family, key, value):
response = self.command('database put %s %s %s' % (family, key,
value))
if 'Updated database successfully' in response.data:
return True
else:
raise ManagerException('Database update was not successful')
def DBDel(self, family, key):
response = self.command('database del %s %s' % (family, key))
if 'Database entry removed' in response.data:
return True
if 'Database entry does not exist.' in response.data:
raise ManagerException('Database entry does not exist.')
else:
raise ManagerException('Database update was not successful')
def DBDelTree(self, family):
response = self.command('database deltree %s' % family)
if 'Database entries removed.' in response.data:
return True
else:
raise ManagerException('Database update was not successful')
these functions should be pretty much self-explaining. like i've said,
probably quite primitive, but it's working. i didn't use the "Action:
DBGet" way because event handling is less compact than using the CLI
commands. anyway, i also wrote something in that direction in order to
understand event handling...
# def DBGet(self, family, key):
# """Return a value from the Asterisk database"""
# def handle_DBGetResponse(event, manager):
# returned_values[event.get_action_id()] =
event.get_header('Val')
#
# returned_values = {}
# self.register_event('DBGetResponse', handle_DBGetResponse)
# response = self.send_action({'Action':'DBGet', 'Family':family,
'Key':key})
# if response['Response'] == 'Error':
# raise ManagerException(response['Message'])
# action_id = response['ActionID']
# while action_id not in returned_values:
# pass
# self.unregister_event('DBGetResponse', handle_DBGetResponse)
#
# return returned_values[action_id]
this function includes an event handler to which it subscribes in order
to fetch the database value. in the while loop, it waits for a value to
be returned.
i haven't implemented timeouts so far. please let me know your opinion.
regards j.
|