-
Notifications
You must be signed in to change notification settings - Fork 452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP Add dependency-injector to Tribler #6197
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ netifaces | |
pyqtgraph | ||
yappi | ||
pydantic | ||
dependency-injector |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from ipv8.messaging.interfaces.dispatcher.endpoint import DispatcherEndpoint | ||
from ipv8.peer import Peer | ||
|
||
from ipv8_service import IPv8 | ||
|
||
from dependency_injector import containers, providers | ||
|
||
from tribler_core.ipv8_config import Ipv8Config | ||
from tribler_core.trustchain_keys import TrustChainKeys | ||
|
||
|
||
class Ipv8Container(containers.DeclarativeContainer): | ||
state_dir = providers.Dependency() | ||
config = providers.Configuration() | ||
|
||
ipv8_config = providers.Singleton(Ipv8Config, state_dir=state_dir, config=config.provider) | ||
|
||
endpoint = providers.Singleton( | ||
DispatcherEndpoint, | ||
providers.List(providers.Object("UDPIPv4")), | ||
UDPIPv4=providers.Dict(port=providers.Factory(config.port), ip=providers.Factory(config.address)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, this line looks not too intuitive 😕. I would expect it to be just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be replaced by: endpoint = providers.Singleton(
DispatcherEndpoint, ["UDPIPv4"],
UDPIPv4=providers.Dict(port=config.port, ip=config.address),
) Explanations later... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you use {'UDPIPv4': {'port': <dependency_injector.providers.ConfigurationOption('config.port') at 0x112961eb0>}} |
||
) | ||
|
||
ipv8 = providers.Singleton( | ||
IPv8, ipv8_config.provided.value, enable_statistics=config.statistics, endpoint_override=endpoint | ||
) | ||
|
||
|
||
class ApplicationContainer(containers.DeclarativeContainer): | ||
state_dir = providers.Dependency() | ||
config = providers.Configuration() | ||
|
||
trustchain_keys = providers.Singleton(TrustChainKeys, state_dir=state_dir, config=config.provider) | ||
peer = providers.Singleton(Peer, trustchain_keys.provided.trustchain_keypair) | ||
|
||
ipv8_container = providers.Container(Ipv8Container, state_dir=state_dir, config=config.ipv8) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from ipv8.configuration import ConfigBuilder | ||
|
||
|
||
class Ipv8Config: | ||
"""Extracted from session.py""" | ||
|
||
def __init__(self, state_dir=None, config=None): | ||
self.value = (ConfigBuilder() | ||
.set_port(config.port()) | ||
.set_address(config.address()) | ||
.clear_overlays() | ||
.clear_keys() # We load the keys ourselves | ||
.set_working_directory(str(state_dir)) | ||
.set_walker_interval(config.walk_interval()) | ||
.finalize()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from dependency_injector import containers | ||
|
||
|
||
class CommunityContainer(containers.DeclarativeContainer): | ||
... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from dependency_injector import providers | ||
|
||
from ipv8.peerdiscovery.discovery import RandomWalk | ||
from tribler_core.modules import container | ||
from tribler_core.modules.metadata_store.community.sync_strategy import RemovePeers | ||
from tribler_core.modules.popularity.community import PopularityCommunity | ||
|
||
|
||
class PopularityCommunityContainer(container.CommunityContainer): | ||
config = providers.Configuration() | ||
rqc_config = providers.Configuration() | ||
|
||
metadata_store = providers.Dependency() | ||
torrent_checker = providers.Dependency() | ||
|
||
peer = providers.Dependency() | ||
endpoint = providers.Dependency() | ||
network = providers.Dependency() | ||
|
||
community = providers.Factory(PopularityCommunity, peer, endpoint, network, | ||
settings=config, rqc_settings=rqc_config, | ||
metadata_store=metadata_store, torrent_checker=torrent_checker) | ||
|
||
strategies = providers.List( | ||
providers.Factory(RandomWalk, community, target_peers=30), | ||
providers.Factory(RemovePeers, community, target_peers=-1), | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A good start point to discover PR.