# Copyright (C) 2024–present Loren Eteval & contributors # # This file is part of Furious. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . """Persist and reconstruct user server configurations.""" from __future__ import annotations from Furious.Frozenlib import * from Furious.Interface import * from Furious.Library.Encoder import * from Furious.Plugins.Configuration import configFactoryFromAny __all__ = ['UserServers'] registerAppSettings('Configuration') class UserServer: """Represent user server.""" remark: str config: str subsId: str class UserServers(Mixins.CleanupOnExit, StorageFactory): # remark, config, subsId. (subsId corresponds to unique in user subscription) """Manage the persisted list of server configurations.""" def __init__(self, *args, **kwargs): """Initialize the UserServers.""" super().__init__(*args, **kwargs) def restore(): """Restore the user servers.""" try: return UJSONEncoder.decode( PyBase64Encoder.decode(AppSettings.get('Configuration')) ) except Exception: # Any non-exit exceptions return {'model': []} self._data = restore() self._list = list( configFactoryFromAny(model.pop('config', ''), index=index, **model) for index, model in enumerate(self._data['model']) ) def sync(self): """Persist the current user servers data.""" AppSettings.set( 'Configuration', PyBase64Encoder.encode( UJSONEncoder.encode( {'model': list(factory.toStorageObject() for factory in self._list)} ).encode() ), ) def data(self) -> list[ConfigFactory]: # Shallow copy """Return the data managed by the user servers.""" return self._list def cleanup(self): """Release resources owned by the user servers.""" self.sync()