Debian Codegraph @

django_xmlrpc/registry.py

raw symbols SCIP
1 """registry module for the django_xmlrpc package
2
3 Authors::
4 Julien Fache
5
6 Credit must go to Brendan W. McAdams <[email protected]>, who
7 posted the original SimpleXMLRPCDispatcher to the Django wiki:
8 http://code.djangoproject.com/wiki/XML-RPC
9
10 New BSD License
11 ===============
12 Copyright (c) 2007, Graham Binns http://launchpad.net/~codedragon
13
14 All rights reserved.
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are met:
18
19 * Redistributions of source code must retain the above copyright notice,
20 this list of conditions and the following disclaimer.
21 * Redistributions in binary form must reproduce the above copyright notice,
22 this list of conditions and the following disclaimer in the documentation
23 and/or other materials provided with the distribution.
24 * Neither the name of the <ORGANIZATION> nor the names of its contributors
25 may be used to endorse or promote products derived from this software
26 without specific prior written permission.
27
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
32 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
35 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
36 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 """
40 from collections import Callable
41 from logging import getLogger
42
43 from django.apps import apps
44 from django.conf import settings
45 from django.core.exceptions import ImproperlyConfigured
46
47 from django_xmlrpc.dispatcher import xmlrpc_dispatcher
48
49 logger = getLogger('xmlrpc.registry')
50
51
52 def register_xmlrpc_methods():
53 """
54 Register all xmlrpc methods in the server.
55 """
56 if hasattr(settings, 'XMLRPC_METHODS'):
57 register_xmlrpc_methods_legacy()
58 else:
59 register_xmlrpc_methods_autodiscover()
60 register_xmlrpc_methods_helpers()
61
62
63 def register_xmlrpc_method(path, name):
64 """
65 Register a method into the server.
66 """
67 # If 'path' is actually a function, just add it without fuss
68 if isinstance(path, Callable):
69 logger.info("Registering '%s:%s' => '%s'" % (
70 path.__module__, path.__name__, name))
71 xmlrpc_dispatcher.register_function(path, name)
72 return
73
74 # Otherwise we try and find something that we can call
75 logger.debug('%s not callable, resolving path...' % path)
76 i = path.rfind('.')
77 module, attr = path[:i], path[i + 1:]
78
79 try:
80 mod = __import__(module, globals(), locals(), [attr])
81 except ImportError:
82 raise ImproperlyConfigured(
83 "Error registering XML-RPC method: "
84 "module %s can't be imported" % module)
85
86 try:
87 func = getattr(mod, attr)
88 except AttributeError:
89 raise ImproperlyConfigured(
90 'Error registering XML-RPC method: '
91 'module %s doesn\'t define a method "%s"' % (module, attr))
92
93 if not isinstance(func, Callable):
94 raise ImproperlyConfigured(
95 'Error registering XML-RPC method: '
96 '"%s" is not callable in module %s' % (attr, module))
97
98 logger.info("Registering '%s:%s' => '%s'" % (module, attr, name))
99 xmlrpc_dispatcher.register_function(func, name)
100
101
102 def register_xmlrpc_methods_legacy():
103 """
104 Load up any methods that have been registered
105 with the server via settings.
106 """
107 logger.info('Register XML-RPC methods from settings.XMLRPC_METHODS')
108 for path, name in settings.XMLRPC_METHODS:
109 register_xmlrpc_method(path, name)
110
111
112 def register_xmlrpc_methods_autodiscover():
113 """
114 Looks in app directories for a module called 'xmlrpc'
115 This should contain a distribution XMLRPC_METHODS declaration.
116 """
117 logger.info('Register XML-RPC methods by inspecting INSTALLED_APPS')
118 for application in apps.get_app_configs():
119 application_name = application.name
120 logger.debug('Checking %s...' % application_name)
121 try:
122 module = __import__('%s.xmlrpc' % application_name,
123 globals(), locals(), [''])
124 logger.debug('Found %s.xmlrpc' % application_name)
125 except ImportError:
126 logger.debug('Not found %s.xmlrpc' % application_name)
127 continue
128 if hasattr(module, 'XMLRPC_METHODS'):
129 logger.info('Found XMLRPC_METHODS in %s.xmlrpc' % application_name)
130 for path, name in module.XMLRPC_METHODS:
131 register_xmlrpc_method(path, name)
132
133
134 def register_xmlrpc_methods_helpers():
135 """Register the introspection and multicall methods
136 with the XML-RPC namespace.
137 """
138 xmlrpc_dispatcher.register_introspection_functions()
139 xmlrpc_dispatcher.register_multicall_functions()