<s> import logging <EOL> import re <EOL> import dircache <EOL> import imp <EOL> import os <EOL> from functools import wraps <EOL> from django . conf import settings <EOL> from django_url_framework . helper import ApplicationHelper <EOL> from django_url_framework . controller import ActionController <EOL> from django_url_framework . controller import InvalidControllerError <EOL> from django_url_framework . controller import InvalidActionError <EOL> from django_url_framework . controller import get_actions <EOL> from django_url_framework . controller import _patterns <EOL> from django_url_framework . controller import get_action_name <EOL> from django_url_framework . controller import get_controller_name <EOL> from django_url_framework . controller import get_controller_urlconf <EOL> from django . utils . translation import ugettext as _ <EOL> from django . http import Http404 <EOL> from django . conf . urls import include , url <EOL> from importlib import import_module <EOL> class Site ( object ) : <EOL> def __init__ ( self ) : <EOL> self . controllers = { } <EOL> self . helpers = { } <EOL> self . logger = logging . getLogger ( __name__ ) <EOL> def autodiscover ( self , include_apps = [ ] , exclude_apps = [ ] ) : <EOL> """<STR_LIT>""" <EOL> if type ( include_apps ) not in ( list , tuple ) : <EOL> include_apps = ( include_apps , ) <EOL> if type ( exclude_apps ) not in ( list , tuple ) : <EOL> exclude_apps = ( exclude_apps , ) <EOL> if len ( include_apps ) == <NUM_LIT:0> : <EOL> include_apps = settings . INSTALLED_APPS <EOL> for app in settings . INSTALLED_APPS : <EOL> must_skip = False <EOL> if app . endswith ( '<STR_LIT>' ) : <EOL> continue <EOL> for inc in include_apps : <EOL> if re . search ( inc , app ) : <EOL> must_skip = False <EOL> break <EOL> else : <EOL> must_skip = True <EOL> for excl in exclude_apps : <EOL> if re . search ( excl , app ) : <EOL> must_skip = True <EOL> break <EOL> if must_skip : <EOL> continue <EOL> try : <EOL> available_controllers = [ ] <EOL> app_path = import_module ( app ) . __path__ <EOL> if app_path [ <NUM_LIT:0> ] [ - <NUM_LIT:1> ] != os . path . sep : <EOL> app_path [ <NUM_LIT:0> ] = app_path [ <NUM_LIT:0> ] + os . path . sep <EOL> for f in dircache . listdir ( app_path [ <NUM_LIT:0> ] ) : <EOL> if f . endswith ( '<STR_LIT>' ) : <EOL> available_controllers . append ( f [ : - <NUM_LIT> ] ) <EOL> self . load_controllers ( app_path , available_controllers ) <EOL> except AttributeError , e : <EOL> self . logger . exception ( e ) <EOL> continue <EOL> def load_controllers ( self , app_path , controllers ) : <EOL> found_controller , found_helper = ( None , None ) <EOL> for controller_file in controllers : <EOL> """<STR_LIT>""" <EOL> try : <EOL> found_controller = imp . find_module ( '<STR_LIT>' % controller_file , app_path ) <EOL> except ImportError , e : <EOL> self . logger . warning ( "<STR_LIT>" % controller_file , exc_info = True ) <EOL> continue <EOL> else : <EOL> controller_module = imp . load_module ( '<STR_LIT>' % controller_file , * found_controller ) <EOL> self . logger . debug ( "<STR_LIT>" % controller_file ) <EOL> for controller_class_name in dir ( controller_module ) : <EOL> if not controller_class_name . endswith ( '<STR_LIT>' ) : <EOL> continue <EOL> controller_class = getattr ( controller_module , controller_class_name ) <EOL> if controller_class != ActionController and issubclass ( controller_class , ActionController ) : <EOL> controller_name = get_controller_name ( controller_class ) <EOL> if controller_name in self . controllers : <EOL> continue <EOL> self . controllers [ controller_name ] = controller_class <EOL> """<STR_LIT>""" <EOL> try : <EOL> found_helper = imp . find_module ( '<STR_LIT>' % controller_file , app_path ) <EOL> except ImportError , e : <EOL> self . logger . debug ( "<STR_LIT>" % controller_name ) <EOL> continue <EOL> else : <EOL> helper_module = imp . load_module ( '<STR_LIT>' % controller_file , * found_helper ) <EOL> self . logger . debug ( "<STR_LIT>" % controller_name ) <EOL> helper_class = getattr ( helper_module , '<STR_LIT>' % controller_file . title ( ) , None ) <EOL> if helper_class and issubclass ( helper_class , ApplicationHelper ) : <EOL> self . helpers [ controller_name ] = helper_class <EOL> finally : <EOL> if found_helper : <EOL> found_helper [ <NUM_LIT:0> ] . close ( ) <EOL> finally : <EOL> if found_controller : <EOL> found_controller [ <NUM_LIT:0> ] . close ( ) <EOL> def _get_urls ( self ) : <EOL> urlpatterns = _patterns ( ) <EOL> for controller_name , controller_class in self . controllers . items ( ) : <EOL> urlpatterns += _patterns ( url ( r'<STR_LIT>' % { '<STR_LIT>' : controller_name } , include ( get_controller_urlconf ( controller_class , site = self ) ) ) ) <EOL> return urlpatterns <EOL> urls = property ( _get_urls ) </s>
