-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapi.py
62 lines (45 loc) · 1.42 KB
/
api.py
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
from __future__ import absolute_import
import functools
import logging
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer as JSONServer
import jsonrpclib
import manager.song
import config
jsonrpclib.config.use_jsonclass = False
def eat_exceptions(func):
@functools.wraps(func)
def wrap(*args, **kwargs):
try:
return func(*args, **kwargs)
except:
logging.exception("API exception, none cares")
return None
return wrap
def clientify(method):
name = method.__name__
print "clientifying:", name
@eat_exceptions
@functools.wraps(method)
def wrap(*args):
server = jsonrpclib.Server(config.irc_url)
print "calling:", name, args
return getattr(server, name)(*args)
wrap.server_side = method
return wrap
@clientify
def request_announce(session, song):
if not hasattr(song, "id"):
song = manager.song.Song(id=song)
session.request_announce(song)
@clientify
def announce(session):
session.announce()
def run_rpc_server(config, session):
funcs = [request_announce, announce]
server = JSONServer((config.host, config.port),
encoding="utf8", logRequests=False)
for f in funcs:
wrapped = functools.wraps(f.server_side)
wrapped = wrapped(functools.partial(f.server_side, session))
server.register_function(wrapped)
server.serve_forever()