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.

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