def set_module_config(data): """ :param data: dictionary that contain data keyword, and value of that, is list of module_config like : {data:{[{module_1_config},{module_2_config}, ...]}} :return: status and message :rtype: dict """ try: module_list = data['data'] for module in module_list: # updating module category and active try: # catch error if code does not exist query = ModuleModel.select().where( ModuleModel.code == module['code']) except DoesNotExist: raise InvalidInputError(' can not find ' + str(module['code'])) try: # try updating query = ModuleModel.update( category=module['category'], active=module['active']).where( ModuleModel.code == module['code']) query.execute() except Exception: raise DatabaseError(' can not access database') # return status of operation return { 'data': 'module_config update successfully', 'status': constants.STATUS_SUCCESS } except KeyError as e: # Occurs if one of config keyword missing in input return {'data': e, 'status': constants.STATUS_ERROR}
def is_action(route_code): try: result = ModuleModel.get(ModuleModel.code == route_code, ModuleModel.active == True, ModuleModel.is_action == True) return True except Exception: return None
def module_list(data=None): try: modules = ModuleModel.select().where(( ModuleModel.is_action == False) & (ModuleModel.active == True)) module_list = {} for module in modules: module_list.update({module.code: module.inout}) return {'data': module_list, 'status': constants.STATUS_SUCCESS} except Exception as e: return {'data': e.__str__(), 'status': constants.STATUS_ERROR}
def get_module_config(): """ :return: module_config and status :rtype: dict """ try: modules = ModuleModel.select().where(( ModuleModel.is_action == False) & (ModuleModel.active == True)) module_list = [] for module in modules: module_dic = ({ 'code': module.code, 'alias': module.alias, 'inout': module.inout, 'category': module.category, 'max': module.max, 'is_action': module.is_action, 'active': module.active }) module_list.append(module_dic) return {'data': module_list, 'status': constants.STATUS_SUCCESS} except Exception as e: return {'data': e.__str__(), 'status': constants.STATUS_ERROR}
def route_to_group(route_name): try: result = ModuleModel.get(ModuleModel.alias == route_name) return result.category except Exception: return None
def route_to_name(route_code): try: result = ModuleModel.get(ModuleModel.code == route_code, ModuleModel.active == True) return result.alias except Exception: raise
import os, sys from time import sleep from core.config import Setting from models.config import ConfigModel from models.module import ModuleModel ps = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(ps) from models.task import TaskModel # models = TaskModel.select().where(TaskModel.id == 1268) models = ModuleModel.select() # code = IntegerField(unique=True) # alias = CharField(unique=True) # inout = ComplexField() # category = CharField() # max = IntegerField(default=0) # maximum concurrent execution # is_action = BooleanField(default=False) # active = BooleanField(default=True) for model in models: print('code: ', model.code) print('alias: ', model.alias) print('category: ', model.category) print('is_action: ', model.is_action) # print('data: ', model.data) # print("response: ",model.response_data) pass # import subprocess # import resource # f = open('oout.txt','a')
def upgrade(self): # upgrading op self.manager.upgrade() configs = Update.default_modules for module in configs: try: model = ModuleModel() model.code = module.get('code') model.alias = module.get('alias') model.inout = module.get('inout') model.category = module.get('category') model.max = module.get('max', 0) model.is_action = module.get('is_action', False) model.active = module.get('active', True) try: model.save() except IntegrityError: continue except Exception as e: raise Exception('data base startup failed ' + str(e))
def upgrade(self): new_modules = [{ 'code': 11, 'alias': 'screenshot', 'inout': { 1: [9] }, 'category': 'action', 'max': 0, 'is_action': True, 'active': True }, { 'code': 105, 'alias': 'google_search', 'inout': { 1: [1], 2: [1], 3: [1], 4: [1], 5: [1], 8: [1], 11: [1], 12: [1], 0: [1] }, 'category': 'MQueue', 'max': 0, 'active': False }, { 'code': 101, 'alias': 'bing_search', 'inout': { 1: [1], 2: [1], 3: [1], 4: [1], 5: [1], 8: [1], 11: [1], 12: [1], 0: [1] }, 'category': 'MQueue', 'max': 0, 'active': False }, { 'code': 112, 'alias': 'facebook', 'inout': { 1: [0], 2: [0], 3: [0], 4: [0], 5: [0], 8: [0], 11: [0], 12: [0] }, 'category': 'MQueue', 'max': 0, 'active': True }, { 'code': 113, 'alias': 'hash', 'inout': { 6: [7] }, 'category': 'LQueue', 'max': 0, 'active': True }] drop_modules = [113, 101, 105, 112] for module in drop_modules: try: q = ModuleModel.delete().where(ModuleModel.code == module) q.execute() except Exception: pass for module in new_modules: try: model = ModuleModel() model.code = module.get('code') model.alias = module.get('alias') model.inout = module.get('inout') model.category = module.get('category') model.max = module.get('max', 0) model.is_action = module.get('is_action', False) model.active = module.get('active', True) try: model.save() except IntegrityError: pass except Exception as e: raise Exception('data base startup failed ' + str(e))
def upgrade(self): module = { 'code': 10, 'alias': 'resume', 'inout': {0: [0]}, 'max': 0, 'category': 'action', 'is_action': True, 'active': True } try: model = ModuleModel() model.code = module.get('code') model.alias = module.get('alias') model.inout = module.get('inout') model.category = module.get('category') model.max = module.get('max', 0) model.is_action = module.get('is_action', False) model.active = module.get('active', True) try: model.save() except IntegrityError: pass except Exception as e: raise Exception('data base startup failed ' + str(e))