From a26ae4eccc011ae0e70ef810464375c4a8cbebcb Mon Sep 17 00:00:00 2001 From: drew2a Date: Wed, 20 Apr 2022 18:18:05 +0200 Subject: [PATCH] Refactor TriblerConfig --- requirements-core.txt | 2 +- .../core/config/tests/test_tribler_config.py | 13 ++++- src/tribler/core/config/tribler_config.py | 57 +++++++++++++++---- .../core/config/tribler_config_section.py | 11 +--- .../core/config/tribler_config_sections.py | 46 --------------- 5 files changed, 61 insertions(+), 68 deletions(-) delete mode 100644 src/tribler/core/config/tribler_config_sections.py diff --git a/requirements-core.txt b/requirements-core.txt index de8cd83d44a..64b882f4ea7 100644 --- a/requirements-core.txt +++ b/requirements-core.txt @@ -14,7 +14,7 @@ networkx==2.6.3 pony==0.7.14 psutil==5.8.0 pyasn1==0.4.8 -pydantic==1.8.2 +pydantic==1.9.0 PyOpenSSL==21.0.0 pyyaml==6.0 sentry-sdk==1.5.0 diff --git a/src/tribler/core/config/tests/test_tribler_config.py b/src/tribler/core/config/tests/test_tribler_config.py index 2639b2e70c6..ba9008a8c79 100644 --- a/src/tribler/core/config/tests/test_tribler_config.py +++ b/src/tribler/core/config/tests/test_tribler_config.py @@ -1,8 +1,7 @@ import shutil -from configobj import ParseError - import pytest +from configobj import ParseError from tribler.core.config.tribler_config import TriblerConfig from tribler.core.tests.tools.common import TESTS_DATA_DIR @@ -94,7 +93,15 @@ async def test_get_path_relative(tmpdir): async def test_get_path_absolute(tmpdir): config = TriblerConfig(state_dir=tmpdir) config.general.log_dir = str(Path(tmpdir).parent) - assert config.general.get_path_as_absolute(property_name='log_dir', state_dir=tmpdir) == Path(tmpdir).parent + state_dir = Path(tmpdir) + assert config.general.get_path_as_absolute(property_name='log_dir', state_dir=state_dir) == Path(tmpdir).parent + + +def test_get_path_absolute_none(tmpdir): + config = TriblerConfig(state_dir=tmpdir) + config.general.log_dir = None + state_dir = Path(tmpdir) + assert config.general.get_path_as_absolute(property_name='log_dir', state_dir=state_dir) is None @pytest.mark.asyncio diff --git a/src/tribler/core/config/tribler_config.py b/src/tribler/core/config/tribler_config.py index 04e2a0beaff..04a98753d83 100644 --- a/src/tribler/core/config/tribler_config.py +++ b/src/tribler/core/config/tribler_config.py @@ -7,25 +7,62 @@ import configobj from configobj import ParseError - -from pydantic import PrivateAttr - -from tribler.core.config.tribler_config_sections import TriblerConfigSections +from pydantic import BaseSettings, Extra, PrivateAttr + +from tribler.core.components.bandwidth_accounting.settings import BandwidthAccountingSettings +from tribler.core.components.gigachannel.community.settings import ChantSettings +from tribler.core.components.ipv8.settings import ( + BootstrapSettings, + DHTSettings, + DiscoveryCommunitySettings, + Ipv8Settings, +) +from tribler.core.components.key.settings import TrustchainSettings +from tribler.core.components.libtorrent.settings import DownloadDefaultsSettings, LibtorrentSettings +from tribler.core.components.metadata_store.remote_query_community.settings import RemoteQueryCommunitySettings +from tribler.core.components.popularity.settings import PopularityCommunitySettings +from tribler.core.components.resource_monitor.settings import ResourceMonitorSettings +from tribler.core.components.restapi.rest.settings import APISettings +from tribler.core.components.torrent_checker.settings import TorrentCheckerSettings +from tribler.core.components.tunnel.settings import TunnelCommunitySettings +from tribler.core.components.watch_folder.settings import WatchFolderSettings +from tribler.core.settings import GeneralSettings logger = logging.getLogger('Tribler Config') -class TriblerConfig(TriblerConfigSections): - """ Tribler config class that contains common logic for manipulating with a config. - """ - _state_dir: Path = PrivateAttr() - _file: Optional[Path] = PrivateAttr() # a last file saved during write-load operations - _error: Optional[Exception] = PrivateAttr() +class TriblerConfig(BaseSettings): + """ Tribler config class that contains common logic for manipulating with a config.""" + + class Config: + extra = Extra.ignore # ignore extra attributes during model initialization + + general: GeneralSettings = GeneralSettings() + tunnel_community: TunnelCommunitySettings = TunnelCommunitySettings() + bandwidth_accounting: BandwidthAccountingSettings = BandwidthAccountingSettings() + bootstrap: BootstrapSettings = BootstrapSettings() + ipv8: Ipv8Settings = Ipv8Settings() + discovery_community: DiscoveryCommunitySettings = DiscoveryCommunitySettings() + dht: DHTSettings = DHTSettings() + trustchain: TrustchainSettings = TrustchainSettings() + watch_folder: WatchFolderSettings = WatchFolderSettings() + chant: ChantSettings = ChantSettings() + torrent_checking: TorrentCheckerSettings = TorrentCheckerSettings() + libtorrent: LibtorrentSettings = LibtorrentSettings() + download_defaults: DownloadDefaultsSettings = DownloadDefaultsSettings() + api: APISettings = APISettings() + resource_monitor: ResourceMonitorSettings = ResourceMonitorSettings() + popularity_community: PopularityCommunitySettings = PopularityCommunitySettings() + remote_query_community: RemoteQueryCommunitySettings = RemoteQueryCommunitySettings() # Special configuration options related to the operation mode of the Core upgrader_enabled: bool = True gui_test_mode: bool = False + _state_dir: Path = PrivateAttr() + _file: Optional[Path] = PrivateAttr() # a last file saved during write-load operations + _error: Optional[Exception] = PrivateAttr() + def __init__(self, *args, state_dir: Path = None, file: Path = None, error: str = None, **kwargs): """ Constructor diff --git a/src/tribler/core/config/tribler_config_section.py b/src/tribler/core/config/tribler_config_section.py index f10c5f474e4..b2c10f5f595 100644 --- a/src/tribler/core/config/tribler_config_section.py +++ b/src/tribler/core/config/tribler_config_section.py @@ -17,7 +17,7 @@ class Config: def put_path_as_relative(self, property_name: str, value: Path = None, state_dir: str = None): """Save a relative path if 'value' is relative to state_dir. - Save an absolute path overwise. + Save an absolute path otherwise. """ if value is not None: # try to put a relative path (if it possible) @@ -32,17 +32,12 @@ def put_path_as_relative(self, property_name: str, value: Path = None, state_dir def get_path_as_absolute(self, property_name: str, state_dir: Path = None) -> Optional[Path]: """ Get path as absolute. If stored value already in absolute form, then it will be returned in "as is". - `state_dir / path` will be returned overwise. + `state_dir / path` will be returned otherwise. """ value = self.__getattribute__(property_name) if value is None: return None - - path = Path(value) - if path.is_absolute(): - return path - - return state_dir / path + return state_dir / value @root_validator(pre=True) def convert_from_none_string_to_none_type(cls, values): # pylint: disable=no-self-argument diff --git a/src/tribler/core/config/tribler_config_sections.py b/src/tribler/core/config/tribler_config_sections.py deleted file mode 100644 index 1a2dc580c80..00000000000 --- a/src/tribler/core/config/tribler_config_sections.py +++ /dev/null @@ -1,46 +0,0 @@ -from pydantic import BaseSettings, Extra - -from tribler.core.components.bandwidth_accounting.settings import BandwidthAccountingSettings -from tribler.core.components.gigachannel.community.settings import ChantSettings -from tribler.core.components.ipv8.settings import ( - BootstrapSettings, - DHTSettings, - DiscoveryCommunitySettings, - Ipv8Settings, -) -from tribler.core.components.key.settings import TrustchainSettings -from tribler.core.components.libtorrent.settings import DownloadDefaultsSettings, LibtorrentSettings -from tribler.core.components.metadata_store.remote_query_community.settings import RemoteQueryCommunitySettings -from tribler.core.components.popularity.settings import PopularityCommunitySettings -from tribler.core.components.resource_monitor.settings import ResourceMonitorSettings -from tribler.core.components.restapi.rest.settings import APISettings -from tribler.core.components.torrent_checker.settings import TorrentCheckerSettings -from tribler.core.components.tunnel.settings import TunnelCommunitySettings -from tribler.core.components.watch_folder.settings import WatchFolderSettings -from tribler.core.settings import GeneralSettings - - -class TriblerConfigSections(BaseSettings): - """ Base Tribler config class that contains section listing. - """ - - class Config: - extra = Extra.ignore # ignore extra attributes during model initialization - - general: GeneralSettings = GeneralSettings() - tunnel_community: TunnelCommunitySettings = TunnelCommunitySettings() - bandwidth_accounting: BandwidthAccountingSettings = BandwidthAccountingSettings() - bootstrap: BootstrapSettings = BootstrapSettings() - ipv8: Ipv8Settings = Ipv8Settings() - discovery_community: DiscoveryCommunitySettings = DiscoveryCommunitySettings() - dht: DHTSettings = DHTSettings() - trustchain: TrustchainSettings = TrustchainSettings() - watch_folder: WatchFolderSettings = WatchFolderSettings() - chant: ChantSettings = ChantSettings() - torrent_checking: TorrentCheckerSettings = TorrentCheckerSettings() - libtorrent: LibtorrentSettings = LibtorrentSettings() - download_defaults: DownloadDefaultsSettings = DownloadDefaultsSettings() - api: APISettings = APISettings() - resource_monitor: ResourceMonitorSettings = ResourceMonitorSettings() - popularity_community: PopularityCommunitySettings = PopularityCommunitySettings() - remote_query_community: RemoteQueryCommunitySettings = RemoteQueryCommunitySettings()