Skip to content

Commit b231a2e

Browse files
committed
update fake_puppet linting
1 parent 749c486 commit b231a2e

File tree

5 files changed

+200
-40
lines changed

5 files changed

+200
-40
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
# GitHb: https://github.com/wechaty/python-wechaty
44
# Author: Huan LI <[email protected]> https://github.com/huan
55
#
6+
export GLOBIGNORE=src/wechaty/fake_puppet.py
67

78
SOURCE_GLOB=$(wildcard bin/*.py src/**/*.py tests/**/*.py examples/*.py)
89

910
#
1011
# Huan(202003)
1112
# F811: https://github.com/PyCQA/pyflakes/issues/320#issuecomment-469337000
1213
#
13-
IGNORE_PEP=E203,E221,E241,E272,E501,F811
14+
IGNORE_PEP=E203,E221,E241,E272,E501,F811,W293
1415

1516
# help scripts to find the right place of wechaty module
1617
export PYTHONPATH=src/

src/wechaty/config.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,34 +132,49 @@ def get_environment_variable(
132132
return default_value
133133
return os.environ[name]
134134

135+
@property
135136
def cache_rooms(self) -> bool:
136137
"""whether cache all of payloads of rooms
137138
138139
Returns:
139140
bool: whether cache the paylaod of rooms
140141
"""
141-
return os.environ.get('CACHE_ROOMS', True)
142+
env_key = 'CACHE_ROOMS'
143+
true_strings = ['true', '1']
144+
if env_key not in os.environ:
145+
return True
146+
value = os.environ[env_key]
147+
return value in true_strings
142148

149+
@property
143150
def cache_room_path(self) -> str:
144151
"""get the room pickle path"""
145-
env_key = "CACHE_CONTACTS_PATH"
152+
env_key = "CACHE_ROOMS_PATH"
146153
if env_key in os.environ:
147154
return os.environ[env_key]
148155

149156
default_path = os.path.join(
150157
self.cache_dir,
151-
"contact_payloads.pkl"
158+
"room_payloads.pkl"
152159
)
153160
return default_path
154161

162+
@property
155163
def cache_contacts(self) -> bool:
156164
"""whether cache all of payloads of contact
157165
158166
Returns:
159167
bool: whether cache the paylaod of contact
160168
"""
161-
return os.environ.get('CACHE_ROOMS', True)
162169

170+
env_key = 'CACHE_CONTACTS'
171+
true_strings = ['true', '1']
172+
if env_key not in os.environ:
173+
return True
174+
value = os.environ[env_key]
175+
return value in true_strings
176+
177+
@property
163178
def cache_contact_path(self) -> str:
164179
"""get the contact pickle path"""
165180
env_key = "CACHE_CONTACTS_PATH"

src/wechaty/fake_puppet.py

Lines changed: 154 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# type: ignore
12
"""
23
Python Wechaty - https://github.com/wechaty/python-wechaty
34
@@ -59,76 +60,151 @@
5960
from pyee import AsyncIOEventEmitter
6061

6162

62-
class FakeMixin:
63-
def get_fake_emitter(self) -> AsyncIOEventEmitter:
64-
"""get fake emitter
63+
class FakeMessageManagerMixin:
64+
"""_summary_
6565
66-
Returns:
67-
AsyncIOEventEmitter: get fake emitter
68-
"""
69-
emitter = getattr(self, 'emitter', None)
70-
assert emitter is not None, 'emitter not found'
71-
assert isinstance(emitter, AsyncIOEventEmitter)
72-
return emitter
73-
74-
class FakeMessageManagerMixin(FakeMixin):
66+
Args:
67+
FakeMixin (_type_): _description_
68+
"""
7569
def __init__(self) -> None:
70+
"""_summary_
71+
"""
7672
super().__init__(self)
7773

7874
self._message_payloads: Dict[str, MessagePayload] = {}
7975

8076
def add_fake_message(self, payload: MessagePayload):
77+
"""_summary_
78+
79+
Args:
80+
payload (MessagePayload): _description_
81+
"""
8182
self._message_payloads[payload.id] = payload
8283

83-
def get_fake_message(self, id: str) -> Optional[MessagePayload]:
84-
return self._message_payloads.get(id, None)
84+
def get_fake_message(self, message_id: str) -> Optional[MessagePayload]:
85+
"""_summary_
86+
87+
Args:
88+
id (str): _description_
89+
90+
Returns:
91+
Optional[MessagePayload]: _description_
92+
"""
93+
return self._message_payloads.get(message_id, None)
8594

86-
def remove_fake_message(self, id: str):
87-
self._message_payloads.pop(id, None)
95+
def remove_fake_message(self, message_id: str):
96+
"""_summary_
97+
98+
Args:
99+
id (str): _description_
100+
"""
101+
self._message_payloads.pop(message_id, None)
88102

89103
def get_all_fake_messages(self) -> List[MessagePayload]:
104+
"""_summary_
105+
106+
Returns:
107+
List[MessagePayload]: _description_
108+
"""
90109
return list(self._message_payloads.values())
91-
92-
def emit_fake_message(self, id: str):
93-
emitter = self.get_fake_emitter()
94-
message = self.get_fake_message(id)
95-
emitter.emit('message', message)
96110

97-
class FakeRoomManagerMixin(FakeMixin):
111+
112+
class FakeRoomManagerMixin:
113+
"""_summary_
114+
115+
Args:
116+
FakeMixin (_type_): _description_
117+
"""
98118
def __init__(self) -> None:
119+
"""_summary_
120+
"""
99121
super().__init__(self)
100122

101123
self._room_payloads: Dict[str, RoomPayload] = {}
102124

103125
def get_all_fake_messages(self) -> List[RoomPayload]:
126+
"""_summary_
127+
128+
Returns:
129+
List[RoomPayload]: _description_
130+
"""
104131
return list(self._room_payloads.values())
105132

106133
def add_fake_room(self, payload: RoomPayload):
134+
"""_summary_
135+
136+
Args:
137+
payload (RoomPayload): _description_
138+
"""
107139
self._room_payloads[payload.id] = payload
108140

109-
def get_fake_room(self, id: str) -> Optional[RoomPayload]:
110-
return self._room_payloads.get(id, None)
141+
def get_fake_room(self, room_id: str) -> Optional[RoomPayload]:
142+
"""_summary_
143+
144+
Args:
145+
id (str): _description_
146+
147+
Returns:
148+
Optional[RoomPayload]: _description_
149+
"""
150+
return self._room_payloads.get(room_id, None)
111151

112-
def remove_fake_room(self, id: str):
113-
self._room_payloads.pop(id, None)
152+
def remove_fake_room(self, room_id: str):
153+
"""_summary_
154+
155+
Args:
156+
id (str): _description_
157+
"""
158+
self._room_payloads.pop(room_id, None)
159+
114160

115-
class FakeContactManagerMixin(FakeMixin):
161+
class FakeContactManagerMixin:
162+
"""_summary_
163+
164+
Args:
165+
FakeMixin (_type_): _description_
166+
"""
116167
def __init__(self) -> None:
168+
"""_summary_
169+
"""
117170
super().__init__(self)
118171

119172
self._contact_payloads: Dict[str, ContactPayload] = {}
120173

121174
def get_all_fake_messages(self) -> List[ContactPayload]:
175+
"""_summary_
176+
177+
Returns:
178+
List[ContactPayload]: _description_
179+
"""
122180
return list(self._contact_payloads.values())
123181

124182
def add_fake_contact(self, payload: ContactPayload):
183+
"""_summary_
184+
185+
Args:
186+
payload (ContactPayload): _description_
187+
"""
125188
self._contact_payloads[payload.id] = payload
126189

127-
def get_fake_contact(self, id: str) -> Optional[ContactPayload]:
128-
return self._contact_payloads.get(id, None)
190+
def get_fake_contact(self, contact_id: str) -> Optional[ContactPayload]:
191+
"""_summary_
192+
193+
Args:
194+
id (str): _description_
195+
196+
Returns:
197+
Optional[ContactPayload]: _description_
198+
"""
199+
return self._contact_payloads.get(contact_id, None)
129200

130-
def remove_fake_contact(self, id: str):
131-
self._contact_payloads.pop(id, None)
201+
def remove_fake_contact(self, contact_id: str):
202+
"""_summary_
203+
204+
Args:
205+
id (str): _description_
206+
"""
207+
self._contact_payloads.pop(contact_id, None)
132208

133209

134210
class FakePuppet(Puppet, FakeContactManagerMixin, FakeMessageManagerMixin, FakeRoomManagerMixin):
@@ -139,12 +215,31 @@ class FakePuppet(Puppet, FakeContactManagerMixin, FakeMessageManagerMixin, FakeR
139215
"""
140216

141217
def __init__(self, options: PuppetOptions, name: str = 'puppet') -> None:
218+
"""_summary_
219+
220+
Args:
221+
options (PuppetOptions): _description_
222+
name (str, optional): _description_. Defaults to 'puppet'.
223+
"""
142224
super().__init__(options, name)
143225
self.name: str = name
144226
self.options = options
145227
self.emitter = AsyncIOEventEmitter()
146228

147-
def add_random_fake_contact_message(self, msg: Optional[str] = None, contact_id: Optional[str] = None) -> str:
229+
def add_random_fake_contact_message(
230+
self,
231+
msg: Optional[str] = None,
232+
contact_id: Optional[str] = None
233+
) -> str:
234+
"""_summary_
235+
236+
Args:
237+
msg (Optional[str], optional): _description_. Defaults to None.
238+
contact_id (Optional[str], optional): _description_. Defaults to None.
239+
240+
Returns:
241+
str: _description_
242+
"""
148243
if not msg:
149244
msg = str(uuid4())
150245
if not contact_id:
@@ -158,7 +253,22 @@ def add_random_fake_contact_message(self, msg: Optional[str] = None, contact_id:
158253
self.add_fake_message(message)
159254
return message.id
160255

161-
def add_random_fake_room_message(self, msg: Optional[str] = None, contact_id: Optional[str] = None, room_id: Optional[str] = None) -> str:
256+
def add_random_fake_room_message(
257+
self,
258+
msg: Optional[str] = None,
259+
contact_id: Optional[str] = None,
260+
room_id: Optional[str] = None
261+
) -> str:
262+
"""_summary_
263+
264+
Args:
265+
msg (Optional[str], optional): _description_. Defaults to None.
266+
contact_id (Optional[str], optional): _description_. Defaults to None.
267+
room_id (Optional[str], optional): _description_. Defaults to None.
268+
269+
Returns:
270+
str: _description_
271+
"""
162272
if not msg:
163273
msg = str(uuid4())
164274
if not contact_id and not room_id:
@@ -183,6 +293,11 @@ def add_random_fake_room_message(self, msg: Optional[str] = None, contact_id: Op
183293
return message_payload.id
184294

185295
def add_random_fake_contact(self) -> str:
296+
"""_summary_
297+
298+
Returns:
299+
str: _description_
300+
"""
186301
payload = ContactPayload(
187302
id=str(uuid4()),
188303
name=str(uuid4()),
@@ -192,6 +307,11 @@ def add_random_fake_contact(self) -> str:
192307
return payload.id
193308

194309
def add_random_fake_room(self) -> str:
310+
"""_summary_
311+
312+
Returns:
313+
str: _description_
314+
"""
195315
contact_ids = random.choices(list(self._contact_payloads.keys()), k=5)
196316
payload = RoomPayload(
197317
id=str(uuid4()),
@@ -426,7 +546,7 @@ async def contact_alias(self, contact_id: str,
426546
contact.alias = alias
427547
self.add_fake_contact(contact)
428548
return alias
429-
549+
430550
return contact.alias
431551

432552
async def contact_payload_dirty(self, contact_id: str) -> None:

src/wechaty/user/url_link.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from wechaty.utils.link import get_url_metadata
1414

15-
from __future__ import annotations
1615
from dataclasses import dataclass
1716
from typing import Dict, List
1817
from abc import abstractmethod

0 commit comments

Comments
 (0)