annotate libervia/pages/_browser/aio_bridge.py @ 1440:939b5c643f3d

doc: use VERSION file to get version in the same way as for backend
author Goffi <goffi@goffi.org>
date Fri, 18 Jun 2021 15:31:29 +0200
parents 72f9639594b2
children 106bae41f5c8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1347
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
1 from browser import window, aio
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
2 import javascript
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
3
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
4
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
5 class BridgeException(Exception):
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
6 """An exception which has been raised from the backend and arrived to the frontend."""
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
7
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
8 def __init__(self, name, message="", condition=""):
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
9 """
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
10
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
11 @param name (str): full exception class name (with module)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
12 @param message (str): error message
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
13 @param condition (str) : error condition
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
14 """
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
15 Exception.__init__(self)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
16 self.fullname = str(name)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
17 self.message = str(message)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
18 self.condition = str(condition) if condition else ""
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
19 self.module, __, self.classname = str(self.fullname).rpartition(".")
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
20
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
21 def __str__(self):
1394
72f9639594b2 browser (lists): code is now async + item removal for non creator:
Goffi <goffi@goffi.org>
parents: 1347
diff changeset
22 return f"{self.classname}: {self.message or ''}"
1347
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
23
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
24 def __eq__(self, other):
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
25 return self.classname == other
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
26
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
27
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
28 class Bridge:
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
29
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
30 def __getattr__(self, attr):
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
31 return lambda *args, **kwargs: self.call(attr, *args, **kwargs)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
32
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
33 async def call(self, method_name, *args, **kwargs):
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
34 data = javascript.JSON.stringify({
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
35 "args": args,
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "kwargs": kwargs,
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
37 })
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
38 url = f"/_bridge/{method_name}"
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
39 r = await aio.post(
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
40 url,
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
41 headers={
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
42 'X-Csrf-Token': window.csrf_token,
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
43 },
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
44 data=data,
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
45 )
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
46
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
47 if r.status == 200:
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
48 return javascript.JSON.parse(r.data)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
49 elif r.status == 502:
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
50 ret = javascript.JSON.parse(r.data)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
51 raise BridgeException(ret['fullname'], ret['message'], ret['condition'])
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
52 else:
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
53 print(f"bridge called failed: code: {r.status}, text: {r.statusText}")
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
54 raise BridgeException("InternalError", r.statusText)