"""registry module for the django_xmlrpc package

Authors::
    Julien Fache

Credit must go to Brendan W. McAdams <brendan.mcadams@thewintergrp.com>, who
posted the original SimpleXMLRPCDispatcher to the Django wiki:
http://code.djangoproject.com/wiki/XML-RPC

New BSD License
===============
Copyright (c) 2007, Graham Binns http://launchpad.net/~codedragon

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of the <ORGANIZATION> nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
from collections import Callable
from importlib import import_module
from logging import getLogger

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from django_xmlrpc.dispatcher import xmlrpc_dispatcher

logger = getLogger('xmlrpc.registry')

DIST_SETTINGS = 'xmlrpc'


def register_xmlrpc_methods():
    """
    Register all xmlrpc methods in the server.
    """
    if hasattr(settings, 'XMLRPC_METHODS'):
        legacy_register_xmlrpc_methods()
    else:
        autodiscover_register_xmlrpc_methods()
    register_xmlrpc_methods_helpers()


def register_xmlrpc_method(path, name):
    """
    Register a method into the server.
    """
    # If 'path' is actually a function, just add it without fuss
    if isinstance(path, Callable):
        logger.info("Registering '%s' => '%s'" % (path, name))
        xmlrpc_dispatcher.register_function(path, name)
        return

    # Otherwise we try and find something that we can call
    logger.debug('%s not callable, resolving path...' % path)
    i = path.rfind('.')
    module, attr = path[:i], path[i + 1:]

    try:
        mod = __import__(module, globals(), locals(), [attr])
    except ImportError:
        raise ImproperlyConfigured(
            "Error registering XML-RPC method: "
            "module %s can't be imported" % module)

    try:
        func = getattr(mod, attr)
    except AttributeError:
        raise ImproperlyConfigured(
            'Error registering XML-RPC method: '
            'module %s doesn\'t define a method "%s"' % (module, attr))

    if not isinstance(func, Callable):
        raise ImproperlyConfigured(
            'Error registering XML-RPC method: '
            '"%s" is not callable in module %s' % (attr, module))

    logger.info("Registering '%s:%s' => '%s'" % (module, attr, name))
    xmlrpc_dispatcher.register_function(func, name)


def legacy_register_xmlrpc_methods():
    """Load up any methods that have been registered
    with the server in settings.
    """
    logger.info('Register XML-RPC methods from settings.XMLRPC_METHODS')
    for path, name in settings.XMLRPC_METHODS:
        register_xmlrpc_method(path, name)


def autodiscover_register_xmlrpc_methods():
    """Implement autodiscovery.

    Looks in app directories for a module called 'xmlrpc'
    This should contain a distribution XMLRPC_METHODS declaration.
    """
    logger.info('Inspecting INSTALLED_APPS')
    for app in settings.INSTALLED_APPS:
        logger.debug('Checking %s' % app)
        try:
            # Check to see if app has a stock xmlrpc_settings module
            logger.debug('Looking for %s.%s' % (app, DIST_SETTINGS))
            xm = import_module('%s.%s' % (app, DIST_SETTINGS))
            logger.info('Found %s.%s' % (app, DIST_SETTINGS))
        except ImportError:
            logger.debug('%s.%s not found, moving on' % (app, DIST_SETTINGS))
            continue
        if hasattr(xm, 'XMLRPC_METHODS'):
            # Has a list for us to register
            logger.info('Found XMLRPC_METHODS in %s' % app)
            for path, method in xm.XMLRPC_METHODS:
                # Check if an imported function got passed
                if isinstance(path, Callable):
                    # Method is a callable, so register it directly
                    logger.info("registering '%s' => '%s')" % (path, method))
                    xmlrpc_dispatcher.register_function(path, method)
                    continue
                else:
                    logger.debug('%s not callable, resolving path' % path)
                # Find the module containing the function
                dot = path.rindex('.')
                module, attr = path[:dot], path[dot + 1:]
                logger.debug('checking module %s' % module)
                try:
                    # See if the module containing the path
                    # to function is importable
                    mod = import_module(module)
                except ImportError as ex:
                    # Couldn't import configured module, could be a typo
                    logger.warn("could not import '%s', "
                                "please check your module's %s" % (
                                    module, DIST_SETTINGS))
                    if settings.DEBUG:
                        raise ex
                    continue
                try:
                    # See if the function path is valid
                    func = getattr(mod, attr)
                except AttributeError:
                    raise ImproperlyConfigured(
                        'Error registering XML-RPC method: '
                        'module %s doesn\'t define a method "%s"' % (
                            module, attr))
                if not isinstance(func, Callable):
                    # Path is not a callable,
                    # could be a variable containing something else
                    raise ImproperlyConfigured(
                        'Error registering XML-RPC method: '
                        '"%s" is not callable in module %s' % (attr, module))
                logger.info("registering '%s.%s' => '%s" % (
                    module, attr, method))
                xmlrpc_dispatcher.register_function(func, method)


def register_xmlrpc_methods_helpers():
    """Register the introspection and multicall methods
    with the XML-RPC namespace.
    """
    xmlrpc_dispatcher.register_introspection_functions()
    xmlrpc_dispatcher.register_multicall_functions()

@@ -4929,22 +4929,20 @@
 r path, 
+na
 me
-thod
  in xm.X
@@ -4976,2138 +4976,41 @@
     
-# Check if an imported function got passed%0A                if isinstance(path, Callable):%0A                    # Method is a callable, so register it directly%0A                    logger.info(%22registering '%25s' =%3E '%25s')%22 %25 (path, method))%0A                    xmlrpc_dispatcher.register_function(path, method)%0A                    continue%0A                else:%0A                    logger.debug('%25s not callable, resolving path' %25 path)%0A                # Find the module containing the function%0A                dot = path.rindex('.')%0A                module, attr = path%5B:dot%5D, path%5Bdot + 1:%5D%0A                logger.debug('checking module %25s' %25 module)%0A                try:%0A                    # See if the module containing the path%0A                    # to function is importable%0A                    mod = import_module(module)%0A                except ImportError as ex:%0A                    # Couldn't import configured module, could be a typo%0A                    logger.warn(%22could not import '%25s', %22%0A                                %22please check your module's %25s%22 %25 (%0A                                    module, DIST_SETTINGS))%0A                    if settings.DEBUG:%0A                        raise ex%0A                    continue%0A                try:%0A                    # See if the function path is valid%0A                    func = getattr(mod, attr)%0A                except AttributeError:%0A                    raise ImproperlyConfigured(%0A                        'Error registering XML-RPC method: '%0A                        'module %25s doesn%5C't define a method %22%25s%22' %25 (%0A                            module, attr))%0A                if not isinstance(func, Callable):%0A                    # Path is not a callable,%0A                    # could be a variable containing something else%0A                    raise ImproperlyConfigured(%0A                        'Error registering XML-RPC method: '%0A                        '%22%25s%22 is not callable in module %25s' %25 (attr, module))%0A                logger.info(%22registering '%25s.%25s' =%3E '%25s%22 %25 (%0A                    module, attr, method))%0A                xmlrpc_dispatcher.register_function(func, method
+register_xmlrpc_method(path, name
 )%0A%0A%0A

Apply DRY principle with register_xmlrpc_method