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
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 18:08 +0000
1import typing as t
2import uuid
4from geoparser.db.crud.context import ContextRepository
5from geoparser.db.db import get_session
6from geoparser.db.models.context import ContextCreate, ContextUpdate
9class Context:
10 """
11 Manages context records for a project.
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 """
18 def __init__(self, project_id: uuid.UUID):
19 """
20 Initialize a Context manager for a project.
22 Args:
23 project_id: ID of the project to manage contexts for
24 """
25 self.project_id = project_id
27 def _ensure_context_record(self, tag: str) -> uuid.UUID:
28 """
29 Ensure a context record exists for the given tag.
31 Creates a new context record with None IDs if it doesn't already exist.
33 Args:
34 tag: Tag identifier
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 )
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)
53 return context_record.id
55 def update_recognizer_context(self, tag: str, recognizer_id: str) -> None:
56 """
57 Update the recognizer ID for a given tag.
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)
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 )
74 def update_resolver_context(self, tag: str, resolver_id: str) -> None:
75 """
76 Update the resolver ID for a given tag.
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)
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 )
93 def get_recognizer_context(self, tag: str) -> t.Optional[str]:
94 """
95 Get the recognizer ID for a given tag.
97 Args:
98 tag: Tag identifier
100 Returns:
101 Recognizer ID if set, None otherwise
102 """
103 context_id = self._ensure_context_record(tag)
105 with get_session() as session:
106 context_record = ContextRepository.get(session, context_id)
107 return context_record.recognizer_id
109 def get_resolver_context(self, tag: str) -> t.Optional[str]:
110 """
111 Get the resolver ID for a given tag.
113 Args:
114 tag: Tag identifier
116 Returns:
117 Resolver ID if set, None otherwise
118 """
119 context_id = self._ensure_context_record(tag)
121 with get_session() as session:
122 context_record = ContextRepository.get(session, context_id)
123 return context_record.resolver_id