Skip to content

Commit ab2a8ec

Browse files
authored
Merge pull request #4 from wj-Mcat/master
add quick test
2 parents 514c835 + e87dd30 commit ab2a8ec

File tree

12 files changed

+410
-16
lines changed

12 files changed

+410
-16
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
overrides
12
flake8
23
mypy
34
mypy_extensions

src/wechaty/accessory.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
docstring
3+
"""
4+
from abc import ABCMeta
5+
from typing import Optional
6+
from wechaty_puppet.puppet import Puppet
7+
from .config import LOG
8+
from .wechaty import Wechaty
9+
10+
11+
class Accessory:
12+
"""
13+
docstring
14+
"""
15+
__metaclass__ = ABCMeta
16+
_puppet: Optional[Puppet] = None
17+
# static _wechaty property to doing ...
18+
_wechaty: Optional[Wechaty] = None
19+
_counter: int = 0
20+
21+
def __init__(self, name: str = "accessory"):
22+
"""
23+
initialize the accessory instance
24+
"""
25+
self.name: str = name
26+
# increase when Accessory is initialized
27+
self._counter += 1
28+
29+
def __str__(self) -> str:
30+
"""
31+
docstring
32+
:return: the base accessory class name
33+
"""
34+
return "Accessory instance : %s" % self.name
35+
36+
@classmethod
37+
def puppet(cls, value: Optional[Puppet] = None) -> Optional[Puppet]:
38+
"""
39+
get/set global single instance of the puppet
40+
:return:
41+
"""
42+
if value is None:
43+
if cls._puppet is None:
44+
raise AttributeError("static puppet instance not found ...")
45+
LOG.info("get puppet instance %s ...",
46+
cls._puppet.name)
47+
return cls._puppet
48+
49+
if cls._puppet is not None:
50+
raise AttributeError("can't set puppet instance %s twice" %
51+
cls._puppet.name)
52+
LOG.info("set puppet instance %s ...",
53+
value.name)
54+
cls._puppet = value
55+
return None
56+
57+
@classmethod
58+
def wechaty(cls, value: Optional[Wechaty] = None) -> Optional[Wechaty]:
59+
"""
60+
get/set wechaty instance
61+
62+
If the param of value is None, then the function will return the
63+
instance of wechaty.Otherwise, the function will check the type
64+
of the value, and set as wechaty instance
65+
:param value:
66+
:return:
67+
"""
68+
if value is None:
69+
if cls._wechaty is None:
70+
raise AttributeError("wechaty instance not found")
71+
LOG.info("get wechaty instance %s",
72+
cls._wechaty.name)
73+
return cls._wechaty
74+
if not isinstance(value, Wechaty):
75+
raise NameError(
76+
"expected wechaty instance type is Wechaty, "
77+
"but got %s" % value.__class__
78+
)
79+
if cls._wechaty is not None:
80+
raise AttributeError("can't set wechaty instance %s twice" %
81+
cls._wechaty.name)
82+
cls._wechaty = value
83+
return None

src/wechaty/config.py

Lines changed: 112 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1-
'''
2-
config module
3-
'''
1+
"""
2+
*
3+
* Wechaty - https://github.com/wechaty/python-wechaty
4+
*
5+
* @copyright wechaty
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*
20+
"""
421
import logging
522
import os
23+
import re
24+
from typing import Optional
25+
from wechaty_puppet.file_box import FileBox
626

7-
logging.basicConfig(
8-
filename="logging.log",
9-
level=logging.DEBUG,
10-
)
11-
12-
log = logging.getLogger('Wechaty')
27+
LOG = logging.getLogger(__name__)
1328

1429
# log.debug('test logging debug')
1530
# log.info('test logging info')
@@ -21,3 +36,91 @@
2136
'../data',
2237
),
2338
)
39+
40+
41+
def global_exception_handler(e: Exception) -> None:
42+
"""
43+
handle the global exception
44+
:param e: exception message
45+
:return:
46+
"""
47+
LOG.error("occur %s %s", e.__class__.__name__, str(e.args))
48+
print(e)
49+
50+
51+
class DefaultSetting(dict):
52+
"""
53+
store global default setting
54+
"""
55+
default_api_host: Optional[str] = None
56+
default_port: Optional[int] = None
57+
default_protocol: Optional[str] = None
58+
59+
60+
# pylint: disable=R0903
61+
def valid_api_host(api_host: str) -> bool:
62+
"""
63+
test the validation of the api_host
64+
:param api_host:
65+
:return:
66+
"""
67+
pattern = re.compile(
68+
r'^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|:?[0-9]*'
69+
r'([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|:?[0-9]*'
70+
r'([a-zA-Z0-9][-_.a-zA-Z0-9]{0,61}[a-zA-Z0-9]))\.:?[0-9]*'
71+
r'([a-zA-Z]{2,13}|[a-zA-Z0-9-]{2,30}.[a-zA-Z]{2,3}):?[0-9]*$'
72+
)
73+
return bool(pattern.match(api_host))
74+
75+
76+
class Config:
77+
"""
78+
store python-wechaty configuration
79+
"""
80+
def __init__(self,
81+
api_host: Optional[str] = None,
82+
token: Optional[str] = None,
83+
protocol: Optional[str] = None,
84+
http_port: Optional[int] = None,
85+
name: str = "python-wechaty",
86+
debug: bool = True,
87+
docker: bool = False):
88+
"""
89+
initialize the configuration
90+
"""
91+
self.default = DefaultSetting
92+
93+
self.api_host = api_host if api_host is not None \
94+
else DefaultSetting.default_api_host
95+
96+
self.http_port = http_port if http_port is not None \
97+
else DefaultSetting.default_port
98+
99+
self.protocol = protocol if protocol is not None \
100+
else DefaultSetting.default_protocol
101+
102+
if token is None:
103+
raise AttributeError("token can't be None")
104+
105+
self.name = name
106+
self.debug = debug
107+
self.docker = docker
108+
109+
if self.api_host is not None and not valid_api_host(self.api_host):
110+
raise AttributeError("api host %s is not valid" % self.api_host)
111+
112+
113+
# export const CHATIE_OFFICIAL_ACCOUNT_ID = 'gh_051c89260e5d'
114+
chatie_official_account_id = "gh_051c89260e5d"
115+
116+
117+
def qr_code_for_chatie() -> FileBox:
118+
"""
119+
create QRcode for chatie
120+
:return:
121+
"""
122+
# const CHATIE_OFFICIAL_ACCOUNT_QRCODE =
123+
# 'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'
124+
chatie_official_account_qr_code: str = \
125+
'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'
126+
return FileBox.from_qr_code(chatie_official_account_qr_code)

src/wechaty/config_test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
'''
1+
"""
22
config unit test
3-
'''
3+
"""
44
from typing import (
55
Any,
66
# Dict,
@@ -9,7 +9,7 @@
99
import pytest # type: ignore
1010

1111
from .config import (
12-
log,
12+
LOG,
1313
)
1414

1515
# pylint: disable=redefined-outer-name
@@ -25,14 +25,14 @@ def fixture_data() -> Iterable[str]:
2525
def test_config(
2626
data: Any,
2727
) -> None:
28-
'''
28+
"""
2929
Unit Test for config function
30-
'''
30+
"""
3131
print(data)
3232

