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
« prev ^ index » next coverage.py v7.4.3, created at 2024-02-26 10:12 +0000
1"""A module for working with template methods."""
3import json
4from typing import Optional
6from ipyvizzu.animation import AbstractAnimation, PlainAnimation
7from ipyvizzu.event import EventHandler
8from ipyvizzu.json import RawJavaScriptEncoder
9from ipyvizzu.template import ChartProperty, VIZZU_VERSION
12class Method:
13 """
14 A class for dumping chart independent parameters to
15 [DisplayTemplate.STORE][ipyvizzu.template.DisplayTemplate] template.
16 """
18 # pylint: disable=too-few-public-methods
20 _data: dict
22 def dump(self) -> dict:
23 """
24 A method for returning the stored data.
26 Returns:
27 The stored data.
28 """
30 return self._data
33class Animate(Method):
34 """
35 It stores and dumps `chart_target` and `chart_anim_opts` parameters.
36 """
38 # pylint: disable=too-few-public-methods
40 def __init__(
41 self,
42 chart_target: AbstractAnimation,
43 chart_anim_opts: Optional[dict] = None,
44 ):
45 """
46 Animate constructor.
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 """
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 }
68class Feature(Method):
69 """
70 It stores and dumps `name` and `enabled` parameters.
71 """
73 # pylint: disable=too-few-public-methods
75 def __init__(self, name: str, enabled: bool):
76 """
77 Feature constructor.
79 Args:
80 name: The name of a chart feature.
81 enabled: The new state of a chart feature.
82 """
84 self._data = {"name": name, "enabled": json.dumps(enabled)}
87class Plugin(Method):
88 """
89 It stores and dumps `plugin`, `options` and `name` parameters.
90 """
92 def __init__(self, plugin: str, options: Optional[dict], name: str, enabled: bool):
93 """
94 Plugin constructor.
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 """
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 }
112 @staticmethod
113 def resolve_url(plugin: str) -> str:
114 """
115 A static method for resolving the url of the plugin.
117 Args:
118 plugin: The package name or the url of the plugin.
120 Returns:
121 The url of the plugin.
122 """
124 if Plugin._is_url(plugin):
125 return plugin
126 return Plugin._get_url(plugin)
128 @staticmethod
129 def _is_url(plugin: str) -> bool:
130 return "/" in plugin
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"
139class Store(Method):
140 """
141 It stores and dumps `snapshot_id` parameter.
142 """
144 # pylint: disable=too-few-public-methods
146 def __init__(self, snapshot_id: str):
147 """
148 Store constructor.
150 Args:
151 snapshot_id: The id of snapshot object.
152 """
154 self._data = {"id": snapshot_id}
157class EventOn(Method):
158 """
159 It stores and dumps the `id`, the `event` and the `handler` of the event handler object.
160 """
162 # pylint: disable=too-few-public-methods
164 def __init__(self, event_handler: EventHandler):
165 """
166 EventOn constructor.
168 Args:
169 event_handler: An event handler object.
170 """
172 self._data = {
173 "id": event_handler.id,
174 "event": event_handler.event,
175 "handler": event_handler.handler,
176 }
179class EventOff(Method):
180 """
181 It stores and dumps the `id` and the `event` of the event handler object.
182 """
184 # pylint: disable=too-few-public-methods
186 def __init__(self, event_handler: EventHandler):
187 """
188 EventOff constructor.
190 Args:
191 event_handler: An event handler object.
192 """
194 self._data = {"id": event_handler.id, "event": event_handler.event}
197class Log(Method):
198 """
199 It stores and dumps the value of the chart property object.
200 """
202 # pylint: disable=too-few-public-methods
204 def __init__(self, chart_property: ChartProperty):
205 """
206 Log constructor.
208 Args:
209 chart_property:
210 A chart property such as
211 [CONFIG][ipyvizzu.template.ChartProperty] and
212 [STYLE][ipyvizzu.template.ChartProperty].
213 """
215 self._data = {"chart_property": chart_property.value}