Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Unimplemented Methods PR #341

Merged
merged 5 commits into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ def update_state_with_device_name(state, device_name):
return updated_state


def create_message(state):
message = {"type": "state", "data": json.dumps(state)}
def create_message(msg, send_type="state"):
if isinstance(msg, dict):
msg = json.dumps(msg)

message = {"type": send_type, "data": msg}
return message


Expand All @@ -41,6 +44,13 @@ def send_to_simulator(state, device_name):
time.sleep(CONSTANTS.TIME_DELAY)


def send_print_to_simulator(raw_msg):
data_str = str(raw_msg)
message = create_message(data_str, "print")
print(json.dumps(message) + "\0", file=sys.__stdout__, flush=True)
time.sleep(CONSTANTS.TIME_DELAY)


def remove_leading_slashes(string):
string = string.lstrip("\\/")
return string
Expand All @@ -52,14 +62,6 @@ def escape_if_OSX(file_name):
return file_name


def print_for_unimplemented_functions(function_name, one_more_call=False):
# Frame 0 is this function call
# Frame 1 is the call that calls this function, which is a microbit function
# Frame 2 is the call that calls the microbit function, which is in the user's file
# If one_more_call is True, then there is another frame between what was originally supposed to be frame 1 and 2.
frame_no = 2 if not one_more_call else 3
line_number = sys._getframe(frame_no).f_lineno
user_file_name = sys._getframe(frame_no).f_code.co_filename
print(
f"'{function_name}' on line {line_number} in {user_file_name} is not implemented in the simulator but it will work on the actual device!"
)
def print_for_unimplemented_functions(function_name):
msg = f"'{function_name}' is not implemented in the simulator but it will work on the actual device!\n"
send_print_to_simulator(msg)
8 changes: 2 additions & 6 deletions src/micropython/microbit/__model/microbit_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ def __init__(self):

def panic(self, n):
# Due to the shim, there is another call frame.
utils.print_for_unimplemented_functions(
MicrobitModel.panic.__name__, one_more_call=True
)
utils.print_for_unimplemented_functions(MicrobitModel.panic.__name__)

def reset(self):
# Due to the shim, there is another call frame.
utils.print_for_unimplemented_functions(
MicrobitModel.reset.__name__, one_more_call=True
)
utils.print_for_unimplemented_functions(MicrobitModel.reset.__name__)

def sleep(self, n):
time.sleep(n / 1000)
Expand Down