3333
assert data == 'test', 'data should equals test'
3434

3535

3636
def test_log():
37-
'''test'''
38-
assert log, 'log should exist'
37+
"""test"""
38+
assert LOG, 'log should exist'

src/wechaty/images.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
docstring
3+
"""
4+
from enum import IntEnum
5+
from typing import Type, TypeVar
6+
from wechaty_puppet.file_box import FileBox
7+
from .accessory import Accessory
8+
from .config import LOG
9+
10+
11+
class ImageType(IntEnum):
12+
"""
13+
docstring ...
14+
"""
15+
Thumbnail = 0
16+
HD = 1
17+
Artwork = 2
18+
19+
20+
T = TypeVar("T", bound="Image")
21+
22+
23+
class Image(Accessory):
24+
"""
25+
docstring ...
26+
"""
27+
28+
def __str__(self):
29+
return "image instance : %d" % self.image_id
30+
31+
def __init__(self, image_id: str) -> None:
32+
"""
33+
:param image_id:
34+
"""
35+
super(Image, self).__init__()
36+
self.image_id = image_id
37+
LOG.info("create image : %d", self.image_id)
38+
if self.puppet is None:
39+
raise NotImplementedError("Image class can not be instanced"
40+
" without a puppet!")
41+
42+
@classmethod
43+
def create(cls: Type[T], image_id: str) -> T:
44+
"""
45+
create image instance by image_id
46+
:param cls:
47+
:param image_id:
48+
:return:
49+
"""
50+
LOG.info("create static image : %d", image_id)
51+
return cls(image_id)
52+
53+
async def thumbnail(self) -> FileBox:
54+
"""
55+
docstring
56+
:return:
57+
"""
58+
LOG.info("image thumbnail for %d", self.image_id)
59+
puppet = self.puppet()
60+
if puppet is None:
61+
raise AttributeError
62+
file_box = await puppet.message_image(self.image_id,
63+
ImageType.Thumbnail)
64+
return file_box
65+
66+
async def hd(self) -> FileBox:
67+
"""
68+
docstring
69+
:return:
70+
"""
71+
LOG.info("image hd for %d", self.image_id)
72+
puppet = self.puppet()
73+
if puppet is None:
74+
raise AttributeError
75+
file_box = await puppet.message_image(self.image_id, ImageType.HD)
76+
return file_box
77+
78+
async def artwork(self) -> FileBox:
79+
"""
80+
docstring
81+
:return:
82+
"""
83+
LOG.info("image artwork for %d", self.image_id)
84+
puppet = self.puppet()
85+
if puppet is None:
86+
raise AttributeError
87+
file_box = await puppet.message_image(self.image_id, ImageType.Artwork)
88+
return file_box

src/wechaty/user/__init__.py

Whitespace-only changes.

src/wechaty/user/contact.py

Whitespace-only changes.

src/wechaty/user/room.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""
2+
python-implementation for room
3+
"""
4+
from threading import Event, Thread
5+
from src.wechaty.accessory import Accessory

src/wechaty/wechaty.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
wechaty instance
3+
"""
4+
from typing import Optional
5+
from .config import LOG
6+
7+
8+
# pylint: disable=R0903
9+
class WechatyOptions:
10+
"""
11+
WechatyOptions instance
12+
"""
13+
def __init__(self):
14+
"""
15+
WechatyOptions constructor
16+
"""
17+
self.io_token: str = None
18+
self.name: str = None
19+
self.profile: Optional[None or str] = None
20+
21+
22+
# pylint: disable=R0903
23+
class Wechaty:
24+
"""
25+
docstring
26+
"""
27+
def __init__(self, name: str = "wechaty"):
28+
"""
29+
docstring
30+
"""
31+
self.name = name
32+
33+
_global_instance: Optional["Wechaty"] = None
34+
35+
async def start(self) -> None:
36+
"""
37+
start the wechaty
38+
:return:
39+
"""
40+
LOG.info("wechaty is starting ...")
41+
42+
async def stop(self) -> None:
43+
"""
44+
stop the wechaty
45+
"""
46+
LOG.info("wechaty is stoping ...")

wechaty_puppet/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)