
from django.conf.urls import url, include
from django.core.urlresolvers import reverse

from . import http

# TODO: Add other patterns to allow introspection?


class Api(object):
    '''Helper class for registering many Publishers in one URL namespace'''
    def __init__(self, name):
        self.name = name
        self.children = {}

    def patterns(self, flat=False):
        urlpatterns = [
            url(r'^$', self.index),
        ] + [
            url(r'^%s/' % name, include(child.patterns(self.name)))
            for name, child in self.children.items()
        ]
        if flat:
            return urlpatterns
        return [
            url(r'^%s/' % self.name, include(urlpatterns)),
        ]

    def index(self, request, *args, **kwargs):
        '''Return a dict of publisher name: url'''
        return http.JsonResponse(dict(
            (name, reverse('%s_%s_list_default' % (self.name, name), kwargs=kwargs))
            for name, child in self.children.items()
        ))

    def register(self, child, name=None):
        if name is None:
            name = getattr(child, 'api_name', child.__class__.__name__.lower())
        if name in self.children:
            raise Warning('Publisher with name %s already registered: %r -> %r' % (
                name, self.children[name], child
            ))
        self.children[name] = child

APIS = {}


def register(name, *args):
    try:
        api = APIS[name]
    except KeyError:
        api = APIS[name] = Api(name=name)
    for resource in args:
        api.register(resource)
    return api


def autodiscover():
    from django.conf import settings
    from django.utils.importlib import import_module
    from django.utils.module_loading import module_has_submodule

    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        # Attempt to import the app's api module.
        try:
            import_module('%s.serialiser' % app)
        except:

            # Decide whether to bubble up this error. If the app just
            # doesn't have an admin module, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(mod, 'serialiser'):
                raise


def patterns(flat=False):
    patterns = []
    for api in APIS.values():
        patterns.extend(api.patterns(flat=flat))
    return patterns

@@ -106,60 +106,8 @@
 tp%0A%0A
-# TODO: Add other patterns to allow introspection?%0A%0A
 %0Acla
@@ -1152,16 +1152,33 @@
 Warning(
+%0A                
 'Publish
@@ -1239,24 +1239,28 @@
             
+  
+  
 name, self.c
@@ -1292,17 +1292,34 @@
         
-)
+    )%0A            
 )%0A      
@@ -2255,24 +2255,27 @@
 False):%0A    
+url
 patterns = %5B
@@ -2314,16 +2314,19 @@
         
+url
 patterns
@@ -2369,16 +2369,19 @@
  return 
+url
 patterns

Tidy for pylint