|
18 | 18 | from swanlab.sdk.internal.core_python import client |
19 | 19 | from swanlab.sdk.internal.pkg import console, helper, scope |
20 | 20 | from swanlab.sdk.internal.settings import Settings, settings |
21 | | -from swanlab.sdk.typings.core_python.api.bootstrap import LoginResponse |
| 21 | +from swanlab.sdk.typings.pkg.client.bootstrap import LoginResponse |
| 22 | + |
| 23 | +__all__ = ["login", "login_interactive", "login_raw"] |
22 | 24 |
|
23 | 25 |
|
24 | 26 | @with_cmd_lock |
@@ -74,10 +76,53 @@ def login( |
74 | 76 | >>> swanlab.login(api_key="new_api_key", relogin=True, save=True) |
75 | 77 | >>> swanlab.init(mode="cloud") |
76 | 78 | """ |
77 | | - return raw_login(api_key=api_key, relogin=relogin, host=host, save=save, timeout=timeout) |
| 79 | + return login_raw(api_key=api_key, relogin=relogin, host=host, save=save, timeout=timeout) |
| 80 | + |
| 81 | + |
| 82 | +def login_interactive( |
| 83 | + api_key: Optional[str] = None, |
| 84 | + relogin: bool = False, |
| 85 | + host: Optional[str] = None, |
| 86 | + save: bool = False, |
| 87 | + timeout: int = 10, |
| 88 | +) -> bool: |
| 89 | + """ |
| 90 | + 带循环输入容错的交互式登录接口。 |
| 91 | + 主要为 CLI 环境或需要极高容错的终端调用设计。 |
| 92 | + 当捕获到 AuthenticationError 时,如果环境允许交互,则会无限循环提示用户重新输入 API Key。 |
| 93 | + """ |
| 94 | + # CLI 每次是新进程,client.exists() 必为 False,需要检查本地凭证判断是否已登录 |
| 95 | + if apikey.exists_locally() and not relogin: |
| 96 | + console.info( |
| 97 | + "You are already logged in. Use", |
| 98 | + Text("`swanlab login --relogin`", style="bold"), |
| 99 | + "to force relogin.", |
| 100 | + sep=" ", |
| 101 | + ) |
| 102 | + return True |
| 103 | + try: |
| 104 | + # 首次尝试登录,复用原子接口 |
| 105 | + return login_raw(api_key=api_key, relogin=relogin, host=host, save=save, timeout=timeout) |
| 106 | + except AuthenticationError as e: |
| 107 | + # 如果全局配置禁用了交互模式,直接抛出异常 |
| 108 | + if not settings.interactive: |
| 109 | + raise e |
| 110 | + console.error(str(e)) |
| 111 | + |
| 112 | + # 进入容错重试循环 |
| 113 | + while True: |
| 114 | + try: |
| 115 | + # 重新要求用户输入新的 Key |
| 116 | + new_key = apikey.prompt() |
| 117 | + return login_raw(api_key=new_key, relogin=relogin, host=host, save=save, timeout=timeout) |
| 118 | + except AuthenticationError as e: |
| 119 | + console.error(str(e)) |
| 120 | + except (KeyboardInterrupt, EOFError): |
| 121 | + console.info("\nLogin cancelled by user.") |
| 122 | + return False |
78 | 123 |
|
79 | 124 |
|
80 | | -def raw_login( |
| 125 | +def login_raw( |
81 | 126 | api_key: Optional[str] = None, |
82 | 127 | relogin: bool = False, |
83 | 128 | host: Optional[str] = None, |
@@ -150,49 +195,6 @@ def raw_login( |
150 | 195 | return True |
151 | 196 |
|
152 | 197 |
|
153 | | -def interactive_login( |
154 | | - api_key: Optional[str] = None, |
155 | | - relogin: bool = False, |
156 | | - host: Optional[str] = None, |
157 | | - save: bool = False, |
158 | | - timeout: int = 10, |
159 | | -) -> bool: |
160 | | - """ |
161 | | - 带循环输入容错的交互式登录接口。 |
162 | | - 主要为 CLI 环境或需要极高容错的终端调用设计。 |
163 | | - 当捕获到 AuthenticationError 时,如果环境允许交互,则会无限循环提示用户重新输入 API Key。 |
164 | | - """ |
165 | | - # CLI 每次是新进程,client.exists() 必为 False,需要检查本地凭证判断是否已登录 |
166 | | - if not relogin and api_key is None and host is None and apikey.exists(): |
167 | | - console.info( |
168 | | - "You are already logged in. Use", |
169 | | - Text("`swanlab login --relogin`", style="bold"), |
170 | | - "to force relogin.", |
171 | | - sep=" ", |
172 | | - ) |
173 | | - return True |
174 | | - try: |
175 | | - # 首次尝试登录,复用原子接口 |
176 | | - return raw_login(api_key=api_key, relogin=relogin, host=host, save=save, timeout=timeout) |
177 | | - except AuthenticationError as e: |
178 | | - # 如果全局配置禁用了交互模式,直接抛出异常 |
179 | | - if not settings.interactive: |
180 | | - raise e |
181 | | - console.error(str(e)) |
182 | | - |
183 | | - # 进入容错重试循环 |
184 | | - while True: |
185 | | - try: |
186 | | - # 重新要求用户输入新的 Key |
187 | | - new_key = apikey.prompt() |
188 | | - return raw_login(api_key=new_key, relogin=relogin, host=host, save=save, timeout=timeout) |
189 | | - except AuthenticationError as e: |
190 | | - console.error(str(e)) |
191 | | - except (KeyboardInterrupt, EOFError): |
192 | | - console.info("\nLogin cancelled by user.") |
193 | | - return False |
194 | | - |
195 | | - |
196 | 198 | @helper.with_loading_animation() |
197 | 199 | def create_client(ctx: RunContext, timeout: int = 10): |
198 | 200 | assert ctx.config.settings.api_key is not None, "API Key not provided" |
|
0 commit comments