Make obtaining the compatibility config filename more explicit
Marco Ricci

Marco Ricci commited on 2024-12-19 15:04:11
Zeige 2 geänderte Dateien mit 26 Einfügungen und 10 Löschungen.


When obtaining the compatibility config filename `settings.json` from
`cli._config_filename`, rename the corresponding subsystem from
`settings` to `old settings.json`.  Furthermore, pass that explicit
subsystem upon each call.  This frees us up to change the default
subsystem in a future commit.

Because this is not a public function, this is not a change in the
public API.

Some reformatting is necessary.
... ...
@@ -877,7 +877,7 @@ def derivepassphrase_export_vault(
877 877
 
878 878
 
879 879
 def _config_filename(
880
-    subsystem: str | None = 'settings',
880
+    subsystem: str | None = 'old settings.json',
881 881
 ) -> str | bytes | pathlib.Path:
882 882
     """Return the filename of the configuration file for the subsystem.
883 883
 
... ...
@@ -912,8 +912,10 @@ def _config_filename(
912 912
     # Use match/case here once Python 3.9 becomes unsupported.
913 913
     if subsystem is None:
914 914
         return path
915
-    elif subsystem in {'vault', 'settings'}:  # noqa: RET505
915
+    elif subsystem == 'vault':  # noqa: RET505
916 916
         filename = f'{subsystem}.json'
917
+    elif subsystem == 'old settings.json':
918
+        filename = 'settings.json'
917 919
     else:  # pragma: no cover
918 920
         msg = f'Unknown configuration subsystem: {subsystem!r}'
919 921
         raise AssertionError(msg)
... ...
@@ -968,7 +970,7 @@ def _migrate_and_load_old_config() -> tuple[
968 970
 
969 971
     """
970 972
     new_filename = _config_filename(subsystem='vault')
971
-    old_filename = _config_filename()
973
+    old_filename = _config_filename(subsystem='old settings.json')
972 974
     with open(old_filename, 'rb') as fileobj:
973 975
         data = json.load(fileobj)
974 976
     if not _types.is_vault_config(data):
... ...
@@ -1741,7 +1743,9 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
1741 1743
                 backup_config, exc = _migrate_and_load_old_config()
1742 1744
             except FileNotFoundError:
1743 1745
                 return {'services': {}}
1744
-            old_name = os.path.basename(_config_filename())
1746
+            old_name = os.path.basename(
1747
+                _config_filename(subsystem='old settings.json')
1748
+            )
1745 1749
             new_name = os.path.basename(_config_filename(subsystem='vault'))
1746 1750
             deprecation.warning(
1747 1751
                 (
... ...
@@ -2034,7 +2034,9 @@ class TestCLITransition:
2034 2034
     ) -> None:
2035 2035
         runner = click.testing.CliRunner()
2036 2036
         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
2037
-            config_filename = cli._config_filename()
2037
+            config_filename = cli._config_filename(
2038
+                subsystem='old settings.json'
2039
+            )
2038 2040
             with open(config_filename, 'w', encoding='UTF-8') as fileobj:
2039 2041
                 print(json.dumps(config, indent=2), file=fileobj)
2040 2042
             assert cli._migrate_and_load_old_config()[0] == config
... ...
@@ -2063,7 +2065,9 @@ class TestCLITransition:
2063 2065
     ) -> None:
2064 2066
         runner = click.testing.CliRunner()
2065 2067
         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
2066
-            config_filename = cli._config_filename()
2068
+            config_filename = cli._config_filename(
2069
+                subsystem='old settings.json'
2070
+            )
2067 2071
             with open(config_filename, 'w', encoding='UTF-8') as fileobj:
2068 2072
                 print(json.dumps(config, indent=2), file=fileobj)
2069 2073
             assert cli._migrate_and_load_old_config() == (config, None)
... ...
@@ -2092,7 +2096,9 @@ class TestCLITransition:
2092 2096
     ) -> None:
2093 2097
         runner = click.testing.CliRunner()
2094 2098
         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
2095
-            config_filename = cli._config_filename()
2099
+            config_filename = cli._config_filename(
2100
+                subsystem='old settings.json'
2101
+            )
2096 2102
             with open(config_filename, 'w', encoding='UTF-8') as fileobj:
2097 2103
                 print(json.dumps(config, indent=2), file=fileobj)
2098 2104
             os.mkdir(cli._config_filename(subsystem='vault'))
... ...
@@ -2125,7 +2131,9 @@ class TestCLITransition:
2125 2131
     ) -> None:
2126 2132
         runner = click.testing.CliRunner()
2127 2133
         with tests.isolated_config(monkeypatch=monkeypatch, runner=runner):
2128
-            config_filename = cli._config_filename()
2134
+            config_filename = cli._config_filename(
2135
+                subsystem='old settings.json'
2136
+            )
2129 2137
             with open(config_filename, 'w', encoding='UTF-8') as fileobj:
2130 2138
                 print(json.dumps(config, indent=2), file=fileobj)
2131 2139
             with pytest.raises(ValueError, match=cli._INVALID_VAULT_CONFIG):
... ...
@@ -2260,7 +2268,9 @@ class TestCLITransition:
2260 2268
             runner=runner,
2261 2269
         ):
2262 2270
             with open(
2263
-                cli._config_filename(), 'w', encoding='UTF-8'
2271
+                cli._config_filename(subsystem='old settings.json'),
2272
+                'w',
2273
+                encoding='UTF-8',
2264 2274
             ) as fileobj:
2265 2275
                 print(
2266 2276
                     json.dumps(
... ...
@@ -2294,7 +2304,9 @@ class TestCLITransition:
2294 2304
             runner=runner,
2295 2305
         ):
2296 2306
             with open(
2297
-                cli._config_filename(), 'w', encoding='UTF-8'
2307
+                cli._config_filename(subsystem='old settings.json'),
2308
+                'w',
2309
+                encoding='UTF-8',
2298 2310
             ) as fileobj:
2299 2311
                 print(
2300 2312
                     json.dumps(
2301 2313