1+ import json
12from typing import Any , Dict
23
34import pykson # type: ignore
@@ -28,12 +29,14 @@ def migrate(self, data: Dict[str, Any]) -> None:
2829
2930class SettingsV2 (SettingsV1 ):
3031 VERSION = 2
31- # Stores user-selected mode per interface: {"wlan0": "normal", "wlan1": "hotspot"}
32- interface_modes = pykson .ObjectField ( dict , default_value = {} )
32+ # Stores user-selected mode per interface as JSON: ' {"wlan0": "normal", "wlan1": "hotspot"}'
33+ interface_modes_json = pykson .StringField ( )
3334
3435 def __init__ (self , * args : str , ** kwargs : int ) -> None :
3536 super ().__init__ (* args , ** kwargs )
3637 self .VERSION = SettingsV2 .VERSION
38+ if not self .interface_modes_json :
39+ self .interface_modes_json = "{}"
3740
3841 def migrate (self , data : Dict [str , Any ]) -> None :
3942 if data ["VERSION" ] == SettingsV2 .VERSION :
@@ -42,8 +45,19 @@ def migrate(self, data: Dict[str, Any]) -> None:
4245 if data ["VERSION" ] < SettingsV2 .VERSION :
4346 super ().migrate (data )
4447
45- # Migration from version 1 to 2: add interface_modes
46- if "interface_modes " not in data :
47- data ["interface_modes " ] = {}
48+ # Migration from version 1 to 2: add interface_modes_json
49+ if "interface_modes_json " not in data :
50+ data ["interface_modes_json " ] = "{}"
4851
4952 data ["VERSION" ] = SettingsV2 .VERSION
53+
54+ def get_interface_modes (self ) -> Dict [str , str ]:
55+ """Get interface modes as a dictionary."""
56+ try :
57+ return json .loads (self .interface_modes_json or "{}" )
58+ except json .JSONDecodeError :
59+ return {}
60+
61+ def set_interface_modes (self , modes : Dict [str , str ]) -> None :
62+ """Set interface modes from a dictionary."""
63+ self .interface_modes_json = json .dumps (modes )
0 commit comments