from src.fw_api import list_libraries, federate_remote_library, scan_remote_library from pyfzf.pyfzf import FzfPrompt from loguru import logger import time fzf = FzfPrompt() @logger.catch def libraries(pg=None, radio=False): libs_res = list_libraries(pg=pg) libs_count = libs_res.get('count') libs_next = libs_res.get('next') libs_prev = libs_res.get('previous') libs = libs_res.get('results') libraries_listing = [] if libs_next: libraries_listing.append('Next') if libs_prev: libraries_listing.append('Prev') if radio == False: libraries_listing.append('Add remote library') for lib_i in libs: index = libs.index(lib_i) lib_name = lib_i.get('name') lib_by = lib_i.get('actor').get('full_username') libraries_listing.append(f'{index}.{lib_name} | by {lib_by}') lib_select = fzf.prompt( libraries_listing, f'--header=\'found {libs_count} libraries\'')[0].split('.', 1) if lib_select[0] == 'Next': return libraries(pg=libs_next) elif lib_select[0] == 'Prev': return libraries(pg=libs_prev) elif lib_select[0] == 'Add remote library': print('Search a remote library (url\\fid):') new_library = federate_remote_library(input()) if new_library.get('detail'): logger.error(new_library['detail']) return if new_library.get('count') > 0: print('Library found') one_lib = new_library['results'][0] scan = scan_remote_library(one_lib['uuid']) if scan.get('detail'): logger.error(scan['detail']) return status = scan['status'] if status == 'scheduled': print(f'Scanning {status}. Please wait few minutes for scan and open libraries menu again') else: print(f'Scan is {status}') time.sleep(3) return else: lib_addr = lib_select[0] lib_name = lib_select[1] lib_uuid = libs[int(lib_addr)]['uuid'] lib_fid = libs[int(lib_addr)]['fid'] if radio: return None, 'library', f'{lib_name}\n{lib_fid}', lib_uuid else: return lib_uuid