<s> """<STR_LIT>""" <EOL> import collections <EOL> import copy <EOL> import inspect <EOL> import logging <EOL> import os <EOL> from django . conf import settings <EOL> from django . conf . urls import include <EOL> from django . conf . urls import patterns <EOL> from django . conf . urls import url <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . datastructures import SortedDict <EOL> from django . utils . functional import SimpleLazyObject <EOL> from django . utils . importlib import import_module <EOL> from django . utils . module_loading import module_has_submodule <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from horizon import conf <EOL> from horizon . decorators import _current_component <EOL> from horizon . decorators import require_auth <EOL> from horizon . decorators import require_perms <EOL> from horizon import loaders <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def _decorate_urlconf ( urlpatterns , decorator , * args , ** kwargs ) : <EOL> for pattern in urlpatterns : <EOL> if getattr ( pattern , '<STR_LIT>' , None ) : <EOL> pattern . _callback = decorator ( pattern . callback , * args , ** kwargs ) <EOL> if getattr ( pattern , '<STR_LIT>' , [ ] ) : <EOL> _decorate_urlconf ( pattern . url_patterns , decorator , * args , ** kwargs ) <EOL> class NotRegistered ( Exception ) : <EOL> pass <EOL> class HorizonComponent ( object ) : <EOL> def __init__ ( self ) : <EOL> super ( HorizonComponent , self ) . __init__ ( ) <EOL> if not self . slug : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' <EOL> % self . __class__ ) <EOL> def __unicode__ ( self ) : <EOL> name = getattr ( self , '<STR_LIT:name>' , u"<STR_LIT>" % self . __class__ . __name__ ) <EOL> return unicode ( name ) <EOL> def _get_default_urlpatterns ( self ) : <EOL> package_string = '<STR_LIT:.>' . join ( self . __module__ . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) <EOL> if getattr ( self , '<STR_LIT>' , None ) : <EOL> try : <EOL> mod = import_module ( '<STR_LIT>' % self . urls , package_string ) <EOL> except ImportError : <EOL> mod = import_module ( self . urls ) <EOL> urlpatterns = mod . urlpatterns <EOL> else : <EOL> if module_has_submodule ( import_module ( package_string ) , '<STR_LIT>' ) : <EOL> urls_mod = import_module ( '<STR_LIT>' , package_string ) <EOL> urlpatterns = urls_mod . urlpatterns <EOL> else : <EOL> urlpatterns = patterns ( '<STR_LIT>' ) <EOL> return urlpatterns <EOL> class Registry ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _registry = { } <EOL> if not getattr ( self , '<STR_LIT>' , None ) : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def _register ( self , cls ) : <EOL> """<STR_LIT>""" <EOL> if not inspect . isclass ( cls ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> elif not issubclass ( cls , self . _registerable_class ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> % self . _registerable_class . __name__ ) <EOL> if cls not in self . _registry : <EOL> cls . _registered_with = self <EOL> self . _registry [ cls ] = cls ( ) <EOL> return self . _registry [ cls ] <EOL> def _unregister ( self , cls ) : <EOL> """<STR_LIT>""" <EOL> if not issubclass ( cls , self . _registerable_class ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % self . _registerable_class ) <EOL> if cls not in self . _registry . keys ( ) : <EOL> raise NotRegistered ( '<STR_LIT>' % cls ) <EOL> del self . _registry [ cls ] <EOL> return True <EOL> def _registered ( self , cls ) : <EOL> if inspect . isclass ( cls ) and issubclass ( cls , self . _registerable_class ) : <EOL> found = self . _registry . get ( cls , None ) <EOL> if found : <EOL> return found <EOL> else : <EOL> for registered in self . _registry . values ( ) : <EOL> if registered . slug == cls : <EOL> return registered <EOL> class_name = self . _registerable_class . __name__ <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> parent = self . _registered_with . _registerable_class . __name__ <EOL> raise NotRegistered ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % { "<STR_LIT:type>" : class_name , <EOL> "<STR_LIT>" : cls , <EOL> "<STR_LIT>" : parent , <EOL> "<STR_LIT:name>" : self . slug } ) <EOL> else : <EOL> slug = getattr ( cls , "<STR_LIT>" , cls ) <EOL> raise NotRegistered ( '<STR_LIT>' <EOL> '<STR_LIT>' % { "<STR_LIT:type>" : class_name , <EOL> "<STR_LIT>" : slug } ) <EOL> class Panel ( HorizonComponent ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> urls = None <EOL> nav = True <EOL> index_url_name = "<STR_LIT:index>" <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . slug <EOL> def get_absolute_url ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return reverse ( '<STR_LIT>' % ( self . _registered_with . slug , <EOL> self . slug , <EOL> self . index_url_name ) ) <EOL> except Exception as exc : <EOL> LOG . info ( "<STR_LIT>" % ( self , exc ) ) <EOL> raise <EOL> @ property <EOL> def _decorated_urls ( self ) : <EOL> urlpatterns = self . _get_default_urlpatterns ( ) <EOL> permissions = getattr ( self , '<STR_LIT>' , [ ] ) <EOL> _decorate_urlconf ( urlpatterns , require_perms , permissions ) <EOL> _decorate_urlconf ( urlpatterns , _current_component , panel = self ) <EOL> return urlpatterns , self . slug , self . slug <EOL> class PanelGroup ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dashboard , slug = None , name = None , panels = None ) : <EOL> self . dashboard = dashboard <EOL> self . slug = slug or getattr ( self , "<STR_LIT>" , "<STR_LIT:default>" ) <EOL> self . name = name or getattr ( self , "<STR_LIT:name>" , None ) <EOL> self . panels = list ( panels or getattr ( self , "<STR_LIT>" , [ ] ) ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . slug ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def __iter__ ( self ) : <EOL> panel_instances = [ ] <EOL> for name in self . panels : <EOL> try : <EOL> panel_instances . append ( self . dashboard . get_panel ( name ) ) <EOL> except NotRegistered as e : <EOL> LOG . debug ( e ) <EOL> return iter ( panel_instances ) <EOL> class Dashboard ( Registry , HorizonComponent ) : <EOL> """<STR_LIT>""" <EOL> _registerable_class = Panel <EOL> name = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> urls = None <EOL> panels = [ ] <EOL> default_panel = None <EOL> nav = True <EOL> supports_tenants = False <EOL> public = False <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . slug <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Dashboard , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _panel_groups = None <EOL> def get_panel ( self , panel ) : <EOL> """<STR_LIT>""" <EOL> return self . _registered ( panel ) <EOL> def get_panels ( self ) : <EOL> """<STR_LIT>""" <EOL> all_panels = [ ] <EOL> panel_groups = self . get_panel_groups ( ) <EOL> for panel_group in panel_groups . values ( ) : <EOL> all_panels . extend ( panel_group ) <EOL> return all_panels <EOL> def get_panel_group ( self , slug ) : <EOL> return self . _panel_groups [ slug ] <EOL> def get_panel_groups ( self ) : <EOL> registered = copy . copy ( self . _registry ) <EOL> panel_groups = [ ] <EOL> if self . _panel_groups is not None : <EOL> for panel_group in self . _panel_groups . values ( ) : <EOL> for panel in panel_group : <EOL> registered . pop ( panel . __class__ ) <EOL> panel_groups . append ( ( panel_group . slug , panel_group ) ) <EOL> if len ( registered ) : <EOL> slugs = [ panel . slug for panel in registered . values ( ) ] <EOL> new_group = PanelGroup ( self , <EOL> slug = "<STR_LIT>" , <EOL> name = _ ( "<STR_LIT>" ) , <EOL> panels = slugs ) <EOL> panel_groups . append ( ( new_group . slug , new_group ) ) <EOL> return SortedDict ( panel_groups ) <EOL> def get_absolute_url ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _registered ( self . default_panel ) . get_absolute_url ( ) <EOL> except Exception : <EOL> LOG . exception ( "<STR_LIT>" % self ) <EOL> raise <EOL> @ property <EOL> def _decorated_urls ( self ) : <EOL> urlpatterns = self . _get_default_urlpatterns ( ) <EOL> default_panel = None <EOL> for panel in self . _registry . values ( ) : <EOL> if panel . slug == self . default_panel : <EOL> default_panel = panel <EOL> continue <EOL> url_slug = panel . slug . replace ( '<STR_LIT:.>' , '<STR_LIT:/>' ) <EOL> urlpatterns += patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' % url_slug , include ( panel . _decorated_urls ) ) ) <EOL> if not default_panel : <EOL> raise NotRegistered ( '<STR_LIT>' <EOL> % self . default_panel ) <EOL> urlpatterns += patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , include ( default_panel . _decorated_urls ) ) ) <EOL> if not self . public : <EOL> _decorate_urlconf ( urlpatterns , require_auth ) <EOL> permissions = getattr ( self , '<STR_LIT>' , [ ] ) <EOL> _decorate_urlconf ( urlpatterns , require_perms , permissions ) <EOL> _decorate_urlconf ( urlpatterns , _current_component , dashboard = self ) <EOL> return urlpatterns , self . slug , self . slug <EOL> def _autodiscover ( self ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( self , "<STR_LIT>" , False ) : <EOL> return <EOL> panels_to_discover = [ ] <EOL> panel_groups = [ ] <EOL> if all ( [ isinstance ( i , basestring ) for i in self . panels ] ) : <EOL> self . panels = [ self . panels ] <EOL> for panel_set in self . panels : <EOL> if not isinstance ( panel_set , collections . Iterable ) and issubclass ( panel_set , PanelGroup ) : <EOL> panel_group = panel_set ( self ) <EOL> elif not isinstance ( panel_set , PanelGroup ) : <EOL> panel_group = PanelGroup ( self , panels = panel_set ) <EOL> panels_to_discover . extend ( panel_group . panels ) <EOL> panel_groups . append ( ( panel_group . slug , panel_group ) ) <EOL> self . _panel_groups = SortedDict ( panel_groups ) <EOL> package = '<STR_LIT:.>' . join ( self . __module__ . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) <EOL> mod = import_module ( package ) <EOL> for panel in panels_to_discover : <EOL> try : <EOL> before_import_registry = copy . copy ( self . _registry ) <EOL> import_module ( '<STR_LIT>' % panel , package ) <EOL> except Exception : <EOL> self . _registry = before_import_registry <EOL> if module_has_submodule ( mod , panel ) : <EOL> raise <EOL> self . _autodiscover_complete = True <EOL> @ classmethod <EOL> def register ( cls , panel ) : <EOL> """<STR_LIT>""" <EOL> panel_class = Horizon . register_panel ( cls , panel ) <EOL> panel_mod = import_module ( panel . __module__ ) <EOL> panel_dir = os . path . dirname ( panel_mod . __file__ ) <EOL> template_dir = os . path . join ( panel_dir , "<STR_LIT>" ) <EOL> if os . path . exists ( template_dir ) : <EOL> key = os . path . join ( cls . slug , panel . slug ) <EOL> loaders . panel_template_dirs [ key ] = template_dir <EOL> return panel_class <EOL> @ classmethod <EOL> def unregister ( cls , panel ) : <EOL> """<STR_LIT>""" <EOL> success = Horizon . unregister_panel ( cls , panel ) <EOL> if success : <EOL> key = os . path . join ( cls . slug , panel . slug ) <EOL> if key in loaders . panel_template_dirs : <EOL> del loaders . panel_template_dirs [ key ] <EOL> return success <EOL> class Workflow ( object ) : <EOL> def __init__ ( * args , ** kwargs ) : <EOL> raise NotImplementedError ( ) <EOL> try : <EOL> from django . utils . functional import empty <EOL> except ImportError : <EOL> empty = None <EOL> class LazyURLPattern ( SimpleLazyObject ) : <EOL> def __iter__ ( self ) : <EOL> if self . _wrapped is empty : <EOL> self . _setup ( ) <EOL> return iter ( self . _wrapped ) <EOL> def __reversed__ ( self ) : <EOL> if self . _wrapped is empty : <EOL> self . _setup ( ) <EOL> return reversed ( self . _wrapped ) <EOL> def __len__ ( self ) : <EOL> if self . _wrapped is empty : <EOL> self . _setup ( ) <EOL> return len ( self . _wrapped ) <EOL> def __getitem__ ( self , idx ) : <EOL> if self . _wrapped is empty : <EOL> self . _setup ( ) <EOL> return self . _wrapped [ idx ] <EOL> class Site ( Registry , HorizonComponent ) : <EOL> """<STR_LIT>""" <EOL> _registerable_class = Dashboard <EOL> name = "<STR_LIT>" <EOL> namespace = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> urls = '<STR_LIT>' <EOL> def __repr__ ( self ) : <EOL> return u"<STR_LIT>" % self . slug <EOL> @ property <EOL> def _conf ( self ) : <EOL> return conf . HORIZON_CONFIG <EOL> @ property <EOL> def dashboards ( self ) : <EOL> return self . _conf [ '<STR_LIT>' ] <EOL> @ property <EOL> def default_dashboard ( self ) : <EOL> return self . _conf [ '<STR_LIT>' ] <EOL> def register ( self , dashboard ) : <EOL> """<STR_LIT>""" <EOL> return self . _register ( dashboard ) <EOL> def unregister ( self , dashboard ) : <EOL> """<STR_LIT>""" <EOL> return self . _unregister ( dashboard ) <EOL> def registered ( self , dashboard ) : <EOL> return self . _registered ( dashboard ) <EOL> def register_panel ( self , dashboard , panel ) : <EOL> dash_instance = self . registered ( dashboard ) <EOL> return dash_instance . _register ( panel ) <EOL> def unregister_panel ( self , dashboard , panel ) : <EOL> dash_instance = self . registered ( dashboard ) <EOL> if not dash_instance : <EOL> raise NotRegistered ( "<STR_LIT>" <EOL> % dashboard ) <EOL> return dash_instance . _unregister ( panel ) <EOL> def get_dashboard ( self , dashboard ) : <EOL> """<STR_LIT>""" <EOL> return self . _registered ( dashboard ) <EOL> def get_dashboards ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . dashboards : <EOL> registered = copy . copy ( self . _registry ) <EOL> dashboards = [ ] <EOL> for item in self . dashboards : <EOL> dashboard = self . _registered ( item ) <EOL> dashboards . append ( dashboard ) <EOL> registered . pop ( dashboard . __class__ ) <EOL> if len ( registered ) : <EOL> extra = registered . values ( ) <EOL> extra . sort ( ) <EOL> dashboards . extend ( extra ) <EOL> return dashboards <EOL> else : <EOL> dashboards = self . _registry . values ( ) <EOL> dashboards . sort ( ) <EOL> return dashboards <EOL> def get_default_dashboard ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . default_dashboard : <EOL> return self . _registered ( self . default_dashboard ) <EOL> elif len ( self . _registry ) : <EOL> return self . get_dashboards ( ) [ <NUM_LIT:0> ] <EOL> else : <EOL> raise NotRegistered ( "<STR_LIT>" ) <EOL> def get_user_home ( self , user ) : <EOL> """<STR_LIT>""" <EOL> user_home = self . _conf [ '<STR_LIT>' ] <EOL> if user_home : <EOL> if callable ( user_home ) : <EOL> return user_home ( user ) <EOL> elif isinstance ( user_home , basestring ) : <EOL> if user_home . find ( "<STR_LIT:/>" ) != - <NUM_LIT:1> : <EOL> return user_home <EOL> else : <EOL> mod , func = user_home . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> return getattr ( import_module ( mod ) , func ) ( user ) <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> return self . get_absolute_url ( ) <EOL> def get_absolute_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_default_dashboard ( ) . get_absolute_url ( ) <EOL> @ property <EOL> def _lazy_urls ( self ) : <EOL> """<STR_LIT>""" <EOL> def url_patterns ( ) : <EOL> return self . _urls ( ) [ <NUM_LIT:0> ] <EOL> return LazyURLPattern ( url_patterns ) , self . namespace , self . slug <EOL> def _urls ( self ) : <EOL> """<STR_LIT>""" <EOL> urlpatterns = self . _get_default_urlpatterns ( ) <EOL> self . _autodiscover ( ) <EOL> for dash in self . _registry . values ( ) : <EOL> dash . _autodiscover ( ) <EOL> if self . _conf . get ( "<STR_LIT>" , None ) : <EOL> customization_module = self . _conf [ "<STR_LIT>" ] <EOL> bits = customization_module . split ( '<STR_LIT:.>' ) <EOL> mod_name = bits . pop ( ) <EOL> package = '<STR_LIT:.>' . join ( bits ) <EOL> mod = import_module ( package ) <EOL> try : <EOL> before_import_registry = copy . copy ( self . _registry ) <EOL> import_module ( '<STR_LIT>' % ( package , mod_name ) ) <EOL> except Exception : <EOL> self . _registry = before_import_registry <EOL> if module_has_submodule ( mod , mod_name ) : <EOL> raise <EOL> for dash in self . _registry . values ( ) : <EOL> urlpatterns += patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' % dash . slug , include ( dash . _decorated_urls ) ) ) <EOL> return urlpatterns , self . namespace , self . slug <EOL> def _autodiscover ( self ) : <EOL> """<STR_LIT>""" <EOL> if not getattr ( self , '<STR_LIT>' , None ) : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> for mod_name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for app in settings . INSTALLED_APPS : <EOL> mod = import_module ( app ) <EOL> try : <EOL> before_import_registry = copy . copy ( self . _registry ) <EOL> import_module ( '<STR_LIT>' % ( app , mod_name ) ) <EOL> except Exception : <EOL> self . _registry = before_import_registry <EOL> if module_has_submodule ( mod , mod_name ) : <EOL> raise <EOL> class HorizonSite ( Site ) : <EOL> """<STR_LIT>""" <EOL> _instance = None <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> if not cls . _instance : <EOL> cls . _instance = super ( Site , cls ) . __new__ ( cls , * args , ** kwargs ) <EOL> return cls . _instance <EOL> Horizon = HorizonSite ( ) </s>
