Skip to content

Commit

Permalink
Refactored classes to reduce redundancy in getting config setting
Browse files Browse the repository at this point in the history
  • Loading branch information
hackrush01 committed May 17, 2018
1 parent 7084dd0 commit 6327c0d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 119 deletions.
15 changes: 6 additions & 9 deletions lbrynet/core/Session.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ class Session(object):
peers can connect to this peer.
"""

def __init__(self, blob_data_payment_rate, db_dir=None,
node_id=None, peer_manager=None, dht_node_port=None,
known_dht_nodes=None, peer_finder=None,
hash_announcer=None, blob_dir=None,
blob_manager=None, peer_port=None, use_upnp=True,
rate_limiter=None, wallet=None,
dht_node_class=node.Node, blob_tracker_class=None,
payment_rate_manager_class=None, is_generous=True, external_ip=None, storage=None):
def __init__(self, blob_data_payment_rate, db_dir=None, node_id=None, peer_manager=None, dht_node_port=None,
known_dht_nodes=None, peer_finder=None, hash_announcer=None, blob_dir=None, blob_manager=None,
peer_port=None, use_upnp=True, rate_limiter=None, wallet=None, dht_node_class=node.Node,
blob_tracker_class=None, payment_rate_manager_class=None, is_generous=True, external_ip=None,
storage=None, dht_node=None):
"""@param blob_data_payment_rate: The default payment rate for blob data
@param db_dir: The directory in which levelDB files should be stored
Expand Down Expand Up @@ -116,7 +113,7 @@ def __init__(self, blob_data_payment_rate, db_dir=None,
self.upnp_redirects = []
self.wallet = wallet
self.dht_node_class = dht_node_class
self.dht_node = None
self.dht_node = dht_node
self.base_payment_rate_manager = BasePaymentRateManager(blob_data_payment_rate)
self.payment_rate_manager = None
self.payment_rate_manager_class = payment_rate_manager_class or NegotiatedPaymentRateManager
Expand Down
186 changes: 76 additions & 110 deletions lbrynet/daemon/Components.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import os
import logging
from twisted.internet import defer, threads

from lbrynet import conf
from lbrynet.database.storage import SQLiteStorage
from lbrynet.core.Session import Session
from lbrynet.core.Wallet import LBRYumWallet
from lbrynet.daemon.Component import Component
from lbrynet.core.Session import Session
from lbrynet.database.storage import SQLiteStorage
from lbrynet.dht import node, hashannouncer

from lbrynet.core.utils import generate_id

log = logging.getLogger(__name__)

# settings must be initialized before this file is imported
Expand All @@ -18,21 +21,38 @@
DHT_COMPONENT = "dht"


class DatabaseComponent(Component):
component_name = DATABASE_COMPONENT
storage = None
class ConfigSettings:
@staticmethod
def get_conf_setting(setting_name):
return conf.settings[setting_name]

@staticmethod
def get_db_dir():
return conf.settings['data_dir']
def get_blobfiles_dir():
if conf.settings['BLOBFILES_DIR'] == "blobfiles":
return os.path.join(GCS("data_dir"), "blobfiles")
else:
log.info("Using non-default blobfiles directory: %s", conf.settings['BLOBFILES_DIR'])
return conf.settings['BLOBFILES_DIR']

@staticmethod
def get_download_directory():
return conf.settings['download_directory']
def get_node_id():
return conf.settings.node_id

@staticmethod
def get_blobfile_dir():
return conf.settings['BLOBFILES_DIR']
def get_external_ip():
from lbrynet.core.system_info import get_platform
platform = get_platform(get_ip=True)
return platform['ip']


# Shorthand for common ConfigSettings methods
CS = ConfigSettings
GCS = ConfigSettings.get_conf_setting


class DatabaseComponent(Component):
component_name = DATABASE_COMPONENT
storage = None

@staticmethod
def get_current_db_revision():
Expand All @@ -52,15 +72,19 @@ def _write_db_revision_file(version_num):
def setup(cls):
# check directories exist, create them if they don't
log.info("Loading databases")
if not os.path.exists(cls.get_download_directory()):
os.mkdir(cls.get_download_directory())
if not os.path.exists(cls.get_db_dir()):
os.mkdir(cls.get_db_dir())

if not os.path.exists(GCS('download_directory')):
os.mkdir(GCS('download_directory'))

if not os.path.exists(GCS('data_dir')):
os.mkdir(GCS('data_dir'))
cls._write_db_revision_file(cls.get_current_db_revision())
log.debug("Created the db revision file: %s", cls.get_revision_filename())
if not os.path.exists(cls.get_blobfile_dir()):
os.mkdir(cls.get_blobfile_dir())
log.debug("Created the blobfile directory: %s", str(cls.get_blobfile_dir()))

if not os.path.exists(CS.get_blobfiles_dir()):
os.mkdir(CS.get_blobfiles_dir())
log.debug("Created the blobfile directory: %s", str(CS.get_blobfiles_dir()))

if not os.path.exists(cls.get_revision_filename()):
log.warning("db_revision file not found. Creating it")
cls._write_db_revision_file(cls.get_current_db_revision())
Expand All @@ -78,14 +102,14 @@ def setup(cls):
from lbrynet.database.migrator import dbmigrator
log.info("Upgrading your databases (revision %i to %i)", old_revision, cls.get_current_db_revision())
yield threads.deferToThread(
dbmigrator.migrate_db, cls.get_db_dir(), old_revision, cls.get_current_db_revision()
dbmigrator.migrate_db, CS.get_blobfiles_dir(), old_revision, cls.get_current_db_revision()
)
cls._write_db_revision_file(cls.get_current_db_revision())
log.info("Finished upgrading the databases.")
migrated = True

# start SQLiteStorage
cls.storage = SQLiteStorage(cls.get_db_dir())
cls.storage = SQLiteStorage(CS.get_blobfiles_dir())
yield cls.storage.setup()
defer.returnValue(migrated)

Expand All @@ -99,38 +123,37 @@ class WalletComponent(Component):
component_name = WALLET_COMPONENT
depends_on = [DATABASE_COMPONENT]
wallet = None

@staticmethod
def get_wallet_type():
return conf.settings['wallet']
wallet_type = None

@classmethod
@defer.inlineCallbacks
def setup(cls):
storage = DatabaseComponent.storage
if cls.get_wallet_type() == conf.LBRYCRD_WALLET:
cls.wallet_type = GCS('wallet')

if cls.wallet_type == conf.LBRYCRD_WALLET:
raise ValueError('LBRYcrd Wallet is no longer supported')
elif cls.get_wallet_type() == conf.LBRYUM_WALLET:
elif cls.wallet_type == conf.LBRYUM_WALLET:

log.info("Using lbryum wallet")

lbryum_servers = {address: {'t': str(port)}
for address, port in conf.settings['lbryum_servers']}
for address, port in GCS('lbryum_servers')}

