-
Notifications
You must be signed in to change notification settings - Fork 6
/
received_data.py
52 lines (37 loc) · 1.38 KB
/
received_data.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
51
52
from __future__ import annotations
import json
from typing import Dict
from dataclasses import field, dataclass
from .logger import get_logger
LOGGER = get_logger()
@dataclass
class ReceivedData:
"""Data received from the client.
Data is expected to be a json string with the following format:
{
"text": "Text to run in the script editor",
"file": "File name to show in the output (optional))"
"formatText": "0" or "1" To format the text or not. Defaults to "1" (True) (optional)
}
"""
raw: str
data: Dict[str, str] = field(init=False)
file: str = field(init=False)
text: str = field(init=False)
format_text: bool = field(init=False)
def __post_init__(self):
try:
self.data = json.loads(self.raw)
self.data.setdefault('file', '')
self.data.setdefault('formatText', '1')
except Exception as e:
LOGGER.error(
f'Nukeserversocket: An exception occurred while decoding the data. {e}'
)
self.data = {'text': '', 'file': '', 'formatText': '1'}
LOGGER.debug('Received data: %s', self.data)
self.text = self.data.get('text', '')
if not self.text:
LOGGER.critical('Data does not contain a text field.')
self.file = self.data['file']
self.format_text = bool(int(self.data['formatText']))