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.

191 lines
5.6 KiB

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