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

EventMetaClass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
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
EVENT_TYPES = {}
7
8
9
class EventMetaClass(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
                    EVENT_TYPES[_type] = cls
18
            else:
19
                EVENT_TYPES[attrs['__type__']] = cls
20
        type.__init__(cls, name, bases, attrs)
21
22
23
@six.add_metaclass(EventMetaClass)
24
class WeChatEvent(object):
25
    target = StringEntry('ToUserName')
26
    source = StringEntry('FromUserName')
27
    time = IntEntry('CreateTime')
28
29
    def __init__(self, message):
30
        self.__dict__.update(message)
31
32
33
class TicketEvent(WeChatEvent):
34
    key = StringEntry('EventKey')
35
    ticket = StringEntry('Ticket')
36
37
38
class SubscribeEvent(TicketEvent):
39
    __type__ = 'subscribe_event'
40
41
42
class UnSubscribeEvent(WeChatEvent):
43
    __type__ = 'unsubscribe_event'
44
45
46
class ScanEvent(TicketEvent):
47
    __type__ = 'scan_event'
48
49
50
class SimpleEvent(WeChatEvent):
51
    key = StringEntry('EventKey')
52
53
54
class ClickEvent(SimpleEvent):
55
    __type__ = 'click_event'
56
57
58
class ViewEvent(SimpleEvent):
59
    __type__ = 'view_event'
60
61
62
class LocationEvent(WeChatEvent):
63
    __type__ = 'location_event'
64
    latitude = FloatEntry('Latitude')
65
    longitude = FloatEntry('Longitude')
66
    precision = FloatEntry('Precision')
67
68
69
class UnknownEvent(WeChatEvent):
70
    __type__ = 'unknown_event'
71