config = {
'auto_connect': True,
'chain': conf.settings['blockchain_name'],
'chain': GCS('blockchain_name'),
'default_servers': lbryum_servers
}

if 'use_keyring' in conf.settings:
config['use_keyring'] = conf.settings['use_keyring']
config['use_keyring'] = GCS('use_keyring')
if conf.settings['lbryum_wallet_dir']:
config['lbryum_path'] = conf.settings['lbryum_wallet_dir']
config['lbryum_path'] = GCS('lbryum_wallet_dir')
cls.wallet = LBRYumWallet(storage, config)
yield cls.wallet.start()
else:
raise ValueError('Wallet Type {} is not valid'.format(cls.get_wallet_type()))
raise ValueError('Wallet Type {} is not valid'.format(cls.wallet_type))

@classmethod
@defer.inlineCallbacks
Expand All @@ -140,71 +163,32 @@ def stop(cls):

class SessionComponent(Component):
component_name = SESSION_COMPONENT
depends_on = [DATABASE_COMPONENT, WALLET_COMPONENT]
depends_on = [DATABASE_COMPONENT, WALLET_COMPONENT, DHT_COMPONENT]
session = None

@staticmethod
def get_db_dir():
return conf.settings['data_dir']

@staticmethod
def get_node_id():
return conf.settings.node_id

