refactor file list sorting per request

This commit is contained in:
kafene 2018-05-21 00:24:18 -04:00
parent 1790393273
commit 76a7cc4e58
2 changed files with 27 additions and 26 deletions

View file

@ -97,12 +97,9 @@ CONNECTION_MESSAGES = {
SHORT_ID_LEN = 20 SHORT_ID_LEN = 20
MAX_UPDATE_FEE_ESTIMATE = 0.3 MAX_UPDATE_FEE_ESTIMATE = 0.3
FILE_SORT_DIRECTION_ASCENDING = 'asc' DIRECTION_ASCENDING = 'asc'
FILE_SORT_DIRECTION_DESCENDING = 'desc' DIRECTION_DESCENDING = 'desc'
FILE_SORT_DIRECTIONS = ( DIRECTIONS = DIRECTION_ASCENDING, DIRECTION_DESCENDING
FILE_SORT_DIRECTION_ASCENDING,
FILE_SORT_DIRECTION_DESCENDING,
)
class IterableContainer(object): class IterableContainer(object):
def __iter__(self): def __iter__(self):
@ -947,18 +944,8 @@ class Daemon(AuthJSONRPCServer):
def _sort_lbry_files(self, lbry_files, sort_by): def _sort_lbry_files(self, lbry_files, sort_by):
for field, direction in sort_by: for field, direction in sort_by:
is_reverse = direction == FILE_SORT_DIRECTION_DESCENDING is_reverse = direction == DIRECTION_DESCENDING
key_getter = None key_getter = create_key_getter(field) if field else None
if field:
search_path = field.split('.')
def key_getter(value):
for key in search_path:
try:
value = value[key]
except KeyError as e:
errmsg = 'Failed to sort by "{}", key "{}" was not found.'
raise Exception(errmsg.format(field, e.message))
return value
lbry_files = sorted(lbry_files, key=key_getter, reverse=is_reverse) lbry_files = sorted(lbry_files, key=key_getter, reverse=is_reverse)
return lbry_files return lbry_files
@ -969,12 +956,13 @@ class Daemon(AuthJSONRPCServer):
Direction defaults to ascending. Direction defaults to ascending.
""" """
pieces = sort.rsplit(',', 1) pieces = [p.strip() for p in sort.split(',')]
field = pieces[0].strip() or None field = pieces.pop(0)
direction = pieces[1].strip().lower() if len(pieces) > 1 else None direction = DIRECTION_ASCENDING
if direction and direction not in FILE_SORT_DIRECTIONS: if pieces and pieces[0] in DIRECTIONS:
raise Exception('Sort direction must be one of {}'.format(FILE_SORT_DIRECTIONS)) direction = pieces[0]
return (field, direction or FILE_SORT_DIRECTION_ASCENDING) return field, direction
def _get_single_peer_downloader(self): def _get_single_peer_downloader(self):
downloader = SinglePeerDownloader() downloader = SinglePeerDownloader()
@ -3435,3 +3423,16 @@ def get_blob_payment_rate_manager(session, payment_rate_manager=None):
payment_rate_manager = rate_managers[payment_rate_manager] payment_rate_manager = rate_managers[payment_rate_manager]
log.info("Downloading blob with rate manager: %s", payment_rate_manager) log.info("Downloading blob with rate manager: %s", payment_rate_manager)
return payment_rate_manager or session.payment_rate_manager return payment_rate_manager or session.payment_rate_manager
def create_key_getter(field):
search_path = field.split('.')
def key_getter(value):
for key in search_path:
try:
value = value[key]
except KeyError as e:
errmsg = 'Failed to get "{}", key "{}" was not found.'
raise Exception(errmsg.format(field, e.message))
return value
return key_getter

View file

@ -250,14 +250,14 @@ class TestFileListSorting(trial.unittest.TestCase):
deferred = defer.maybeDeferred(self.test_daemon.jsonrpc_file_list, sort=sort_options) deferred = defer.maybeDeferred(self.test_daemon.jsonrpc_file_list, sort=sort_options)
failure_assertion = self.assertFailure(deferred, Exception) failure_assertion = self.assertFailure(deferred, Exception)
exception = self.successResultOf(failure_assertion) exception = self.successResultOf(failure_assertion)
expected_message = 'Failed to sort by "meta.author", key "meta" was not found.' expected_message = 'Failed to get "meta.author", key "meta" was not found.'
self.assertEquals(expected_message, exception.message) self.assertEquals(expected_message, exception.message)
sort_options = ['metadata.foo.bar'] sort_options = ['metadata.foo.bar']
deferred = defer.maybeDeferred(self.test_daemon.jsonrpc_file_list, sort=sort_options) deferred = defer.maybeDeferred(self.test_daemon.jsonrpc_file_list, sort=sort_options)
failure_assertion = self.assertFailure(deferred, Exception) failure_assertion = self.assertFailure(deferred, Exception)
exception = self.successResultOf(failure_assertion) exception = self.successResultOf(failure_assertion)
expected_message = 'Failed to sort by "metadata.foo.bar", key "foo" was not found.' expected_message = 'Failed to get "metadata.foo.bar", key "foo" was not found.'
self.assertEquals(expected_message, exception.message) self.assertEquals(expected_message, exception.message)
def _get_fake_lbry_files(self): def _get_fake_lbry_files(self):