Skip to content

Commit

Permalink
Merge pull request #6879 from drew2a/feature/config_refactoring
Browse files Browse the repository at this point in the history
Config refactoring
  • Loading branch information
drew2a authored Apr 20, 2022
2 parents a479278 + a26ae4e commit 864be46
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 68 deletions.
2 changes: 1 addition & 1 deletion requirements-core.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions src/tribler/core/config/tests/test_tribler_config.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
57 changes: 47 additions & 10 deletions src/tribler/core/config/tribler_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions src/tribler/core/config/tribler_config_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
46 changes: 0 additions & 46 deletions src/tribler/core/config/tribler_config_sections.py

This file was deleted.

0 comments on commit 864be46

Please sign in to comment.