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.

92 lines
3.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # kate: space-indent on; indent-width 4; replace-tabs on;
  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 simplejson
  17. import inspect
  18. import functools
  19. from django.http import HttpResponse
  20. def getname( cls_or_name ):
  21. if type(cls_or_name) not in ( str, unicode ):
  22. return cls_or_name.__name__
  23. return cls_or_name
  24. class Provider( object ):
  25. def __init__( self, base_url ):
  26. self.base_url = base_url
  27. self.classes = {}
  28. def register_instance( self, cls_or_name, instance ):
  29. name = getname(cls_or_name)
  30. if name not in self.classes:
  31. raise KeyError(name)
  32. self.classes[ name ][0] = instance
  33. return instance
  34. def register_method( self, cls_or_name ):
  35. """ Return a function that takes a method as an argument and adds that to cls_or_name. """
  36. print "REGMETHOD", cls_or_name
  37. clsname = getname(cls_or_name)
  38. if clsname not in self.classes:
  39. self.classes[clsname] = ( None, {} )
  40. return functools.partial( self._register_method, cls_or_name )
  41. def _register_method( self, cls_or_name, method ):
  42. print "REGREALMETHOD", cls_or_name, method
  43. self.classes[ getname(cls_or_name) ][1][ method.__name__ ] = method
  44. return method
  45. def get_api( self, name="Ext.app.REMOTING_API" ):
  46. """ Introspect the methods and get a JSON description of this API. """
  47. actdict = {}
  48. for clsname in self.classes:
  49. actdict[clsname] = []
  50. for methodname in self.classes[clsname][1]:
  51. actdict[clsname].append( {
  52. "name": methodname,
  53. "len": len( inspect.getargspec( self.classes[clsname][1][methodname] ).args )
  54. } )
  55. return "%s = %s" % ( name, simplejson.dumps({
  56. "url": ("%s/router" % self.base_url),
  57. "type": "remoting",
  58. "actions": actdict
  59. }))
  60. def request( self, request ):
  61. cls = request.POST['extAction']
  62. methname = request.POST['extMethod']
  63. data = request.POST['extData']
  64. rtype = request.POST['extType']
  65. tid = request.POST['extTID']
  66. if cls not in self.classes:
  67. raise KeyError(cls)
  68. if methname not in self.classes[cls][1]:
  69. raise KeyError(methname)
  70. result = self.classes[cls][1][methname]( *data, request=request )
  71. return HttpResponse( simplejson.dumps({
  72. "type": rtype,
  73. "tid": tid,
  74. "action": cls,
  75. "method": methname,
  76. "result": result
  77. }))