-
Notifications
You must be signed in to change notification settings - Fork 129
/
plugins.py
703 lines (588 loc) · 22.7 KB
/
plugins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
import json
import time
from abc import abstractmethod
from abc import ABC
from PIL import Image
from enum import Enum
from typing import Any
from typing import Dict
from typing import List
from typing import Type
from typing import Union
from typing import TypeVar
from typing import Callable
from typing import Optional
from typing import Coroutine
from aiohttp import ClientSession
from pydantic import Field
from pydantic import BaseModel
from cfdraw import constants
from cfdraw.utils.misc import deprecated
from cfdraw.schema.fields import IFieldDefinition
from cfdraw.parsers.noli import IStr
from cfdraw.parsers.noli import Matrix2D
from cfdraw.parsers.noli import INodeType
from cfdraw.parsers.noli import PivotType
from cfdraw.parsers.noli import SingleNodeType
from cfdraw.parsers.noli import NodeConstraints
from cfdraw.parsers.noli import NodeConstraintRules
from cfdraw.parsers.chakra import IChakra
from cfdraw.parsers.chakra import TextAlign
TPluginModel = TypeVar("TPluginModel")
ISend = Callable[["ISocketMessage"], Coroutine[Any, Any, bool]]
class PluginType(str, Enum):
"""
These types should align with the `allPythonPlugins` locates at
`cfdraw/.web/src/schema/plugins.ts`
"""
PLUGIN_GROUP = "_python.pluginGroup"
FIELDS = "_python.fields"
TEXT_AREA = "_python.textArea"
QA = "_python.QA"
CHAT = "_python.chat"
MARKDOWN = "_python.markdown"
# this type of plugins will not be rendered on the drawboard 🎨
_INTERNAL = "_internal"
class ReactPluginType(str, Enum):
"""
These types should align with the `allReactPlugins` locates at
`cfdraw/.web/src/schema/plugins.ts`
"""
META = "meta"
SETTINGS = "settings"
PROJECT = "project"
ADD = "add"
ARRANGE = "arrange"
UNDO = "undo"
REDO = "redo"
DOWNLOAD = "download"
DELETE = "delete"
WIKI = "wiki"
EMAIL = "email"
GITHUB = "github"
LOGO = "logo"
TEXT_EDITOR = "textEditor"
GROUP_EDITOR = "groupEditor"
MULTI_EDITOR = "multiEditor"
BRUSH = "brush"
# general
def hash_identifier(hash: str, identifier: str) -> str:
return f"{identifier}.{hash}"
class IPluginInfo(BaseModel):
"""
This should align with the following interfaces locate at `cfdraw/.web/src/schema/_python.ts`:
* `IPythonPluginInfo`: `name`
* `IPythonSocketIntervals`: `retryInterval`, `updateInterval`
* `IPythonPluginWithSubmitPluginInfo`: `closeOnSubmit`, `toastOnSubmit`, `toastMessageOnSubmit`
"""
name: Optional[IStr] = Field(None, description="The name of the plugin")
retryInterval: Optional[int] = Field(
None,
ge=0,
description="If not None, the plugin will retry in `retryInterval` ms when exception occurred",
)
updateInterval: Optional[int] = Field(
None,
gt=0,
description="If not None, the plugin will be called every `updateInterval` ms",
)
closeOnSubmit: Optional[bool] = Field(
None,
description="Whether close the expanded panel when the submit button is clicked",
)
toastOnSubmit: Optional[bool] = Field(
None,
description="Whether trigger a toast message when the submit button is clicked",
)
toastMessageOnSubmit: Optional[IStr] = Field(
None,
description="The message of the toast, only take effect when `toastOnSubmit` is `True`",
)
class IPluginSettings(IChakra):
"""
This should align with the `IPythonPlugin` locate at `cfdraw/.web/src/schema/_python.ts`,
but for the sake of accessibility, we 'flattened' the fields. Here's a detailed explanation:
--- IChakra --
> Fields of `IChakra`, except `w` & `h`, will be injected to the `buttonProps`.
>> `w` & `h` has special meanings so we need to skip them.
> Documents of `charaProps` is listed below (the --- React fields --- section).
--- required fields ---
* `w` and `h` are the width and height of the expanded plugin, respectively.
> Sometimes a plugin need not to be expanded, in this case, you can set `w` and `h` to `0`.
> These two fields will go to the `renderInfo` part of the `IRender`, locates at
`cfdraw/.web/src/schema/plugins.ts`.
--- node constraints ---
> These fields will go to the `NodeConstraintSettings` part of the `IRender`, locates at
`cfdraw/.web/src/schema/plugins.ts`.
> See Plugin Positioning (https://github.com/carefree0910/carefree-drawboard/wiki/Details#plugin-positioning)
for detailed explanations of these fields.
--- style fields ---
> These fields will go to the `renderInfo` part of the `IRender`, locates at
`cfdraw/.web/src/schema/plugins.ts`.
> Which means they should align with the `IRenderInfo` locates at the same file.
--- React fields ---
* the `pluginInfo` maps to `IPythonPluginInfo`, but the `identifier` is injected on the fly.
* the `buttonProps` is the universal fallback for you to inject any `ButtonProps` to the
plugin button. (see `cfdraw/.web/src/schema/plugins.ts`, where you can see
`export interface IFloating extends ButtonProps`)
"""
# required fields
w: int = Field(..., gt=0, description="Width of the expanded plugin") # type: ignore
h: int = Field(..., gt=0, description="Height of the expanded plugin") # type: ignore
# node constraints
nodeConstraint: Optional[NodeConstraints] = Field(
None,
description="""
Spcify when the plugin will be shown.
> If set to 'none', the plugin will always be shown.
> If set to 'anyNode', the plugin will be shown when any node is selected.
> If set to 'singleNode', the plugin will be shown when only one node is selected.
> If set to 'multiNode', the plugin will be shown when more than one node is selected.
> Otherwise, the plugin will be shown when the selected node is of the specified type.
""",
)
nodeConstraintRules: Optional[NodeConstraintRules] = Field(
None,
description=(
"Specify the complex rule of the node constraint, "
"will work together with `nodeConstraint`, but it is often "
"not necessary to use `nodeConstraint` when this field is set."
),
)
nodeConstraintValidator: Optional[str] = Field(
None,
description=(
"The universal fallback. "
"It specifies the name of the validator registered by `register_node_validator`."
),
)
# style fields
src: Optional[IStr] = Field(
None,
description="""
The image url that will be shown for the plugin.
> If not specified, we will use a default plugin-ish image.
""",
)
tooltip: Optional[IStr] = Field(
None,
description="""
The tooltip that will be shown for the plugin.
> It is recommended to specify an informative tooltip, but it's also OK to leave it as `None`,
in which case we will not show any tooltip for the plugin.
""",
)
pivot: Optional[PivotType] = Field(
None,
description="""
Pivot of the plugin.
> If `follow` is set to `true`, the plugin will be shown at the pivot of the selected node.
> Otherwise, the plugin will be shown at the pivot of the entire drawboard.
""",
)
follow: Optional[bool] = Field(
None,
description="Whether the plugin follows the node",
)
expandOffsetX: Optional[int] = Field(
None,
description="X offset of the expanded plugin",
)
expandOffsetY: Optional[int] = Field(
None,
description="Y offset of the expanded plugin",
)
iconW: Optional[int] = Field(None, description="Width of the plugin button")
iconH: Optional[int] = Field(None, description="Height of the plugin button")
offsetX: Optional[int] = Field(None, description="X offset of the plugin button")
offsetY: Optional[int] = Field(None, description="Y offset of the plugin button")
bgOpacity: Optional[float] = Field(None, description="Opacity of the plugin button")
useModal: bool = Field(False, description="Whether popup a modal for the plugin")
keepOpen: bool = Field(
False,
description="Whether should we keep the expanded panel of the plugin open, even when users already clicked on the drawboard.",
)
expandOpacity: Optional[float] = Field(None, description="Opacity of the modal")
expandProps: Optional[IChakra] = Field(
None,
description="Extra (chakra) props of the plugin's expanded panel",
)
# React fields
pluginInfo: IPluginInfo = Field(IPluginInfo(), description="Plugin info")
buttonProps: Optional[Dict[str, Any]] = Field(
None,
description="Extra (chakra) props of the plugin button",
)
# internal fields
no_offload: bool = Field(
False,
description=(
"Whether not to offload the plugin to sub-thread when it is executed, "
"useful when you know the plugin is fast enough.\n"
"> This is introduced mainly because some libraries (e.g., `matplotlib`) "
"need to be executed in the main thread."
),
)
def to_react(self, type: str, hash: str, identifier: str) -> Dict[str, Any]:
d = self.dict(exclude={"pluginInfo"})
pI = self.pluginInfo
kw = dict(exclude={"plugins"}) if isinstance(pI, IPluginGroupInfo) else {}
plugin_info = self.pluginInfo.dict(**kw)
plugin_info["identifier"] = identifier
if isinstance(pI, IPluginGroupInfo):
plugins: List[Dict[str, Any]] = []
for p_identifier, p_base in pI.plugins.items():
p_base.hash = hash
p = p_base()
p.identifier = p_identifier
plugins.append(p.to_react())
plugin_info["plugins"] = plugins
node_constraint = d.pop("nodeConstraint")
node_constraint_rules = d.pop("nodeConstraintRules")
node_constraint_validator = d.pop("nodeConstraintValidator")
button_props = d.pop("buttonProps", None) or {}
for field in IChakra.__fields__:
# `w` and `h` are special fields, should not be included in `chakra_props`
if field in ["w", "h"]:
continue
chakra_value = d.pop(field)
if chakra_value is not None:
button_props[field] = chakra_value
for k, v in list(d.items()):
if v is None:
d.pop(k)
# src
if not isinstance(pI, IPluginGroupInfo):
d.setdefault("src", constants.DEFAULT_PLUGIN_ICON)
else:
d.setdefault("src", constants.DEFAULT_PLUGIN_GROUP_ICON)
# gather
props = dict(pluginInfo=plugin_info, renderInfo=d, **button_props)
if node_constraint is not None:
props["nodeConstraint"] = node_constraint
if node_constraint_rules is not None:
props["nodeConstraintRules"] = node_constraint_rules
if node_constraint_validator is not None:
props["nodeConstraintValidator"] = node_constraint_validator
return dict(type=type, props=props)
class ElapsedTimes(BaseModel):
"""This should align with `IElapsedTimes` at `cfdraw/.web/src/schema/meta.ts`"""
createTime: Optional[float]
startTime: Optional[float]
endTime: Optional[float]
pending: Optional[float]
executing: Optional[float]
upload: Optional[float]
total: Optional[float]
def __init__(self, **data: Any):
super().__init__(**data)
self.createTime = time.time()
def start(self) -> None:
start = time.time()
self.startTime = start
if self.createTime is not None:
self.pending = start - self.createTime
def end(self) -> None:
end = time.time()
self.endTime = end
if self.startTime is not None:
self.executing = end - self.startTime
if self.upload is not None:
self.executing -= self.upload
if self.createTime is not None:
self.total = end - self.createTime
# web
class INodeData(BaseModel):
"""This should align with `INodeData` at `src/schema/_python.ts`"""
type: Optional[INodeType] = Field(None, description="Type of the node")
x: Optional[float] = Field(None, description="X of the node")
y: Optional[float] = Field(None, description="Y of the node")
w: Optional[float] = Field(None, description="Width of the node")
h: Optional[float] = Field(None, description="Height of the node")
z: Optional[float] = Field(None, description="Layer of the node")
transform: Optional[Matrix2D] = Field(
None,
description="Transform matrix of the node",
)
text: Optional[str] = Field(
None,
description="Content of the (text) node, will be `None` if the node is not a text node",
)
src: Optional[str] = Field(
None,
description="""
Image url of the node, will be `None` if the node is not intended to be converted to image.
> Currently only `ImageNode` / `PathNode` / `BlankNode` will have this field defined.
""",
)
meta: Optional[Dict[str, Any]] = Field(None, description="Meta of the node")
children: Optional[List["INodeData"]] = Field(
None,
description=(
"Will be a list of `INodeData` if and only if "
"the node is a `Group` (i.e. `type` == 'group')"
),
)
@property
def identifier(self) -> Optional[str]:
if self.meta is None:
return None
return self.meta.get("data", {}).get("identifier")
@property
def extra_responses(self) -> Optional[Dict[str, Any]]:
if self.meta is None:
return None
return self.meta.get("data", {}).get("response", {}).get("extra")
class ISocketRequest(BaseModel):
"""This should align with `IPythonSocketRequest` at `src/schema/_python.ts`"""
hash: str = Field(..., description="The hash of the request")
userId: str = Field(..., description="The id of the user")
userJson: Optional[str] = Field(None, description="Full json of the user info")
baseURL: str = Field(..., description="The base url of the request")
identifier: str = Field(..., description="The identifier of the plugin")
nodeData: INodeData = Field(
...,
description="""
Data extracted from `node`.
> If multiple nodes are selected, this field will be empty and please use `nodeDataList` instead.
""",
)
nodeDataList: List[INodeData] = Field(
...,
description="""
List of data extracted from `nodes`.
> If only one node is selected, this field will be empty and please use `nodeData` instead.
""",
)
extraData: Dict[str, Any] = Field(..., description="Extra data of each plugin")
isInternal: bool = Field(False, description="Whether the request is internal")
def get_user_json(self) -> str:
if self.userJson is not None:
return self.userJson
return json.dumps(dict(userId=self.userId))
class SocketStatus(str, Enum):
"""This should align with `PythonSocketStatus` at `src/schema/_python.ts`"""
PENDING = "pending"
WORKING = "working"
FINISHED = "finished"
EXCEPTION = "exception"
INTERRUPTED = "interrupted"
class ISocketIntermediate(BaseModel):
"""This should align with `IPythonSocketIntermediate` at `src/schema/_python.ts`"""
imageList: Optional[List[str]] = Field(
None,
description="Intermediate images, if any",
)
textList: Optional[List[str]] = Field(
None,
description="Intermediate texts, if any",
)
class ISocketResponse(BaseModel):
"""This should align with `IPythonSocketResponse` at `src/schema/_python.ts`"""
progress: Optional[float] = Field(
None,
ge=0.0,
le=1.0,
description="Progress of current task, if any",
)
intermediate: Optional[ISocketIntermediate] = Field(
None,
description="Intermediate responses, if any",
)
final: Optional[Dict[str, Any]] = Field(None, description="Final response, if any")
injections: Optional[Dict[str, Any]] = Field(None, description="Injections, if any")
elapsedTimes: Optional[ElapsedTimes] = Field(None, description="Elapsed times.")
class ISocketMessage(BaseModel):
"""This should align with `IPythonSocketMessage` at `src/schema/_python.ts`"""
hash: str = Field(..., description="Hash of the current task")
status: SocketStatus = Field(..., description="Status of the current task")
total: int = Field(..., description="Number of tasks")
pending: int = Field(..., description="Number of pending tasks")
message: str = Field(..., description="Message of the current status")
data: ISocketResponse = Field(ISocketResponse(), description="Response data")
@classmethod
def make_progress(
cls,
hash: str,
progress: Optional[float] = None,
intermediate: Optional[ISocketIntermediate] = None,
) -> "ISocketMessage":
return cls(
hash=hash,
status=SocketStatus.WORKING,
total=0,
pending=0,
message="",
data=ISocketResponse(progress=progress, intermediate=intermediate),
)
@classmethod
def make_success(cls, hash: str, final: Dict[str, Any]) -> "ISocketMessage":
return cls(
hash=hash,
status=SocketStatus.FINISHED,
total=0,
pending=0,
message="",
data=ISocketResponse(final=final),
)
@classmethod
def make_exception(cls, hash: str, message: str) -> "ISocketMessage":
return cls(
hash=hash,
status=SocketStatus.EXCEPTION,
total=0,
pending=0,
message=message,
)
# plugin interface
class IPlugin(ABC):
hash: str
identifier: str
http_session: ClientSession
# task specific
task_hash: str
send_message: ISend
elapsed_times: ElapsedTimes
extra_responses: Dict[str, Any]
injections: Dict[str, Any]
# internal
_in_group: bool = False
# abstract
@property
@abstractmethod
def type(self) -> PluginType:
pass
@property
@abstractmethod
def settings(self) -> IPluginSettings:
pass
@abstractmethod
async def __call__(self, data: ISocketRequest) -> ISocketMessage:
pass
@abstractmethod
def to_react(self) -> Dict[str, Any]:
pass
@abstractmethod
def filter(self, nodes: List[INodeData], target: SingleNodeType) -> List[INodeData]:
pass
@abstractmethod
async def load_image(self, src: str) -> Image.Image:
pass
@abstractmethod
def send_progress(
self,
progress: float,
*,
textList: Optional[List[str]] = None,
imageList: Optional[List[str]] = None,
) -> bool:
pass
# optional
## Whether the images generated by this plugin should be audited
image_should_audit: bool = True
## The notification (introductions, hardware requirements, etc.) you want to print out
notification: Optional[str] = None
class Subscription(str, Enum):
ALL = "__all__"
class IMiddleware(ABC):
hash: str
plugin: IPlugin
# abstract
@property
@abstractmethod
def subscriptions(self) -> Union[List[PluginType], Subscription]:
pass
@abstractmethod
async def process(self, response: Any) -> ISocketMessage:
"""
If `can_handle_message` is `False`, the `response` here could be anything except
`ISocketMessage`, because in this case if `response` is already an `ISocketMessage`,
it will be returned directly in the `__call__` method.
"""
# optional callbacks
@property
def can_handle_message(self) -> bool:
return False
async def before(self, request: ISocketRequest) -> None:
self.hash = request.hash
# api
def __init__(self, plugin: IPlugin) -> None:
self.plugin = plugin
async def __call__(self, response: Any) -> ISocketMessage:
if (
self.subscriptions != Subscription.ALL
and self.plugin.type not in self.subscriptions
):
return response
if isinstance(response, ISocketMessage) and not self.can_handle_message:
return response
return await self.process(response)
def make_success(self, final: Dict[str, Any]) -> ISocketMessage:
return ISocketMessage.make_success(self.hash, final)
# (react) bindings
class ILogoPluginInfo(IPluginInfo):
"""This should align with `ILogoPlugin` at `cfdraw/.web/src/schema/plugins.ts`"""
redirectUrl: Optional[IStr] = Field(
None,
description="Redirection target when clicking the logo, `None` means no redirection",
)
class ILogoSettings(IPluginSettings):
w: int = Field(0, gt=0, description="Width of the expanded plugin") # type: ignore
h: int = Field(0, gt=0, description="Height of the expanded plugin") # type: ignore
class IPluginGroupInfo(IPluginInfo):
header: Optional[IStr] = Field(None, description="Header of the plugin group")
plugins: Dict[str, Type[IPlugin]] = Field(..., description="Plugins in the group")
class IFieldsPluginInfo(IPluginInfo):
"""This should align with `IPythonFieldsPlugin` at `cfdraw/.web/src/schema/_python.ts`"""
header: Optional[IStr] = Field(None, description="Header of the plugin")
definitions: Dict[str, IFieldDefinition] = Field(
...,
description="Field definitions",
)
numColumns: Optional[int] = Field(None, description="Number of columns")
noErrorToast: Optional[bool] = Field(None, description="Whether not to toast error")
class ITextAreaPluginInfo(IPluginInfo):
"""This should align with `IPythonTextAreaPlugin` at `cfdraw/.web/src/schema/_python.ts`"""
noLoading: bool = Field(
False, description="Whether to show the 'Loading...' text or not"
)
textAlign: Optional[TextAlign] = Field(None, description="Text align")
class IQAPluginInfo(IPluginInfo):
"""This should align with `IPythonQAPlugin` at `cfdraw/.web/src/schema/_python.ts`"""
initialText: IStr = Field(
...,
description="The initial text to be displayed in the text area",
)
class IChatPluginInfo(IPluginInfo):
"""This should align with `IPythonChatPlugin` at `cfdraw/.web/src/schema/_python.ts`"""
initialText: IStr = Field(
"",
description="The initial text to be displayed in the text area",
)
__all__ = [
"ISend",
"PluginType",
"ReactPluginType",
# general
"hash_identifier",
"IPluginInfo",
"IPluginSettings",
# web
"INodeData",
"ISocketRequest",
"SocketStatus",
"ISocketIntermediate",
"ISocketResponse",
"ISocketMessage",
# plugin interface
"IPlugin",
"IMiddleware",
"IFieldsPluginInfo",
# bindings
"ILogoPluginInfo",
"ILogoSettings",
"IPluginGroupInfo",
"ITextAreaPluginInfo",
"IQAPluginInfo",
"IChatPluginInfo",
]