Completed
Push — develop ( f3eb03...f601c5 )
by Wu
10s
created

MessageMetaClass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
c 3
b 1
f 0
dl 0
loc 12
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 8 4
A __new__() 0 2 1
1
# -*- coding: utf-8 -*-
2
3
import six
4
from werobot.messages.entries import StringEntry, IntEntry, FloatEntry
5
6
MESSAGE_TYPES = {}
7
8
9
class MessageMetaClass(type):
10
    def __new__(mcs, name, bases, attrs):
11
        return type.__new__(mcs, name, bases, attrs)
12
13
    def __init__(cls, name, bases, attrs):
14
        if '__type__' in attrs:
15
            if isinstance(attrs['__type__'], list):
16
                for _type in attrs['__type__']:
17
                    MESSAGE_TYPES[_type] = cls
18
            else:
19
                MESSAGE_TYPES[attrs['__type__']] = cls
20
        type.__init__(cls, name, bases, attrs)
21
22
23
@six.add_metaclass(MessageMetaClass)
24
class WeChatMessage(object):
25
    id = IntEntry('MsgId', 0)
26
    target = StringEntry('ToUserName')
27
    source = StringEntry('FromUserName')
28
    time = IntEntry('CreateTime', 0)
29
30
    def __init__(self, message):
31
        self.__dict__.update(message)
32
33
34
class TextMessage(WeChatMessage):
35
    __type__ = 'text'
36
    content = StringEntry('Content')
37
38
39
class ImageMessage(WeChatMessage):
40
    __type__ = 'image'
41
    img = StringEntry('PicUrl')
42
43
44
class LocationMessage(WeChatMessage):
45
    __type__ = 'location'
46
    location_x = FloatEntry('Location_X')
47
    location_y = FloatEntry('Location_Y')
48
    label = StringEntry('Label')
49
    scale = IntEntry('Scale')
50
51
    @property
52
    def location(self):
53
        return self.location_x, self.location_y
54
55
56
class LinkMessage(WeChatMessage):
57
    __type__ = 'link'
58
    title = StringEntry('Title')
59
    description = StringEntry('Description')
60
    url = StringEntry('Url')
61
62
63
class VoiceMessage(WeChatMessage):
64
    __type__ = 'voice'
65
    media_id = StringEntry('MediaId')
66
    format = StringEntry('Format')
67
    recognition = StringEntry('Recognition')
68
69
70
class VideoMessage(WeChatMessage):
71
    __type__ = ['video', 'shortvideo']
72
    media_id = StringEntry('MediaId')
73
    thumb_media_id = StringEntry('ThumbMediaId')
74
75
76
class UnknownMessage(WeChatMessage):
77
    __type__ = 'unknown'
78