-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway_helper.py
50 lines (35 loc) · 1.26 KB
/
gateway_helper.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
import json
import yaml
from flask import jsonify
def error_handler(code):
"""
Create a new Flask error handler dynamically. Returns the given error code and
a lambda function in a tuple which can be unpacked with * upon registering
:param: code int
:return: int, lambda
"""
return code, lambda e: (jsonify(error=str(e)), code)
def parse_known_rooms(rooms):
"""
Parse a known rooms string (token:[email protected]:nick [...]) to a Python dictionary
Keys are tokens, values are nested dicts of room JIDs and associated user nicks
:param: rooms string
:return: dict
"""
known_rooms = {}
for pairs in rooms.split(' '):
valid_token, valid_room, nick = pairs.split(':')
known_rooms[valid_token] = {'room': valid_room, 'nick': nick}
return known_rooms
def format_message(msg_format, request_json):
"""
Prepares the incoming request JSON object for the XMPP forward
:param: msg_format string
:param: request_json dict
:return: string
"""
if msg_format == 'json':
return json.dumps(request_json, indent=2) # ensure_ascii=False allows unicode chars
if msg_format == 'yaml':
return yaml.dump(request_json, indent=2, allow_unicode=True)
raise EnvironmentError