diff --git a/lbrynet/core/utils.py b/lbrynet/core/utils.py index e4580f2db..d71e01e3a 100644 --- a/lbrynet/core/utils.py +++ b/lbrynet/core/utils.py @@ -1,11 +1,12 @@ import base64 import distutils.version import random +import os +import json +import yaml from lbrynet.core.cryptoutils import get_lbry_hash_obj - - blobhash_length = get_lbry_hash_obj().digest_size * 2 # digest_size is in bytes, and blob hashes are hex encoded @@ -46,3 +47,33 @@ def deobfuscate(obfustacated): def obfuscate(plain): return base64.b64encode(plain).encode('rot13') + + +settings_decoders = { + '.json': json.loads, + '.yml': yaml.load +} + +settings_encoders = { + '.json': json.dumps, + '.yml': yaml.safe_dump +} + + +def load_settings(path): + ext = os.path.splitext(path)[1] + f = open(path, 'r') + data = f.read() + f.close() + decoder = settings_decoders.get(ext, False) + assert decoder is not False, "Unknown settings format .%s" % ext + return decoder(data) + + +def save_settings(path, settings): + ext = os.path.splitext(path)[1] + encoder = settings_encoders.get(ext, False) + assert encoder is not False, "Unknown settings format .%s" % ext + f = open(path, 'w') + f.write(encoder(settings)) + f.close() \ No newline at end of file diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index 5280da520..e23a9aa85 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -198,7 +198,18 @@ class LBRYDaemon(jsonrpc.JSONRPC): log.info("Couldn't make download directory, using home") default_download_directory = os.path.expanduser("~") - self.daemon_conf = os.path.join(self.db_dir, 'daemon_settings.json') + old_conf_path = os.path.join(self.db_dir, 'daemon_settings.json') + self.daemon_conf = os.path.join(self.db_dir, 'daemon_settings.yml') + + if os.path.isfile(old_conf_path): + log.info("Migrating .json config file to .yml") + tmp_settings = utils.load_settings(old_conf_path) + utils.save_settings(self.daemon_conf, tmp_settings) + try: + os.remove(old_conf_path) + log.info("Cleaned up old config file") + except: + log.warning("Failed to remove old config file") self.default_settings = { 'run_on_startup': False, @@ -226,9 +237,7 @@ class LBRYDaemon(jsonrpc.JSONRPC): } if os.path.isfile(self.daemon_conf): - f = open(self.daemon_conf, "r") - loaded_settings = json.loads(f.read()) - f.close() + loaded_settings = utils.load_settings(self.daemon_conf) missing_settings = {} removed_settings = {} for k in self.default_settings.keys(): @@ -268,9 +277,7 @@ class LBRYDaemon(jsonrpc.JSONRPC): log.info("Lowering name cache time") self.session_settings['cache_time'] = DEFAULT_CACHE_TIME - f = open(self.daemon_conf, "w") - f.write(json.dumps(self.session_settings)) - f.close() + utils.save_settings(self.daemon_conf, self.session_settings) self.run_on_startup = self.session_settings['run_on_startup'] self.data_rate = self.session_settings['data_rate'] @@ -940,9 +947,7 @@ class LBRYDaemon(jsonrpc.JSONRPC): self.search_timeout = self.session_settings['search_timeout'] self.cache_time = self.session_settings['cache_time'] - f = open(self.daemon_conf, "w") - f.write(json.dumps(self.session_settings)) - f.close() + utils.save_settings(self.daemon_conf, self.session_settings) return defer.succeed(True) @@ -1406,9 +1411,7 @@ class LBRYDaemon(jsonrpc.JSONRPC): startup_scripts = self.startup_scripts self.startup_scripts = self.session_settings['startup_scripts'] = remaining_scripts - f = open(self.daemon_conf, "w") - f.write(json.dumps(self.session_settings)) - f.close() + utils.save_settings(self.daemon_conf, self.session_settings) for script in startup_scripts: if script['script_name'] == 'migrateto025': diff --git a/setup.py b/setup.py index 9224af09c..d93a5dbeb 100644 --- a/setup.py +++ b/setup.py @@ -44,7 +44,8 @@ requires = [ 'six==1.9.0', 'base58', 'googlefinance', - 'requests_futures' + 'requests_futures', + 'PyYAML' ] console_scripts = [ @@ -273,7 +274,8 @@ elif platform == WINDOWS: 'wsgiref', 'zope.interface', 'os', - 'pkg_resources' + 'pkg_resources', + 'PyYAML' ], 'excludes': ['distutils', 'collections.sys', 'collections._weakref', 'collections.abc', 'Tkinter', 'tk', 'tcl', 'PyQt4', 'nose', 'mock'