Coverage for src/galaxy_beep_frame/converter/token/__init__.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-08-31 04:18 +0200

1"""Subpackage to manage token.""" 

2 

3from dataclasses import dataclass, field 

4 

5from galaxy_beep_frame.exception import ( 

6 TokenImmutableError, 

7) 

8 

9from .mutability_state import MutabilityState 

10from .mutable_state import MutableState 

11from .token_type import TokenType 

12 

13 

14@dataclass 

15class Token: 

16 """ 

17 Hold information about current state and value. 

18 

19 Start mutable (eg. can be modified) and can become irreversably 

20 immutable. 

21 """ 

22 

23 type: TokenType 

24 value: bytearray = field(default_factory=bytearray) 

25 

26 mutability: MutabilityState = field(init=False) 

27 

28 def __post_init__(self) -> None: 

29 """Post initialization process.""" 

30 self.mutability = MutableState(self) 

31 

32 def _ensure_mutable(self) -> MutableState: 

33 """ 

34 Check if the current mutability of the Token is MutableState. 

35 

36 Notes 

37 ----- 

38 Internal use 

39 

40 """ 

41 mutability_state = self.mutability 

42 if not isinstance(mutability_state, MutableState): 

43 raise TokenImmutableError(self) 

44 return mutability_state 

45 

46 def update(self, char: int) -> None: 

47 """ 

48 Update value stored in the token by appending a byte. 

49 

50 Notes 

51 ----- 

52 This token should not be finished. 

53 

54 """ 

55 mutability_state = self._ensure_mutable() 

56 return mutability_state.update(char) 

57 

58 def end(self) -> None: 

59 """ 

60 Update value stored in the token by appending a byte. 

61 

62 Notes 

63 ----- 

64 After this method is called, the token will not be updatable 

65 anymore. 

66 

67 """ 

68 mutability_state = self._ensure_mutable() 

69 mutability_state.end() 

70 

71 

72__all__ = [ 

73 "MutabilityState", 

74 "MutableState", 

75 "Token", 

76 "TokenType", 

77]