From 10254a157c3b2cc3d401370e0dac487adbab4dff Mon Sep 17 00:00:00 2001 From: Tim <53166177+TimMcCool@users.noreply.github.com> Date: Sat, 24 Sep 2022 14:35:34 +0200 Subject: [PATCH] formatted Signed-off-by: Tim <53166177+TimMcCool@users.noreply.github.com> --- scratchattach/_cloud_requests.py | 228 ++++++++++++++++++++++++------- 1 file changed, 179 insertions(+), 49 deletions(-) diff --git a/scratchattach/_cloud_requests.py b/scratchattach/_cloud_requests.py index 4f6b4f2c..89edfa01 100644 --- a/scratchattach/_cloud_requests.py +++ b/scratchattach/_cloud_requests.py @@ -1,4 +1,4 @@ -#----- The cloud request handler class +# ----- The cloud request handler class from . import _cloud import time from ._encoder import * @@ -7,19 +7,34 @@ import json import traceback -class CloudRequests: +class CloudRequests: class Request: def __init__(self, **entries): self.__dict__.update(entries) self.id = self.request_id - def __init__(self, cloud_connection : _cloud.CloudConnection, *, used_cloud_vars = ["1","2","3","4","5","6","7","8","9"], ignore_exceptions=True, force_reconnect=True, _log_url="https://clouddata.scratch.mit.edu/logs", _packet_length=245): - print("\033[1mIf you use CloudRequests in your Scratch project, please credit TimMcCool!\033[0m") + def __init__( + self, + cloud_connection: _cloud.CloudConnection, + *, + used_cloud_vars=["1", "2", "3", "4", "5", "6", "7", "8", "9"], + ignore_exceptions=True, + force_reconnect=True, + _log_url="https://clouddata.scratch.mit.edu/logs", + _packet_length=245, + ): + print( + "\033[1mIf you use CloudRequests in your Scratch project, please credit TimMcCool!\033[0m" + ) if _log_url != "https://clouddata.scratch.mit.edu/logs": - print("Warning: Log URL isn't the URL of Scratch's clouddata logs. Don't use the _log_url parameter unless you know what you are doing.") + print( + "Warning: Log URL isn't the URL of Scratch's clouddata logs. Don't use the _log_url parameter unless you know what you are doing." + ) if _packet_length > 245: - print("Warning: The packet length was set to a value higher than default (245). Your project most likely won't work on Scratch.") + print( + "Warning: The packet length was set to a value higher than default (245). Your project most likely won't work on Scratch." + ) self.used_cloud_vars = used_cloud_vars self.connection = cloud_connection self.project_id = cloud_connection.project_id @@ -36,7 +51,7 @@ def __init__(self, cloud_connection : _cloud.CloudConnection, *, used_cloud_vars self.request_parts = {} self.outputs = {} - self.respond_in_thread= False + self.respond_in_thread = False self.current_var = 0 self.idle_since = 0 @@ -44,12 +59,25 @@ def request(self, function=None, *, enabled=True, name=None, thread=False): def inner(function): if thread: self.respond_in_thread = True - print("Warning: Running individual requests in threads increases CPU usage") - self.requests[function.__name__ if name is None else name] = {"name":function.__name__ if name is None else name,"enabled":enabled,"on_call":function,"thread":thread} + print( + "Warning: Running individual requests in threads increases CPU usage" + ) + self.requests[function.__name__ if name is None else name] = { + "name": function.__name__ if name is None else name, + "enabled": enabled, + "on_call": function, + "thread": thread, + } + if function is None: return inner else: - self.requests[function.__name__] = {"name":function.__name__,"enabled":True,"on_call":function,"thread":False} + self.requests[function.__name__] = { + "name": function.__name__, + "enabled": True, + "on_call": function, + "thread": False, + } def call_request(self, request_id, req_obj, arguments): if not self.respond_in_thread: @@ -58,15 +86,40 @@ def call_request(self, request_id, req_obj, arguments): try: if not req_obj["enabled"]: print(f"Warning: Client received the disabled request '{request}'") - self.call_event("on_disabled_request", [self.Request(name=request,request=request,requester=self.last_requester,timestamp=self.last_timestamp,arguments=arguments,request_id=request_id)]) + self.call_event( + "on_disabled_request", + [ + self.Request( + name=request, + request=request, + requester=self.last_requester, + timestamp=self.last_timestamp, + arguments=arguments, + request_id=request_id, + ) + ], + ) return None output = req_obj["on_call"](*arguments) if req_obj["thread"]: - self.outputs[request_id] = {"output":output, "request":req_obj} + self.outputs[request_id] = {"output": output, "request": req_obj} else: self._parse_output(output, request, req_obj, request_id) except Exception as e: - self.call_event("on_error", [self.Request(name=request,request=request,requester=self.last_requester,timestamp=self.last_timestamp,arguments=arguments,request_id=request_id), e]) + self.call_event( + "on_error", + [ + self.Request( + name=request, + request=request, + requester=self.last_requester, + timestamp=self.last_timestamp, + arguments=arguments, + request_id=request_id, + ), + e, + ], + ) if self.ignore_exceptions: print(f"Caught error in request '{request}' - Full error below") try: @@ -75,11 +128,16 @@ def call_request(self, request_id, req_obj, arguments): print(e) else: print(f"Exception in request '{request}':") - raise(e) + raise (e) if req_obj["thread"]: - self.outputs[request_id] = {"output":f"Error: Check the Python console", "request":req_obj} + self.outputs[request_id] = { + "output": f"Error: Check the Python console", + "request": req_obj, + } else: - self._parse_output("Error: Check the Python console", request, req_obj, request_id) + self._parse_output( + "Error: Check the Python console", request, req_obj, request_id + ) if not self.respond_in_thread: self.cloud_events.resume() @@ -89,9 +147,11 @@ def add_request(self, function, *, enabled=True, name=None): def remove_request(self, name): self.requests.pop(name) - def edit_request(self, name, *, enabled=None, new_name=None, new_function=None, thread=None): + def edit_request( + self, name, *, enabled=None, new_name=None, new_function=None, thread=None + ): if name not in self.requests: - raise(_exceptions.RequestNotFound(name)) + raise (_exceptions.RequestNotFound(name)) if enabled is not None: self.requests[name]["enabled"] = enabled if new_name is not None: @@ -113,7 +173,9 @@ def get_timestamp(self): return self.last_timestamp - def _respond(self, request_id, response, limit, *, force_reconnect=False, validation=2222): + def _respond( + self, request_id, response, limit, *, force_reconnect=False, validation=2222 + ): if self.idle_since + 8 < time.time() or force_reconnect: self.connection._connect(cloud_host=self.connection.cloud_host) @@ -121,28 +183,33 @@ def _respond(self, request_id, response, limit, *, force_reconnect=False, valida remaining_response = str(response) - i = 0 while not remaining_response == "": if len(remaining_response) > limit: response_part = remaining_response[:limit] remaining_response = remaining_response[limit:] - i+=1 + i += 1 if i > 99: iteration_string = str(i) elif i > 9: - iteration_string = "0"+str(i) + iteration_string = "0" + str(i) else: - iteration_string = "00"+str(i) + iteration_string = "00" + str(i) - self.connection.set_var(f"FROM_HOST_{self.used_cloud_vars[self.current_var]}", f"{response_part}.{request_id}{iteration_string}1") + self.connection.set_var( + f"FROM_HOST_{self.used_cloud_vars[self.current_var]}", + f"{response_part}.{request_id}{iteration_string}1", + ) self.current_var += 1 if self.current_var == len(self.used_cloud_vars): self.current_var = 0 time.sleep(0.1) else: - self.connection.set_var(f"FROM_HOST_{self.used_cloud_vars[self.current_var]}", f"{remaining_response}.{request_id}{validation}") + self.connection.set_var( + f"FROM_HOST_{self.used_cloud_vars[self.current_var]}", + f"{remaining_response}.{request_id}{validation}", + ) self.current_var += 1 if self.current_var == len(self.used_cloud_vars): self.current_var = 0 @@ -154,11 +221,22 @@ def _respond(self, request_id, response, limit, *, force_reconnect=False, valida def run(self, thread=False, data_from_websocket=False): if data_from_websocket is True: - events = _cloud.WsCloudEvents(self.project_id, _cloud.CloudConnection(project_id = self.project_id, username = self.connection._username, session_id=self.connection._session_id)) + events = _cloud.WsCloudEvents( + self.project_id, + _cloud.CloudConnection( + project_id=self.project_id, + username=self.connection._username, + session_id=self.connection._session_id, + ), + ) else: events = _cloud.CloudEvents(self.project_id) if thread: - thread = Thread(target=self._run, args=[events], kwargs={"data_from_websocket":data_from_websocket}) + thread = Thread( + target=self._run, + args=[events], + kwargs={"data_from_websocket": data_from_websocket}, + ) thread.start() else: self._run(events, data_from_websocket=data_from_websocket) @@ -173,7 +251,9 @@ def call_event(self, event, args=[]): def _parse_output(self, output, request, req_obj, request_id): if len(str(output)) > 3000 and not self.data_from_websocket: - print(f"Warning: Output of request '{request}' is longer than 3000 characters (length: {len(str(output))} characters). Responding the request will take >4 seconds.") + print( + f"Warning: Output of request '{request}' is longer than 3000 characters (length: {len(str(output))} characters). Responding the request will take >4 seconds." + ) if str(request_id).endswith("0"): try: @@ -185,7 +265,6 @@ def _parse_output(self, output, request, req_obj, request_id): else: send_as_integer = False - if output is None: print(f"Warning: Request '{request}' didn't return anything.") return @@ -206,7 +285,6 @@ def _parse_output(self, output, request, req_obj, request_id): else: self._respond(request_id, output, self.packet_length) - def _run(self, events, data_from_websocket=False): self.ws_data = [] self.data_from_websocket = data_from_websocket @@ -215,23 +293,33 @@ def _run(self, events, data_from_websocket=False): @self.cloud_events.event def on_set(event): if event.name == "TO_HOST": - self.ws_data.insert(0, {"user":event.user,"value":event.value,"timestamp":event.timestamp}) + self.ws_data.insert( + 0, + { + "user": event.user, + "value": event.value, + "timestamp": event.timestamp, + }, + ) self.ws_data = self.ws_data[:100] self.cloud_events.start(update_interval=0) - self.connection._connect(cloud_host=self.connection.cloud_host) self.connection._handshake() self.idle_since = time.time() if data_from_websocket: self.last_data = [] else: - self.last_data = _cloud.get_cloud_logs(self.project_id, limit=100, log_url=self.log_url) + self.last_data = _cloud.get_cloud_logs( + self.project_id, limit=100, log_url=self.log_url + ) self.last_timestamp = 0 if not data_from_websocket: - data = _cloud.get_cloud_logs(self.project_id, limit=100, log_url=self.log_url) + data = _cloud.get_cloud_logs( + self.project_id, limit=100, log_url=self.log_url + ) if data == []: pass else: @@ -252,17 +340,16 @@ def on_set(event): pass else: for activity in data: - if activity['timestamp'] > self.last_timestamp: + if activity["timestamp"] > self.last_timestamp: self.last_requester = activity["user"] - self.last_timestamp = activity['timestamp'] + self.last_timestamp = activity["timestamp"] try: raw_request, request_id = activity["value"].split(".") except Exception: - self.last_timestamp = activity['timestamp'] + self.last_timestamp = activity["timestamp"] continue - if activity["value"][0] == "-": if not request_id in self.request_parts: self.request_parts[request_id] = [] @@ -280,17 +367,47 @@ def on_set(event): request = Encoding.decode(raw_request) arguments = request.split("&") request = arguments.pop(0) - self.call_event("on_request", [self.Request(name=request,request=request,requester=self.last_requester,timestamp=self.last_timestamp,arguments=arguments,request_id=request_id,id=request_id)]) + self.call_event( + "on_request", + [ + self.Request( + name=request, + request=request, + requester=self.last_requester, + timestamp=self.last_timestamp, + arguments=arguments, + request_id=request_id, + id=request_id, + ) + ], + ) output = "" if request not in self.requests: - print(f"Warning: Client received an unknown request called '{request}'") - self.call_event("on_unknown_request", [self.Request(name=request,request=request,requester=self.last_requester,timestamp=self.last_timestamp,arguments=arguments,request_id=request_id)]) + print( + f"Warning: Client received an unknown request called '{request}'" + ) + self.call_event( + "on_unknown_request", + [ + self.Request( + name=request, + request=request, + requester=self.last_requester, + timestamp=self.last_timestamp, + arguments=arguments, + request_id=request_id, + ) + ], + ) continue else: req_obj = self.requests[request] if req_obj["thread"]: - Thread(target=self.call_request, args=(request_id, req_obj, arguments)).start() + Thread( + target=self.call_request, + args=(request_id, req_obj, arguments), + ).start() else: self.call_request(request_id, req_obj, arguments) self.last_data = data @@ -303,12 +420,24 @@ def on_set(event): req_obj = self.outputs[request_id]["request"] self._parse_output(output, request, req_obj, request_id) -class TwCloudRequests(CloudRequests): - def __init__(self, cloud_connection, *, used_cloud_vars = ["1","2","3","4","5","6","7","8","9"], ignore_exceptions=True, force_reconnect=True, _packet_length=98800): - print("\033[1mIf you use CloudRequests in your Scratch project, please credit TimMcCool!\033[0m") +class TwCloudRequests(CloudRequests): + def __init__( + self, + cloud_connection, + *, + used_cloud_vars=["1", "2", "3", "4", "5", "6", "7", "8", "9"], + ignore_exceptions=True, + force_reconnect=True, + _packet_length=98800, + ): + print( + "\033[1mIf you use CloudRequests in your Scratch project, please credit TimMcCool!\033[0m" + ) if _packet_length > 98800: - print("Warning: The packet length was set to a value higher than TurboWarp's default (98800).") + print( + "Warning: The packet length was set to a value higher than TurboWarp's default (98800)." + ) self.used_cloud_vars = used_cloud_vars self.connection = cloud_connection self.project_id = cloud_connection.project_id @@ -324,18 +453,19 @@ def __init__(self, cloud_connection, *, used_cloud_vars = ["1","2","3","4","5"," self.request_parts = {} self.outputs = {} - self.respond_in_thread= False + self.respond_in_thread = False self.current_var = 0 self.idle_since = 0 - def get_requester(self): return None def run(self, thread=False, data_from_websocket=True): events = _cloud.TwCloudEvents(self.project_id) if thread: - thread = Thread(target=self._run, args=[events], kwargs={"data_from_websocket":True}) + thread = Thread( + target=self._run, args=[events], kwargs={"data_from_websocket": True} + ) thread.start() else: self._run(events, data_from_websocket=True)