2015-08-20 17:27:15 +02:00
|
|
|
import binascii
|
2016-08-12 17:08:21 +02:00
|
|
|
import functools
|
2015-08-20 17:27:15 +02:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import os
|
2016-08-12 17:08:21 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
from twisted.internet import threads, defer
|
2016-08-12 17:08:21 +02:00
|
|
|
import unqlite
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2015-09-08 21:42:56 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-08-12 17:08:21 +02:00
|
|
|
def run_in_thread(fn):
|
|
|
|
@functools.wraps(fn)
|
|
|
|
def wrapped(*args, **kwargs):
|
|
|
|
return threads.deferToThread(fn, *args, **kwargs)
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
class LBRYSettings(object):
|
2016-08-12 17:08:21 +02:00
|
|
|
NAME = "settings.db"
|
2015-08-20 17:27:15 +02:00
|
|
|
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):
|
2016-08-12 17:08:21 +02:00
|
|
|
self.db.close()
|
2015-08-20 17:27:15 +02:00
|
|
|
self.db = None
|
|
|
|
return defer.succeed(True)
|
|
|
|
|
|
|
|
def _open_db(self):
|
2016-08-12 17:08:21 +02:00
|
|
|
filename = os.path.join(self.db_dir, self.NAME)
|
|
|
|
log.debug("Opening %s as the settings database", filename)
|
|
|
|
self.db = unqlite.UnQLite(filename)
|
2015-09-04 22:22:02 +02:00
|
|
|
return defer.succeed(True)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-08-12 17:08:21 +02:00
|
|
|
@run_in_thread
|
2015-08-20 17:27:15 +02:00
|
|
|
def save_lbryid(self, lbryid):
|
2016-08-12 17:08:21 +02:00
|
|
|
self.db['lbryid'] = binascii.hexlify(lbryid)
|
|
|
|
self.db.commit()
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-08-12 17:08:21 +02:00
|
|
|
@run_in_thread
|
2015-08-20 17:27:15 +02:00
|
|
|
def get_lbryid(self):
|
2016-08-12 17:08:21 +02:00
|
|
|
if 'lbryid' in self.db:
|
|
|
|
return binascii.unhexlify(self.db['lbryid'])
|
|
|
|
else:
|
|
|
|
return None
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-08-12 17:08:21 +02:00
|
|
|
@run_in_thread
|
2015-08-20 17:27:15 +02:00
|
|
|
def get_server_running_status(self):
|
2016-08-12 17:08:21 +02:00
|
|
|
if 'server_running' in self.db:
|
|
|
|
return json.loads(self.db['server_running'])
|
|
|
|
else:
|
|
|
|
return True
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-08-12 17:08:21 +02:00
|
|
|
@run_in_thread
|
2015-08-20 17:27:15 +02:00
|
|
|
def save_server_running_status(self, running):
|
2016-08-12 17:08:21 +02:00
|
|
|
self.db['server_running'] = json.dumps(running)
|
|
|
|
self.db.commit()
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-08-12 17:08:21 +02:00
|
|
|
@run_in_thread
|
2015-08-20 17:27:15 +02:00
|
|
|
def _get_payment_rate(self, rate_type):
|
2016-08-12 17:08:21 +02:00
|
|
|
if rate_type in self.db:
|
|
|
|
return json.loads(self.db[rate_type])
|
|
|
|
else:
|
|
|
|
return None
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-08-12 17:08:21 +02:00
|
|
|
@run_in_thread
|
2015-08-20 17:27:15 +02:00
|
|
|
def _save_payment_rate(self, rate_type, rate):
|
2016-08-12 17:08:21 +02:00
|
|
|
if rate is not None:
|
|
|
|
self.db[rate_type] = json.dumps(rate)
|
|
|
|
elif rate_type in self.db:
|
|
|
|
del self.db[rate_type]
|
|
|
|
self.db.commit()
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-08-12 17:08:21 +02:00
|
|
|
@run_in_thread
|
2015-08-20 17:27:15 +02:00
|
|
|
def get_query_handler_status(self, query_identifier):
|
2016-08-12 17:08:21 +02:00
|
|
|
if json.dumps(('q_h', query_identifier)) in self.db:
|
|
|
|
return json.loads(self.db[(json.dumps(('q_h', query_identifier)))])
|
|
|
|
else:
|
|
|
|
return True
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-08-12 17:08:21 +02:00
|
|
|
@run_in_thread
|
2015-08-20 17:27:15 +02:00
|
|
|
def _set_query_handler_status(self, query_identifier, status):
|
2016-08-12 17:08:21 +02:00
|
|
|
self.db[json.dumps(('q_h', query_identifier))] = json.dumps(status)
|
|
|
|
self.db.commit()
|