-
Notifications
You must be signed in to change notification settings - Fork 2
/
command.py_blzhang
84 lines (78 loc) · 3.44 KB
/
command.py_blzhang
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# coding: utf8
import ujson as json
import msgpack
import commands.login as login
import commands.get_notices as get_notices
import commands.publish_notice as publish_notice
import commands.get_notices as get_notices
import commands.publish_proposal as publish_proposal
import commands.get_proposal_list as get_proposal_list
import commands.add_comment as add_comment
import commands.del_comment as del_comment
import commands.get_comments as get_comments
class Command(object):
def __init__(self, command_list):
self.command_handles = []
#try:
# for command in command_list:
# self.command_handles.append(
# getattr(__import__()))
#except (KeyError, ImportError), exception_data:
# print "exception %r", (exception_data)
#TODO append function to dict then run the command
def handle(self, command):
handle_dict = {}
handle_dict['login'] = login
handle_dict['publish_notice'] = publish_notice
handle_dict['get_notices'] = get_notices
handle_dict['publish_proposal'] = publish_proposal
handle_dict['get_proposal_list'] = get_proposal_list
handle_dict['add_comment'] = add_comment
handle_dict['del_comment'] = del_comment
handle_dict['get_comments'] = get_comments
try:
command_dict = msgpack.loads(command)[0]
print command_dict
if "command" in command_dict:
#TODO auto import command file call the process function
if cmp(command_dict['command'], 'login') == 0:
print 'login'
reply = login.process(command_dict)
elif cmp(command_dict['command'], 'publish_notice') == 0:
print 'publish_notice'
reply = publish_notice.process(command_dict)
elif cmp(command_dict['command'], 'get_notices') == 0:
print 'get_notices'
reply = get_notices.process(command_dict)
elif cmp(command_dict['command'], 'publish_proposal') == 0:
print 'publish_proposal'
reply = publish_proposal.process(command_dict)
elif cmp(command_dict['command'], 'get_proposal_list') == 0:
print 'get_proposal_list'
reply = get_proposal_list.process(command_dict)
elif cmp(command_dict['command'], 'add_comment') == 0:
print 'add_comment'
reply = add_comment.process(command_dict)
elif cmp(command_dict['command'], 'del_comment') == 0:
print 'del_comment'
reply = del_comment.process(command_dict)
elif cmp(command_dict['command'], 'get_comments') == 0:
print 'get_comments'
reply = get_comments.process(command_dict)
else:
print command_dict['command']
return msgpack.dumps(reply)
else:
print "Unsupport command"
return msgpack.dumps({"result":False})
return msgpack.dumps({"result":False})
except Exception,e:
print e
return msgpack.dumps({"result":False})
def test_login():
test_data = {"command": "login", "user_name": "blzhang", "password": "123"}
json_result = json.dumps(test_data)
result = handle(json_result)
print result
def test():
test_login()