valar 0.0.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of valar might be problematic. Click here for more details.
- module/__init__.py +0 -0
- module/channels.py +135 -0
- valar-0.0.5.dist-info/LICENSE +19 -0
- valar-0.0.5.dist-info/METADATA +19 -0
- valar-0.0.5.dist-info/RECORD +7 -0
- valar-0.0.5.dist-info/WHEEL +5 -0
- valar-0.0.5.dist-info/top_level.txt +1 -0
module/__init__.py
ADDED
|
File without changes
|
module/channels.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import importlib
|
|
3
|
+
import json
|
|
4
|
+
from datetime import datetime
|
|
5
|
+
|
|
6
|
+
from channels.generic.websocket import AsyncJsonWebsocketConsumer
|
|
7
|
+
from channels.layers import get_channel_layer
|
|
8
|
+
from django.core.exceptions import ImproperlyConfigured
|
|
9
|
+
from django.http import JsonResponse
|
|
10
|
+
from django.conf import settings
|
|
11
|
+
|
|
12
|
+
GROUP = 'VALAR'
|
|
13
|
+
|
|
14
|
+
class ValarSocketSender:
|
|
15
|
+
def __init__(self, data: dict, handler: str, message = None, code = None):
|
|
16
|
+
self.data = data
|
|
17
|
+
self.handler = handler
|
|
18
|
+
self.send = get_channel_layer().group_send
|
|
19
|
+
self.message = message
|
|
20
|
+
self.code = code
|
|
21
|
+
|
|
22
|
+
def __convert_body(self, emit, clients = None, users = None):
|
|
23
|
+
return {
|
|
24
|
+
'type': emit,
|
|
25
|
+
'data': {
|
|
26
|
+
'handler': self.handler,
|
|
27
|
+
'payload': self.data,
|
|
28
|
+
'message': self.message,
|
|
29
|
+
'type': self.code,
|
|
30
|
+
'timestamp': datetime.now().timestamp()
|
|
31
|
+
},
|
|
32
|
+
'clients': clients or [],
|
|
33
|
+
'users': users or [],
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async def set_message(self, message, code):
|
|
37
|
+
self.message = message
|
|
38
|
+
self.code = code
|
|
39
|
+
|
|
40
|
+
async def to_users(self, *users):
|
|
41
|
+
body = self.__convert_body('user.emit', users=users)
|
|
42
|
+
await self.send(GROUP, body)
|
|
43
|
+
|
|
44
|
+
async def to_clients(self, *clients):
|
|
45
|
+
body = self.__convert_body('client.emit', clients=clients)
|
|
46
|
+
await self.send(GROUP, body)
|
|
47
|
+
|
|
48
|
+
async def broadcast(self):
|
|
49
|
+
body = self.__convert_body('broadcast.emit')
|
|
50
|
+
await self.send(GROUP, body)
|
|
51
|
+
|
|
52
|
+
async def register(self, client, uid):
|
|
53
|
+
body = self.__convert_body('register.emit', [client], [uid])
|
|
54
|
+
await self.send(GROUP, body)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class ValarConsumer(AsyncJsonWebsocketConsumer):
|
|
58
|
+
|
|
59
|
+
def __init__(self):
|
|
60
|
+
self.client = None
|
|
61
|
+
self.uid = None
|
|
62
|
+
super().__init__()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
async def connect(self):
|
|
66
|
+
params = self.scope['url_route']['kwargs']
|
|
67
|
+
self.client = params.get('client')
|
|
68
|
+
await self.channel_layer.group_add(GROUP, self.channel_name)
|
|
69
|
+
await self.accept()
|
|
70
|
+
|
|
71
|
+
async def disconnect(self, code):
|
|
72
|
+
await self.channel_layer.group_discard(GROUP, self.channel_name)
|
|
73
|
+
await self.close(code)
|
|
74
|
+
|
|
75
|
+
async def receive_json(self, data, *args, **kwargs):
|
|
76
|
+
print(data)
|
|
77
|
+
pass
|
|
78
|
+
|
|
79
|
+
async def user_emit(self, event):
|
|
80
|
+
users: list = event.get('users',[])
|
|
81
|
+
data = event.get('data',{})
|
|
82
|
+
if self.uid in users:
|
|
83
|
+
await self.send_json(data)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
async def client_emit(self, event):
|
|
87
|
+
clients: list = event.get('clients',[])
|
|
88
|
+
data = event.get('data',{})
|
|
89
|
+
if self.client in clients:
|
|
90
|
+
await self.send_json(data)
|
|
91
|
+
|
|
92
|
+
async def broadcast_emit(self, event):
|
|
93
|
+
data = event.get('data',{})
|
|
94
|
+
await self.send_json(data)
|
|
95
|
+
|
|
96
|
+
async def register_emit(self, event):
|
|
97
|
+
users: list = event.get('users', [])
|
|
98
|
+
clients: list = event.get('clients',[])
|
|
99
|
+
if self.client in clients:
|
|
100
|
+
self.uid = users[0]
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def get_valar_channel_handler(handler):
|
|
105
|
+
try:
|
|
106
|
+
root = settings.VALAR_CHANNEL_HANDLER_MAPPING
|
|
107
|
+
path, name = root.rsplit(".", 1)
|
|
108
|
+
except (ValueError, AttributeError):
|
|
109
|
+
raise ImproperlyConfigured("Cannot find VALAR_CHANNEL_HANDLER_MAPPING setting.")
|
|
110
|
+
try:
|
|
111
|
+
module = importlib.import_module(path)
|
|
112
|
+
mapping = getattr(module, name)
|
|
113
|
+
except ImportError:
|
|
114
|
+
raise ImproperlyConfigured("Cannot import VALAR_CHANNEL_HANDLER_MAPPING module %r" % path)
|
|
115
|
+
except AttributeError:
|
|
116
|
+
raise ImproperlyConfigured("module %r has no attribute %r" % (path, name))
|
|
117
|
+
try:
|
|
118
|
+
method = mapping[handler]
|
|
119
|
+
except KeyError:
|
|
120
|
+
raise ImproperlyConfigured("Cannot find handler in %r" % root)
|
|
121
|
+
return method
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
async def socket(request, handler):
|
|
127
|
+
client = request.headers.get('CLIENT')
|
|
128
|
+
uid = request.session.get('UID')
|
|
129
|
+
data = json.loads(request.body)
|
|
130
|
+
method = get_valar_channel_handler(handler)
|
|
131
|
+
loop = asyncio.get_event_loop()
|
|
132
|
+
loop.create_task(method(data, handler, client, uid))
|
|
133
|
+
return JsonResponse({'status':'OK'}, safe=False)
|
|
134
|
+
|
|
135
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2018 The Python Packaging Authority
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: valar
|
|
3
|
+
Version: 0.0.5
|
|
4
|
+
Summary: valar for morghulis
|
|
5
|
+
Author: LYP
|
|
6
|
+
Author-email: liuyinpeng@buaa.edu.cn
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: channels==3.0.3
|
|
11
|
+
Dynamic: author
|
|
12
|
+
Dynamic: author-email
|
|
13
|
+
Dynamic: description
|
|
14
|
+
Dynamic: description-content-type
|
|
15
|
+
Dynamic: requires-dist
|
|
16
|
+
Dynamic: requires-python
|
|
17
|
+
Dynamic: summary
|
|
18
|
+
|
|
19
|
+
valar for morghulis
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
module/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
module/channels.py,sha256=zkON_7qUDZayMxcOmFchhZUlFNsG4BLAS8ibr7WiPKg,4127
|
|
3
|
+
valar-0.0.5.dist-info/LICENSE,sha256=2bm9uFabQZ3Ykb_SaSU_uUbAj2-htc6WJQmS_65qD00,1073
|
|
4
|
+
valar-0.0.5.dist-info/METADATA,sha256=1GWsyg2uuE_8r7xzt4HjF9BSzjygdTJ_KlEeSaPJn04,422
|
|
5
|
+
valar-0.0.5.dist-info/WHEEL,sha256=jB7zZ3N9hIM9adW7qlTAyycLYW9npaWKLRzaoVcLKcM,91
|
|
6
|
+
valar-0.0.5.dist-info/top_level.txt,sha256=rCVQUTc7DucbyvV1vRKPlp8MRkSTuDqKf-ZCJsPhQHc,7
|
|
7
|
+
valar-0.0.5.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module
|