Coverage for src/qdrant_loader/cli/commands/project/list_cmd.py: 94%

33 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-08 06:05 +0000

1from __future__ import annotations 

2 

3import json 

4from collections.abc import Mapping 

5from typing import Any 

6 

7 

8def run_project_list(settings: Any, project_manager: Any, *, output_format: str) -> str: 

9 """Return project list in the requested format (json or table as text).""" 

10 from rich.console import Console 

11 from rich.table import Table 

12 

13 def _gather() -> list[dict[str, str | int]]: 

14 projects_data = [] 

15 contexts = project_manager.get_all_project_contexts() 

16 for context in contexts.values(): 

17 sources = context.config.sources if context.config else None 

18 # Safely sum lengths of available source collections; treat missing/None as empty 

19 if sources: 

20 if isinstance(sources, Mapping): 

21 source_count = sum( 

22 len(sources.get(name, {}) or {}) 

23 for name in ( 

24 "publicdocs", 

25 "git", 

26 "confluence", 

27 "jira", 

28 "localfile", 

29 ) 

30 ) 

31 else: 

32 source_count = sum( 

33 len(getattr(sources, name, {}) or {}) 

34 for name in ( 

35 "publicdocs", 

36 "git", 

37 "confluence", 

38 "jira", 

39 "localfile", 

40 ) 

41 ) 

42 else: 

43 source_count = 0 

44 projects_data.append( 

45 { 

46 "project_id": context.project_id, 

47 "display_name": context.display_name or "N/A", 

48 "description": context.description or "N/A", 

49 "collection_name": context.collection_name or "N/A", 

50 "source_count": source_count, 

51 } 

52 ) 

53 return projects_data 

54 

55 data = _gather() 

56 if output_format.lower() == "json": 

57 return json.dumps(data, indent=2) 

58 

59 # table output 

60 table = Table(title="Configured Projects") 

61 table.add_column("Project ID", style="cyan", no_wrap=True) 

62 table.add_column("Display Name", style="magenta") 

63 table.add_column("Description", style="green") 

64 table.add_column("Collection", style="blue") 

65 table.add_column("Sources", justify="right", style="yellow") 

66 

67 for item in data: 

68 table.add_row( 

69 str(item["project_id"]), 

70 str(item["display_name"]), 

71 str(item["description"]), 

72 str(item["collection_name"]), 

73 str(item["source_count"]), 

74 ) 

75 console = Console(record=True) 

76 console.print(table) 

77 return console.export_text()