Initialize settings, along with lbryid and session_id on startup

This commit is contained in:
Job Evers-Meltzer 2016-12-21 10:43:13 -08:00
parent 39b2e44492
commit 8f61fb3fc4
6 changed files with 34 additions and 8 deletions

View file

@ -1,3 +1,4 @@
import base58
import copy import copy
import json import json
import logging import logging
@ -8,6 +9,9 @@ import yaml
from appdirs import user_data_dir from appdirs import user_data_dir
import envparse import envparse
from lbrynet.core import utils
LBRYCRD_WALLET = 'lbrycrd' LBRYCRD_WALLET = 'lbrycrd'
LBRYUM_WALLET = 'lbryum' LBRYUM_WALLET = 'lbryum'
PTC_WALLET = 'ptc' PTC_WALLET = 'ptc'
@ -366,9 +370,23 @@ def save_settings(path=None):
settings_file.write(encoder(to_save)) settings_file.write(encoder(to_save))
# TODO: don't load the configuration automatically. The configuration settings = None
# 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 def initialize_settings():
# tests, which is rarely what you want to happen. global settings
settings = Config() 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

View file

@ -259,14 +259,14 @@ class Daemon(AuthJSONRPCServer):
self.db_revision_file = conf.settings.get_db_revision_filename() self.db_revision_file = conf.settings.get_db_revision_filename()
self.session = None self.session = None
self.uploaded_temp_files = [] 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 # TODO: this should probably be passed into the daemon, or
# possibly have the entire log upload functionality taken out # possibly have the entire log upload functionality taken out
# of the daemon, but I don't want to deal with that now # 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.log_uploader = log_support.LogUploader.load('lbrynet', self.log_file)
self.analytics_manager = analytics_manager self.analytics_manager = analytics_manager
self.lbryid = PENDING_ID self.lbryid = conf.settings.lbryid
self.daemon_conf = conf.settings.get_conf_filename() self.daemon_conf = conf.settings.get_conf_filename()
self.wallet_user = None self.wallet_user = None

View file

@ -63,6 +63,7 @@ def start():
'should selectively be applied.')) 'should selectively be applied.'))
args = parser.parse_args() args = parser.parse_args()
conf.initialize_settings()
utils.setup_certs_for_windows() utils.setup_certs_for_windows()
conf.update_settings_from_file() conf.update_settings_from_file()

View file

@ -14,6 +14,7 @@ log = logging.getLogger()
def main(): def main():
conf.initialize_settings()
conf.update_settings_from_file() conf.update_settings_from_file()
log_file = conf.settings.get_log_filename() log_file = conf.settings.get_log_filename()
log_support.configure_logging(log_file, console=True) log_support.configure_logging(log_file, console=True)

View file

@ -276,6 +276,7 @@ def main(lbry_name=None):
if __name__ == '__main__': if __name__ == '__main__':
utils.setup_certs_for_windows() utils.setup_certs_for_windows()
conf.initialize_settings()
conf.update_settings_from_file() conf.update_settings_from_file()
log_file = conf.settings.get_log_filename() log_file = conf.settings.get_log_filename()

View file

@ -2,3 +2,8 @@
# and so we need to ensure that it is also # and so we need to ensure that it is also
# setup for the tests # setup for the tests
from lbrynet.core import log_support 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()