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.

180 lines
5.2 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. from optparse import OptionParser
  21. from mumble.mctl import MumbleCtlBase
  22. usage = """Usage: %prog [options] [<method name>] [<method arguments>]
  23. Each method argument has the form: [<data type: bool|int|float|string>:]value
  24. If you do not specify a data type, string will be assumed, otherwise
  25. `value' will be converted to the given type first. The bool conversion
  26. interprets each of 'True', 'true', '1', 'Yes', 'yes' as True, everything else
  27. as False.
  28. Example: int:4 float:3.5 string:oh:hai foobar
  29. """
  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( "-s", "--slice",
  41. help="path to the slice file. Default is '%s'." % DEFAULT_SLICEFILE,
  42. default=None
  43. )
  44. parser.add_option( "-e", "--encoding",
  45. help="Character set arguments are encoded in. Default: Read from LANG env variable with fallback to UTF-8.",
  46. default=None
  47. )
  48. parser.add_option(
  49. "-v", "--verbose",
  50. help="Show verbose messages on stderr",
  51. default=False,
  52. action="store_true"
  53. )
  54. options, progargs = parser.parse_args()
  55. if options.django_settings is not None:
  56. if options.verbose:
  57. print >> sys.stderr, "Reading settings from module '%s'." % options.django_settings
  58. os.environ['DJANGO_SETTINGS_MODULE'] = options.django_settings
  59. from django.conf import settings
  60. if options.connstring is None:
  61. if options.verbose:
  62. print >> sys.stderr, "Setting connstring from settings module"
  63. options.connstring = settings.DEFAULT_CONN
  64. if options.slice is None:
  65. if options.verbose:
  66. print >> sys.stderr, "Setting slice from settings module"
  67. options.slice = settings.SLICE
  68. else:
  69. if options.connstring is None:
  70. if options.verbose:
  71. print >> sys.stderr, "Setting default connstring"
  72. options.connstring = DEFAULT_CONNSTRING
  73. if options.slice is None:
  74. if options.verbose:
  75. print >> sys.stderr, "Setting default slice"
  76. options.slice = DEFAULT_SLICEFILE
  77. if options.encoding is None:
  78. locale = os.environ['LANG']
  79. try:
  80. _, options.encoding = locale.split('.')
  81. except ValueError:
  82. options.encoding = "UTF-8"
  83. if options.verbose:
  84. print >> sys.stderr, "Connection info:"
  85. print >> sys.stderr, " Connstring: %s" % options.connstring
  86. print >> sys.stderr, " Slice: %s" % options.slice
  87. print >> sys.stderr, "Encoding: %s" % options.encoding
  88. ctl = MumbleCtlBase.newInstance( connstring=options.connstring, slicefile=options.slice )
  89. if not progargs:
  90. # Print available methods.
  91. for method in inspect.getmembers( ctl ):
  92. if method[0][0] == '_' or not callable( method[1] ):
  93. continue
  94. if hasattr( method[1], "innerfunc" ):
  95. args = inspect.getargspec( method[1].innerfunc )[0]
  96. else:
  97. args = inspect.getargspec( method[1] )[0]
  98. if len( args ) > 1:
  99. if args[0] == 'self':
  100. print "%s( %s )" % ( method[0], ', '.join( args[1:] ) )
  101. else:
  102. print "%s()" % method[0]
  103. else:
  104. # function name given. check if its args matches ours, if yes call it, if not print usage
  105. if options.verbose:
  106. print >> sys.stderr, "Method name: %s" % progargs[0]
  107. method = getattr( ctl, progargs[0] )
  108. if hasattr( method, "innerfunc" ):
  109. method = method.innerfunc
  110. args = inspect.getargspec( method )[0]
  111. if len(progargs) == len(args) and args[0] == 'self':
  112. if len(args) == 1:
  113. print method(ctl)
  114. else:
  115. cleanargs = []
  116. for param in progargs[1:]:
  117. try:
  118. argtype, argval = param.split(':', 1)
  119. except ValueError:
  120. cleanargs.append( param.decode(options.encoding) )
  121. else:
  122. cleanval = {
  123. 'bool': lambda val: val in ('True', 'true', '1', 'Yes', 'yes'),
  124. 'int': int,
  125. 'float': float,
  126. 'string': str
  127. }[ argtype ]( argval )
  128. if argtype == 'string':
  129. cleanval = cleanval.decode(options.encoding)
  130. cleanargs.append(cleanval)
  131. if options.verbose:
  132. print >> sys.stderr, "Call arguments: %s" % repr(cleanargs)
  133. print method( ctl, *cleanargs )
  134. elif len(args) == 1:
  135. print >> sys.stderr, "Method '%s' does not take any arguments." % progargs[0]
  136. print >> sys.stderr, "Expected %s()" % progargs[0]
  137. else:
  138. print >> sys.stderr, "Invalid arguments for method '%s': %s" % ( progargs[0], ', '.join( progargs[1:] ) )
  139. print >> sys.stderr, "Expected %s( %s )" % ( progargs[0], ', '.join( args[1:] ) )