From 8f61fb3fc4dca0f7cdb00256323a0fbd1fddab10 Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Wed, 21 Dec 2016 10:43:13 -0800 Subject: [PATCH] Initialize settings, along with lbryid and session_id on startup --- lbrynet/conf.py | 30 +++++++++++++++---- lbrynet/lbrynet_daemon/Daemon.py | 4 +-- lbrynet/lbrynet_daemon/DaemonControl.py | 1 + packaging/osx/lbry-osx-app/lbrygui/main.py | 1 + .../windows/lbry-win32-app/LBRYWin32App.py | 1 + tests/__init__.py | 5 ++++ 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/lbrynet/conf.py b/lbrynet/conf.py index acd265809..ef87333a5 100644 --- a/lbrynet/conf.py +++ b/lbrynet/conf.py @@ -1,3 +1,4 @@ +import base58 import copy import json import logging @@ -8,6 +9,9 @@ import yaml from appdirs import user_data_dir import envparse +from lbrynet.core import utils + + LBRYCRD_WALLET = 'lbrycrd' LBRYUM_WALLET = 'lbryum' PTC_WALLET = 'ptc' @@ -366,9 +370,23 @@ def save_settings(path=None): settings_file.write(encoder(to_save)) -# TODO: don't load the configuration automatically. The configuration -# should be loaded at runtime, not at module import time. Module -# import should have no side-effects. This is also bad because -# it means that settings are read from the environment even for -# tests, which is rarely what you want to happen. -settings = Config() +settings = None + + +def initialize_settings(): + global settings + settings = Config() + settings.lbryid = get_lbryid() + settings.session_id = base58.b58encode(utils.generate_id()) + + +def get_lbryid(): + lbryid_filename = os.path.join(settings.ensure_data_dir(), "lbryid") + if os.path.isfile(lbryid_filename): + with open(lbryid_filename, "r") as lbryid_file: + return base58.b58decode(lbryid_file.read()) + else: + lbryid = utils.generate_id() + with open(lbryid_filename, "w") as lbryid_file: + lbryid_file.write(base58.b58encode(lbryid)) + return lbryid diff --git a/lbrynet/lbrynet_daemon/Daemon.py b/lbrynet/lbrynet_daemon/Daemon.py index 5c3cdb26c..bcc6a4d52 100644 --- a/lbrynet/lbrynet_daemon/Daemon.py +++ b/lbrynet/lbrynet_daemon/Daemon.py @@ -259,14 +259,14 @@ class Daemon(AuthJSONRPCServer): self.db_revision_file = conf.settings.get_db_revision_filename() self.session = None self.uploaded_temp_files = [] - self._session_id = base58.b58encode(utils.generate_id()) + self._session_id = conf.settings.session_id # TODO: this should probably be passed into the daemon, or # possibly have the entire log upload functionality taken out # of the daemon, but I don't want to deal with that now self.log_uploader = log_support.LogUploader.load('lbrynet', self.log_file) self.analytics_manager = analytics_manager - self.lbryid = PENDING_ID + self.lbryid = conf.settings.lbryid self.daemon_conf = conf.settings.get_conf_filename() self.wallet_user = None diff --git a/lbrynet/lbrynet_daemon/DaemonControl.py b/lbrynet/lbrynet_daemon/DaemonControl.py index 9119e31c3..54d1b1e21 100644 --- a/lbrynet/lbrynet_daemon/DaemonControl.py +++ b/lbrynet/lbrynet_daemon/DaemonControl.py @@ -63,6 +63,7 @@ def start(): 'should selectively be applied.')) args = parser.parse_args() + conf.initialize_settings() utils.setup_certs_for_windows() conf.update_settings_from_file() diff --git a/packaging/osx/lbry-osx-app/lbrygui/main.py b/packaging/osx/lbry-osx-app/lbrygui/main.py index 5a0aa10d7..dbb4428a4 100644 --- a/packaging/osx/lbry-osx-app/lbrygui/main.py +++ b/packaging/osx/lbry-osx-app/lbrygui/main.py @@ -14,6 +14,7 @@ log = logging.getLogger() def main(): + conf.initialize_settings() conf.update_settings_from_file() log_file = conf.settings.get_log_filename() log_support.configure_logging(log_file, console=True) diff --git a/packaging/windows/lbry-win32-app/LBRYWin32App.py b/packaging/windows/lbry-win32-app/LBRYWin32App.py index 62895c163..a5b0b5998 100644 --- a/packaging/windows/lbry-win32-app/LBRYWin32App.py +++ b/packaging/windows/lbry-win32-app/LBRYWin32App.py @@ -276,6 +276,7 @@ def main(lbry_name=None): if __name__ == '__main__': utils.setup_certs_for_windows() + conf.initialize_settings() conf.update_settings_from_file() log_file = conf.settings.get_log_filename() diff --git a/tests/__init__.py b/tests/__init__.py index 6ce67146e..70081d97d 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -2,3 +2,8 @@ # and so we need to ensure that it is also # setup for the tests from lbrynet.core import log_support +from lbrynet import conf + + +# TODO: stop doing this, would be better to mock out the settings +conf.initialize_settings()