Skip to content

Commit

Permalink
Refactor received_data.py to use "formatText" instead of "noReply"
Browse files Browse the repository at this point in the history
  • Loading branch information
sisoe24 committed Dec 27, 2023
1 parent ae61620 commit 91a90e1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion nukeserversocket/editor_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def run(self, data: ReceivedData) -> str:

if (
not self.settings.get('mirror_script_editor') or
data.no_reply
not data.format_text
):
LOGGER.debug('Restoring script editor.')
self.input_editor.setPlainText(initial_input)
Expand Down
17 changes: 8 additions & 9 deletions nukeserversocket/received_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,29 @@ class ReceivedData:
{
"text": "Text to run in the script editor",
"file": "File name to show in the output (optional))"
"noReply": "0" or "1" To disable output in the script editor (optional)"
"formatText": "0" or "1" To format the text or not. Defaults to "1" (True) (optional)
}
Raises:
ValueError: If the data is not a valid json string or if it does not contain a `text` field.
"""

raw: str

data: Dict[str, str] = field(init=False)
file: str = field(init=False)
text: str = field(init=False)
no_reply: bool = 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('noReply', '0')
self.data.setdefault('formatText', '1')
except Exception as e:
LOGGER.error(f'An exception occurred while decoding the data. {e}')
self.data = {'text': '', 'file': '', 'noReply': '0'}
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)

Expand All @@ -50,4 +49,4 @@ def __post_init__(self):
LOGGER.critical('Data does not contain a text field.')

self.file = self.data['file']
self.no_reply = bool(int(self.data['noReply']))
self.format_text = bool(int(self.data['formatText']))
24 changes: 12 additions & 12 deletions tests/test_received_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,44 @@ class ReceivedTestData:
data: Dict[str, str]
text: str
file: str
no_reply: bool = False
format_text: bool = False


@pytest.mark.parametrize('data', [
ReceivedTestData(
'{"text": "Hello World", "file": "test.py", "noReply": "1"}',
{'text': 'Hello World', 'file': 'test.py', 'noReply': '1'},
'{"text": "Hello World", "file": "test.py", "formatText": "1"}',
{'text': 'Hello World', 'file': 'test.py', 'formatText': '1'},
'Hello World',
'test.py',
True
),
ReceivedTestData(
'{"text": "Hello World", "file": "test.py", "noReply": "0"}',
{'text': 'Hello World', 'file': 'test.py', 'noReply': '0'},
'{"text": "Hello World", "file": "test.py", "formatText": "0"}',
{'text': 'Hello World', 'file': 'test.py', 'formatText': '0'},
'Hello World',
'test.py',
False
),
ReceivedTestData(
'{"text": "Hello World", "file": ""}',
{'text': 'Hello World', 'file': '', 'noReply': '0'},
{'text': 'Hello World', 'file': '', 'formatText': '1'},
'Hello World',
'',
False
True
),
ReceivedTestData(
'{"text": "Hello World"}',
{'text': 'Hello World', 'file': '', 'noReply': '0'},
{'text': 'Hello World', 'file': '', 'formatText': '1'},
'Hello World',
'',
False
True
),
ReceivedTestData(
'{"text": "",',
{'text': '', 'file': '', 'noReply': '0'},
{'text': '', 'file': '', 'formatText': '1'},
'',
'',
False
True
)
])
Expand All @@ -62,4 +62,4 @@ def test_received_data(data: ReceivedTestData):
assert received.data == data.data
assert received.text == data.text
assert received.file == data.file
assert received.no_reply == data.no_reply
assert received.format_text == data.format_text

0 comments on commit 91a90e1

Please sign in to comment.