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.

178 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 bool:yes"""
  29. parser = OptionParser(usage=usage)
  30. parser.add_option( "-d", "--django-settings",
  31. help="if specified, get connstring and slice defaults from the given Django "
  32. "settings module. Default: empty.",
  33. default=None
  34. )
  35. parser.add_option( "-c", "--connstring",
  36. help="connection string to use. Default is '%s'." % DEFAULT_CONNSTRING,
  37. default=None
  38. )
  39. parser.add_option( "-s", "--slice",
  40. help="path to the slice file. Default is '%s'." % DEFAULT_SLICEFILE,
  41. default=None
  42. )
  43. parser.add_option( "-e", "--encoding",
  44. help="Character set arguments are encoded in. Default: Read from LANG env variable with fallback to UTF-8.",
  45. default=None
  46. )
  47. parser.add_option(
  48. "-v", "--verbose",
  49. help="Show verbose messages on stderr",
  50. default=False,
  51. action="store_true"
  52. )
  53. options, progargs = parser.parse_args()
  54. if options.django_settings is not None:
  55. if options.verbose:
  56. print >> sys.stderr, "Reading settings from module '%s'." % options.django_settings
  57. os.environ['DJANGO_SETTINGS_MODULE'] = options.django_settings
  58. from django.conf import settings
  59. if options.connstring is None:
  60. if options.verbose:
  61. print >> sys.stderr, "Setting connstring from settings module"
  62. options.connstring = settings.DEFAULT_CONN
  63. if options.slice is None:
  64. if options.verbose:
  65. print >> sys.stderr, "Setting slice from settings module"
  66. options.slice = settings.SLICE
  67. else:
  68. if options.connstring is None:
  69. if options.verbose:
  70. print >> sys.stderr, "Setting default connstring"
  71. options.connstring = DEFAULT_CONNSTRING
  72. if options.slice is None:
  73. if options.verbose:
  74. print >> sys.stderr, "Setting default slice"
  75. options.slice = DEFAULT_SLICEFILE
  76. if options.encoding is None:
  77. locale = os.environ['LANG']
  78. try:
  79. _, options.encoding = locale.split('.')
  80. except ValueError:
  81. options.encoding = "UTF-8"
  82. if options.verbose:
  83. print >> sys.stderr, "Connection info:"
  84. print >> sys.stderr, " Connstring: %s" % options.connstring
  85. print >> sys.stderr, " Slice: %s" % options.slice
  86. print >> sys.stderr, "Encoding: %s" % options.encoding
  87. ctl = MumbleCtlBase.newInstance( connstring=options.connstring, slicefile=options.slice )
  88. if not progargs:
  89. # Print available methods.
  90. for method in inspect.getmembers( ctl ):
  91. if method[0][0] == '_' or not callable( method[1] ):
  92. continue
  93. if hasattr( method[1], "innerfunc" ):
  94. args = inspect.getargspec( method[1].innerfunc )[0]
  95. else:
  96. args = inspect.getargspec( method[1] )[0]
  97. if len( args ) > 1:
  98. if args[0] == 'self':
  99. print "%s( %s )" % ( method[0], ', '.join( args[1:] ) )
  100. else:
  101. print "%s()" % method[0]
  102. else:
  103. # function name given. check if its args matches ours, if yes call it, if not print usage
  104. if options.verbose:
  105. print >> sys.stderr, "Method name: %s" % progargs[0]
  106. method = getattr( ctl, progargs[0] )
  107. if hasattr( method, "innerfunc" ):
  108. method = method.innerfunc
  109. args = inspect.getargspec( method )[0]
  110. if len(progargs) == len(args) and args[0] == 'self':
  111. if len(args) == 1:
  112. print method(ctl)
  113. else:
  114. cleanargs = []
  115. for param in progargs[1:]:
  116. try:
  117. argtype, argval = param.split(':', 1)
  118. except ValueError:
  119. cleanargs.append( param.decode(options.encoding) )
  120. else:
  121. cleanval = {
  122. 'bool': lambda val: val in ('True', 'true', '1', 'Yes', 'yes'),
  123. 'int': int,
  124. 'float': float,
  125. 'string': str
  126. }[ argtype ]( argval )
  127. if argtype == 'string':
  128. cleanval = cleanval.decode(options.encoding)
  129. cleanargs.append(cleanval)
  130. if options.verbose:
  131. print >> sys.stderr, "Call arguments: %s" % repr(cleanargs)
  132. print method( ctl, *cleanargs )
  133. elif len(args) == 1:
  134. print >> sys.stderr, "Method '%s' does not take any arguments." % progargs[0]
  135. print >> sys.stderr, "Expected %s()" % progargs[0]
  136. else:
  137. print >> sys.stderr, "Invalid arguments for method '%s': %s" % ( progargs[0], ', '.join( progargs[1:] ) )
  138. print >> sys.stderr, "Expected %s( %s )" % ( progargs[0], ', '.join( args[1:] ) )