Skip to content

Commit

Permalink
Merge pull request #1282 from OpenC3/python_script
Browse files Browse the repository at this point in the history
Python script runner api
  • Loading branch information
jmthomas authored May 23, 2024
2 parents 29ff60b + 819b673 commit 10c602c
Show file tree
Hide file tree
Showing 8 changed files with 538 additions and 123 deletions.
8 changes: 2 additions & 6 deletions openc3-cosmos-script-runner-api/scripts/running_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,12 +1323,8 @@ def load_utility(procedure_name):
not_cached = start(procedure_name)
finally:
RunningScript.instance.use_instrumentation = saved
else: # Just call require
# TODO
# importlib.import_module(module)
# importlib.reload(module)
# not_cached = require(procedure_name)
pass
else:
raise RuntimeError("load_utility not supported outside of Script Runner")
# Return whether we had to load and instrument this file, i.e. it was not cached
# This is designed to match the behavior of Ruby's require and load keywords
return not_cached
Expand Down
31 changes: 23 additions & 8 deletions openc3/python/openc3/io/json_api_object.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 OpenC3, Inc.
# Copyright 2024 OpenC3, Inc.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
Expand Down Expand Up @@ -44,7 +44,12 @@ class JsonApiObject:

USER_AGENT = "OpenC3 v5 (python/openc3/io/json_api_object)"

def __init__(self, url: str, timeout: float = 1.0, authentication: Optional[OpenC3Authentication] = None):
def __init__(
self,
url: str,
timeout: float = 1.0,
authentication: Optional[OpenC3Authentication] = None,
):
"""
Args:
url (str): The url of openc3-cosmos-cmd-tlm-api http://openc3-cosmos-cmd-tlm-api:2901
Expand Down Expand Up @@ -127,7 +132,9 @@ def _generate_scope(kwargs):
if not scope:
raise JsonApiError(f"no scope keyword found: {kwargs}")
elif type(scope) is not str:
raise JsonApiError(f"incorrect type for keyword 'scope' MUST be String: {scope}")
raise JsonApiError(
f"incorrect type for keyword 'scope' MUST be String: {scope}"
)
return scope

def _generate_headers(self, kwargs):
Expand All @@ -136,7 +143,9 @@ def _generate_headers(self, kwargs):
if not headers:
headers = kwargs["headers"] = {}
elif type(headers) is not dict:
raise JsonApiError(f"incorrect type for keyword 'headers' MUST be Dictionary: {headers}")
raise JsonApiError(
f"incorrect type for keyword 'headers' MUST be Dictionary: {headers}"
)

if "json" in kwargs and kwargs["json"]:
headers["Content-Type"] = "application/json"
Expand All @@ -157,7 +166,9 @@ def _generate_data(kwargs):
if not data:
data = kwargs["data"] = {}
elif type(data) is not dict and type(data) is not str:
raise JsonApiError(f"incorrect type for keyword 'data' MUST be Dictionary or String: {data}")
raise JsonApiError(
f"incorrect type for keyword 'data' MUST be Dictionary or String: {data}"
)
if "json" in kwargs and kwargs["json"]:
return json.dumps(kwargs["data"])
else:
Expand All @@ -170,7 +181,9 @@ def _generate_query(kwargs):
if query is None:
query = kwargs["query"] = {}
elif type(query) is not dict:
raise JsonApiError(f"incorrect type for keyword 'query' MUST be Dictionary: {query}")
raise JsonApiError(
f"incorrect type for keyword 'query' MUST be Dictionary: {query}"
)
if "scope" in kwargs and kwargs["scope"]:
kwargs["query"]["scope"] = kwargs["scope"]
return kwargs["query"]
Expand All @@ -181,11 +194,13 @@ def _send_request(self, method, endpoint, kwargs):
kwargs["url"] = f"{self.url}{endpoint}"
self.log[0] = f"{method} Request: {kwargs}"
resp = getattr(self.http, method)(**kwargs)
self.log[1] = f"{method} Response: {resp.status_code} {resp.headers} {resp.text}"
self.log[1] = (
f"{method} Response: {resp.status_code} {resp.headers} {resp.text}"
)
self.response_data = resp.text
return resp
except Exception as error:
self.log[2] = f"{method} Exception: {repr(error)}"
self.disconnect()
error = f"Api Exception: {self.log[0]} ::: {self.log[1]} ::: {self.log[2]}"
raise error
raise RuntimeError(error)
Loading

0 comments on commit 10c602c

Please sign in to comment.