Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add quick test #4

Merged
merged 6 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
overrides
flake8
mypy
mypy_extensions
Expand Down
83 changes: 83 additions & 0 deletions src/wechaty/accessory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
docstring
"""
from abc import ABCMeta
from typing import Optional
from wechaty_puppet.puppet import Puppet
from .config import LOG
from .wechaty import Wechaty


class Accessory:
"""
docstring
"""
__metaclass__ = ABCMeta
_puppet: Optional[Puppet] = None
# static _wechaty property to doing ...
_wechaty: Optional[Wechaty] = None
_counter: int = 0

def __init__(self, name: str = "accessory"):
"""
initialize the accessory instance
"""
self.name: str = name
# increase when Accessory is initialized
self._counter += 1

def __str__(self) -> str:
"""
docstring
:return: the base accessory class name
"""
return "Accessory instance : %s" % self.name

@classmethod
def puppet(cls, value: Optional[Puppet] = None) -> Optional[Puppet]:
"""
get/set global single instance of the puppet
:return:
"""
if value is None:
if cls._puppet is None:
raise AttributeError("static puppet instance not found ...")
LOG.info("get puppet instance %s ...",
cls._puppet.name)
return cls._puppet

if cls._puppet is not None:
raise AttributeError("can't set puppet instance %s twice" %
cls._puppet.name)
LOG.info("set puppet instance %s ...",
value.name)
cls._puppet = value
return None

@classmethod
def wechaty(cls, value: Optional[Wechaty] = None) -> Optional[Wechaty]:
"""
get/set wechaty instance

If the param of value is None, then the function will return the
instance of wechaty.Otherwise, the function will check the type
of the value, and set as wechaty instance
:param value:
:return:
"""
if value is None:
if cls._wechaty is None:
raise AttributeError("wechaty instance not found")
LOG.info("get wechaty instance %s",
cls._wechaty.name)
return cls._wechaty
if not isinstance(value, Wechaty):
raise NameError(
"expected wechaty instance type is Wechaty, "
"but got %s" % value.__class__
)
if cls._wechaty is not None:
raise AttributeError("can't set wechaty instance %s twice" %
cls._wechaty.name)
cls._wechaty = value
return None
121 changes: 112 additions & 9 deletions src/wechaty/config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
'''
config module
'''
"""
*
* Wechaty - https://github.com/wechaty/python-wechaty
*
* @copyright wechaty
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
"""
import logging
import os
import re
from typing import Optional
from wechaty_puppet.file_box import FileBox

logging.basicConfig(
filename="logging.log",
level=logging.DEBUG,
)

log = logging.getLogger('Wechaty')
LOG = logging.getLogger(__name__)

# log.debug('test logging debug')
# log.info('test logging info')
Expand All @@ -21,3 +36,91 @@
'../data',
),
)


def global_exception_handler(e: Exception) -> None:
"""
handle the global exception
:param e: exception message
:return:
"""
LOG.error("occur %s %s", e.__class__.__name__, str(e.args))
print(e)


class DefaultSetting(dict):
"""
store global default setting
"""
default_api_host: Optional[str] = None
default_port: Optional[int] = None
default_protocol: Optional[str] = None


# pylint: disable=R0903
def valid_api_host(api_host: str) -> bool:
"""
test the validation of the api_host
:param api_host:
:return:
"""
pattern = re.compile(
r'^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|:?[0-9]*'
r'([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|:?[0-9]*'
r'([a-zA-Z0-9][-_.a-zA-Z0-9]{0,61}[a-zA-Z0-9]))\.:?[0-9]*'
r'([a-zA-Z]{2,13}|[a-zA-Z0-9-]{2,30}.[a-zA-Z]{2,3}):?[0-9]*$'
)
return bool(pattern.match(api_host))


class Config:
"""
store python-wechaty configuration
"""
def __init__(self,
api_host: Optional[str] = None,
token: Optional[str] = None,
protocol: Optional[str] = None,
http_port: Optional[int] = None,
name: str = "python-wechaty",
debug: bool = True,
docker: bool = False):
"""
initialize the configuration
"""
self.default = DefaultSetting

self.api_host = api_host if api_host is not None \
else DefaultSetting.default_api_host

self.http_port = http_port if http_port is not None \
else DefaultSetting.default_port

self.protocol = protocol if protocol is not None \
else DefaultSetting.default_protocol

if token is None:
raise AttributeError("token can't be None")

self.name = name
self.debug = debug
self.docker = docker

if self.api_host is not None and not valid_api_host(self.api_host):
raise AttributeError("api host %s is not valid" % self.api_host)


# export const CHATIE_OFFICIAL_ACCOUNT_ID = 'gh_051c89260e5d'
chatie_official_account_id = "gh_051c89260e5d"


def qr_code_for_chatie() -> FileBox:
"""
create QRcode for chatie
:return:
"""
# const CHATIE_OFFICIAL_ACCOUNT_QRCODE =
# 'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'
chatie_official_account_qr_code: str = \
'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'
return FileBox.from_qr_code(chatie_official_account_qr_code)
14 changes: 7 additions & 7 deletions src/wechaty/config_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'''
"""
config unit test
'''
"""
from typing import (
Any,
# Dict,
Expand All @@ -9,7 +9,7 @@
import pytest # type: ignore

from .config import (
log,
LOG,
)

# pylint: disable=redefined-outer-name
Expand All @@ -25,14 +25,14 @@ def fixture_data() -> Iterable[str]:
def test_config(
data: Any,
) -> None:
'''
"""
Unit Test for config function
'''
"""
print(data)

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


