| 1 |  |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | log = logging.getLogger(__name__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | class ModuleManagerMeta(type): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     def __getitem__(self, key): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |         return self.modules[key] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class ModuleManager(object): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     __metaclass__ = ModuleManagerMeta | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     modules = {} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     @classmethod | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     def initialize(cls): | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         cls.modules = dict(cls.construct()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     def discover(cls): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         from plugin.modules.scheduler.main import Scheduler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         from plugin.modules.upgrade.main import Upgrade | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             Scheduler, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             Upgrade | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     def construct(cls): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             available = cls.discover() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         except Exception, ex: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             log.error('Unable to import modules: %s', ex, exc_info=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         constructed = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         for module in available: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 if module.__key__ is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                     # Automatically set module `__key__` (if one isn't specified) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                     module.__key__ = module.__class__.__name__.lower() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 yield module.__key__, module() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 constructed.append(module.__key__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             except Exception, ex: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 log.warn('Unable to construct module: %r', module) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         log.debug('Constructed %d module(s): %s', len(constructed), ', '.join(constructed)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     def start(cls): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         started = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         for key, module in cls.modules.items(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 module.start() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 started.append(key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             except Exception, ex: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 log.warn('Unable to start %r module - %s', key, ex, exc_info=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         log.debug('Started %d module(s): %s', len(started), ', '.join(started)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     def get(self, key, default=None): | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |         return self.modules.get(key, default) | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |  |