Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python script runner api #1282

Merged
merged 8 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
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 @@ -1322,12 +1322,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
12 changes: 6 additions & 6 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 @@ -43,7 +43,7 @@ class JsonApiObject:
# attr_reader :request_data
# attr_reader :response_data

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

# @param url [String] The url of openc3-cosmos-cmd-tlm-api http://openc3-cosmos-cmd-tlm-api:2901
# @param timeout [Float] The time to wait before disconnecting 1.0
Expand Down Expand Up @@ -187,13 +187,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)
2 changes: 1 addition & 1 deletion openc3/python/openc3/packets/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def template(self, template):
# self.return [Array<String>] Warning messages for bit definition overlaps
def check_bit_offsets(self):
if self.ignore_overlap:
Logger.info(
Logger.debug(
f"{self.target_name} {self.packet_name} has IGNORE_OVERLAP so bit overlaps ignored"
)
return []
Expand Down
10 changes: 7 additions & 3 deletions openc3/python/openc3/script/__init__.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 All @@ -15,10 +15,11 @@
# if purchased from OpenC3, Inc.

from openc3.api import WHITELIST
from openc3.script.server_proxy import ServerProxy
from openc3.script.server_proxy import ApiServerProxy, ScriptServerProxy
from openc3.utilities.extract import convert_to_value

API_SERVER = ServerProxy()
API_SERVER = ApiServerProxy()
SCRIPT_RUNNER_API_SERVER = ScriptServerProxy()
RUNNING_SCRIPT = None
DISCONNECT = False
OPENC3_IN_CLUSTER = False
Expand All @@ -29,6 +30,8 @@
def shutdown_script():
global API_SERVER
API_SERVER.shutdown()
global SCRIPT_RUNNER_API_SERVER
SCRIPT_RUNNER_API_SERVER.shutdown()


def prompt_for_hazardous(target_name, cmd_name, hazardous_description):
Expand Down Expand Up @@ -141,6 +144,7 @@ def prompt(
from .telemetry import *
from .metadata import *
from .screen import *
from .script_runner import *
from .storage import *

# Define all the WHITELIST methods
Expand Down
65 changes: 19 additions & 46 deletions openc3/python/openc3/script/api_shared.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 Ball Aerospace & Technologies Corp.
# Copyright 2024 OpenC3, Inc.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
Expand All @@ -11,10 +11,6 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.

# Modified by OpenC3, Inc.
# All changes Copyright 2023, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.

Expand Down Expand Up @@ -590,47 +586,24 @@ def get_max_output():
if openc3.script.RUNNING_SCRIPT:
return openc3.script.RUNNING_SCRIPT.max_output_characters

###########################################################################
# Scripts Outside of ScriptRunner Support
# ScriptRunner overrides these methods to work in the OpenC3 cluster
# They are only here to allow for scripts to have a chance to work
# unaltered outside of the cluster
###########################################################################

# TODO:
# def start(procedure_name)
# cached = false
# begin
# Kernel::load(procedure_name)
# rescue LoadError => error
# raise LoadError, f"Error loading -- {procedure_name}\n{error.message}"
# end
# # Return whether we had to load and instrument this file, i.e. it was not cached
# !cached
# end

# # Require an additional ruby file
# def load_utility(procedure_name)
# return start(procedure_name)
# end
# def require_utility(procedure_name)
# # Ensure require_utility works like require where you don't need the .rb extension
# if File.extname(procedure_name) != '.rb'
# procedure_name += '.rb'
# end
# @require_utility_cache ||= {}
# if @require_utility_cache[procedure_name]
# return false
# else
# @require_utility_cache[procedure_name] = true
# begin
# return start(procedure_name)
# rescue LoadError
# @require_utility_cache[procedure_name] = false
# raise # reraise the error
# end
# end
# end

###########################################################################
# Scripts Outside of ScriptRunner Support
# ScriptRunner overrides these methods to work in the OpenC3 cluster
# They are only here to allow for scripts to have a chance to work
# unaltered outside of the cluster
###########################################################################


# Exec a procedure
def start(procedure_name):
with open(procedure_name) as f:
exec(f.read())


# Require an additional python file
def load_utility(procedure_name):
raise RuntimeError("load_utility not supported outside of Script Runner")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

load_utility and require_utility need to work differently in Python.

If I require_utility or load_utility I should expect to have new methods to call. (like the collect() utility method from collect.py)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supported not support



###########################################################################
Expand Down
Loading
Loading