Skip to content

Commit 32c8b11

Browse files
authored
Merge pull request #1 from Class-Widgets/refactor/notification_api
chore: type hints
2 parents 9fee128 + 43ef96c commit 32c8b11

7 files changed

Lines changed: 119 additions & 32 deletions

File tree

ClassWidgets/SDK/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
from typing import TYPE_CHECKING
66

7-
__version__ = '0.3.2'
7+
__version__ = '0.4.0'
88
__author__ = 'Class Widgets Official'
99

1010
if TYPE_CHECKING:

ClassWidgets/SDK/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ __all__ = [
2020
'NotificationProvider',
2121
'NotificationLevel',
2222
'NotificationData',
23+
'NotificationProviderConfig',
2324
"__version__",
2425
"__author__",
2526
]

ClassWidgets/SDK/api.pyi

Lines changed: 87 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,69 @@
1-
from typing import Dict, List, Optional, Any, Union
1+
from typing import Dict, List, Optional, Any, Union, TypedDict
22
from datetime import datetime
33
from pathlib import Path
4+
from enum import Enum
45

6+
from .notification import NotificationPayload, NotificationProvider
7+
from .base_model import QObject, Signal
58

6-
class QObject: ...
79

10+
class ConfigBaseModel: ...
811

9-
class Signal: ...
1012

13+
class RuntimeMetaPayload(TypedDict):
14+
id: str
15+
version: int
16+
maxWeekCycle: int
17+
startDate: str
1118

12-
class ConfigBaseModel: ...
19+
20+
class RuntimeEntryPayload(TypedDict):
21+
id: str
22+
type: str
23+
startTime: str
24+
endTime: str
25+
subjectId: Optional[str]
26+
title: Optional[str]
27+
28+
29+
class RuntimeEntryChangedPayload(TypedDict, total=False):
30+
id: str
31+
type: str
32+
startTime: str
33+
endTime: str
34+
subjectId: Optional[str]
35+
title: Optional[str]
36+
37+
38+
class RuntimeSubjectPayload(TypedDict):
39+
id: str
40+
name: str
41+
simplifiedName: Optional[str]
42+
teacher: Optional[str]
43+
icon: Optional[str]
44+
color: Optional[str]
45+
location: Optional[str]
46+
isLocalClassroom: bool
47+
48+
49+
class RuntimeRemainingTimePayload(TypedDict):
50+
minute: int
51+
second: int
52+
53+
54+
class SettingsPagePayload(TypedDict):
55+
id: str
56+
page: str
57+
title: str
58+
icon: str
59+
60+
61+
class EntryType(str, Enum):
62+
CLASS = "class"
63+
BREAK = "break"
64+
FREE = "free"
65+
ACTIVITY = "activity"
66+
UNKNOWN = "unknown"
1367

1468

1569
# WidgetsAPI
@@ -29,7 +83,7 @@ class WidgetsAPI:
2983

3084
# NotificationAPI
3185
class NotificationAPI(QObject):
32-
pushed: Signal # Signal(str)
86+
pushed: Signal[NotificationPayload]
3387

3488
def __init__(self, app: Any) -> None: ...
3589

@@ -39,15 +93,15 @@ class NotificationAPI(QObject):
3993
name: Optional[str] = ...,
4094
icon: Optional[Union[str, Path]] = ...,
4195
use_system_notify: bool = ...
42-
) -> Any: ...
96+
) -> NotificationProvider: ...
4397

4498
def register_provider(
4599
self,
46100
provider_id: str,
47101
name: Optional[str] = ...,
48102
icon: Optional[Union[str, Path]] = ...,
49103
use_system_notify: bool = ...
50-
) -> Any: ...
104+
) -> NotificationProvider: ...
51105

52106

53107
# ScheduleAPI
@@ -61,7 +115,7 @@ class ScheduleAPI:
61115

62116
# ThemeAPI
63117
class ThemeAPI(QObject):
64-
changed: Signal # Signal(str)
118+
changed: Signal[str]
65119

66120
def __init__(self, app: Any) -> None: ...
67121

@@ -70,9 +124,9 @@ class ThemeAPI(QObject):
70124

71125
# RuntimeAPI
72126
class RuntimeAPI(QObject):
73-
updated: Signal # Signal()
74-
statusChanged: Signal # Signal(str)
75-
entryChanged: Signal # Signal(dict)
127+
updated: Signal
128+
statusChanged: Signal[str]
129+
entryChanged: Signal[RuntimeEntryChangedPayload]
76130

77131
def __init__(self, app: Any) -> None: ...
78132

@@ -94,19 +148,19 @@ class RuntimeAPI(QObject):
94148

95149
# 日程属性
96150
@property
97-
def schedule_meta(self) -> Optional[Dict[str, Any]]: ...
151+
def schedule_meta(self) -> Optional[RuntimeMetaPayload]: ...
98152

99153
@property
100-
def current_day_entries(self) -> List[Dict[str, Any]]: ...
154+
def current_day_entries(self) -> List[RuntimeEntryPayload]: ...
101155

102156
@property
103-
def current_entry(self) -> Optional[Dict[str, Any]]: ...
157+
def current_entry(self) -> Optional[RuntimeEntryPayload]: ...
104158

