Coverage for src/ptf/cmds/base_cmds.py: 88%

79 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-05 09:56 +0000

1##################################################################### 

2# 

3# baseCmd: base class of all commands 

4# 

5# Set self.required_params to declare the required params 

6# They are checked at the beginning of the do() 

7# 

8# All strings params are converted to unicode, except for "body" 

9# body is used in xml cmds (see xml_cmds.py): 

10# etree will give an error if you pass unicode to the fromstring function 

11# with an XML that defines its encoding (<?xml version="1.0" encoding="utf-8"?>) 

12# 

13# sub cmds can be declared by passing the parent command in the .do() function 

14# sub cmds are undoed in the reverse order during .undo() 

15# 

16###################################################################### 

17from __future__ import annotations 

18from typing import TYPE_CHECKING 

19 

20if TYPE_CHECKING: 20 ↛ 21line 20 didn't jump to line 21 because the condition on line 20 was never true

21 from django.db import models 

22 

23 

24def make_int(value): 

25 if value is None or not value: 

26 return 0 

27 

28 if not isinstance(value, int): 

29 value = value.split("-")[0] 

30 try: 

31 value = int(value) 

32 except ValueError: 

33 value = "".join(char for char in value if char.isdigit()) 

34 try: 

35 value = int(value) 

36 except ValueError: 

37 value = 0 

38 else: 

39 pass 

40 return value 

41 

42 

43class baseCmd: 

44 def __init__(self, params={}): 

45 super().__init__() 

46 

47 self.from_folder = None 

48 self.to_folder = None 

49 self.set_params(params) 

50 

51 self.required_params = [] 

52 self.cmds = [] 

53 

54 self.is_add_cmd = True 

55 self.has_do_been_called = False 

56 self.required_delete_params = [] 

57 self.warnings = [] 

58 

59 def set_params(self, params): 

60 if params is not None: 

61 for key in params: 

62 setattr(self, key, params[key]) 

63 

64 # def set_if_not_defined(self, name, value): 

65 # if not hasattr(self, name) or not getattr(self, name): 

66 # setattr(self, name, value) 

67 

68 def check_params(self): 

69 required_params = self.required_params if self.is_add_cmd else self.required_delete_params 

70 

71 for name in required_params: 

72 if not hasattr(self, name) or not getattr(self, name): 

73 raise ValueError( 

74 "required param " + name + " est vide : " + self.__class__.__name__ 

75 ) 

76 

77 def set_additional_info(self, data, infos): 

78 for info in infos: 

79 value = getattr(self, info) 

80 if value: 

81 data[info] = value 

82 

83 def do(self, parent: baseCmd | None = None): 

84 self.has_do_been_called = True 

85 self.cmds = [] 

86 self.check_params() 

87 self.pre_do() # Ex: add required things, like a provider for addPublisherDatabaseCmd 

88 obj = self.internal_do() 

89 if obj is not None: 

90 self.post_do(obj) # Ex: add other things, like ResourceId 

91 

92 if parent: 

93 parent.cmds.append(self) 

94 

95 return obj 

96 

97 def pre_do(self): 

98 pass 

99 

100 def internal_do(self) -> models.Model | None: 

101 return None 

102 

103 def post_do(self, obj: models.Model): 

104 pass 

105 

106 def undo(self): 

107 if not self.has_do_been_called: 

108 self.is_add_cmd = False 

109 

110 self.check_params() 

111 self.pre_undo() 

112 id = self.internal_undo() 

113 self.post_undo() 

114 

115 return id 

116 

117 def pre_undo(self): 

118 for cmd in reversed(self.cmds): 

119 cmd.undo() 

120 

121 def internal_undo(self): 

122 pass 

123 

124 def post_undo(self): 

125 pass 

126 

127 def get_warnings(self): 

128 return self.warnings