mirror of
https://github.com/bottlesdevs/Bottles.git
synced 2026-10-28 23:14:14 +08:00
* refactor: migrate config from dict to object * fix: type hint import * fix: BottleConfig.load() * fix: config loading in BackupManager.import_backup() * fix: Remove unused import * fix: more guard code, type hints and Config object usage * fix: a potential crash when SteamManager.get_app_config() failed * Revert "fix: Remove unused import" This reverts commit2d48a4e08a. * add: new config field * fix: Config object value read * misc: remove review TODO comments * adapt to new BottleConfig object * fix RunAsync usage * add: BottleConfig._fix() * fix: type hint and Config object usage * fix: typo in FSR setting default value * adapt to new BottleConfig object * remove unnecessary DictCompat method * add: BottleConfig._filter() * fix: TemplateManager.new() * misc: critical guard code * fix: typo in commitb7ed61e678* misc: more type hinting * Revert "remove unnecessary DictCompat method" This reverts commit265633e733. * fix crash when user choose Cancel in ProtonAlertDialog * fix force stop all programs not work * adapt NVAPIComponent.check_bottle_nvngx to Config object * Better DictCompatMixIn * InstallerManager.__set_parameters incorrect adopt * fix typo incbe1dc760b* adopt LibraryManager.download_thumbnail to Config object * serialize Config object to yaml automatically * fix translation and remove workaround in0830fddbc9* fix missing locale for Chinese * comment * fix incorrect variable init position * remove 'in' extensions and some bugfix * fix executable filemode Co-authored-by: Mirko Brombin <send@mirko.pm>
96 lines
3.0 KiB
Python
96 lines
3.0 KiB
Python
# data.py
|
|
#
|
|
# Copyright 2022 brombinmirko <send@mirko.pm>
|
|
#
|
|
# 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, in version 3 of the License.
|
|
#
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
#
|
|
|
|
import os
|
|
from bottles.backend.utils import yaml
|
|
import contextlib
|
|
from gi.repository import GLib
|
|
|
|
from bottles.backend.logger import Logger
|
|
from bottles.backend.models.samples import Samples
|
|
|
|
logging = Logger()
|
|
|
|
|
|
class DataManager:
|
|
"""
|
|
The DataManager class is used to store and retrieve data
|
|
from the user data.yml file. Should be stored only info
|
|
and settings that should not be stored in gsettings.
|
|
"""
|
|
|
|
__data: dict = {}
|
|
__p_xdg_data_home = GLib.get_user_data_dir()
|
|
__p_base = f"{__p_xdg_data_home}/bottles"
|
|
__p_data = f"{__p_base}/data.yml"
|
|
|
|
def __init__(self):
|
|
self.__get_data()
|
|
|
|
def __get_data(self):
|
|
try:
|
|
with open(self.__p_data, 'r') as s:
|
|
self.__data = yaml.load(s)
|
|
if self.__data is None:
|
|
raise AttributeError
|
|
except FileNotFoundError:
|
|
logging.error('Data file not found. Creating new one.', )
|
|
self.__create_data_file()
|
|
except AttributeError:
|
|
logging.error('Data file is empty. Creating new one.', )
|
|
self.__create_data_file()
|
|
|
|
def __create_data_file(self):
|
|
if not os.path.exists(self.__p_base):
|
|
os.makedirs(self.__p_base)
|
|
with open(self.__p_data, 'w') as s:
|
|
yaml.dump(Samples.data, s)
|
|
self.__get_data()
|
|
|
|
def list(self):
|
|
"""Returns the whole data dictionary."""
|
|
return self.__data
|
|
|
|
def set(self, key, value, of_type=None):
|
|
"""Sets a value in the data dictionary."""
|
|
if self.__data.get(key):
|
|
if isinstance(self.__data[key], list):
|
|
self.__data[key].append(value)
|
|
else:
|
|
self.__data[key] = value
|
|
else:
|
|
if of_type == list:
|
|
self.__data[key] = [value]
|
|
else:
|
|
self.__data[key] = value
|
|
|
|
with contextlib.suppress(FileNotFoundError):
|
|
with open(self.__p_data, 'w') as s:
|
|
yaml.dump(self.__data, s)
|
|
|
|
def remove(self, key):
|
|
"""Removes a key from the data dictionary."""
|
|
if self.__data.get(key):
|
|
del self.__data[key]
|
|
with contextlib.suppress(FileNotFoundError):
|
|
with open(self.__p_data, 'w') as s:
|
|
yaml.dump(self.__data, s)
|
|
|
|
def get(self, key):
|
|
"""Returns the value of a key in the data dictionary."""
|
|
return self.__data.get(key)
|