From f84f97732d5a2debe54e9923594ec23e31baf041 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Sat, 28 Dec 2019 12:10:46 -0700 Subject: [PATCH] Closes #75: added infrastructure for versioning of config --- sublime/cache_manager.py | 2 +- sublime/config.py | 31 ++++++++++++++++++------------- sublime/state_manager.py | 10 +++++++++- sublime/ui/settings.py | 7 ++++++- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/sublime/cache_manager.py b/sublime/cache_manager.py index 63eb4e5..916e0de 100644 --- a/sublime/cache_manager.py +++ b/sublime/cache_manager.py @@ -816,7 +816,7 @@ class CacheManager(metaclass=Singleton): _instance: Optional[__CacheManagerInternal] = None - def __init__(self, server_config: ServerConfiguration): + def __init__(self): raise Exception('Do not instantiate the CacheManager.') @classmethod diff --git a/sublime/config.py b/sublime/config.py index 3bf7d2b..426f88c 100644 --- a/sublime/config.py +++ b/sublime/config.py @@ -5,6 +5,7 @@ from typing import List, Optional class ServerConfiguration: + version: int name: str server_address: str local_network_address: str @@ -40,6 +41,9 @@ class ServerConfiguration: self.sync_enabled = sync_enabled self.disable_cert_verify = disable_cert_verify + def migrate(self): + pass + @property def password(self): return keyring.get_password( @@ -60,22 +64,23 @@ class AppConfiguration: prefetch_amount: int = 3 concurrent_download_limit: int = 5 port_number: int = 8080 + version: int = 1 def to_json(self): - # TODO can we simplify? - return { - 'servers': [s.__dict__ for s in self.servers], - 'current_server': self.current_server, - '_cache_location': getattr(self, '_cache_location', None), - 'max_cache_size_mb': self.max_cache_size_mb, - 'show_headers': self.show_headers, - 'always_stream': self.always_stream, - 'download_on_stream': self.download_on_stream, - 'song_play_notification': self.song_play_notification, - 'prefetch_amount': self.prefetch_amount, - 'concurrent_download_limit': self.concurrent_download_limit, - 'port_number': self.port_number, + exclude = ('servers') + json_object = { + k: getattr(self, k) + for k in self.__annotations__.keys() + if k not in exclude } + json_object.update({ + 'servers': [s.__dict__ for s in self.servers], + }) + return json_object + + def migrate(self): + for server in self.servers: + server.migrate() @property def cache_location(self): diff --git a/sublime/state_manager.py b/sublime/state_manager.py index c024d5b..d3b56bb 100644 --- a/sublime/state_manager.py +++ b/sublime/state_manager.py @@ -49,6 +49,7 @@ class ApplicationState: functions define what part of the state will be saved across application loads. """ + version: int = 1 config: AppConfiguration = AppConfiguration() config_file: str = None playing: bool = False @@ -90,6 +91,7 @@ class ApplicationState: return json_object def load_from_json(self, json_object): + self.version = json_object.get('version', 0) self.current_song_index = json_object.get('current_song_index', -1) self.play_queue = json_object.get('play_queue', []) self.old_play_queue = json_object.get('old_play_queue', []) @@ -130,10 +132,16 @@ class ApplicationState: self.load_from_json(json.load(f)) except json.decoder.JSONDecodeError: # Who cares, it's just state. - pass + self.load_from_json({}) else: self.load_from_json({}) + self.migrate() + + def migrate(self): + """Use this function to migrate any state storage that has changed.""" + self.config.migrate() + def save(self): # Make the necessary directories before writing the state. os.makedirs(os.path.dirname(self.state_filename), exist_ok=True) diff --git a/sublime/ui/settings.py b/sublime/ui/settings.py index 4ee7568..aace4af 100644 --- a/sublime/ui/settings.py +++ b/sublime/ui/settings.py @@ -8,7 +8,12 @@ class SettingsDialog(EditFormDialog): title: str = 'Settings' initial_size = (450, 250) text_fields = [ - ('Port Number (will take effect on restart)', 'port_number', False), + ( + 'Port Number (for streaming to Chromecasts on the local network, ' + 'will take effect on restart)', + 'port_number', + False, + ), ] boolean_fields = [ ('Show headers on song lists', 'show_headers'),