<s> import os <EOL> import logging <EOL> import re <EOL> import sys <EOL> import traceback <EOL> import pkg_resources <EOL> import desktop <EOL> from desktop . lib . paths import get_desktop_root <EOL> from django . utils . translation import ugettext as _ <EOL> APP_DIRS = [ get_desktop_root ( '<STR_LIT>' ) , <EOL> get_desktop_root ( '<STR_LIT>' ) , <EOL> get_desktop_root ( '<STR_LIT>' ) ] <EOL> LOG = logging . getLogger ( __name__ ) <EOL> DESKTOP_LIBS = None <EOL> DESKTOP_APPS = None <EOL> DESKTOP_MODULES = [ ] <EOL> def _import_module_or_none ( module ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> __import__ ( module ) <EOL> return sys . modules [ module ] <EOL> except ImportError , ie : <EOL> tb = sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> top_frame = traceback . extract_tb ( tb ) [ - <NUM_LIT:1> ] <EOL> err_file = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , top_frame [ <NUM_LIT:0> ] ) <EOL> my_file = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , __file__ ) <EOL> if err_file == my_file : <EOL> return None <EOL> else : <EOL> LOG . error ( "<STR_LIT>" % ( module , ) ) <EOL> raise <EOL> class DesktopModuleInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , module ) : <EOL> """<STR_LIT>""" <EOL> self . module = module <EOL> self . name = module . __name__ <EOL> self . display_name = module . __name__ <EOL> module_root = os . path . dirname ( module . __file__ ) <EOL> self . root_dir = os . path . join ( module_root , "<STR_LIT:..>" , "<STR_LIT:..>" ) <EOL> self . _load_settings_module ( module . __name__ + "<STR_LIT>" ) <EOL> if hasattr ( self . settings , "<STR_LIT>" ) : <EOL> self . nice_name = self . settings . NICE_NAME <EOL> else : <EOL> self . nice_name = self . name <EOL> if hasattr ( self . settings , "<STR_LIT>" ) : <EOL> self . icon_path = self . settings . ICON <EOL> else : <EOL> self . icon_path = "<STR_LIT>" <EOL> if hasattr ( self . settings , "<STR_LIT>" ) : <EOL> self . menu_index = self . settings . MENU_INDEX <EOL> else : <EOL> self . menu_index = <NUM_LIT> <EOL> self . is_url_namespaced = hasattr ( self . settings , '<STR_LIT>' ) <EOL> if self . config_key is not None : <EOL> self . display_name = self . config_key <EOL> new_style , old_style = [ os . path . abspath ( p ) for p in [ <EOL> os . path . join ( module_root , "<STR_LIT>" , self . name ) , <EOL> os . path . join ( self . root_dir , "<STR_LIT>" ) <EOL> ] ] <EOL> self . static_dir = None <EOL> if os . path . isdir ( new_style ) : <EOL> self . static_dir = new_style <EOL> else : <EOL> if os . path . isdir ( old_style ) : <EOL> LOG . debug ( "<STR_LIT>" + str ( old_style ) ) <EOL> self . static_dir = old_style <EOL> self . help_dir = None <EOL> if self . static_dir : <EOL> potential_help_dir = os . path . join ( self . static_dir , "<STR_LIT>" ) <EOL> if os . path . exists ( potential_help_dir ) : <EOL> self . help_dir = potential_help_dir <EOL> def _load_settings_module ( self , module_name ) : <EOL> """<STR_LIT>""" <EOL> s = _import_module_or_none ( module_name ) <EOL> if s is not None : <EOL> self . django_apps = getattr ( s , '<STR_LIT>' , [ ] ) <EOL> self . config_key = getattr ( s , '<STR_LIT>' , None ) <EOL> else : <EOL> self . django_apps = [ ] <EOL> self . config_key = None <EOL> def _resolve_appdir_path ( self , path ) : <EOL> """<STR_LIT>""" <EOL> return os . path . abspath ( os . path . join ( self . root_dir , path ) ) <EOL> @ property <EOL> def urls ( self ) : <EOL> return self . _submodule ( "<STR_LIT>" ) <EOL> @ property <EOL> def conf ( self ) : <EOL> return self . _submodule ( "<STR_LIT>" ) <EOL> def get_bootstrap_file ( self ) : <EOL> if self . static_dir : <EOL> f = os . path . join ( self . static_dir , "<STR_LIT>" ) <EOL> if os . path . exists ( f ) : <EOL> return file ( f ) <EOL> else : <EOL> return None <EOL> @ property <EOL> def settings ( self ) : <EOL> return self . _submodule ( "<STR_LIT>" ) <EOL> @ property <EOL> def locale_path ( self ) : <EOL> return os . path . join ( os . path . dirname ( self . module . __file__ ) , '<STR_LIT>' ) <EOL> def _submodule ( self , name ) : <EOL> return _import_module_or_none ( self . module . __name__ + "<STR_LIT:.>" + name ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . nice_name , self . module . __name__ ) <EOL> def get_apps ( user ) : <EOL> return filter ( lambda app : user . has_hue_permission ( action = "<STR_LIT>" , app = app . display_name ) , DESKTOP_APPS ) <EOL> def get_apps_dict ( user = None ) : <EOL> if user is not None : <EOL> apps = get_apps ( user ) <EOL> else : <EOL> apps = DESKTOP_APPS <EOL> return dict ( [ ( app . name , app ) for app in apps ] ) <EOL> def load_libs ( ) : <EOL> global DESKTOP_MODULES <EOL> global DESKTOP_LIBS <EOL> if DESKTOP_LIBS is not None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> DESKTOP_LIBS = [ ] <EOL> for lib in pkg_resources . iter_entry_points ( "<STR_LIT>" ) : <EOL> m = lib . load ( ) <EOL> DESKTOP_LIBS . append ( DesktopModuleInfo ( m ) ) <EOL> LOG . debug ( "<STR_LIT>" + "<STR_LIT:U+002CU+0020>" . join ( a . name for a in DESKTOP_LIBS ) ) <EOL> DESKTOP_MODULES += DESKTOP_LIBS <EOL> DESKTOP_MODULES . append ( DesktopModuleInfo ( desktop ) ) <EOL> def load_apps ( app_blacklist ) : <EOL> """<STR_LIT>""" <EOL> global DESKTOP_MODULES <EOL> global DESKTOP_APPS <EOL> if DESKTOP_APPS is not None : <EOL> raise Exception ( _ ( "<STR_LIT>" ) ) <EOL> DESKTOP_APPS = [ ] <EOL> for sdk_app in pkg_resources . iter_entry_points ( "<STR_LIT>" ) : <EOL> if sdk_app . name not in app_blacklist : <EOL> m = sdk_app . load ( ) <EOL> dmi = DesktopModuleInfo ( m ) <EOL> DESKTOP_APPS . append ( dmi ) <EOL> LOG . debug ( "<STR_LIT>" + "<STR_LIT:U+002CU+0020>" . join ( a . name for a in DESKTOP_APPS ) ) <EOL> DESKTOP_MODULES += DESKTOP_APPS <EOL> def get_desktop_module ( name ) : <EOL> """<STR_LIT>""" <EOL> global DESKTOP_MODULES <EOL> for app in DESKTOP_MODULES : <EOL> if app . name == name : <EOL> return app <EOL> return None </s>