@staticmethod
def get_blobfile_dir():
if conf.settings['BLOBFILES_DIR'] == "blobfiles":
return os.path.join(SessionComponent.get_db_dir(), "blobfiles")
else:
log.info("Using non-default blobfiles directory: %s", conf.settings['BLOBFILES_DIR'])
return conf.settings['BLOBFILES_DIR']

@staticmethod
def get_dht_node_port():
return conf.settings['dht_node_port']

@staticmethod
def get_known_dht_nodes():
return conf.settings['known_dht_nodes']

@staticmethod
def get_peer_port():
return conf.settings['peer_port']

@staticmethod
def use_upnp():
return conf.settings['use_upnp']

@staticmethod
def is_generous_host():
return conf.settings['is_generous_host']

@staticmethod
def get_external_ip():
from lbrynet.core.system_info import get_platform
platform = get_platform(get_ip=True)
return platform['ip']

@classmethod
@defer.inlineCallbacks
def setup(cls):
wallet = WalletComponent.wallet
storage = DatabaseComponent.storage
dht_node = DHTComponennt.dht_node

log.info("in session setup")

cls.session = Session(
conf.settings['data_rate'],
db_dir=cls.get_db_dir(),
node_id=cls.get_node_id(),
blob_dir=cls.get_blobfile_dir(),
dht_node_port=cls.get_dht_node_port(),
known_dht_nodes=cls.get_known_dht_nodes(),
peer_port=cls.get_peer_port(),
use_upnp=cls.use_upnp(),
GCS('data_rate'),
db_dir=GCS('data_dir'),
node_id=CS.get_node_id(),
blob_dir=CS.get_blobfiles_dir(),
dht_node=dht_node,
dht_node_port=GCS('dht_node_port'),
known_dht_nodes=GCS('known_dht_nodes'),
peer_port=GCS('peer_port'),
use_upnp=GCS('use_upnp'),
wallet=wallet,
is_generous=cls.is_generous_host(),
external_ip=cls.get_external_ip(),
is_generous=GCS('is_generous_host'),
external_ip=CS.get_external_ip(),
storage=storage
)

Expand All @@ -222,44 +206,26 @@ class DHTComponennt(Component):
dht_node = None
dht_node_class = node.Node

@staticmethod
def get_node_id():
return conf.settings.node_id

@staticmethod
def get_dht_node_port():
return conf.settings['dht_node_port']

@staticmethod
def get_known_dht_nodes():
return conf.settings['known_dht_nodes']

@staticmethod
def get_peer_port():
return conf.settings['peer_port']

@staticmethod
def get_external_ip():
from lbrynet.core.system_info import get_platform
platform = get_platform(get_ip=True)
return platform['ip']

@classmethod
@defer.inlineCallbacks
def setup(cls):
storage = DatabaseComponent.storage

node_id = CS.get_node_id()
if node_id is None:
node_id = generate_id()

cls.dht_node = cls.dht_node_class(
node_id=cls.get_node_id(),
udpPort=cls.get_dht_node_port(),
externalIP=cls.get_external_ip(),
peerPort=cls.get_peer_port()
node_id=node_id,
udpPort=GCS('dht_node_port'),
externalIP=CS.get_external_ip(),
peerPort=GCS('peer_port')
)
if not cls.hash_announcer:
cls.hash_announcer = hashannouncer.DHTHashAnnouncer(cls.dht_node, storage)
cls.peer_manager = cls.dht_node.peer_manager
cls.peer_finder = cls.dht_node.peer_finder
cls._join_dht_deferred = cls.dht_node.joinNetwork(cls.get_known_dht_nodes())
cls._join_dht_deferred = cls.dht_node.joinNetwork(GCS('known_dht_nodes')())
cls._join_dht_deferred.addCallback(lambda _: log.info("Joined the dht"))
cls._join_dht_deferred.addCallback(lambda _: cls.hash_announcer.start())

Expand Down

0 comments on commit 6327c0d

Please sign in to comment.