def provider_choice(institutes: List[dict], orgs: List[dict]) -> Tuple[str, str, Optional[str], bool]: """ Ask the user to make a choice from a list of institute and secure internet providers. returns: url, display_name, contact, bool. Bool indicates if it is secure_internet or not. """ print("\nPlease choose server:\n") print("Institute access:") for i, row in enumerate(institutes): print(f"[{i}] {extract_translation(row['display_name'])}") print("Secure internet: \n") for i, row in enumerate(orgs, start=len(institutes)): print(f"[{i}] {extract_translation(row['display_name'])}") choice = input_int(max_=len(institutes) + len(orgs)) if choice < len(institutes): institute = institutes[choice] return institute['base_url'], extract_translation( institute['display_name']), institute['support_contact'], False else: org = orgs[choice - len(institutes)] return org['secure_internet_home'], extract_translation( org['display_name']), None, True
def configure( args: Namespace ) -> Tuple[str, str, Optional[str], Optional[ServerListType]]: search_term = args.match servers, orgs = fetch_servers_orgs() secure_internets = [ s for s in servers if s['server_type'] == 'secure_internet' ] institute_matches, org_matches = match_term(servers, orgs, search_term, exact=True) if isinstance(search_term, str) and search_term.lower().startswith('https://'): return search_term, search_term, None, None else: if len(institute_matches) == 0 and len(org_matches) == 0: print(f"The filter '{search_term}' had no matches") exit(1) elif len(institute_matches) == 1 and len(org_matches) == 0: index, institute = institute_matches[0] print( f"filter '{search_term}' matched with institute '{institute['display_name']}'" ) return institute['base_url'], extract_translation( institute['display_name']), institute['support_contact'], None elif len(institute_matches) == 0 and len(org_matches) == 1: index, org = org_matches[0] print( f"filter '{search_term}' matched with organisation '{org['display_name']}'" ) return org['secure_internet_home'], extract_translation( org['display_name']), None, secure_internets else: matches = [ i[1]['display_name'] for i in chain(institute_matches, org_matches) ] print( f"filter '{search_term}' matched with {len(matches)} institutes and organisations, please be more specific." ) print("Matches:") for m in matches: print(f" - {extract_translation(m)}") exit(1)
def search(institutes: List[dict], orgs: List[dict], search_term: str) -> Optional[Tuple[str, str]]: """ Search the list of institutes and organisations for a string match. returns: None or (type ('base_url' or 'secure_internet_home'), url) """ institute_match = [ i for i in institutes if search_term in extract_translation(i['display_name']).lower() ] org_match = [ i for i in orgs if search_term in extract_translation(i['display_name']).lower() or ( 'keyword_list' in i and search_term in i['keyword_list']) ] if len(institute_match) == 0 and len(org_match) == 0: print(f"The filter '{search_term}' had no matches") return None elif len(institute_match) == 1 and len(org_match) == 0: institute = institute_match[0] print( f"filter '{search_term}' matched with institute '{institute['display_name']}'" ) return 'base_url', institute['base_url'] elif len(institute_match) == 0 and len(org_match) == 1: org = org_match[0] print( f"filter '{search_term}' matched with organisation '{org['display_name']}'" ) return 'secure_internet_home', org['secure_internet_home'] else: matches = [ i['display_name'] for i in chain(institute_match, org_match) ] print( f"filter '{search_term}' matched with {len(matches)} organisations, please be more specific." ) print("Matches:") [print(f" - {extract_translation(m)}") for m in matches] return None
def match_term( servers: ServerListType, orgs: ServerListType, search_term: Optional[str], exact=False ) -> Tuple[List[Tuple[int, Dict[str, Any]]], List[Tuple[int, Dict[str, Any]]]]: """ Search the list of institutes and organisations for a string match. returns: None or (type ('base_url' or 'secure_internet_home'), url) """ institute_access = [ s for s in servers if s['server_type'] == 'institute_access' ] if not search_term: return list(enumerate(institute_access)), list( enumerate(orgs, len(institute_access))) institute_matches: List[Tuple[int, Dict[str, Any]]] = [] for x, i in enumerate(institute_access): if not exact: if search_term.lower() in extract_translation( i['display_name']).lower(): institute_matches.append((x, i)) else: if search_term.lower() == extract_translation( i['display_name']).lower(): institute_matches.append((x, i)) org_matches: List[Tuple[int, Dict[str, Any]]] = [] for x, i in enumerate(orgs, len(institute_access)): if not exact: if search_term.lower() in extract_translation(i['display_name']).lower() \ or 'keyword_list' in i and search_term in i['keyword_list']: org_matches.append((x, i)) else: if search_term.lower() == extract_translation( i['display_name']).lower(): org_matches.append((x, i)) return institute_matches, org_matches
def test_extract_translation(self): self.assertEqual("bla bla", extract_translation("bla bla")) self.assertEqual('bleu bleu', extract_translation({'de': 'bleu bleu'}))
def __str__(self): return extract_translation(self.display_name)
def keyword(self) -> Optional[str]: if self.keyword_list: return extract_translation(self.keyword_list) return None