From f8dd3d05dce0f989090c0b726407b56bab06415d Mon Sep 17 00:00:00 2001
From: Job Evers-Meltzer <jobevers@users.noreply.github.com>
Date: Sun, 17 Jul 2016 09:00:00 -0500
Subject: [PATCH] misc bug fixes and code cleanup

---
 lbrynet/core/LBRYWallet.py               |  3 +--
 lbrynet/core/StreamDescriptor.py         |  4 ++--
 lbrynet/core/client/DownloadManager.py   |  2 +-
 lbrynet/lbrynet_daemon/LBRYDownloader.py | 30 +++++++++++++++++-------
 4 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/lbrynet/core/LBRYWallet.py b/lbrynet/core/LBRYWallet.py
index 99538cea8..c4391bf88 100644
--- a/lbrynet/core/LBRYWallet.py
+++ b/lbrynet/core/LBRYWallet.py
@@ -56,7 +56,6 @@ class LBRYWallet(object):
     _FIRST_RUN_NO = 2
 
     def __init__(self, db_dir):
-
         self.db_dir = db_dir
         self.db = None
         self.next_manage_call = None
@@ -631,7 +630,7 @@ class LBRYcrdWallet(LBRYWallet):
         settings = {"username": "rpcuser",
                     "password": "rpcpassword",
                     "rpc_port": 9245}
-        if os.path.exists(self.wallet_conf):
+        if self.wallet_conf and os.path.exists(self.wallet_conf):
             conf = open(self.wallet_conf)
             for l in conf:
                 if l.startswith("rpcuser="):
diff --git a/lbrynet/core/StreamDescriptor.py b/lbrynet/core/StreamDescriptor.py
index a965969e8..fd21a4b87 100644
--- a/lbrynet/core/StreamDescriptor.py
+++ b/lbrynet/core/StreamDescriptor.py
@@ -198,7 +198,7 @@ class StreamDescriptorIdentifier(object):
         return self._stream_downloader_factories[stream_type]
 
     def _get_validator(self, stream_type):
-        if not stream_type in self._stream_downloader_factories:
+        if not stream_type in self._sd_info_validators:
             raise UnknownStreamTypeError(stream_type)
         return self._sd_info_validators[stream_type]
 
@@ -238,4 +238,4 @@ def download_sd_blob(session, blob_hash, payment_rate_manager):
     """
     downloader = StandaloneBlobDownloader(blob_hash, session.blob_manager, session.peer_finder,
                                           session.rate_limiter, payment_rate_manager, session.wallet)
-    return downloader.download()
\ No newline at end of file
+    return downloader.download()
diff --git a/lbrynet/core/client/DownloadManager.py b/lbrynet/core/client/DownloadManager.py
index d601833dd..265e090eb 100644
--- a/lbrynet/core/client/DownloadManager.py
+++ b/lbrynet/core/client/DownloadManager.py
@@ -66,7 +66,7 @@ class DownloadManager(object):
 
     def add_blobs_to_download(self, blob_infos):
 
-        log.debug("Adding %s to blobs", str(blob_infos))
+        log.debug("Adding %s blobs to blobs", len(blob_infos))
 
         def add_blob_to_list(blob, blob_num):
             self.blobs[blob_num] = blob
diff --git a/lbrynet/lbrynet_daemon/LBRYDownloader.py b/lbrynet/lbrynet_daemon/LBRYDownloader.py
index 9187cc56d..e2aa74ead 100644
--- a/lbrynet/lbrynet_daemon/LBRYDownloader.py
+++ b/lbrynet/lbrynet_daemon/LBRYDownloader.py
@@ -73,6 +73,7 @@ class GetStream(object):
     def check_status(self):
         self.timeout_counter += 1
 
+        # TODO: Why is this the stopping condition for the finished callback?
         if self.download_path:
             self.checker.stop()
             self.finished.callback((self.stream_hash, self.download_path))
@@ -108,7 +109,9 @@ class GetStream(object):
         else:
             pass
 
-        def _cause_timeout():
+        def _cause_timeout(err):
+            log.error(err)
+            log.debug('Forcing a timeout')
             self.timeout_counter = self.timeout * 2
 
         def _set_status(x, status):
@@ -116,20 +119,29 @@ class GetStream(object):
             self.code = next(s for s in STREAM_STAGES if s[0] == status)
             return x
 
+        def get_downloader_factory(metadata):
+            for factory in metadata.factories:
+                if isinstance(factory, ManagedLBRYFileDownloaderFactory):
+                    return factory, metadata
+            raise Exception('No suitable factory was found in {}'.format(metadata.factories))
+
+        def make_downloader(args):
+            factory, metadata = args
+            return factory.make_downloader(metadata,
+                                           [self.data_rate, True],
+                                           self.payment_rate_manager,
+                                           download_directory=self.download_directory,
+                                           file_name=self.file_name)
+
         self.checker.start(1)
 
         self.d.addCallback(lambda _: _set_status(None, DOWNLOAD_METADATA_CODE))
         self.d.addCallback(lambda _: download_sd_blob(self.session, self.stream_hash, self.payment_rate_manager))
         self.d.addCallback(self.sd_identifier.get_metadata_for_sd_blob)
         self.d.addCallback(lambda r: _set_status(r, DOWNLOAD_RUNNING_CODE))
-        self.d.addCallback(lambda metadata: (next(factory for factory in metadata.factories if isinstance(factory, ManagedLBRYFileDownloaderFactory)),
-                                             metadata))
-        self.d.addCallback(lambda (factory, metadata): factory.make_downloader(metadata,
-                                                                               [self.data_rate, True],
-                                                                               self.payment_rate_manager,
-                                                                               download_directory=self.download_directory,
-                                                                               file_name=self.file_name))
-        self.d.addCallbacks(self._start_download, lambda _: _cause_timeout())
+        self.d.addCallback(get_downloader_factory)
+        self.d.addCallback(make_downloader)
+        self.d.addCallbacks(self._start_download, _cause_timeout)
         self.d.callback(None)
 
         return self.finished