Coverage for src/lilbee/core/config/login_host.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-11-06 08:17 +0000

1"""The one rule for the host a stored crawl login names, shared by every source of the setting.""" 

2 

3from __future__ import annotations 

4 

5LOGIN_HOST_NOT_A_HOST = "crawl_login_host must be a host name, such as wiki.example.com" 

6 

7 

8def login_host(value: str) -> str | None: 

9 """*value* read as a bare host with an optional port, or None when it is anything else. 

10 

11 A scheme, a path, a query or a user name makes the value a URL, not a host. ada-url is in 

12 the optional ``crawler`` extra, so this imports it on call. 

13 """ 

14 import ada_url 

15 

16 try: 

17 parsed = ada_url.URL(f"http://{value}/") 

18 except ValueError: 

19 return None 

20 if parsed.pathname != "/" or parsed.search or parsed.hash: 

21 return None 

22 if parsed.username or parsed.password: 

23 return None 

24 return parsed.hostname or None 

25 

26 

27def refuse_unless_host(value: str) -> None: 

28 """Raise ``ValueError`` when a non-empty *value* is not a host name. 

29 

30 Without ada-url no crawl runs, so nothing can use the host and the value passes. 

31 """ 

32 if not value: 

33 return 

34 try: 

35 host = login_host(value) 

36 except ModuleNotFoundError: 

37 return 

38 if host is None: 

39 raise ValueError(LOGIN_HOST_NOT_A_HOST)