2015-08-20 17:27:15 +02:00
|
|
|
import binascii
|
|
|
|
import json
|
2015-09-04 22:22:02 +02:00
|
|
|
import unqlite
|
2015-08-20 17:27:15 +02:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
from twisted.internet import threads, defer
|
|
|
|
|
|
|
|
|
2015-09-08 21:42:56 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
class LBRYSettings(object):
|
|
|
|
def __init__(self, db_dir):
|
|
|
|
self.db_dir = db_dir
|
|
|
|
self.db = None
|
|
|
|
|
|
|
|
def start(self):
|
2015-09-04 22:22:02 +02:00
|
|
|
return self._open_db()
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.db = None
|
|
|
|
return defer.succeed(True)
|
|
|
|
|
|
|
|
def _open_db(self):
|
2015-09-08 21:42:56 +02:00
|
|
|
log.debug("Opening %s as the settings database", str(os.path.join(self.db_dir, "settings.db")))
|
2015-09-04 22:22:02 +02:00
|
|
|
self.db = unqlite.UnQLite(os.path.join(self.db_dir, "settings.db"))
|
|
|
|
return defer.succeed(True)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def save_lbryid(self, lbryid):
|
|
|
|
|
|
|
|
def save_lbryid():
|
2015-09-04 22:22:02 +02:00
|
|
|
self.db['lbryid'] = binascii.hexlify(lbryid)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
return threads.deferToThread(save_lbryid)
|
|
|
|
|
|
|
|
def get_lbryid(self):
|
|
|
|
|
|
|
|
def get_lbryid():
|
2015-09-04 22:22:02 +02:00
|
|
|
if 'lbryid' in self.db:
|
|
|
|
return binascii.unhexlify(self.db['lbryid'])
|
|
|
|
else:
|
2015-08-20 17:27:15 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
return threads.deferToThread(get_lbryid)
|
|
|
|
|
|
|
|
def get_server_running_status(self):
|
|
|
|
|
|
|
|
def get_status():
|
2015-09-04 22:22:02 +02:00
|
|
|
if 'server_running' in self.db:
|
|
|
|
return json.loads(self.db['server_running'])
|
|
|
|
else:
|
2015-08-20 17:27:15 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
return threads.deferToThread(get_status)
|
|
|
|
|
|
|
|
def save_server_running_status(self, running):
|
|
|
|
|
|
|
|
def save_status():
|
2015-09-04 22:22:02 +02:00
|
|
|
self.db['server_running'] = json.dumps(running)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
return threads.deferToThread(save_status)
|
|
|
|
|
|
|
|
def get_default_data_payment_rate(self):
|
|
|
|
return self._get_payment_rate("default_data_payment_rate")
|
|
|
|
|
|
|
|
def save_default_data_payment_rate(self, rate):
|
|
|
|
return self._save_payment_rate("default_data_payment_rate", rate)
|
|
|
|
|
|
|
|
def get_server_data_payment_rate(self):
|
|
|
|
return self._get_payment_rate("server_data_payment_rate")
|
|
|
|
|
|
|
|
def save_server_data_payment_rate(self, rate):
|
|
|
|
return self._save_payment_rate("server_data_payment_rate", rate)
|
|
|
|
|
|
|
|
def get_server_crypt_info_payment_rate(self):
|
|
|
|
return self._get_payment_rate("server_crypt_info_payment_rate")
|
|
|
|
|
|
|
|
def save_server_crypt_info_payment_rate(self, rate):
|
|
|
|
return self._save_payment_rate("server_crypt_info_payment_rate", rate)
|
|
|
|
|
|
|
|
def _get_payment_rate(self, rate_type):
|
|
|
|
|
|
|
|
def get_rate():
|
2015-09-04 22:22:02 +02:00
|
|
|
if rate_type in self.db:
|
2015-10-26 16:00:07 +01:00
|
|
|
return json.loads(self.db[rate_type])
|
2015-09-04 22:22:02 +02:00
|
|
|
else:
|
2015-08-20 17:27:15 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
return threads.deferToThread(get_rate)
|
|
|
|
|
|
|
|
def _save_payment_rate(self, rate_type, rate):
|
|
|
|
|
|
|
|
def save_rate():
|
|
|
|
if rate is not None:
|
2015-09-04 22:22:02 +02:00
|
|
|
self.db[rate_type] = json.dumps(rate)
|
|
|
|
elif rate_type in self.db:
|
|
|
|
del self.db[rate_type]
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
return threads.deferToThread(save_rate)
|
|
|
|
|
|
|
|
def get_query_handler_status(self, query_identifier):
|
|
|
|
|
|
|
|
def get_status():
|
2015-09-04 22:22:02 +02:00
|
|
|
if json.dumps(('q_h', query_identifier)) in self.db:
|
|
|
|
return json.loads(self.db[(json.dumps(('q_h', query_identifier)))])
|
|
|
|
else:
|
2015-08-20 17:27:15 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
return threads.deferToThread(get_status)
|
|
|
|
|
|
|
|
def enable_query_handler(self, query_identifier):
|
|
|
|
return self._set_query_handler_status(query_identifier, True)
|
|
|
|
|
|
|
|
def disable_query_handler(self, query_identifier):
|
|
|
|
return self._set_query_handler_status(query_identifier, False)
|
|
|
|
|
|
|
|
def _set_query_handler_status(self, query_identifier, status):
|
|
|
|
def set_status():
|
2015-09-04 22:22:02 +02:00
|
|
|
self.db[json.dumps(('q_h', query_identifier))] = json.dumps(status)
|
2015-10-26 16:00:07 +01:00
|
|
|
return threads.deferToThread(set_status)
|