From: Matthew N. <mni...@di...> - 2007-09-17 15:39:27
|
Jan, I don't think using self.command directly is the correct way to implement this feature. Also I don't think automatically handling events for the user is the right way to do it either. I think pyst should be fairly low level and only provide an convent interface to AGI and the Manager. Also on the flip side, pyst should not be designed to prevent the user from building a layer of abstraction on top of it either. Either way, pyst 0.3 will make it easier to do these things in a more straightforward manner. No ETA on that though, for an idea of what it might look like, check out astxx (http://matt-land.com/astxx). Jan Müller wrote: > 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. > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2005. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Pyst-users mailing list > Pys...@li... > https://lists.sourceforge.net/lists/listinfo/pyst-users -- Matthew Nicholson Digium |