-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdata_renderer.py
More file actions
41 lines (32 loc) · 1.53 KB
/
data_renderer.py
File metadata and controls
41 lines (32 loc) · 1.53 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
#!/usr/bin/env python3
import asyncio
import json
import logging
from encoders import _JSONEncoder
logger = logging.getLogger(__name__)
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)
logger.debug("Saved %d chat messages to file (%s/chat.json)", len(self.data.chat['channels']['0']['messages']), self.config['paths']['data'])
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)
logger.debug("Saved %d nodes to file (%s/nodes.json)", len(nodes), self.config['paths']['data'])
self.save_file("telemetry.json", self.data.telemetry)
logger.debug("Saved %d telemetry to file (%s/telemetry.json)", len(self.data.telemetry), self.config['paths']['data'])
self.save_file("traceroutes.json", self.data.traceroutes)
logger.debug("Saved %d traceroutes to file (%s/traceroutes.json)", len(self.data.traceroutes), self.config['paths']['data'])
def save_file(self, filename, data):
logger.debug("Saving %s", filename)
with open(f"{self.config['paths']['data']}/{filename}", "w", encoding='utf-8') as f:
json.dump(data, f, indent=2, sort_keys=True, cls=_JSONEncoder)