def __init__(self): self.configuration = Configuration() self.settings = self.configuration.get_settings() self.logger = get_logger('uthportal', self.settings) self.db_manager = MongoDatabaseManager(self.settings) if not self.db_manager.connect(): self.logger.info('Exiting...') sys.exit(1) self.pushd_client = PushdClient( self.settings, self.db_manager, event_templates ) self.load_tasks() self.scheduler = Scheduler( self.tasks, self.settings)
def check_task(task_path, presult = False, outfile = None): dot_path = task_path # Get setings and init db_manager config = Configuration() settings = config.get_settings() db_manager = MemoryDatabaseManager() # Construct path & module name spath = dot_path.split('.') task_type = '.'.join(spath[:-1]) if task_type not in FIELDS_DICT: print 'Invalid task type, or only type given' print 'Your input was: [%s]' % dot_path print 'Valid task types are: \n\t%s' % "\n\t".join(FIELDS_DICT.keys()) sys.exit(1) path = 'uthportal/' + settings['library_path'] + '/' + '/'.join(spath) classname = spath[-1] # Retrieve task class task_class = None loader = get_loader(path) if not loader: print 'Unable to find module: ' + classname print 'Make sure it exists' return module = loader.load_module(classname) if not module: print 'Unable to import module: ' + dot_path return for name, object in getmembers(module): if isclass(object) and name == classname: task_class = object break if not task_class: print 'Unable to import task: ' + path return print '\n\nTask type: [%s], Task name: [%s]' %( task_type, classname) print 'Press Enter to continue' raw_input() # Initialize & call the task print '\nInitiallizing task...' task_instance = task_class('.'.join(spath), settings, db_manager) print '\nRunning task...' task_instance.__call__() # Contruct the data dictionary result = { } for field in task_instance.update_fields: result[field] = task_instance._get_document_field(task_instance.document, field) #Prettify result data data = dumps( result, cls=BSONEncoderEx, sort_keys=True, ensure_ascii=False, indent=4 ) if presult: print "\n==============================================================" print "Actual returned data:" print "==============================================================\n" print data print "\n==============================================================" print "\nDone...\nResults:" print "======================================" #Check if fields exist and are not empty fields_to_check = FIELDS_DICT[task_type] for field in fields_to_check: if field in result and result[field]: print "%s: [%s]" %(field, GREEN_TICK) else : print "%s: [%s]" %(field, RED_CROSS) if outfile: print "\nWriting data to file: [%s]..." % outfile[0] with open(outfile[0], 'w') as f: f.write(data.encode('utf8'))
class UthPortal(object): def __init__(self): self.configuration = Configuration() self.settings = self.configuration.get_settings() self.logger = get_logger('uthportal', self.settings) self.db_manager = MongoDatabaseManager(self.settings) if not self.db_manager.connect(): self.logger.info('Exiting...') sys.exit(1) self.pushd_client = PushdClient( self.settings, self.db_manager, event_templates ) self.load_tasks() self.scheduler = Scheduler( self.tasks, self.settings) def load_tasks(self): self.logger.info("Loading modules...") current_path = dirname(abspath(__file__)) full_libary_path = current_path + '/' + self.settings['library_path'] self.logger.debug('Modules will be loaded from "%s"' % full_libary_path) tasks = {} tasks_num = 0 for loader, module, is_pkg in walk_packages( [full_libary_path], onerror = self._import_error): #Load next package current_module = loader.find_module(module).load_module(module) if not is_pkg: instance = None class_name = None #list all classes for name, obj in getmembers(current_module): # class name must be contained in module name e.g. # module name: inf.courses.ce121 # class name: ce121 # this is to avoid importing interface/base classes if isclass(obj) and (name in current_module.__name__): self.logger.debug('Importing: %s object: %s' % (name, obj)) tasks_num += 1 class_name = name instance = obj(current_module.__name__, self.settings, self.db_manager, self.pushd_client ) modules = module.split('.') current_task = tasks for task in modules: if task not in current_task: if task == class_name: current_task[task] = instance else: current_task[task] = {} current_task = current_task[task] self.tasks = tasks self.logger.info('Loaded %s tasks' % tasks_num) def _import_error(self, name): self.logger.error("Error importing module %s" % name) error_type, value, traceback = sys.exc_info() self.logger.error(traceback) def start(self): self._start_scheduler() def stop(self): self.db_manager.disconnect() self._save_settings() def _start_scheduler(self): self.logger.debug('Starting the scheduler') self.scheduler.init() self.scheduler.start() self.logger.debug('Scheduler started successfully!') def _force_update(self, job_id=None): self.scheduler.force_update(job_id) def _stop_scheduler(self): pass def _load_settings(self): self.configuration.load_settings() self.settings = self.configuration.get_settings() def _save_settings(self): self.configuration.set_settings(self.settings) self.configuration.save_settings()