def _import_and_init_class(self, module_name, class_name, extend_path, init_para, as_name=''): """ 装载并初始化对象返回(如果对象已存在则直接返回) @param {string} module_name - 模块名 @param {string} class_name - 处理类名 @param {string} extend_path - 模块所在搜索路径 @param {string} init_para - 初始化的json字符串 @param {string} as_name - 对象别名,可以设置不一样的值让类可以多次实例化 @return {object} - 初始化后的模块对象 @throws {ImportError} - 初始化失败返回该异常 """ # 检查对象是否已存在 _key = as_name _class_tag = '%s.%s' % (module_name, class_name) if as_name == '': _key = _class_tag if self._import_object_dict is None: self._import_object_dict = dict() if _key in self._import_object_dict.keys(): # 已存在,直接返回即可 return self._import_object_dict[_key] # 装载模块 _class = None if ImportTool.check_module_imported(module_name): # 模块已存在 _class = ImportTool.get_member_from_module( ImportTool.get_imported_module(module_name), class_name ) else: # 动态装载模块 _class = ImportTool.get_member_from_module( ImportTool.import_module( module_name, extend_path=extend_path ), class_name ) if _class is None: raise ImportError(_("config file error: can't import module: $1!", (_class_tag, ))) # 初始化对象并返回 _init_para = dict() if init_para != '': _init_para = StringTool.json_to_object(init_para) self._import_object_dict[_key] = _class(**_init_para) return self._import_object_dict[_key]
def demo5(): print('demo5 create process queue and put task\n') _task_queue = multiprocessing.Queue() _i = 0 while _i < 100: _task_queue.put(_i) _i = _i + 1 print('demo5 create parallel pool\n') _pool = ParallelPool( deal_fun=_deal_fun_demo5, parallel_class=getattr( ImportTool.import_module('HiveNetLib.simple_parallel'), 'ProcessParallel'), run_args=(_task_queue, 'demo5-a'), run_kwargs={'p1': 'demo5-p1'}, pname='demo5', callback_fun=demo_callback, logger=_logger, use_distributed_logger=True, distributed_logger_module_name='HiveNetLib.simple_log', distributed_logger_class_name='Logger', distributed_logger_args=None, distributed_logger_kwargs=_logger_kwargs, auto_start=False, auto_stop=True, task_queue=_task_queue, get_task_num_fun=None, get_task_num_fun_args=None, maxsize=10, minsize=0, worker_release_time=10, worker_overtime=0, force_kill_overtime_worker=False, replace_overtime_worker=False, daemon_thread_time=0.01, sharedict_class=getattr( ImportTool.import_module('HiveNetLib.simple_parallel'), 'ProcessParallelShareDict'), parallel_lock_class=getattr( ImportTool.import_module('HiveNetLib.simple_parallel'), 'ProcessParallelLock')) print('demo5 start parallel pool\n') _pool.start() print('demo5 wait parallel pool stop...\n') while not _pool.is_stop: time.sleep(1) print('demo5 parallel pool finished!\n')
def load_action_module(cls, module_file: str, robot_id: str = None): """ 加载动作模块文件 @param {str} module_file - 要加载的动作模块文件 @param {str} robot_id=None - 机器人id,如果有传值代表导入对应机器人实例自有路由中 """ # 检查环境是否已加载 cls._check_inited_raise() _path, _file = os.path.split(os.path.realpath(module_file)) # 加载文件 _module = ImportTool.import_module(_file[0: -3], extend_path=_path, is_force=False) # 导入路由信息 cls._add_action_router_by_module(_module, robot_id=robot_id)
def load_plugins(self, path: str): """ 装载job运行插件 @param {str} path - 插件所在目录 """ _file_list = FileTool.get_filelist(path=path, regex_str=r'.*\.py$', is_fullname=False) for _file in _file_list: if _file == '__init__.py': continue # 执行加载 _module = ImportTool.import_module(_file[0:-3], extend_path=path, is_force=True) _clsmembers = inspect.getmembers(_module, inspect.isclass) for (_class_name, _class) in _clsmembers: if _module.__name__ != _class.__module__: # 不是当前模块定义的函数 continue # 判断类型 _type_fun = getattr(_class, 'plugin_type', None) if _type_fun is None or not callable(_type_fun): # 不是标准的插件类 continue _plugin_type = _type_fun() self.plugins.setdefault(_plugin_type, dict()) self.plugins[_plugin_type][_class_name] = dict() self._log_debug( 'add [%s] plugin file[%s] class[%s]:' % (_plugin_type, _file, _class_name), ) for _name, _value in inspect.getmembers(_class): if not _name.startswith('_') and callable( _value) and _name not in ['plugin_type']: if _name == 'initialize': # 装载时执行一次初始化 _value(self, self.qa_manager, self.qa) else: self.plugins[_plugin_type][_class_name][ _name] = _value self._log_debug(' add fun[%s]' % _name)
def load_plugins_by_file(cls, file: str): """ 装载指定文件的管道插件 @param {str} file - 模块文件路径 """ # 执行加载 _file = os.path.realpath(file) if not _file.endswith('.py'): raise AttributeError('not supported plugins file [%s]!' % _file) _module_name = os.path.split(_file)[1][0:-3] _module = ImportTool.import_module(_module_name, extend_path=os.path.split(_file)[0], is_force=True) _clsmembers = inspect.getmembers(_module, inspect.isclass) for (_class_name, _class) in _clsmembers: if _module.__name__ != _class.__module__: # 不是当前模块定义的函数 continue cls.add_plugin(_class)
import sys import os sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '../../..')) from HiveNetLib.base_tools.import_tool import ImportTool __MOUDLE__ = 'import_tool_demo' # 模块名 __DESCRIPT__ = u'动态模块加载示例' # 模块描述 __VERSION__ = '0.1.0' # 版本 __AUTHOR__ = u'黎慧剑' # 作者 __PUBLISH__ = '2018.10.02' # 发布日期 if __name__ == '__main__': # 示例1,导入公共库 print('datetime库导入状态:%s' % (str(ImportTool.check_module_imported('datetime')))) _datetime = ImportTool.import_module('datetime') # 导入公共库 # 执行公共库 print('当前时间为:%s' % (str(_datetime.datetime.now()))) print('datetime库导入状态:%s' % (str(ImportTool.check_module_imported('datetime')))) # 示例2,导入模块 _generic = ImportTool.import_module('HiveNetLib.generic') # 使用模块的对象 _result = _generic.CResult('00000') print('\nCResult: %s' % (str(_result))) # 示例3,导入具体对象,并访问该成员属性,或类 _CResult = ImportTool.import_module('HiveNetLib.generic', import_member='CResult')
def initialize(cls, loader, qa_manager, qa, **kwargs): """ 装载插件前执行的初始化处理 可以不定义 @param {QAServerLoader} loader - 服务装载器 @param {QAManager} qa_manager - 数据管理 @param {QA} qa - 问答服务 """ FORM_PLUGIN_CONFIG = RunTool.get_global_var('FORM_PLUGIN_CONFIG') if FORM_PLUGIN_CONFIG is None: FORM_PLUGIN_CONFIG = dict() RunTool.set_global_var('FORM_PLUGIN_CONFIG', FORM_PLUGIN_CONFIG) FORM_PLUGIN_SELF_TABLE = RunTool.get_global_var( 'FORM_PLUGIN_SELF_TABLE') if FORM_PLUGIN_SELF_TABLE is None: FORM_PLUGIN_SELF_TABLE = dict() RunTool.set_global_var('FORM_PLUGIN_SELF_TABLE', FORM_PLUGIN_SELF_TABLE) # 获取表单实例库 if FORM_PLUGIN_SEARCH_PATH is not None: _path = os.path.join(os.path.dirname(__file__), FORM_PLUGIN_SEARCH_PATH) _file_list = FileTool.get_filelist(path=_path, regex_str=r'.*\.py$', is_fullname=False) for _file in _file_list: if _file == '__init__.py': continue # 执行加载 _module = ImportTool.import_module(_file[0:-3], extend_path=_path, is_force=True) _clsmembers = inspect.getmembers(_module, inspect.isclass) for (_class_name, _class) in _clsmembers: if _module.__name__ != _class.__module__: # 不是当前模块定义的函数 continue # 判断类型 _get_form_type = getattr(_class, 'get_form_type', None) if _get_form_type is None or not callable(_get_form_type): # 不是标准的插件类 continue _form_type = _get_form_type() _get_form_config = getattr(_class, 'get_form_config', None) # 加入配置 FORM_PLUGIN_CONFIG[_form_type] = _get_form_config() # 循环插件实例进行处理 for _form_type in FORM_PLUGIN_CONFIG.keys(): _config = FORM_PLUGIN_CONFIG[_form_type] # 执行初始化 _initialize_fun = _config.get('initialize_fun', None) if _initialize_fun is not None: _initialize_fun(loader, qa_manager, qa, **kwargs)
def import_config(cls, qa_manager: QAManager, logger: Logger): """ 添加标准配置(不考虑删除问题) @param {QAManager} qa_manager - 数据管理对象 @param {Logger} logger - 日志对象 """ FORM_PLUGIN_CONFIG = RunTool.get_global_var('FORM_PLUGIN_CONFIG') if FORM_PLUGIN_CONFIG is None: FORM_PLUGIN_CONFIG = dict() RunTool.set_global_var('FORM_PLUGIN_CONFIG', FORM_PLUGIN_CONFIG) FORM_PLUGIN_SELF_TABLE = RunTool.get_global_var( 'FORM_PLUGIN_SELF_TABLE') if FORM_PLUGIN_SELF_TABLE is None: FORM_PLUGIN_SELF_TABLE = dict() RunTool.set_global_var('FORM_PLUGIN_SELF_TABLE', FORM_PLUGIN_SELF_TABLE) # 插入标准问题 _std_q = StdQuestion.create(tag='form_direct_action', q_type='context', milvus_id=-1, collection=FORM_PLUGIN_COLLECTION, partition=FORM_PLUGIN_PARTITION, question='表单插件通用处理') # 插入问题答案 Answer.create(std_question_id=_std_q.id, a_type='job', type_param="['FormPlugin', 'operate', {}]", replace_pre_def='N', answer='表单插件通用处理') if logger is not None: logger.info('create form plugin std question config success!') # 处理扩展插件 if FORM_PLUGIN_SEARCH_PATH is not None: _path = os.path.join(os.path.dirname(__file__), FORM_PLUGIN_SEARCH_PATH) _file_list = FileTool.get_filelist(path=_path, regex_str=r'.*\.py$', is_fullname=False) for _file in _file_list: if _file == '__init__.py': continue # 执行加载 _module = ImportTool.import_module(_file[0:-3], extend_path=_path, is_force=True) _clsmembers = inspect.getmembers(_module, inspect.isclass) for (_class_name, _class) in _clsmembers: if _module.__name__ != _class.__module__: # 不是当前模块定义的函数 continue # 判断类型 _get_form_type = getattr(_class, 'get_form_type', None) if _get_form_type is None or not callable(_get_form_type): # 不是标准的插件类 continue _form_type = _get_form_type() _get_form_config = getattr(_class, 'get_form_config', None) # 加入配置 FORM_PLUGIN_CONFIG[_form_type] = _get_form_config() # 循环插件实例进行处理 for _form_type in FORM_PLUGIN_CONFIG.keys(): _config = FORM_PLUGIN_CONFIG[_form_type] # 创建表单类型意图参数 NlpPurposConfigDict.create( action=_form_type, match_collection='', match_partition='', collection=FORM_PLUGIN_COLLECTION, partition=FORM_PLUGIN_PARTITION, std_question_id=_std_q.id, order_num=_config['order_num'], exact_match_words=str(_config['exact_match_words']), exact_ignorecase=_config['exact_ignorecase'], match_words=str(_config['match_words']), ignorecase=_config['ignorecase'], word_scale=_config['word_scale'], info=str(_config['info']), check=str(_config['check'])) if logger is not None: logger.info('create form plugin [%s] success!' % _form_type)
def load_plugin(self, plugin_name: str): """ 装载已安装的插件到服务中 @param {str} plugin_name - 插件名 """ _fetchs = self._exec_sql( 'select dir_name from t_installed where plugin_name=?', para=(plugin_name, ), is_fetchall=True) _dir_name = _fetchs[0][0] # 获取插件配置 _plugin_config = SimpleXml( os.path.join(self.plugin_path, _dir_name, 'plugin.xml')).to_dict()['config'] # 装载后台服务 self.imported_moudles[plugin_name] = dict() _import = _plugin_config['import'] for _module_name in _import.keys(): # 装载模块 _extend_path = os.path.join(self.plugin_path, _dir_name, 'lib', _import[_module_name]['extend_path']) _module = ImportTool.import_module(_module_name, extend_path=_extend_path) self.imported_moudles[plugin_name][_module_name] = _module # 初始化类并加入Restful服务 _init_class = _import[_module_name]['init_class'] for _class_name in _init_class.keys(): _class_object = ImportTool.get_member_from_module( _module, _class_name) if _init_class[_class_name]['init_type'] == 'instance': _class_object = _class_object( config_services=self.config_services, device_services=self.device_services, config_path=os.path.join( self.config_path, 'plugin', _plugin_config['info']['plugin_name']), plugin_path=self.plugin_path, logger=self.logger) # 加入Restful服务 if _init_class[_class_name].get('add_route_by_class', False): # 处理服务黑名单 _blacklist = _init_class[_class_name].get('blacklist', []) for _i in range(len(_blacklist)): _blacklist[_i] = '%s/%s' % (_class_name, _blacklist[_i]) # 添加路由 self.flask_server.add_route_by_class([_class_object], blacklist=_blacklist) # 装载主页的入口路由 if _plugin_config['info'].get('url', '') != '': self.flask_server.add_route(_plugin_config['info']['url'], self.plugin_index, endpoint=plugin_name) # 处理插件内存信息 _plugin_config['info']['plugin_name'] = plugin_name # 避免中途被修改 self.plugins_dict[plugin_name] = _plugin_config['info'] if _plugin_config['info']['entrance_type'] == 'toolbar': self.plugins_toolbar.append(plugin_name) else: self.plugins_tab.append(plugin_name)