def discover(cls, storage=False): ''' Discover installed platforms, cache them globally. ''' if (hasattr(cls, '_injected') and not cls._injected) or not hasattr(cls, '_injected'): # Setup environment adapters = {} platforms = [] environ = os.environ if _APPCONFIG: config = sysconfig.config.get('apptools.system.platform', {}) _found = config.get('installed_platforms', []) else: config = {'debug': True} _found = list(_BUILTIN_PLATFORMS[:]) # Consider installed platforms for platform in _found: try: # Import adapter if we don't have it yet if platform.get('path') not in adapters.keys(): platform_adapter = _loadModule(('.'.join(platform.get('path').split('.')[0:-1]), platform.get('path').split('.')[-1])) adapters[platform.get('path')] = platform_adapter else: platform_adapter = adapters.get(platform.get('path')) # Check if the platform is compatible if hasattr(platform_adapter, 'check_environment'): assert platform_adapter.check_environment(environ, sysconfig) is True # Couldn't find the platform... except ImportError: if (not _APPCONFIG) or sysconfig.debug: logging.error('Platform "%s" is mentioned in config but could not be found at the configured path ("%s").' % (platform.get('name', 'UnkownPlatform'), platform.get('path'))) continue # Platform wasn't compatible... except AssertionError: if (not _APPCONFIG) or sysconfig.debug: logging.debug('Platform "%s" was tested and is not compatible with this environment. Continuing.' % str(platform.get('name', 'UnknownPlatform'))) continue # Platform is A-OK else: # Create it, so it has a chance to init platform['adapter'] = platform_adapter() platforms.append(platform) # Set to globals cls.adapters = adapters.items() cls.platforms = platforms return cls
def lazyload(self, module): ''' Lazy load a module, or return False if it cannot be found/imported. ''' try: module = _loadModule(module) except ImportError: self.logging.warning('Could not resolve shortcutted module "' + module + '". Encountered ImportError, returning False.') if config.debug: raise return False else: return module
def _resolve_services(svcs=_project_services, load=False): ''' Resolve installed service classes, optionally importing them as we go. ''' services = [] for service, cfg in svcs['services'].items(): if cfg.get('enabled', True): urlpath = '/'.join(svcs.get('config', {}).get('url_prefix').split('/') + [service]) servicepath = cfg['service'] if load: sp_split = servicepath.split('.') servicepath = util._loadModule(('.'.join(sp_split[0:-1]), sp_split[-1])) services.append((urlpath, servicepath)) return services
def _lazyloader(self, module): ''' Lazy load a module, or return False if it cannot be found/imported. ''' if not config.debug: if module in sys.modules: return sys.modules[module] try: module = _loadModule(module) except ImportError: self.logging.warning('Could not resolve shortcutted module "' + str(module) + '". Encountered ImportError, assigning to empty DictProxy.') if config.debug: raise return module
def installed_mappers(self): ''' Return installed mappers, calculated from config. ''' global _global_debug # Decide whether we should output logs if self._servicesConfig.get('debug', False) is True: output_debug = True else: output_debug = False mappers = [] for mapper in self._globalServicesConfig.get('mappers', []): if mapper.get('enabled', False) is not True: if output_debug: self.logging.info('Mapper at name "' + str(mapper.get('name', 'UnknownMapper')) + '" skipped according to config.') continue try: mapper_cls = _loadModule(tuple(['.'.join(mapper.get('path').split('.')[0:-1]), mapper.get('path').split('.')[-1]])) mapper = mapper_cls() except ImportError: self.logging.warning('Invalid path to RPCMapper of name "%s". Given path: "%s" does not exist or otherwise could not be imported.' % (str(mapper.get('name', 'UnknownMapper')), str(mapper.get('path', 'UnknownPath')))) if _global_debug: raise else: continue except Exception, e: self.logging.error('Unknown exception encountered when trying to install the RPC mapper at name "%s" with path "%s". Exception: "%s".' % (str(mapper.get('name', 'UnknownMapper')), str(mapper.get('path', 'UnknownPath')), str(e))) if _global_debug: raise else: continue else: mappers.append(mapper)
def discover(cls): ''' Discover installed platforms, cache them globally. ''' # Globals global logging global _adapters global _platforms # Setup environment adapters = {} platforms = [] environ = os.environ config = sysconfig.config.get('apptools.system.platform') # If we have everything cached, just return it if len(_adapters) > 0 and len(_platforms) > 0: adapters = _adapters platforms = _platforms return platforms, adapters # Build our list of platforms/adapters manually else: # Consider installed platforms for platform in config.get('installed_platforms', []): try: # Import adapter if we don't have it yet if platform.get('path') not in adapters.keys(): platform_adapter = _loadModule(('.'.join(platform.get('path').split('.')[0:-1]), platform.get('path').split('.')[-1])) adapters[platform.get('path')] = platform_adapter else: platform_adapter = adapters.get(platform.get('path')) # Check if the platform is compatible if hasattr(platform_adapter, 'check_environment'): assert platform_adapter.check_environment(environ, config) == True # Couldn't find the platform... except ImportError: if sysconfig.debug: logging.error('Platform "%s" is mentioned in config but could not be found at the configured path ("%s").' % (platform.get('name', 'UnkownPlatform'), platform.get('path'))) continue # Platform wasn't compatible... except AssertionError: if sysconfig.debug: logging.debug('Platform "%s" was tested and is not compatible with this environment. Continuing.' % str(platform.get('name', 'UnknownPlatform'))) continue # Platform is A-OK else: # Create it, so it has a chance to init platform['adapter'] = platform_adapter() platforms.append(platform) # Set to globals _adapters = dict(adapters.items()[:]) _platforms = platforms[:] return cls