Coverage for geoparser/context/context.py: 100%

37 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 18:08 +0000

1import typing as t 

2import uuid 

3 

4from geoparser.db.crud.context import ContextRepository 

5from geoparser.db.db import get_session 

6from geoparser.db.models.context import ContextCreate, ContextUpdate 

7 

8 

9class Context: 

10 """ 

11 Manages context records for a project. 

12 

13 A context associates a tag with specific recognizer and resolver IDs, 

14 allowing users to manage multiple result sets using human-friendly tags 

15 instead of module IDs. 

16 """ 

17 

18 def __init__(self, project_id: uuid.UUID): 

19 """ 

20 Initialize a Context manager for a project. 

21 

22 Args: 

23 project_id: ID of the project to manage contexts for 

24 """ 

25 self.project_id = project_id 

26 

27 def _ensure_context_record(self, tag: str) -> uuid.UUID: 

28 """ 

29 Ensure a context record exists for the given tag. 

30 

31 Creates a new context record with None IDs if it doesn't already exist. 

32 

33 Args: 

34 tag: Tag identifier 

35 

36 Returns: 

37 Context record ID 

38 """ 

39 with get_session() as session: 

40 context_record = ContextRepository.get_by_project_and_tag( 

41 session, self.project_id, tag 

42 ) 

43 

44 if context_record is None: 

45 context_create = ContextCreate( 

46 project_id=self.project_id, 

47 tag=tag, 

48 recognizer_id=None, 

49 resolver_id=None, 

50 ) 

51 context_record = ContextRepository.create(session, context_create) 

52 

53 return context_record.id 

54 

55 def update_recognizer_context(self, tag: str, recognizer_id: str) -> None: 

56 """ 

57 Update the recognizer ID for a given tag. 

58 

59 Args: 

60 tag: Tag identifier 

61 recognizer_id: ID of the recognizer to associate with this tag 

62 """ 

63 context_id = self._ensure_context_record(tag) 

64 

65 with get_session() as session: 

66 context_record = ContextRepository.get(session, context_id) 

67 context_update = ContextUpdate( 

68 id=context_record.id, recognizer_id=recognizer_id 

69 ) 

70 ContextRepository.update( 

71 session, db_obj=context_record, obj_in=context_update 

72 ) 

73 

74 def update_resolver_context(self, tag: str, resolver_id: str) -> None: 

75 """ 

76 Update the resolver ID for a given tag. 

77 

78 Args: 

79 tag: Tag identifier 

80 resolver_id: ID of the resolver to associate with this tag 

81 """ 

82 context_id = self._ensure_context_record(tag) 

83 

84 with get_session() as session: 

85 context_record = ContextRepository.get(session, context_id) 

86 context_update = ContextUpdate( 

87 id=context_record.id, resolver_id=resolver_id 

88 ) 

89 ContextRepository.update( 

90 session, db_obj=context_record, obj_in=context_update 

91 ) 

92 

93 def get_recognizer_context(self, tag: str) -> t.Optional[str]: 

94 """ 

95 Get the recognizer ID for a given tag. 

96 

97 Args: 

98 tag: Tag identifier 

99 

100 Returns: 

101 Recognizer ID if set, None otherwise 

102 """ 

103 context_id = self._ensure_context_record(tag) 

104 

105 with get_session() as session: 

106 context_record = ContextRepository.get(session, context_id) 

107 return context_record.recognizer_id 

108 

109 def get_resolver_context(self, tag: str) -> t.Optional[str]: 

110 """ 

111 Get the resolver ID for a given tag. 

112 

113 Args: 

114 tag: Tag identifier 

115 

116 Returns: 

117 Resolver ID if set, None otherwise 

118 """ 

119 context_id = self._ensure_context_record(tag) 

120 

121 with get_session() as session: 

122 context_record = ContextRepository.get(session, context_id) 

123 return context_record.resolver_id