def test_log():
'''test'''
assert log, 'log should exist'
"""test"""
assert LOG, 'log should exist'
88 changes: 88 additions & 0 deletions src/wechaty/images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""
docstring
"""
from enum import IntEnum
from typing import Type, TypeVar
from wechaty_puppet.file_box import FileBox
from .accessory import Accessory
from .config import LOG


class ImageType(IntEnum):
"""
docstring ...
"""
Thumbnail = 0
HD = 1
Artwork = 2


T = TypeVar("T", bound="Image")


class Image(Accessory):
"""
docstring ...
"""

def __str__(self):
return "image instance : %d" % self.image_id

def __init__(self, image_id: str) -> None:
"""
:param image_id:
"""
super(Image, self).__init__()
self.image_id = image_id
LOG.info("create image : %d", self.image_id)
if self.puppet is None:
raise NotImplementedError("Image class can not be instanced"
" without a puppet!")

@classmethod
def create(cls: Type[T], image_id: str) -> T:
"""
create image instance by image_id
:param cls:
:param image_id:
:return:
"""
LOG.info("create static image : %d", image_id)
return cls(image_id)

async def thumbnail(self) -> FileBox:
"""
docstring
:return:
"""
LOG.info("image thumbnail for %d", self.image_id)
puppet = self.puppet()
if puppet is None:
raise AttributeError
file_box = await puppet.message_image(self.image_id,
ImageType.Thumbnail)
return file_box

async def hd(self) -> FileBox:
"""
docstring
:return:
"""
LOG.info("image hd for %d", self.image_id)
puppet = self.puppet()
if puppet is None:
raise AttributeError
file_box = await puppet.message_image(self.image_id, ImageType.HD)
return file_box

async def artwork(self) -> FileBox:
"""
docstring
:return:
"""
LOG.info("image artwork for %d", self.image_id)
puppet = self.puppet()
if puppet is None:
raise AttributeError
file_box = await puppet.message_image(self.image_id, ImageType.Artwork)
return file_box
Empty file added src/wechaty/user/__init__.py
Empty file.
Empty file added src/wechaty/user/contact.py
Empty file.
5 changes: 5 additions & 0 deletions src/wechaty/user/room.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
python-implementation for room
"""
from threading import Event, Thread
from src.wechaty.accessory import Accessory
46 changes: 46 additions & 0 deletions src/wechaty/wechaty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
wechaty instance
"""
from typing import Optional
from .config import LOG


# pylint: disable=R0903
class WechatyOptions:
"""
WechatyOptions instance
"""
def __init__(self):
"""
WechatyOptions constructor
"""
self.io_token: str = None
self.name: str = None
self.profile: Optional[None or str] = None


# pylint: disable=R0903
class Wechaty:
"""
docstring
"""
def __init__(self, name: str = "wechaty"):
"""
docstring
"""
self.name = name

_global_instance: Optional["Wechaty"] = None

async def start(self) -> None:
"""
start the wechaty
:return:
"""
LOG.info("wechaty is starting ...")

async def stop(self) -> None:
"""
stop the wechaty
"""
LOG.info("wechaty is stoping ...")
Empty file added wechaty_puppet/__init__.py
Empty file.
Loading