|
9 | 9 | import pathlib |
10 | 10 | import sys |
11 | 11 | from typing import List |
| 12 | +from deepl.util import _optional_import |
12 | 13 |
|
13 | 14 | # Program name for integration with click.testing |
14 | 15 | name = "python -m deepl" |
|
17 | 18 | env_server_url = "DEEPL_SERVER_URL" |
18 | 19 | env_proxy_url = "DEEPL_PROXY_URL" |
19 | 20 |
|
| 21 | +keyring_key_folder = "deepl" |
| 22 | +keyring_key_name = env_auth_key |
| 23 | + |
20 | 24 |
|
21 | 25 | def action_usage(translator: deepl.Translator): |
22 | 26 | """Action function for the usage command.""" |
@@ -208,7 +212,9 @@ def get_parser(prog_name): |
208 | 212 | "--auth-key", |
209 | 213 | default=None, |
210 | 214 | help="authentication key as given in your DeepL account; the " |
211 | | - f"{env_auth_key} environment variable is used as secondary fallback", |
| 215 | + f"{env_auth_key} environment variable is used as secondary fallback; " |
| 216 | + f"the key {keyring_key_name} in {keyring_key_folder} is used " |
| 217 | + "as tertiary fallback", |
212 | 218 | ) |
213 | 219 | parser.add_argument( |
214 | 220 | "--server-url", |
@@ -500,14 +506,26 @@ def main(args=None, prog_name=None): |
500 | 506 | logger.setLevel(logging.WARNING) |
501 | 507 |
|
502 | 508 | server_url = args.server_url or os.getenv(env_server_url) |
503 | | - auth_key = args.auth_key or os.getenv(env_auth_key) |
504 | 509 | proxy_url = args.proxy_url or os.getenv(env_proxy_url) |
505 | 510 |
|
| 511 | + auth_key = args.auth_key or os.getenv(env_auth_key) |
| 512 | + keyring = _optional_import("keyring") |
| 513 | + if keyring: |
| 514 | + keyring_pw = None |
| 515 | + try: |
| 516 | + keyring_pw = keyring.get_password( |
| 517 | + keyring_key_folder, keyring_key_name |
| 518 | + ) |
| 519 | + except keyring.errors.NoKeyringError: |
| 520 | + pass |
| 521 | + auth_key = auth_key or keyring_pw |
| 522 | + |
506 | 523 | try: |
507 | 524 | if auth_key is None: |
508 | 525 | raise Exception( |
509 | 526 | f"Please provide authentication key via the {env_auth_key} " |
510 | | - "environment variable or --auth_key argument" |
| 527 | + "environment variable or --auth_key argument or via " |
| 528 | + f"{keyring_key_name} in {keyring_key_folder} in keyring" |
511 | 529 | ) |
512 | 530 |
|
513 | 531 | # Note: the get_languages() call to verify language codes is skipped |
|
0 commit comments