Coverage for silkaj/wot/lookup.py: 86%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-17 17:03 +0000

1# Copyright 2016-2024 Maël Azimi <m.a@moul.re> 

2# 

3# Silkaj is free software: you can redistribute it and/or modify 

4# it under the terms of the GNU Affero General Public License as published by 

5# the Free Software Foundation, either version 3 of the License, or 

6# (at your option) any later version. 

7# 

8# Silkaj is distributed in the hope that it will be useful, 

9# but WITHOUT ANY WARRANTY; without even the implied warranty of 

10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

11# GNU Affero General Public License for more details. 

12# 

13# You should have received a copy of the GNU Affero General Public License 

14# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. 

15 

16import urllib 

17 

18import rich_click as click 

19 

20from silkaj.network import exit_on_http_error 

21from silkaj.public_key import gen_pubkey_checksum, is_pubkey_and_check 

22from silkaj.wot import tools as wt 

23 

24 

25@click.command("lookup", help="User identifier and public key lookup") 

26@click.argument("uid_pubkey") 

27def lookup_cmd(uid_pubkey: str) -> None: 

28 checked_pubkey = is_pubkey_and_check(uid_pubkey) 

29 if checked_pubkey: 

30 uid_pubkey = str(checked_pubkey) 

31 

32 try: 

33 lookups = wt.wot_lookup(uid_pubkey) 

34 except urllib.error.HTTPError as e: 

35 exit_on_http_error(e, 404, f"No identity found for {uid_pubkey}") 

36 

37 content = f"Public keys or user id found matching '{uid_pubkey}':\n" 

38 for lookup in lookups: 

39 for identity in lookup["uids"]: 

40 pubkey_checksum = gen_pubkey_checksum(lookup["pubkey"]) 

41 content += f'\n→ {pubkey_checksum}{identity["uid"]}' 

42 click.echo(content)