Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
6.4 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # kate: space-indent on; indent-width 4; replace-tabs on;
  4. """
  5. * Copyright (C) 2010, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  6. *
  7. * Mumble-Django is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This package is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. """
  17. DEFAULT_CONNSTRING = 'Meta:tcp -h 127.0.0.1 -p 6502'
  18. DEFAULT_SLICEFILE = '/usr/share/slice/Murmur.ice'
  19. import os, sys
  20. import inspect
  21. import getpass
  22. from optparse import OptionParser
  23. from mumble.mctl import MumbleCtlBase
  24. usage = """Usage: %prog [options] [<method name>] [<method arguments>]
  25. Each method argument has the form: [<data type: bool|int|float|string>:]value
  26. If you do not specify a data type, string will be assumed, otherwise
  27. `value' will be converted to the given type first. The bool conversion
  28. interprets each of 'True', 'true', '1', 'Yes', 'yes' as True, everything else
  29. as False.
  30. Example: int:4 float:3.5 string:oh:hai foobar bool:yes"""
  31. parser = OptionParser(usage=usage)
  32. parser.add_option( "-d", "--django-settings",
  33. help="if specified, get connstring and slice defaults from the given Django "
  34. "settings module. Default: empty.",
  35. default=None
  36. )
  37. parser.add_option( "-c", "--connstring",
  38. help="connection string to use. Default is '%s'." % DEFAULT_CONNSTRING,
  39. default=None
  40. )
  41. parser.add_option( "-i", "--icesecret",
  42. help="Ice secret to use in the connection. Also see --asksecret.",
  43. default=None
  44. )
  45. parser.add_option( "-a", "--asksecret",
  46. help="Ask for the Ice secret on the shell instead of taking it from the command line.",
  47. action="store_true", default=False
  48. )
  49. parser.add_option( "-s", "--slice",
  50. help="path to the slice file. Default is '%s'." % DEFAULT_SLICEFILE,
  51. default=None
  52. )
  53. parser.add_option( "-e", "--encoding",
  54. help="Character set arguments are encoded in. Default: Read from LANG env variable with fallback to UTF-8.",
  55. default=None
  56. )
  57. parser.add_option(
  58. "-v", "--verbose",
  59. help="Show verbose messages on stderr",
  60. default=False,
  61. action="store_true"
  62. )
  63. options, progargs = parser.parse_args()
  64. if options.django_settings is not None:
  65. if options.verbose:
  66. print >> sys.stderr, "Reading settings from module '%s'." % options.django_settings
  67. os.environ['DJANGO_SETTINGS_MODULE'] = options.django_settings
  68. from django.conf import settings
  69. if options.connstring is None:
  70. if options.verbose:
  71. print >> sys.stderr, "Setting connstring from settings module"
  72. options.connstring = settings.DEFAULT_CONN
  73. if options.slice is None:
  74. if options.verbose:
  75. print >> sys.stderr, "Setting slice from settings module"
  76. options.slice = settings.SLICE
  77. else:
  78. if options.connstring is None:
  79. if options.verbose:
  80. print >> sys.stderr, "Setting default connstring"
  81. options.connstring = DEFAULT_CONNSTRING
  82. if options.slice is None:
  83. if options.verbose:
  84. print >> sys.stderr, "Setting default slice"
  85. options.slice = DEFAULT_SLICEFILE
  86. if options.encoding is None:
  87. try:
  88. locale = os.environ['LANG']
  89. _, options.encoding = locale.split('.')
  90. except (KeyError, ValueError):
  91. options.encoding = "UTF-8"
  92. if options.verbose:
  93. print >> sys.stderr, "Connection info:"
  94. print >> sys.stderr, " Connstring: %s" % options.connstring
  95. print >> sys.stderr, " Slice: %s" % options.slice
  96. print >> sys.stderr, "Encoding: %s" % options.encoding
  97. if options.asksecret or options.icesecret == '':
  98. options.icesecret = getpass.getpass( "Ice secret: " )
  99. ctl = MumbleCtlBase.newInstance( options.connstring, options.slice, options.icesecret )
  100. if not progargs:
  101. # Print available methods.
  102. for method in inspect.getmembers( ctl ):
  103. if method[0][0] == '_' or not callable( method[1] ):
  104. continue
  105. if hasattr( method[1], "innerfunc" ):
  106. args = inspect.getargspec( method[1].innerfunc )[0]
  107. else:
  108. args = inspect.getargspec( method[1] )[0]
  109. if len( args ) > 1:
  110. if args[0] == 'self':
  111. print "%s( %s )" % ( method[0], ', '.join( args[1:] ) )
  112. else:
  113. print "%s()" % method[0]
  114. else:
  115. # function name given. check if its args matches ours, if yes call it, if not print usage
  116. if options.verbose:
  117. print >> sys.stderr, "Method name: %s" % progargs[0]
  118. method = getattr( ctl, progargs[0] )
  119. bound = True
  120. if hasattr( method, "innerfunc" ):
  121. method = method.innerfunc
  122. bound = False
  123. args = inspect.getargspec( method )[0]
  124. if len(progargs) == len(args) and args[0] == 'self':
  125. if len(args) == 1:
  126. print method(ctl)
  127. else:
  128. cleanargs = []
  129. for param in progargs[1:]:
  130. try:
  131. argtype, argval = param.split(':', 1)
  132. except ValueError:
  133. cleanargs.append( param.decode(options.encoding) )
  134. else:
  135. cleanval = {
  136. 'bool': lambda val: val in ('True', 'true', '1', 'Yes', 'yes'),
  137. 'int': int,
  138. 'float': float,
  139. 'string': str
  140. }[ argtype ]( argval )
  141. if argtype == 'string':
  142. cleanval = cleanval.decode(options.encoding)
  143. cleanargs.append(cleanval)
  144. if options.verbose:
  145. print >> sys.stderr, "Call arguments: %s" % repr(cleanargs)
  146. if bound:
  147. print method( *cleanargs )
  148. else:
  149. print method( ctl, *cleanargs )
  150. elif len(args) == 1:
  151. print >> sys.stderr, "Method '%s' does not take any arguments." % progargs[0]
  152. print >> sys.stderr, "Expected %s()" % progargs[0]
  153. else:
  154. print >> sys.stderr, "Invalid arguments for method '%s': %s" % ( progargs[0], ', '.join( progargs[1:] ) )
  155. print >> sys.stderr, "Expected %s( %s )" % ( progargs[0], ', '.join( args[1:] ) )