mirror of
https://github.com/LorenEteval/Furious.git
synced 2026-09-27 09:27:55 +03:00
73 lines
2.2 KiB
Python
73 lines
2.2 KiB
Python
# Copyright (C) 2024–present Loren Eteval & contributors <loren.eteval@proton.me>
|
||
#
|
||
# 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 <https://www.gnu.org/licenses/>.
|
||
|
||
from __future__ import annotations
|
||
|
||
from Furious.Frozenlib import *
|
||
from Furious.Interface import *
|
||
from Furious.Library.Encoder import *
|
||
from Furious.Library.Configuration import configFactoryFromAny
|
||
|
||
__all__ = ['UserServers']
|
||
|
||
registerAppSettings('Configuration')
|
||
|
||
|
||
class UserServer:
|
||
remark: str
|
||
config: str
|
||
subsId: str
|
||
|
||
|
||
class UserServers(Mixins.CleanupOnExit, StorageFactory):
|
||
# remark, config, subsId. (subsId corresponds to unique in user subscription)
|
||
def __init__(self, *args, **kwargs):
|
||
super().__init__(*args, **kwargs)
|
||
|
||
def restore():
|
||
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):
|
||
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 self._list
|
||
|
||
def cleanup(self):
|
||
self.sync()
|