def get_init_objs(cls) -> dict: """ 获取工具初始化对象 @returns {dict} - 初始化对象 """ # 获取配置文件信息 _execute_path = os.path.realpath(FileTool.get_file_path(__file__)) _config = os.path.join(_execute_path, '../conf/server.xml') _config_xml = SimpleXml(_config, encoding='utf-8') _server_config = _config_xml.to_dict()['server'] # 日志对象 _logger: Logger = None if 'logger' in _server_config.keys(): _logger = Logger.create_logger_by_dict(_server_config['logger']) # 连接数据库操作对象 _qa_manager = QAManager( _server_config['answerdb'], _server_config['milvus'], _server_config['bert_client'], logger=_logger, excel_batch_num=_server_config['excel_batch_num'], excel_engine=_server_config['excel_engine'], load_para=False) return {'logger': _logger, 'qa_manager': _qa_manager}
def generate_thumbnail(cls, upload_type: str, note: str, file_name: str, save_path: str, url: str, **kwargs): """ 生成传入图片的缩略图 @param {str} upload_type - 文件类型,必须在UploadFileConfig表中有配置 @param {str} note - 文件注解 @param {str} file_name - 保存的文件名 @param {str} save_path - 保存的文件路径(含文件名) @param {str} url - 可访问的url @returns {str, str, list} - 返回处理结果二元组 status, msg, answers 注:可以通过这个返回改变上传成功的结果,answers是返回的回答字符数组,如果不需要回答则应可返回[] """ _path = FileTool.get_file_path(save_path) _filename = 'thumbnail_' + file_name _url = os.path.join(url[0:url.rfind('/') + 1], _filename) # 等比例生成缩略图 _img = Image.open(save_path) _ori_w, _ori_h = _img.size _size = ( THUMBNAIL_SIZE[0] if THUMBNAIL_SIZE[0] != 0 else math.ceil( _ori_w * THUMBNAIL_SIZE[1] / _ori_h), THUMBNAIL_SIZE[1] if THUMBNAIL_SIZE[1] != 0 else math.ceil( _ori_h * THUMBNAIL_SIZE[0] / _ori_w), ) _img.thumbnail(_size, Image.ANTIALIAS) if _img.mode == "P": _img = _img.convert('RGB') _img.save(os.path.join(_path, _filename)) return '00000', 'success', [{'thumbnail': _url}]
def remove_data(cls, qa_manager: QAManager, logger: Logger): """ 清空所有数据 @param {QAManager} qa_manager - 数据管理对象 @param {Logger} logger - 日志对象 """ AnswerDao.drop_tables(DATA_TABLES) AnswerDao.create_tables(DATA_TABLES) # 删除上传文件 _execute_path = os.path.realpath(FileTool.get_file_path(__file__)) FileTool.remove_all_with_path( path=os.path.join(_execute_path, '../client/static/leave_message')) if logger is not None: logger.debug('remove leave message plugin data success!')
def console_main(execute_file_path=None, default_config_file=None, console_self_config: dict = None, **kwargs): """ 启动命令行框架的主函数 @param {string} execute_file_path=None - 外部调用该函数应将程序主目录传入,这样才能找到配置文件 @param {string} default_config_file=None - 如果您希望用不同的配置文件路径作为默认路径,可传入该函数指定 @param {dict} console_self_config=None - 命令行自定义配置信息,将添加到全局参数中 """ # 获取命令行参数,需要外部传入config、encoding参数 # 例如 console.py config=/conf/config.xml encoding=utf-8 help=y shell_cmd=以命令行方式执行命令 shell_cmdfile=cmdfile.txt cmdfile_encoding=utf-8 CONSOLE_GLOBAL_PARA = RunTool.get_global_var('CONSOLE_GLOBAL_PARA') if CONSOLE_GLOBAL_PARA is None: CONSOLE_GLOBAL_PARA = {} RunTool.set_global_var('CONSOLE_GLOBAL_PARA', CONSOLE_GLOBAL_PARA) # 自定义配置信息 CONSOLE_GLOBAL_PARA['console_self_config'] = console_self_config # 程序主目录 if execute_file_path is None: CONSOLE_GLOBAL_PARA['execute_file_path'] = os.path.realpath( FileTool.get_file_path(__file__)) else: CONSOLE_GLOBAL_PARA['execute_file_path'] = execute_file_path # 工作目录,可以通过cd命令切换,通过pwd命令查看工作目录路径 CONSOLE_GLOBAL_PARA['work_path'] = os.getcwd() _cmd_opts = RunTool.get_kv_opts() _default_config_file = default_config_file if _default_config_file is None: _default_config_file = os.path.join( CONSOLE_GLOBAL_PARA['execute_file_path'], 'conf/config.xml') _config = ( _default_config_file ) if 'config' not in _cmd_opts.keys() else _cmd_opts['config'] _encoding = 'utf-8' if 'encoding' not in _cmd_opts.keys() else _cmd_opts['encoding'] CONSOLE_GLOBAL_PARA['config_encoding'] = _encoding CONSOLE_GLOBAL_PARA['config_file'] = _config # 获取配置文件信息 _config_xml = SimpleXml(os.path.realpath(_config), encoding=_encoding) _config_dict = _config_xml.to_dict() # 启动控制台服务 _server = ConsoleServer(_config_dict['console']) # 判断是否 if 'help' in _cmd_opts.keys(): # 执行帮助命令 _lang = _config_dict['console']['language'] _help_tips = StringTool.json_to_object( _config_dict['console']['shell_cmd_help'] ) _tips = '' # 如果找不到对应的语言,优先找英语,如果再找不到就找第一个 if _lang in _help_tips.keys(): _tips = _help_tips[_lang] elif 'en' in _help_tips.keys(): _tips = _help_tips['en'] else: _tips = _help_tips[_help_tips.keys()[0]] _print_str = '\r\n'.join(_tips).replace( '{{VERSION}}', _config_dict['console']['version'] ).replace( '{{NAME}}', _config_dict['console']['name'] ).replace( '{{SHELL_CMD_NAME}}', _config_dict['console']['shell_cmd_name'] ) # 打印 print(_print_str) elif 'shell_cmd' in _cmd_opts.keys(): # 命令行模式执行 _cmd_list = None if _cmd_opts['shell_cmd'][0: 1] == '[' and _cmd_opts['shell_cmd'][-1:] == ']': # 是json数组格式 _cmd_list = StringTool.json_to_object(_cmd_opts['shell_cmd']) else: _cmd_list = [_cmd_opts['shell_cmd']] # 逐个命令执行 for _cmd in _cmd_list: _result = _server.call_cmd_directly(_cmd, shell_cmd=True) if not _result.is_success(): # 执行有错误,不继续执行 exit(1) # 正常完成 exit(0) elif 'shell_cmdfile' in _cmd_opts.keys(): _file_encoding = None if 'cmdfile_encoding' in _cmd_opts.keys() and _cmd_opts['cmdfile_encoding'] != '': _file_encoding = _cmd_opts['cmdfile_encoding'] _cmd_text = FileTool.get_file_text(_cmd_opts['shell_cmdfile'], encoding=_file_encoding) _cmd_text = _cmd_text.replace('\r\n', '\n').replace('\r', '\n') _cmd_list = _cmd_text.split('\n') # 逐个命令执行 for _cmd in _cmd_list: _result = _server.call_cmd_directly(_cmd, shell_cmd=True) if not _result.is_success(): # 执行有错误,不继续执行 exit(1) # 正常完成 exit(0) else: _server.start_console()
def __init__(self, config_dict): """ 初始化构造函数 @param {dict} server_config_dict - 服务器的初始化参数字典(console节点) """ # 初始化参数 self._config_dict = copy.deepcopy(config_dict) # 将部分公共参数放到全局变量 self._console_global_para = RunTool.get_global_var('CONSOLE_GLOBAL_PARA') self._console_global_para['name'] = self._config_dict['name'] self._console_global_para['version'] = self._config_dict['version'] self._console_global_para['shell_cmd_name'] = self._config_dict['shell_cmd_name'] self._console_global_para['language'] = self._config_dict['language'] self._console_global_para['shell_encoding'] = self._config_dict['shell_encoding'] # 控制台编码 self._console_global_para['exit_with_prompt'] = self._config_dict.get( 'exit_with_prompt', 'y') # 是否提示退出 # i18n多语言加载 _trans_file_path = None if self._config_dict['i18n'] == '': _trans_file_path = os.path.join( self._console_global_para['execute_file_path'], 'i18n/' ) else: _trans_file_path = self._config_dict['i18n'] _i18n_obj: SimpleI18N = get_global_i18n() if _i18n_obj is None: _i18n_obj = SimpleI18N( lang=self._config_dict['language'], trans_file_path=_trans_file_path, trans_file_prefix='message', auto_loads=True ) else: # 装载默认的多国语言 _i18n_obj.load_trans_from_dir( _trans_file_path, 'message', encoding='utf-8', append=True ) _i18n_obj.lang = self._config_dict['language'] # 再装载自身命令的国际语言文件 _i18n_obj.load_trans_from_dir( os.path.join(os.path.realpath(FileTool.get_file_path(__file__)), 'i18n/'), 'message', encoding='utf-8', append=True ) set_global_i18n(_i18n_obj) # 装载默认执行函数 _temp_dict = self._config_dict['default_dealfun'] self._default_cmd_dealfun = self._import_and_init_class( _temp_dict['module_name'], _temp_dict['class_name'], _temp_dict['extend_path'], _temp_dict['init_para'], as_name='' if 'as_name' not in _temp_dict.keys() else _temp_dict['as_name'] ).cmd_dealfun _temp_dict = self._config_dict['on_abort'] self._on_abort = self._import_and_init_class( _temp_dict['module_name'], _temp_dict['class_name'], _temp_dict['extend_path'], _temp_dict['init_para'], as_name='' if 'as_name' not in _temp_dict.keys() else _temp_dict['as_name'] ).cmd_dealfun _temp_dict = self._config_dict['on_exit'] self._on_exit = self._import_and_init_class( _temp_dict['module_name'], _temp_dict['class_name'], _temp_dict['extend_path'], _temp_dict['init_para'], as_name='' if 'as_name' not in _temp_dict.keys() else _temp_dict['as_name'] ).cmd_dealfun # 遍历参数装载 self._is_fw_help_command = False self._init_cmd_paras() # 加入到CONSOLE_GLOBAL_PARA参数中 self._console_global_para['CMD_PARA'] = self._CMD_PARA self._console_global_para['CMD_HELP_INFO'] = self._CMD_HELP_INFO self._console_global_para['CMD_LIST'] = self._CMD_LIST # 检查help命令是否框架自带的,如果是则增加提示帮助 if self._is_fw_help_command: self._CMD_PARA['help']['word_para'] = self._CMD_LIST # 控制台启动时的提示语言 self._CONSOLE_TIPS = StringTool.json_to_object(self._config_dict['start_tips']) # 日志对象 _logger = None if 'logger' in self._config_dict.keys(): _logger = Logger.create_logger_by_dict( self._config_dict['logger']) # 颜色调整 _color_set = self._config_dict.get('color_set', None) if _color_set is not None: _input_color = _color_set.get('input', None) if _input_color is not None: _color_set[''] = _input_color # 初始化命令行工具对象 self._prompt = PromptPlus( message=self._config_dict['message'], default='', # 默认输入值 cmd_para=self._CMD_PARA, # 命令定义参数 default_dealfun=self._default_cmd_dealfun, # 默认处理函数 on_abort=self._on_abort, # Ctrl + C 取消本次输入执行函数 on_exit=self._on_exit, # Ctrl + D 关闭命令行执行函数 logger=_logger, # 日志 color_set=_color_set, # 命令行配色方案 ) # 最后才添加这个对象 self._console_global_para['prompt_obj'] = self._prompt # 将命令行工具对象放到可访问的参数中 # 执行命令处理类的后初始化函数 for _key in self._import_object_dict.keys(): if hasattr(self._import_object_dict[_key], 'init_after_console_init'): self._import_object_dict[_key].init_after_console_init()
@file test.py """ import os import sys from HiveNetLib.base_tools.file_tool import FileTool from HiveNetLib.simple_xml import SimpleXml # 根据当前文件路径将包路径纳入,在非安装的情况下可以引用到 sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))) from chat_robot.lib.data_manager import QAManager from chat_robot.lib.qa import QA from chat_robot.lib.loader import QAServerLoader # 数据管理模块 _file_path = os.path.realpath(FileTool.get_file_path(__file__)) _execute_path = os.path.join(_file_path, os.path.pardir, 'chat_robot') # _config = os.path.join(_execute_path, os.path.pardir, 'chat_robot/conf/server.xml') _config = os.path.join(_execute_path, './conf/server.xml') _config_xml = SimpleXml(_config, encoding='utf-8') SERVER_CONFIG = _config_xml.to_dict()['server'] SERVER_CONFIG['debug'] = True SERVER_CONFIG['config'] = _config SERVER_CONFIG['encoding'] = 'utf-8' SERVER_CONFIG['execute_path'] = _execute_path # 装载服务 _loader = QAServerLoader(SERVER_CONFIG) _qa_manager: QAManager = _loader.qa_manager
def main(**kwargs): ConsoleServer.console_main(execute_file_path=os.path.realpath( FileTool.get_file_path(__file__)), **kwargs)