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

44 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-02-26 10:12 +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.json import RawJavaScriptEncoder 

9from ipyvizzu.template import ChartProperty, VIZZU_VERSION 

10 

11 

12class Method: 

13 """ 

14 A class for dumping chart independent parameters to 

15 [DisplayTemplate.STORE][ipyvizzu.template.DisplayTemplate] template. 

16 """ 

17 

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

19 

20 _data: dict 

21 

22 def dump(self) -> dict: 

23 """ 

24 A method for returning the stored data. 

25 

26 Returns: 

27 The stored data. 

28 """ 

29 

30 return self._data 

31 

32 

33class Animate(Method): 

34 """ 

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

36 """ 

37 

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

39 

40 def __init__( 

41 self, 

42 chart_target: AbstractAnimation, 

43 chart_anim_opts: Optional[dict] = None, 

44 ): 

45 """ 

46 Animate constructor. 

47 

48 Args: 

49 chart_target: 

50 AbstractAnimation inherited object such as 

51 [Data][ipyvizzu.animation.Data] 

52 [Config][ipyvizzu.animation.Config] or 

53 [Style][ipyvizzu.animation.Style]. 

54 chart_anim_opts: 

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

56 """ 

57 

58 self._data = { 

59 "chart_target": chart_target.dump(), 

60 "chart_anim_opts": ( 

61 PlainAnimation(chart_anim_opts).dump() 

62 if chart_anim_opts 

63 else "undefined" 

64 ), 

65 } 

66 

67 

68class Feature(Method): 

69 """ 

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

71 """ 

72 

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

74 

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

76 """ 

77 Feature constructor. 

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 Plugin(Method): 

88 """ 

89 It stores and dumps `plugin`, `options` and `name` parameters. 

90 """ 

91 

92 def __init__(self, plugin: str, options: Optional[dict], name: str, enabled: bool): 

93 """ 

94 Plugin constructor. 

95 

96 Args: 

97 plugin: The package name or the url of the plugin. 

98 options: The plugin constructor options. 

99 name: The name of the plugin (default `default`). 

100 enabled: The state of the plugin (default `True`). 

101 """ 

102 

103 self._data = { 

104 "plugin": Plugin.resolve_url(plugin), 

105 "options": ( 

106 {} if options is None else json.dumps(options, cls=RawJavaScriptEncoder) 

107 ), 

108 "name": name, 

109 "enabled": json.dumps(enabled), 

110 } 

111 

112 @staticmethod 

113 def resolve_url(plugin: str) -> str: 

114 """ 

115 A static method for resolving the url of the plugin. 

116 

117 Args: 

118 plugin: The package name or the url of the plugin. 

119 

120 Returns: 

121 The url of the plugin. 

122 """ 

123 

124 if Plugin._is_url(plugin): 

125 return plugin 

126 return Plugin._get_url(plugin) 

127 

128 @staticmethod 

129 def _is_url(plugin: str) -> bool: 

130 return "/" in plugin 

131 

132 @staticmethod 

133 def _get_url(plugin: str) -> str: 

134 jsdelivr = "https://cdn.jsdelivr.net/npm/@vizzu" 

135 tag = f"vizzu-{VIZZU_VERSION}" 

136 return f"{jsdelivr}/{plugin}@{tag}/dist/mjs/index.min.js" 

137 

138 

139class Store(Method): 

140 """ 

141 It stores and dumps `snapshot_id` parameter. 

142 """ 

143 

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

145 

146 def __init__(self, snapshot_id: str): 

147 """ 

148 Store constructor. 

149 

150 Args: 

151 snapshot_id: The id of snapshot object. 

152 """ 

153 

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

155 

156 

157class EventOn(Method): 

158 """ 

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

160 """ 

161 

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

163 

164 def __init__(self, event_handler: EventHandler): 

165 """ 

166 EventOn constructor. 

167 

168 Args: 

169 event_handler: An event handler object. 

170 """ 

171 

172 self._data = { 

173 "id": event_handler.id, 

174 "event": event_handler.event, 

175 "handler": event_handler.handler, 

176 } 

177 

178 

179class EventOff(Method): 

180 """ 

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

182 """ 

183 

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

185 

186 def __init__(self, event_handler: EventHandler): 

187 """ 

188 EventOff constructor. 

189 

190 Args: 

191 event_handler: An event handler object. 

192 """ 

193 

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

195 

196 

197class Log(Method): 

198 """ 

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

200 """ 

201 

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

203 

204 def __init__(self, chart_property: ChartProperty): 

205 """ 

206 Log constructor. 

207 

208 Args: 

209 chart_property: 

210 A chart property such as 

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

212 [STYLE][ipyvizzu.template.ChartProperty]. 

213 """ 

214 

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