forked from MeshAddicts/meshinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_renderer.py
More file actions
51 lines (39 loc) · 1.68 KB
/
data_renderer.py
File metadata and controls
51 lines (39 loc) · 1.68 KB
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
#!/usr/bin/env python3
import asyncio
import json
import logging
import os
from encoders import _JSONEncoder
class DataRenderer:
def __init__(self, config, data):
self.config = config
self.data = data
async def render(self):
await asyncio.to_thread(self._render)
def _render(self):
self.save_file("chat.json", self.data.chat)
logging.info(
f"Saved {len(self.data.chat['channels']['0']['messages'])} chat messages to file ({self.config['paths']['data']}/chat.json)")
nodes = {}
for id, node in self.data.nodes.items():
if id.startswith('!'):
id = id.replace('!', '')
if len(id) != 8: # 8 hex chars required, if not, we abandon it
continue
nodes[id] = node
self.save_file("nodes.json", nodes)
logging.info(
f"Saved {len(nodes)} nodes to file ({self.config['paths']['data']}/nodes.json)")
for filename, data, label in (
("telemetry.json", self.data.telemetry, 'telemetry'),
("traceroutes.json", self.data.traceroutes, 'traceroutes'),
):
self.save_file(filename, data)
logging.info(
f"Saved {len(data)} {label} to file ({self.config['paths']['data']}/{filename})")
def save_file(self, filename, data,):
logging.info(f"Saving {filename}")
with open(f"{self.config['paths']['data']}/{filename}.swp", "w", encoding='utf-8') as f:
json.dump(data, f, indent=2, sort_keys=True, cls=_JSONEncoder)
os.replace(f"{self.config['paths']['data']}/{filename}.swp",
f"{self.config['paths']['data']}/{filename}")