Skip to content

Commit

Permalink
Fix stdout forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Apr 7, 2024
1 parent b3a5c21 commit 304bfd6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
15 changes: 14 additions & 1 deletion flapi/server/capout.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# This is the module in FL Studio for some reason
from _io import StringIO, _TextIOBase as TextIOBase # type: ignore
try:
from typing import Optional, Callable
from typing import Optional, Callable, Self
except ImportError:
pass

Expand Down Expand Up @@ -113,6 +113,19 @@ def __init__(self, callback: 'Callable[[str], None]') -> None:
self.enabled = False
self.real_stdout = sys.stdout
self.fake_stdout = CapoutBuffer(callback)
self.target = 0

def __call__(self, target: int) -> Self:
self.target = target
return self

def __enter__(self) -> None:
self.enable()

def __exit__(self, exc_type, exc_val, exc_tb) -> None:
self.flush()
self.disable()
self.target = 0

def flush(self) -> None:
if self.enabled:
Expand Down
2 changes: 0 additions & 2 deletions flapi/server/device_flapi_respond.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ def OnSysEx(event: 'FlMidiMsg'):

# Ignore events that don't target the respond script
if header != SYSEX_HEADER:
print("Header no match")
return

# Check message origin
if sysex_data[0] != MessageOrigin.INTERNAL:
print("Origin")
return

# Forward message back to client
Expand Down
65 changes: 41 additions & 24 deletions flapi/server/flapi_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"""
import device
import pickle
from typing import Any, Literal, overload
import sys
from typing import Any, Literal, overload, Self
from base64 import b64encode, b64decode
from consts import SYSEX_HEADER, MessageOrigin, MessageType, MessageStatus

Expand All @@ -18,7 +19,7 @@ def send_sysex(msg: bytes):
"""
# capout.fl_print(f"MSG OUT -- {bytes_to_str(msg)}")
if device.dispatchReceiverCount() == 0:
print("ERROR: No response device found")
print("ERROR: No response device found", file=sys.stderr)
for i in range(device.dispatchReceiverCount()):
device.dispatch(i, 0xF0, msg)
# capout.fl_print("MSG OUT SUCCESS")
Expand Down Expand Up @@ -49,9 +50,18 @@ def __init__(self, client_id: int) -> None:
Create a FlapiResponse
"""
self.client_id = client_id
self.__messages: list[bytes] = []

def fail(self, type: MessageType, info: str):
send_sysex(
def send(self) -> None:
"""
Send the required messages
"""
for msg in self.__messages:
send_sysex(msg)
self.__messages.clear()

def fail(self, type: MessageType, info: str) -> Self:
self.__messages.append(
bytes([0xF0])
+ SYSEX_HEADER
+ bytes([MessageOrigin.INTERNAL])
Expand All @@ -61,9 +71,10 @@ def fail(self, type: MessageType, info: str):
+ b64encode(info.encode())
+ bytes([0xF7])
)
return self

def client_hello(self):
send_sysex(
def client_hello(self) -> Self:
self.__messages.append(
bytes([0xF0])
+ SYSEX_HEADER
+ bytes([MessageOrigin.INTERNAL])
Expand All @@ -72,23 +83,25 @@ def client_hello(self):
+ bytes([MessageStatus.OK])
+ bytes([0xF7])
)
return self

def client_goodbye(self, exit_code: int):
send_sysex(
def client_goodbye(self, exit_code: int) -> Self:
self.__messages.append(
bytes([0xF0])
+ SYSEX_HEADER
+ bytes([MessageOrigin.INTERNAL])
+ bytes([self.client_id])
+ bytes([MessageType.CLIENT_GOODBYE])
+ bytes([MessageStatus.OK])
+ encode_python_object(exit_code)
+ b64encode(str(exit_code).encode())
+ bytes([0xF7])
)
return self

# Server goodbye is handled externally in `device_flapi_respond.py`

def version_query(self, version_info: tuple[int, int, int]):
send_sysex(
def version_query(self, version_info: tuple[int, int, int]) -> Self:
self.__messages.append(
bytes([0xF0])
+ SYSEX_HEADER
+ bytes([MessageOrigin.INTERNAL])
Expand All @@ -98,17 +111,18 @@ def version_query(self, version_info: tuple[int, int, int]):
+ bytes(version_info)
+ bytes([0xF7])
)
return self

@overload
def exec(self, status: Literal[MessageStatus.OK]):
def exec(self, status: Literal[MessageStatus.OK]) -> Self:
...

@overload
def exec(
self,
status: Literal[MessageStatus.ERR],
exc_info: Exception,
):
) -> Self:
...
...

Expand All @@ -117,20 +131,20 @@ def exec(
self,
status: Literal[MessageStatus.FAIL],
exc_info: str,
):
) -> Self:
...

def exec(
self,
status: MessageStatus,
exc_info: Exception | str | None = None,
):
) -> Self:
if status != MessageStatus.OK:
response_data = encode_python_object(exc_info)
else:
response_data = bytes()

send_sysex(
self.__messages.append(
bytes([0xF0])
+ SYSEX_HEADER
+ bytes([MessageOrigin.INTERNAL])
Expand All @@ -140,21 +154,22 @@ def exec(
+ response_data
+ bytes([0xF7])
)
return self

@overload
def eval(
self,
status: Literal[MessageStatus.OK],
data: Any,
):
) -> Self:
...

@overload
def eval(
self,
status: Literal[MessageStatus.ERR],
data: Exception,
):
) -> Self:
...
...

Expand All @@ -163,15 +178,15 @@ def eval(
self,
status: Literal[MessageStatus.FAIL],
data: str,
):
) -> Self:
...

def eval(
self,
status: MessageStatus,
data: Exception | str | Any,
):
send_sysex(
) -> Self:
self.__messages.append(
bytes([0xF0])
+ SYSEX_HEADER
+ bytes([MessageOrigin.INTERNAL])
Expand All @@ -181,15 +196,17 @@ def eval(
+ encode_python_object(data)
+ bytes([0xF7])
)
return self

def stdout(self, content: str):
send_sysex(
def stdout(self, content: str) -> Self:
self.__messages.append(
bytes([0xF0])
+ SYSEX_HEADER
+ bytes([MessageOrigin.INTERNAL])
+ bytes([self.client_id])
+ bytes([MessageType.STDOUT])
+ bytes([MessageStatus.OK])
+ encode_python_object(content)
+ b64encode(content.encode())
+ bytes([0xF7])
)
return self

0 comments on commit 304bfd6

Please sign in to comment.