diff --git a/app.py b/app.py index 0509187..81c0683 100644 --- a/app.py +++ b/app.py @@ -14,8 +14,8 @@ import asyncio tag = "\033[31m[warn]\033[0m" -defalut_port = 8000 -defalut_host = "127.0.0.1" +default_port = 8000 +default_host = "127.0.0.1" def normalize_paths(paths: List[str]): """ @@ -66,7 +66,7 @@ def paths_check(paths): print(f"{tag} The path '{abs_path}' will be ignored (value: {path}).") -def update_image_index(sd_webui_config: str): +def update_image_index_func(sd_webui_config: str): dirs = get_valid_img_dirs(get_sd_webui_conf(sd_webui_config=sd_webui_config)) if not len(dirs): return print(f"{tag} no valid image directories, skipped") @@ -77,11 +77,11 @@ def update_image_index(sd_webui_config: str): print("update image index completed. ✨") -class AppUltils(object): +class AppUtils(object): def __init__(self, sd_webui_config: Optional[str] = None, - use_update_image_index: bool = False, # update_image_index会与外层函数重名 + update_image_index: bool = False, extra_paths: List[str] = [], ): """ @@ -90,7 +90,7 @@ class AppUltils(object): extra_paths", nargs="+", help="extra paths to use, will be added to Quick Move.", default=[] """ self.sd_webui_config = sd_webui_config - self.use_update_image_index = use_update_image_index + self.update_image_index = update_image_index self.extra_paths = extra_paths def set_params(self, *args, **kwargs) -> None: @@ -98,7 +98,7 @@ class AppUltils(object): self.__init__(*args, **kwargs) @staticmethod - def async_run(app: FastAPI, port: int=defalut_port) -> Coroutine: + def async_run(app: FastAPI, port: int=default_port) -> Coroutine: """ 用于从异步运行的FastAPI,在jupyter notebook环境中非常有用 @@ -109,7 +109,7 @@ class AppUltils(object): """ # 不建议改成async def,并且用await替换return # 因为这样会失去对server.serve()的控制 - config = uvicorn.Config(app, host=defalut_host, port=port) + config = uvicorn.Config(app, host=default_host, port=port) server = uvicorn.Server(config) return server.serve() @@ -120,13 +120,13 @@ class AppUltils(object): 为传递的app挂载上infinite_image_browsing后端 """ sd_webui_config = self.sd_webui_config - use_update_image_index = self.use_update_image_index + update_image_index = self.update_image_index extra_paths = self.extra_paths if sd_webui_config: sd_webui_paths_check(sd_webui_config) - if use_update_image_index: - update_image_index(sd_webui_config) + if update_image_index: + update_image_index_func(sd_webui_config) paths_check(extra_paths) infinite_image_browsing_api(app, @@ -151,78 +151,64 @@ class AppUltils(object): def setup_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(description="Process some integers.") - parser.add_argument("--port", type=int, help="The port to use", default=defalut_port) + parser.add_argument("--port", type=int, help="The port to use", default=default_port) parser.add_argument("--sd_webui_config", type=str, default=None, help="The path to the config file") - # 弃用警告,因为update_image_index会与处理函数同名 - # 所以class AppUltils(object)采用的是use_update_image_index - # 有必要保证两者的一致性 - parser.add_argument( - "--update_image_index", action="store_true", help="Update the image index. Will be deprecated. Recommend to use --use_update_image_index" - ) - parser.add_argument( - "--use_update_image_index", action="store_true", help="Update the image index" - ) + parser.add_argument("--update_image_index", action="store_true", help="Update the image index") parser.add_argument( "--extra_paths", nargs="+", help="Extra paths to use, will be added to Quick Move.", default=[] ) return parser -def check_cmd_param(*args, **kwargs) -> argparse.Namespace: +def check_cmd_params(*args, **kwargs) -> argparse.Namespace: """ parser.parse_known_args()的装饰器,用于报告非法参数 无任何传递将从sys.argv中解析参数 """ - cmd_param, unkonwn = parser.parse_known_args(*args, **kwargs) + cmd_params, unkonwn = parser.parse_known_args(*args, **kwargs) if unkonwn: logging.warning(f"{tag} Illegal args will be ingored: {unkonwn}") - if cmd_param.update_image_index: - # 弃用警告,因为update_image_index会与处理函数同名 - # 所以class AppUltils(object)采用的是use_update_image_index - # 有必要保证两者的一致性 - logging.warning(f"{tag} --update_image_index will be deprecated, please use --use_update_image_index") - cmd_param.use_update_image_index = True - return cmd_param + return cmd_params -def launch_app(port, *args, **kwargs) -> None: +def launch_app(port: int=default_port, *args, **kwargs) -> None: """ 同步函数 - 所传入的要求参数都会传递给AppUltils() + 除 port 参数外,所传入的其他参数全都会被传递给AppUtils() sd_webui_config, type=str, default=None, help="the path to the config file" update_image_index, action="store_true", help="update the image index" extra_paths", nargs="+", help="extra paths to use, will be added to Quick Move.", default=[] """ - app_ultils = AppUltils(*args, **kwargs) - app = app_ultils.get_root_browser_app() - uvicorn.run(app, host=defalut_host, port=port) + app_utils = AppUtils(*args, **kwargs) + app = app_utils.get_root_browser_app() + uvicorn.run(app, host=default_host, port=port) -async def async_launch_app(port, *args, **kwargs) -> None: +async def async_launch_app(port: int=default_port, *args, **kwargs) -> None: """ 协程函数 - 所传入的要求参数都会传递给AppUltils() + 除 port 参数外,所传入的其他参数全都会被传递给AppUtils() sd_webui_config, type=str, default=None, help="the path to the config file" update_image_index, action="store_true", help="update the image index" extra_paths", nargs="+", help="extra paths to use, will be added to Quick Move.", default=[] """ - app_ultils = AppUltils(*args, **kwargs) - app = app_ultils.get_root_browser_app() - await app_ultils.async_run(app, port=port) + app_utils = AppUtils(*args, **kwargs) + app = app_utils.get_root_browser_app() + await app_utils.async_run(app, port=port) parser = setup_parser() if __name__ == "__main__": - cmd_param = check_cmd_param() - params_dict = dict(sd_webui_config = cmd_param.sd_webui_config, - use_update_image_index = cmd_param.use_update_image_index, - extra_paths = cmd_param.extra_paths, + cmd_params = check_cmd_params() + params_dict = dict(sd_webui_config = cmd_params.sd_webui_config, + update_image_index = cmd_params.update_image_index, + extra_paths = cmd_params.extra_paths, ) - app_ultils = AppUltils(**params_dict) - app = app_ultils.get_root_browser_app() - uvicorn.run(app, host=defalut_host, port=cmd_param.port) + app_utils = AppUtils(**params_dict) + app = app_utils.get_root_browser_app() + uvicorn.run(app, host=default_host, port=cmd_params.port)