/usr/share/pyshared/Asterisk/CLI.py is in python-asterisk 0.5.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | '''
Asterisk/CLI.py: Command-line wrapper around the Asterisk Manager API.
'''
__author__ = 'David M. Wilson <dw@autosols.com>'
__id__ = '$Id$'
import sys, os, inspect
from Asterisk import Manager, BaseException, Config
import Asterisk.Util
class ArgumentsError(BaseException):
_prefix = 'bad arguments'
def usage(argv0, out_file):
'''
Print command-line program usage.
'''
argv0 = os.path.basename(argv0)
usage = '''
%(argv0)s actions
Show available actions and their arguments.
%(argv0)s action <API action> [<arg1> [<argn> ..]]
Execute the specified action.
For named arguments "--name=<val>" syntax may also be used.
%(argv0)s command "<console command>"
Execute the specified Asterisk console command.
%(argv0)s usage
Display this message.
%(argv0)s help <action>
Display usage message for the given <action>.
''' % locals()
out_file.writelines([ line[6:] + '\n' for line in usage.splitlines() ])
def show_actions(action = None):
if action is None:
print
print 'Supported actions and their arguments.'
print '======================================'
print
class AllActions(Manager.CoreActions, Manager.ZapataActions):
pass
methods = [
(name, obj) for (name, obj) in inspect.getmembers(AllActions) \
if inspect.ismethod(obj) and name[0] != '_'
]
if action is not None:
methods = [ x for x in methods if x[0].lower() == action.lower() ]
for name, method in methods:
arg_spec = inspect.getargspec(method)
arg_spec[0].pop(0)
print ' Action:', name
fmt = inspect.formatargspec(*arg_spec)[1:-1]
if fmt:
print 'Arguments:', fmt
foo = [ x.strip() for x in method.__doc__.strip().splitlines() ]
print ' ' + '\n '.join(foo)
print
def execute_action(manager, argv):
method_name = argv.pop(0).lower()
method_dict = dict(\
[ (k.lower(), v) for (k, v) in inspect.getmembers(manager) \
if inspect.ismethod(v) ])
try:
method = method_dict[method_name]
except KeyError, e:
raise ArgumentsError('%r is not a valid action.' % (method_name,))
pos_args = []
kw_args = {}
process_kw = True
for arg in argv:
if process_kw and arg == '--':
process_kw = False # stop -- processing.
elif process_kw and arg[:2] == '--' and '=' in arg:
key, val = arg[2:].split('=', 2)
kw_args[key] = val
else:
pos_args.append(arg)
Asterisk.Util.dump_human(method(*pos_args, **kw_args))
def command_line(argv):
'''
Act as a command-line tool.
'''
commands = [ 'actions', 'action', 'command', 'usage', 'help' ]
if len(argv) < 2:
raise ArgumentsError('please specify at least one argument.')
command = argv[1]
if command not in commands:
raise ArgumentsError('invalid arguments.')
if command == 'usage':
return usage(argv[0], sys.stdout)
manager = Manager.Manager(*Config.Config().get_connection())
if command == 'actions':
show_actions()
if command == 'help':
if len(argv) < 3:
raise ArgumentsError('please specify an action.')
show_actions(argv[2])
elif command == 'action':
if len(argv) < 3:
raise ArgumentsError('please specify an action.')
try:
execute_action(manager, argv[2:])
except TypeError, e:
print "Bad arguments specified. Help for %s:" % (argv[2],)
show_actions(argv[2])
elif command == 'command':
execute_action('command', argv[2])
|