Coverage for src/ipyvizzu/method.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-10 09:30 +0000

1"""A module for working with template methods.""" 

2 

3import json 

4from typing import Optional 

5 

6from ipyvizzu.animation import AbstractAnimation, PlainAnimation 

7from ipyvizzu.event import EventHandler 

8from ipyvizzu.template import ChartProperty 

9 

10 

11class Method: 

12 """A class for storing and dumping any kind of data.""" 

13 

14 # pylint: disable=too-few-public-methods 

15 

16 _data: dict 

17 

18 def dump(self) -> dict: 

19 """ 

20 A method for returning the stored data. 

21 

22 Returns: 

23 The stored data. 

24 """ 

25 

26 return self._data 

27 

28 

29class Animate(Method): 

30 """ 

31 A class for dumping chart independent parameters to 

32 [DisplayTemplate.ANIMATE][ipyvizzu.template.DisplayTemplate] template. 

33 """ 

34 

35 # pylint: disable=too-few-public-methods 

36 

37 def __init__( 

38 self, 

39 chart_target: AbstractAnimation, 

40 chart_anim_opts: Optional[dict] = None, 

41 ): 

42 """ 

43 Animate constructor. 

44 

45 It stores and dumps `chart_target` and `chart_anim_opts` parameters. 

46 

47 Args: 

48 chart_target: 

49 AbstractAnimation inherited object such as 

50 [Data][ipyvizzu.animation.Data] 

51 [Config][ipyvizzu.animation.Config] or 

52 [Style][ipyvizzu.animation.Style]. 

53 chart_anim_opts: 

54 Animation options' dictionary. If it is not set, it dumps `undefined`. 

55 """ 

56 

57 self._data = { 

58 "chart_target": chart_target.dump(), 

59 "chart_anim_opts": PlainAnimation(chart_anim_opts).dump() 

60 if chart_anim_opts 

61 else "undefined", 

62 } 

63 

64 

65class Feature(Method): 

66 """ 

67 A class for dumping chart independent parameters to 

68 [DisplayTemplate.FEATURE][ipyvizzu.template.DisplayTemplate] template. 

69 """ 

70 

71 # pylint: disable=too-few-public-methods 

72 

73 def __init__(self, name: str, enabled: bool): 

74 """ 

75 Feature constructor. 

76 

77 It stores and dumps `name` and `enabled` parameters. 

78 

79 Args: 

80 name: The name of a chart feature. 

81 enabled: The new state of a chart feature. 

82 """ 

83 

84 self._data = {"name": name, "enabled": json.dumps(enabled)} 

85 

86 

87class Store(Method): 

88 """ 

89 A class for dumping chart independent parameters to 

90 [DisplayTemplate.STORE][ipyvizzu.template.DisplayTemplate] template. 

91 """ 

92 

93 # pylint: disable=too-few-public-methods 

94 

95 def __init__(self, snapshot_id: str): 

96 """ 

97 Store constructor. 

98 

99 It stores and dumps `snapshot_id` parameter. 

100 

101 Args: 

102 snapshot_id: The id of snapshot object. 

103 """ 

104 

105 self._data = {"id": snapshot_id} 

106 

107 

108class EventOn(Method): 

109 """ 

110 A class for dumping chart independent parameters to 

111 [DisplayTemplate.SET_EVENT][ipyvizzu.template.DisplayTemplate] template. 

112 """ 

113 

114 # pylint: disable=too-few-public-methods 

115 

116 def __init__(self, event_handler: EventHandler): 

117 """ 

118 EventOn constructor. 

119 

120 It stores and dumps the `id`, the `event` and the `handler` of the event handler object. 

121 

122 Args: 

123 event_handler: An event handler object. 

124 """ 

125 

126 self._data = { 

127 "id": event_handler.id, 

128 "event": event_handler.event, 

129 "handler": event_handler.handler, 

130 } 

131 

132 

133class EventOff(Method): 

134 """ 

135 A class for dumping chart independent parameters to 

136 [DisplayTemplate.CLEAR_EVENT][ipyvizzu.template.DisplayTemplate] template. 

137 """ 

138 

139 # pylint: disable=too-few-public-methods 

140 

141 def __init__(self, event_handler: EventHandler): 

142 """ 

143 EventOff constructor. 

144 

145 It stores and dumps the `id` and the `event` of the event handler object. 

146 

147 Args: 

148 event_handler: An event handler object. 

149 """ 

150 

151 self._data = {"id": event_handler.id, "event": event_handler.event} 

152 

153 

154class Log(Method): 

155 """ 

156 A class for dumping chart independent parameters to 

157 [DisplayTemplate.LOG][ipyvizzu.template.DisplayTemplate] template. 

158 """ 

159 

160 # pylint: disable=too-few-public-methods 

161 

162 def __init__(self, chart_property: ChartProperty): 

163 """ 

164 Log constructor. 

165 

166 It stores and dumps the value of the chart property object. 

167 

168 Args: 

169 chart_property: 

170 A chart property such as 

171 [CONFIG][ipyvizzu.template.ChartProperty] and 

172 [STYLE][ipyvizzu.template.ChartProperty]. 

173 """ 

174 

175 self._data = {"chart_property": chart_property.value}