Marco Ricci commited on 2025-01-31 15:03:58
Zeige 7 geänderte Dateien mit 77 Einfügungen und 44 Löschungen.
| ... | ... |
@@ -239,9 +239,7 @@ def load_config() -> _types.VaultConfig: |
| 239 | 239 |
|
| 240 | 240 |
# TODO(the-13th-letter): Remove this function. |
| 241 | 241 |
# https://the13thletter.info/derivepassphrase/latest/upgrade-notes.html#v1.0-old-settings-file |
| 242 |
-def migrate_and_load_old_config() -> tuple[ |
|
| 243 |
- _types.VaultConfig, OSError | None |
|
| 244 |
-]: |
|
| 242 |
+def migrate_and_load_old_config() -> tuple[_types.VaultConfig, OSError | None]: |
|
| 245 | 243 |
"""Load and migrate a vault(1)-compatible config. |
| 246 | 244 |
|
| 247 | 245 |
The (old) filename is obtained via [`config_filename`][]. This |
| ... | ... |
@@ -297,9 +297,7 @@ class _VaultConfigValidator: |
| 297 | 297 |
if key in {'key', 'phrase'}:
|
| 298 | 298 |
if not isinstance(value, str): |
| 299 | 299 |
raise TypeError(err_not_a_string.format(**kwargs)) |
| 300 |
- elif key == 'unicode_normalization_form' and path == ( |
|
| 301 |
- 'global', |
|
| 302 |
- ): |
|
| 300 |
+ elif key == 'unicode_normalization_form' and path == ('global',):
|
|
| 303 | 301 |
if not isinstance(value, str): |
| 304 | 302 |
raise TypeError(err_not_a_string.format(**kwargs)) |
| 305 | 303 |
if not allow_unknown_settings: |
| ... | ... |
@@ -385,9 +383,7 @@ class _VaultConfigValidator: |
| 385 | 383 |
) |
| 386 | 384 |
service_obj[key] = 20 |
| 387 | 385 |
elif key == 'repeat' and falsy_but_not_zero(value): |
| 388 |
- yield CleanupStep( |
|
| 389 |
- (*path, key), service_obj[key], 'replace', 0 |
|
| 390 |
- ) |
|
| 386 |
+ yield CleanupStep((*path, key), service_obj[key], 'replace', 0) |
|
| 391 | 387 |
service_obj[key] = 0 |
| 392 | 388 |
elif key in {
|
| 393 | 389 |
'lower', |
| ... | ... |
@@ -812,7 +812,9 @@ def derivepassphrase_vault( # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915 |
| 812 | 812 |
backup_config, exc = cli_helpers.migrate_and_load_old_config() |
| 813 | 813 |
except FileNotFoundError: |
| 814 | 814 |
return {'services': {}}
|
| 815 |
- old_name = cli_helpers.config_filename(subsystem='old settings.json').name |
|
| 815 |
+ old_name = cli_helpers.config_filename( |
|
| 816 |
+ subsystem='old settings.json' |
|
| 817 |
+ ).name |
|
| 816 | 818 |
new_name = cli_helpers.config_filename(subsystem='vault').name |
| 817 | 819 |
deprecation.warning( |
| 818 | 820 |
_msg.TranslatedString( |
| ... | ... |
@@ -906,19 +908,33 @@ def derivepassphrase_vault( # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915 |
| 906 | 908 |
configuration: _types.VaultConfig |
| 907 | 909 |
|
| 908 | 910 |
check_incompatible_options('--phrase', '--key')
|
| 909 |
- for group in (cli_machinery.ConfigurationOption, cli_machinery.StorageManagementOption): |
|
| 911 |
+ for group in ( |
|
| 912 |
+ cli_machinery.ConfigurationOption, |
|
| 913 |
+ cli_machinery.StorageManagementOption, |
|
| 914 |
+ ): |
|
| 910 | 915 |
for opt in options_in_group[group]: |
| 911 | 916 |
if opt != params_by_str['--config']: |
| 912 |
- for other_opt in options_in_group[cli_machinery.PassphraseGenerationOption]: |
|
| 917 |
+ for other_opt in options_in_group[ |
|
| 918 |
+ cli_machinery.PassphraseGenerationOption |
|
| 919 |
+ ]: |
|
| 913 | 920 |
check_incompatible_options(opt, other_opt) |
| 914 | 921 |
|
| 915 |
- for group in (cli_machinery.ConfigurationOption, cli_machinery.StorageManagementOption): |
|
| 922 |
+ for group in ( |
|
| 923 |
+ cli_machinery.ConfigurationOption, |
|
| 924 |
+ cli_machinery.StorageManagementOption, |
|
| 925 |
+ ): |
|
| 916 | 926 |
for opt in options_in_group[group]: |
| 917 |
- for other_opt in options_in_group[cli_machinery.ConfigurationOption]: |
|
| 927 |
+ for other_opt in options_in_group[ |
|
| 928 |
+ cli_machinery.ConfigurationOption |
|
| 929 |
+ ]: |
|
| 918 | 930 |
check_incompatible_options(opt, other_opt) |
| 919 |
- for other_opt in options_in_group[cli_machinery.StorageManagementOption]: |
|
| 931 |
+ for other_opt in options_in_group[ |
|
| 932 |
+ cli_machinery.StorageManagementOption |
|
| 933 |
+ ]: |
|
| 920 | 934 |
check_incompatible_options(opt, other_opt) |
| 921 |
- sv_or_global_options = options_in_group[cli_machinery.PassphraseGenerationOption] |
|
| 935 |
+ sv_or_global_options = options_in_group[ |
|
| 936 |
+ cli_machinery.PassphraseGenerationOption |
|
| 937 |
+ ] |
|
| 922 | 938 |
for param in sv_or_global_options: |
| 923 | 939 |
if is_param_set(param) and not ( |
| 924 | 940 |
service is not None or is_param_set(params_by_str['--config']) |
| ... | ... |
@@ -972,11 +988,15 @@ def derivepassphrase_vault( # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915 |
| 972 | 988 |
notes_marker = _msg.TranslatedString( |
| 973 | 989 |
_msg.Label.DERIVEPASSPHRASE_VAULT_NOTES_MARKER |
| 974 | 990 |
) |
| 975 |
- old_notes_value = configuration['services'].get( |
|
| 976 |
- service, cast('_types.VaultConfigServicesSettings', {})
|
|
| 977 |
- ).get('notes', '')
|
|
| 991 |
+ old_notes_value = ( |
|
| 992 |
+ configuration['services'] |
|
| 993 |
+ .get(service, cast('_types.VaultConfigServicesSettings', {}))
|
|
| 994 |
+ .get('notes', '')
|
|
| 995 |
+ ) |
|
| 978 | 996 |
text = '\n'.join([ |
| 979 |
- str(notes_instructions), str(notes_marker), old_notes_value |
|
| 997 |
+ str(notes_instructions), |
|
| 998 |
+ str(notes_marker), |
|
| 999 |
+ old_notes_value, |
|
| 980 | 1000 |
]) |
| 981 | 1001 |
notes_value = click.edit(text=text) |
| 982 | 1002 |
if notes_value is not None: |
| ... | ... |
@@ -34,8 +34,8 @@ import pathlib |
| 34 | 34 |
import struct |
| 35 | 35 |
from typing import TYPE_CHECKING, Any |
| 36 | 36 |
|
| 37 |
-from derivepassphrase._internals import cli_messages as _msg |
|
| 38 | 37 |
from derivepassphrase import _types, exporter |
| 38 |
+from derivepassphrase._internals import cli_messages as _msg |
|
| 39 | 39 |
|
| 40 | 40 |
if TYPE_CHECKING: |
| 41 | 41 |
from collections.abc import Iterator |
| ... | ... |
@@ -35,8 +35,8 @@ import pathlib |
| 35 | 35 |
import warnings |
| 36 | 36 |
from typing import TYPE_CHECKING |
| 37 | 37 |
|
| 38 |
-from derivepassphrase._internals import cli_messages as _msg |
|
| 39 | 38 |
from derivepassphrase import exporter, vault |
| 39 |
+from derivepassphrase._internals import cli_messages as _msg |
|
| 40 | 40 |
|
| 41 | 41 |
if TYPE_CHECKING: |
| 42 | 42 |
from typing import Any |
| ... | ... |
@@ -1113,7 +1113,10 @@ class TestCLI: |
| 1113 | 1113 |
tests.isolated_vault_config( |
| 1114 | 1114 |
monkeypatch=monkeypatch, |
| 1115 | 1115 |
runner=runner, |
| 1116 |
- vault_config={'global': {'phrase': 'abc'}, 'services': {}},
|
|
| 1116 |
+ vault_config={
|
|
| 1117 |
+ 'global': {'phrase': 'abc'},
|
|
| 1118 |
+ 'services': {},
|
|
| 1119 |
+ }, |
|
| 1117 | 1120 |
) |
| 1118 | 1121 |
) |
| 1119 | 1122 |
monkeypatch.setattr( |
| ... | ... |
@@ -1447,7 +1450,9 @@ class TestCLI: |
| 1447 | 1450 |
runner=runner, |
| 1448 | 1451 |
) |
| 1449 | 1452 |
) |
| 1450 |
- cli_helpers.config_filename(subsystem='vault').unlink(missing_ok=True) |
|
| 1453 |
+ cli_helpers.config_filename(subsystem='vault').unlink( |
|
| 1454 |
+ missing_ok=True |
|
| 1455 |
+ ) |
|
| 1451 | 1456 |
result_ = runner.invoke( |
| 1452 | 1457 |
# Test parent context navigation by not calling |
| 1453 | 1458 |
# `cli.derivepassphrase_vault` directly. Used e.g. in |
| ... | ... |
@@ -2329,7 +2334,9 @@ contents go here |
| 2329 | 2334 |
monkeypatch.setattr(cli_helpers, 'save_config', save_config_) |
| 2330 | 2335 |
return save_config_(*args, **kwargs) |
| 2331 | 2336 |
|
| 2332 |
- monkeypatch.setattr(cli_helpers, 'save_config', obstruct_config_saving) |
|
| 2337 |
+ monkeypatch.setattr( |
|
| 2338 |
+ cli_helpers, 'save_config', obstruct_config_saving |
|
| 2339 |
+ ) |
|
| 2333 | 2340 |
result_ = runner.invoke( |
| 2334 | 2341 |
cli.derivepassphrase_vault, |
| 2335 | 2342 |
['--config', '-p'], |
| ... | ... |
@@ -2996,7 +3003,9 @@ Boo. |
| 2996 | 3003 |
actually emits to standard error. |
| 2997 | 3004 |
|
| 2998 | 3005 |
""" |
| 2999 |
- warnings_cm = cli_machinery.StandardCLILogging.ensure_standard_warnings_logging() |
|
| 3006 |
+ warnings_cm = ( |
|
| 3007 |
+ cli_machinery.StandardCLILogging.ensure_standard_warnings_logging() |
|
| 3008 |
+ ) |
|
| 3000 | 3009 |
THE_FUTURE = 'the future will be here sooner than you think' # noqa: N806 |
| 3001 | 3010 |
JUST_TESTING = 'just testing whether warnings work' # noqa: N806 |
| 3002 | 3011 |
with warnings_cm: |
| ... | ... |
@@ -3549,9 +3558,9 @@ class TestCLITransition: |
| 3549 | 3558 |
runner=runner, |
| 3550 | 3559 |
) |
| 3551 | 3560 |
) |
| 3552 |
- cli_helpers.config_filename(subsystem='old settings.json').write_text( |
|
| 3553 |
- json.dumps(config, indent=2) + '\n', encoding='UTF-8' |
|
| 3554 |
- ) |
|
| 3561 |
+ cli_helpers.config_filename( |
|
| 3562 |
+ subsystem='old settings.json' |
|
| 3563 |
+ ).write_text(json.dumps(config, indent=2) + '\n', encoding='UTF-8') |
|
| 3555 | 3564 |
assert cli_helpers.migrate_and_load_old_config()[0] == config |
| 3556 | 3565 |
|
| 3557 | 3566 |
@pytest.mark.parametrize( |
| ... | ... |
@@ -3590,9 +3599,9 @@ class TestCLITransition: |
| 3590 | 3599 |
runner=runner, |
| 3591 | 3600 |
) |
| 3592 | 3601 |
) |
| 3593 |
- cli_helpers.config_filename(subsystem='old settings.json').write_text( |
|
| 3594 |
- json.dumps(config, indent=2) + '\n', encoding='UTF-8' |
|
| 3595 |
- ) |
|
| 3602 |
+ cli_helpers.config_filename( |
|
| 3603 |
+ subsystem='old settings.json' |
|
| 3604 |
+ ).write_text(json.dumps(config, indent=2) + '\n', encoding='UTF-8') |
|
| 3596 | 3605 |
assert cli_helpers.migrate_and_load_old_config() == (config, None) |
| 3597 | 3606 |
|
| 3598 | 3607 |
@pytest.mark.parametrize( |
| ... | ... |
@@ -3631,9 +3640,9 @@ class TestCLITransition: |
| 3631 | 3640 |
runner=runner, |
| 3632 | 3641 |
) |
| 3633 | 3642 |
) |
| 3634 |
- cli_helpers.config_filename(subsystem='old settings.json').write_text( |
|
| 3635 |
- json.dumps(config, indent=2) + '\n', encoding='UTF-8' |
|
| 3636 |
- ) |
|
| 3643 |
+ cli_helpers.config_filename( |
|
| 3644 |
+ subsystem='old settings.json' |
|
| 3645 |
+ ).write_text(json.dumps(config, indent=2) + '\n', encoding='UTF-8') |
|
| 3637 | 3646 |
cli_helpers.config_filename(subsystem='vault').mkdir( |
| 3638 | 3647 |
parents=True, exist_ok=True |
| 3639 | 3648 |
) |
| ... | ... |
@@ -3678,10 +3687,12 @@ class TestCLITransition: |
| 3678 | 3687 |
runner=runner, |
| 3679 | 3688 |
) |
| 3680 | 3689 |
) |
| 3681 |
- cli_helpers.config_filename(subsystem='old settings.json').write_text( |
|
| 3682 |
- json.dumps(config, indent=2) + '\n', encoding='UTF-8' |
|
| 3683 |
- ) |
|
| 3684 |
- with pytest.raises(ValueError, match=cli_helpers.INVALID_VAULT_CONFIG): |
|
| 3690 |
+ cli_helpers.config_filename( |
|
| 3691 |
+ subsystem='old settings.json' |
|
| 3692 |
+ ).write_text(json.dumps(config, indent=2) + '\n', encoding='UTF-8') |
|
| 3693 |
+ with pytest.raises( |
|
| 3694 |
+ ValueError, match=cli_helpers.INVALID_VAULT_CONFIG |
|
| 3695 |
+ ): |
|
| 3685 | 3696 |
cli_helpers.migrate_and_load_old_config() |
| 3686 | 3697 |
|
| 3687 | 3698 |
def test_200_forward_export_vault_path_parameter( |
| ... | ... |
@@ -3849,7 +3860,9 @@ class TestCLITransition: |
| 3849 | 3860 |
runner=runner, |
| 3850 | 3861 |
) |
| 3851 | 3862 |
) |
| 3852 |
- cli_helpers.config_filename(subsystem='old settings.json').write_text( |
|
| 3863 |
+ cli_helpers.config_filename( |
|
| 3864 |
+ subsystem='old settings.json' |
|
| 3865 |
+ ).write_text( |
|
| 3853 | 3866 |
json.dumps( |
| 3854 | 3867 |
{'services': {DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS}},
|
| 3855 | 3868 |
indent=2, |
| ... | ... |
@@ -3888,7 +3901,9 @@ class TestCLITransition: |
| 3888 | 3901 |
runner=runner, |
| 3889 | 3902 |
) |
| 3890 | 3903 |
) |
| 3891 |
- cli_helpers.config_filename(subsystem='old settings.json').write_text( |
|
| 3904 |
+ cli_helpers.config_filename( |
|
| 3905 |
+ subsystem='old settings.json' |
|
| 3906 |
+ ).write_text( |
|
| 3892 | 3907 |
json.dumps( |
| 3893 | 3908 |
{'services': {DUMMY_SERVICE: DUMMY_CONFIG_SETTINGS}},
|
| 3894 | 3909 |
indent=2, |
| ... | ... |
@@ -3938,7 +3953,9 @@ class TestCLITransition: |
| 3938 | 3953 |
vault_config=config, |
| 3939 | 3954 |
) |
| 3940 | 3955 |
) |
| 3941 |
- old_name = cli_helpers.config_filename(subsystem='old settings.json') |
|
| 3956 |
+ old_name = cli_helpers.config_filename( |
|
| 3957 |
+ subsystem='old settings.json' |
|
| 3958 |
+ ) |
|
| 3942 | 3959 |
new_name = cli_helpers.config_filename(subsystem='vault') |
| 3943 | 3960 |
old_name.unlink(missing_ok=True) |
| 3944 | 3961 |
new_name.rename(old_name) |
| ... | ... |
@@ -5194,7 +5211,9 @@ class TestShellCompletion: |
| 5194 | 5211 |
}, |
| 5195 | 5212 |
) |
| 5196 | 5213 |
) |
| 5197 |
- cli_helpers.config_filename(subsystem='vault').unlink(missing_ok=True) |
|
| 5214 |
+ cli_helpers.config_filename(subsystem='vault').unlink( |
|
| 5215 |
+ missing_ok=True |
|
| 5216 |
+ ) |
|
| 5198 | 5217 |
assert not cli_helpers.shell_complete_service( |
| 5199 | 5218 |
click.Context(cli.derivepassphrase), |
| 5200 | 5219 |
click.Argument(['some_parameter']), |
| ... | ... |
@@ -21,7 +21,7 @@ from hypothesis import strategies |
| 21 | 21 |
|
| 22 | 22 |
import tests |
| 23 | 23 |
from derivepassphrase import _types, ssh_agent, vault |
| 24 |
-from derivepassphrase._internals import cli_helpers, cli_machinery |
|
| 24 |
+from derivepassphrase._internals import cli_helpers |
|
| 25 | 25 |
|
| 26 | 26 |
if TYPE_CHECKING: |
| 27 | 27 |
from collections.abc import Iterable |
| 28 | 28 |