File size: 2,059 Bytes
79d4fd5
 
 
5ea2143
 
79d4fd5
 
 
 
 
 
 
 
5ea2143
 
 
 
1bc651d
 
5ea2143
 
 
 
 
c9983e1
5ea2143
 
 
 
 
79d4fd5
 
5ea2143
79d4fd5
 
 
5ea2143
 
 
 
 
 
 
 
 
 
 
79d4fd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import logging
from typing import Any, Dict, List, Optional

from database.view_manager import IdentifierType

logger = logging.getLogger(__name__)

MAX_HISTORY_MESSAGES = 10


class ContextManager:

    def __init__(self) -> None:
        self._active_identifiers: Dict[IdentifierType, str] = {}
        self._last_database_response: Optional[str] = None

    def get_active_identifier(self, id_type: IdentifierType) -> Optional[str]:
        if id_type is None:
            raise TypeError("id_type must not be None")
        return self._active_identifiers.get(id_type)

    def set_active_identifier(self, id_type: IdentifierType, value: Optional[str]) -> None:
        if not value or not value.strip():
            return
        self._active_identifiers[id_type] = value.strip()
        self._last_database_response = None

    def clear_all_identifiers(self) -> None:
        self._active_identifiers.clear()
        self._last_database_response = None

    def get_active_credit_file_id(self) -> Optional[str]:
        return self._active_identifiers.get(IdentifierType.CREDIT_FILE_ID)

    def set_active_credit_file_id(self, credit_file_id: Optional[str]) -> None:
        if credit_file_id and credit_file_id.strip():
            self.set_active_identifier(IdentifierType.CREDIT_FILE_ID, credit_file_id)

    def get_last_database_response(self) -> Optional[str]:
        return self._last_database_response

    def set_last_database_response(self, response: Optional[str]) -> None:
        self._last_database_response = response

    @property
    def active_identifiers(self) -> Dict[IdentifierType, str]:
        return self._active_identifiers.copy()


_context_manager_instance: Optional[ContextManager] = None


def get_context_manager() -> ContextManager:
    global _context_manager_instance
    if _context_manager_instance is None:
        _context_manager_instance = ContextManager()
    return _context_manager_instance


def reset_context_manager() -> None:
    global _context_manager_instance
    _context_manager_instance = None