From 4b7e5c27c07496a241f856058113fc8b46f9924c Mon Sep 17 00:00:00 2001 From: drew2a Date: Wed, 20 Apr 2022 18:14:16 +0200 Subject: [PATCH] Rename files --- .../core/config/tests/test_tribler_config.py | 13 +++- src/tribler/core/config/tribler_config.py | 59 +++++++++++++++---- ..._settings.py => tribler_config_section.py} | 2 +- .../core/config/tribler_config_sections.py | 46 --------------- 4 files changed, 59 insertions(+), 61 deletions(-) rename src/tribler/core/config/{tribler_base_settings.py => tribler_config_section.py} (97%) delete mode 100644 src/tribler/core/config/tribler_config_sections.py 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..8abc8aa0ecd 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 @@ -45,7 +82,7 @@ def __init__(self, *args, state_dir: Path = None, file: Path = None, error: str self._error = error @staticmethod - def load(file: Path, state_dir: Path, reset_config_on_error: bool = False) -> TriblerConfig: + def load(file: Path, state_dir: Path, reset_config_on_error: bool = False) -> Config: """ Load a config from a file Args: diff --git a/src/tribler/core/config/tribler_base_settings.py b/src/tribler/core/config/tribler_config_section.py similarity index 97% rename from src/tribler/core/config/tribler_base_settings.py rename to src/tribler/core/config/tribler_config_section.py index c90ca0d65c0..b2c10f5f595 100644 --- a/src/tribler/core/config/tribler_base_settings.py +++ b/src/tribler/core/config/tribler_config_section.py @@ -6,7 +6,7 @@ from pydantic import BaseSettings, Extra, root_validator -class TriblerBaseSettings(BaseSettings): +class TriblerConfigSection(BaseSettings): """Base Class that defines Tribler Config Section We are waiting https://github.com/samuelcolvin/pydantic/pull/2625 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()