Skip to content

Commit

Permalink
misc: added more docstrings, types
Browse files Browse the repository at this point in the history
test: changed test to new execution
  • Loading branch information
WinPlay02 committed Nov 21, 2023
1 parent 5d6cda5 commit 6887865
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
5 changes: 3 additions & 2 deletions src/safeds_runner/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import logging
import typing
from typing import Any

import simple_websocket
Expand Down Expand Up @@ -97,7 +98,7 @@ def send_websocket_value(connection: simple_websocket.Server, name: str, var_typ
send_websocket_message(connection, "value", {"name": name, "type": var_type, "value": value})


def send_websocket_message(connection: simple_websocket.Server, msg_type: str, msg_data) -> None:
def send_websocket_message(connection: simple_websocket.Server, msg_type: str, msg_data: typing.Any) -> None:
message = {"type": msg_type, "data": msg_data}
connection.send(json.dumps(message))

Expand All @@ -107,7 +108,7 @@ def send_websocket_message(connection: simple_websocket.Server, msg_type: str, m
import functools
import builtins

builtins.print = functools.partial(print, flush=True)
builtins.print = functools.partial(print, flush=True) # type: ignore[assignment]

logging.getLogger().setLevel(logging.DEBUG)
from gevent.pywsgi import WSGIServer
Expand Down
2 changes: 1 addition & 1 deletion src/safeds_runner/server/module_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, code: dict[str, dict[str, str]]):
key = key.rpartition(".")[0]
self.allowed_packages.add(key)

def find_spec(self, fullname: str, path: typing.Sequence[bytes | str] = None,
def find_spec(self, fullname: str, path: typing.Sequence[str] | None = None,
target: types.ModuleType | None = None) -> ModuleSpec | None:
logging.debug("Find Spec: %s %s %s", fullname, path, target)
if fullname in self.allowed_packages:
Expand Down
26 changes: 15 additions & 11 deletions src/safeds_runner/server/pipeline_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ def setup_pipeline_execution() -> None:


def _handle_queue_messages() -> None:
global websocket_target
while True:
message = global_messages_queue.get()
if websocket_target is not None:
websocket_target.send(json.dumps(message))
global websocket_target, global_messages_queue
try:
while global_messages_queue is not None:
message = global_messages_queue.get()
if websocket_target is not None:
websocket_target.send(json.dumps(message))
except BaseException as error:
logging.warn("Message queue terminated: %s", error.__repr__())


def set_new_websocket_target(ws: simple_websocket.Server) -> None:
Expand Down Expand Up @@ -138,14 +141,15 @@ def execute_pipeline(code: dict[str, dict[str, str]], sdspackage: str, sdsmodule
:param exec_id: Unique ID to identify this execution
"""
global multiprocessing_manager, global_messages_queue, global_placeholder_map
if exec_id not in global_placeholder_map:
global_placeholder_map[exec_id] = multiprocessing_manager.dict()
process = PipelineProcess(code, sdspackage, sdsmodule, sdspipeline, exec_id, global_messages_queue,
global_placeholder_map[exec_id])
process.execute()
if global_placeholder_map is not None and global_messages_queue is not None:
if exec_id not in global_placeholder_map:
global_placeholder_map[exec_id] = multiprocessing_manager.dict()
process = PipelineProcess(code, sdspackage, sdsmodule, sdspipeline, exec_id, global_messages_queue,
global_placeholder_map[exec_id])
process.execute()


def _get_placeholder_type(value: typing.Any):
def _get_placeholder_type(value: typing.Any) -> str:
"""
:param value: any python object
:return: Safe-DS name corresponding to the given python object instance
Expand Down
17 changes: 6 additions & 11 deletions tests/safeds_runner/server/test_execute_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
from safeds_runner.server.main import execute_pipeline
from safeds_runner.server.pipeline_manager import setup_pipeline_execution


def test_execute_pipeline() -> None:
context_globals = {}
context_locals = {}
result = execute_pipeline({
setup_pipeline_execution()
execute_pipeline({
"": {
"gen_b": "import safeds_runner.codegen\n\ndef c():\n\ta1 = 1\n\ta2 = safeds_runner.codegen.eager_or(True, False)\n\tprint('test')\n\tprint('dynamic pipeline output')\n\treturn a1 + a2\n",
"gen_b_c": "from gen_b import c\n\nif __name__ == '__main__':\n\tc()"}
}, "a.test", "b", "c", context_globals)
}, "a.test", "b", "c", "test-id")

context_globals = {}
context_locals = {}

result = execute_pipeline({
execute_pipeline({
"": {
"gen_b": "import safeds_runner.codegen\nfrom a.stub import u\nfrom v.u.s.testing import add1\n\ndef c():\n\ta1 = 1\n\ta2 = safeds_runner.codegen.eager_or(True, False)\n\tprint('test2')\n\tprint('new dynamic output')\n\tprint(f'Add1: {add1(1, 2)}')\n\treturn a1 + a2\n",
"gen_b_c": "from gen_b import c\n\nif __name__ == '__main__':\n\tc()"
Expand All @@ -24,6 +21,4 @@ def test_execute_pipeline() -> None:
"v.u.s": {
"testing": "import a.stub;\n\ndef add1(v1, v2):\n\treturn v1 + v2 + a.stub.u()\n"
}
}, "a.test", "b", "c", context_globals)
# // __name__ is not __main__; Therefore the main script is not executed; use pyrun to execute main script (loading main script just like any other script is acceptable)
# exec("from a.b import c", context_globals, context_locals)
}, "a.test", "b", "c", "test-id2")

0 comments on commit 6887865

Please sign in to comment.