105159
@property
106-
def next_entries(self) -> List[Dict[str, Any]]: ...
160+
def next_entries(self) -> List[RuntimeEntryPayload]: ...
107161

108162
@property
109-
def remaining_time(self) -> Dict[str, int]: ...
163+
def remaining_time(self) -> RuntimeRemainingTimePayload: ...
110164

111165
@property
112166
def progress(self) -> float: ...
@@ -115,7 +169,7 @@ class RuntimeAPI(QObject):
115169
def current_status(self) -> str: ...
116170

117171
@property
118-
def current_subject(self) -> Optional[Dict[str, Any]]: ...
172+
def current_subject(self) -> Optional[RuntimeSubjectPayload]: ...
119173

120174
@property
121175
def current_title(self) -> Optional[str]: ...
@@ -129,7 +183,7 @@ class ConfigAPI:
129183

130184
def get_plugin_model(self, plugin_id: str) -> Optional[ConfigBaseModel]: ...
131185

132-
def save(self) -> None: ...
186+
def save(self) -> Any: ...
133187

134188

135189
# AutomationAPI
@@ -141,12 +195,12 @@ class AutomationAPI:
141195

142196
# UiAPI
143197
class UiAPI(QObject):
144-
settingsPageRegistered: Signal # Signal()
198+
settingsPageRegistered: Signal
145199

146-
def __init__(self) -> None: ...
200+
def __init__(self, app: Any) -> None: ...
147201

148202
@property
149-
def pages(self) -> List[Dict[str, Any]]: ...
203+
def pages(self) -> List[SettingsPagePayload]: ...
150204

151205
def unregister_settings_page(self, qml_path: Union[str, Path]) -> None: ...
152206

@@ -162,6 +216,11 @@ class UiAPI(QObject):
162216
class PluginAPI:
163217
def __init__(self, app: Any) -> None: ...
164218

219+
def set_current_plugin(self, plugin: Any) -> None: ...
220+
221+
@property
222+
def current_plugin(self) -> Any: ...
223+
165224
widgets: WidgetsAPI
166225
notification: NotificationAPI
167226
schedule: ScheduleAPI
@@ -173,6 +232,13 @@ class PluginAPI:
173232

174233

175234
__all__ = [
235+
'RuntimeMetaPayload',
236+
'RuntimeEntryPayload',
237+
'RuntimeEntryChangedPayload',
238+
'RuntimeSubjectPayload',
239+
'RuntimeRemainingTimePayload',
240+
'SettingsPagePayload',
241+
'EntryType',
176242
'WidgetsAPI',
177243
'NotificationAPI',
178244
'ScheduleAPI',

ClassWidgets/SDK/base_model.pyi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import Any, Generic, TypeVar, Callable
2+
3+
class QObject: ...
4+
5+
T = TypeVar("T")
6+
7+
# 模拟信号类,支持索引语法
8+
class Signal(Generic[T]):
9+
def connect(self, slot: Callable[..., Any]) -> None: ...
10+
def emit(self, *args: Any) -> None: ...
11+
def disconnect(self, slot: Callable[..., Any]) -> None: ...

ClassWidgets/SDK/config.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from typing import Dict, List, Optional, Any, Union
22
from enum import Enum
33

4+
from .base_model import Signal
5+
46

57
# Enums
68
class LayoutAnchor(str, Enum):
@@ -126,7 +128,7 @@ class RootConfig(ConfigBaseModel):
126128

127129
# ============ Config Manager ============
128130
class ConfigManager:
129-
configChanged: Any = ... # Signal 类型
131+
configChanged: Signal[str] = ...
130132

131133
def __init__(self, path: Any, filename: str) -> None: ...
132134

ClassWidgets/SDK/notification.pyi

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, Union, Any, TYPE_CHECKING
1+
from typing import Optional, Union, Any, TypedDict, TYPE_CHECKING
22
from pathlib import Path
33
from enum import IntEnum
44
from pydantic import BaseModel
@@ -30,6 +30,18 @@ class NotificationData(BaseModel):
3030
silent: bool = False
3131
use_system: bool = False
3232

33+
# 与 NotificationData 对应的字典结构
34+
class NotificationPayload(TypedDict):
35+
provider_id: str
36+
level: int
37+
title: str
38+
message: Optional[str]
39+
icon: Optional[Union[str, Path]]
40+
duration: int
41+
closable: bool
42+
silent: bool
43+
use_system: bool
44+
3345

3446
class NotificationProviderConfig(BaseModel):
3547
"""
@@ -80,5 +92,6 @@ __all__ = [
8092
'NotificationLevel',
8193
'NotificationData',
8294
'NotificationProviderConfig',
83-
'NotificationProvider'
95+
'NotificationProvider',
96+
'NotificationPayload'
8497
]

ClassWidgets/SDK/plugin_base.pyi

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@ from pathlib import Path
33
from enum import IntEnum
44
from pydantic import BaseModel
55

6-
# 为非检查时提供的抽象基类
7-
class QObject: ...
8-
9-
class Signal:
10-
def emit(self, *args) -> None:
11-
pass
12-
136
from .api import PluginAPI
147

8+
from .base_model import QObject, Signal
159

1610
class ConfigBaseModel(BaseModel):
1711
"""

0 commit comments

Comments
 (0)