Coverage for apps/ptf/cmds/base_cmds.py: 92%

71 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-02-28 09:09 +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###################################################################### 

17 

18 

19def make_int(value): 

20 if value is None or not value: 

21 return 0 

22 

23 if not isinstance(value, int): 

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

25 try: 

26 value = int(value) 

27 except ValueError: 

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

29 value = int(value) 

30 else: 

31 # Donc on retourne un string ? 

32 pass 

33 return value 

34 

35 

36class baseCmd: 

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

38 super().__init__() 

39 

40 self.from_folder = None 

41 self.to_folder = None 

42 self.set_params(params) 

43 

44 self.required_params = [] 

45 self.cmds = [] 

46 

47 self.is_add_cmd = True 

48 self.has_do_been_called = False 

49 self.required_delete_params = [] 

50 self.warnings = [] 

51 

52 def set_params(self, params): 

53 if params is not None: 

54 for key in params: 

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

56 

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

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

59 # setattr(self, name, value) 

60 

61 def check_params(self): 

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

63 

64 for name in required_params: 

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

66 raise ValueError( 

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

68 ) 

69 

70 def set_additional_info(self, data, infos): 

71 for info in infos: 

72 value = getattr(self, info) 

73 if value: 

74 data[info] = value 

75 

76 def do(self, parent=None): 

77 self.has_do_been_called = True 

78 self.cmds = [] 

79 self.check_params() 

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

81 obj = self.internal_do() 

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

83 

84 if parent: 

85 parent.cmds.append(self) 

86 

87 return obj 

88 

89 def pre_do(self): 

90 pass 

91 

92 def internal_do(self): 

93 return None 

94 

95 def post_do(self, obj): 

96 pass 

97 

98 def undo(self): 

99 if not self.has_do_been_called: 

100 self.is_add_cmd = False 

101 

102 self.check_params() 

103 self.pre_undo() 

104 id = self.internal_undo() 

105 self.post_undo() 

106 

107 return id 

108 

109 def pre_undo(self): 

110 for cmd in reversed(self.cmds): 

111 cmd.undo() 

112 

113 def internal_undo(self): 

114 pass 

115 

116 def post_undo(self): 

117 pass 

118 

119 def get_warnings(self): 

120 return self.warnings