Coverage for dakweb/routers/madison.py: 85%

21 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2026-08-03 16:46 +0000

1# SPDX-License-Identifier: GPL-2.0-or-later 

2# © 2026, Anton Gladky <gladk@debian.org> 

3 

4from typing import Annotated 

5 

6from fastapi import APIRouter, Query 

7from starlette.responses import JSONResponse, StreamingResponse 

8 

9from daklib.ls import list_packages 

10 

11router = APIRouter() 

12 

13 

14@router.get("/madison") 

15def madison( 

16 package: Annotated[ 

17 str, Query(description="Space separated list of packages.") 

18 ] = "", 

19 a: Annotated[ 

20 str | None, Query(description="Comma separated list of architectures.") 

21 ] = None, 

22 b: Annotated[str | None, Query(description="Binary type (deb/udeb/dsc).")] = None, 

23 c: Annotated[ 

24 str | None, Query(description="Comma separated list of components.") 

25 ] = None, 

26 s: Annotated[ 

27 str | None, Query(description="Comma separated list of suites.") 

28 ] = None, 

29 S: Annotated[ 

30 str | None, 

31 Query( 

32 alias="S", 

33 description="If present, include binary children of source pkgs.", 

34 ), 

35 ] = None, 

36 f: Annotated[str | None, Query(description="If present, output JSON.")] = None, 

37): 

38 """Display information about package(s) ("madison" interface).""" 

39 

40 packages = package.split() 

41 

42 architectures = a.split(",") if a is not None else None 

43 binary_types = [b] if b is not None else None 

44 components = c.split(",") if c is not None else None 

45 suites = s.split(",") if s is not None else None 

46 source_and_binary = S is not None 

47 # Bottle used presence of 'f' to indicate JSON output, regardless of value. 

48 output_format = "python" if f is not None else None 

49 

50 result = list_packages( 

51 packages, 

52 suites=suites, 

53 components=components, 

54 architectures=architectures, 

55 binary_types=binary_types, 

56 source_and_binary=source_and_binary, 

57 format=output_format, 

58 ) 

59 

60 if f is None: 60 ↛ 68line 60 didn't jump to line 68 because the condition on line 60 was always true

61 

62 def gen(): 

63 for row in result: 63 ↛ 64line 63 didn't jump to line 64 because the loop on line 63 never started

64 yield f"{row}\n" 

65 

66 return StreamingResponse(gen(), media_type="text/plain; charset=UTF-8") 

67 

68 return JSONResponse( 

69 content=list(result), media_type="application/json; charset=UTF-8" 

70 )