From 65179ca55cf91cb354e5f3807bd24ee4664bcbc7 Mon Sep 17 00:00:00 2001 From: Jason Thomas Date: Sat, 13 Jan 2024 22:08:04 -0700 Subject: [PATCH 1/2] Allow python upload/download --- .../procedures/{download.rb => download.py} | 8 ++- .../procedures/{upload.rb => upload.py} | 12 ++-- .../src/tools/TableManager/TableManager.vue | 58 +++++++++++++------ openc3/python/openc3/script/storage.py | 4 +- 4 files changed, 55 insertions(+), 27 deletions(-) rename openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/{download.rb => download.py} (80%) rename openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/{upload.rb => upload.py} (75%) diff --git a/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/download.rb b/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/download.py similarity index 80% rename from openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/download.rb rename to openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/download.py index 7e0cdc1aee..3d5481dd09 100644 --- a/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/download.rb +++ b/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/download.py @@ -1,9 +1,11 @@ +import os + # TBL_FILENAME is set to the name of the table file to overwrite -puts "file:#{ENV['TBL_FILENAME']}" +print(f"file:{os.environ['TBL_FILENAME']}") # Download the file # Implement custom commanding logic to download the table # You probably want to do something like: -buffer = '' +buffer = "" # i = 1 # num_segments = 5 # calculate based on TBL_FILENAME # table_id = 1 # calculate based on TBL_FILENAME @@ -13,4 +15,4 @@ # buffer += tlm("TGT DUMP_PKT DATA") # i += 1 # end -put_target_file(ENV['TBL_FILENAME'], buffer) +put_target_file(os.environ["TBL_FILENAME"], buffer) diff --git a/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/upload.rb b/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/upload.py similarity index 75% rename from openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/upload.rb rename to openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/upload.py index 9a9f70e69c..13bc79c376 100644 --- a/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/upload.rb +++ b/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST2/tables/procedures/upload.py @@ -1,9 +1,11 @@ +import os +from openc3.utilities.string import formatted + # TBL_FILENAME is set to the name of the table file -puts "file:#{ENV['TBL_FILENAME']}" +print(f"file:{os.environ['TBL_FILENAME']}") # Open the file -file = get_target_file(ENV['TBL_FILENAME']) -buffer = file.read -# puts buffer.formatted +file = get_target_file(os.environ["TBL_FILENAME"]) +buffer = file.read() # Implement custom commanding logic to upload the table # Note that buffer is a Ruby string of bytes # You probably want to do something like: @@ -16,4 +18,4 @@ # cmd("TGT", "UPLOAD", "DATA" => buffer[i...(i + buf_size)]) # i += buf_size # end -file.delete +file.close() diff --git a/openc3-cosmos-init/plugins/packages/openc3-cosmos-tool-tablemanager/src/tools/TableManager/TableManager.vue b/openc3-cosmos-init/plugins/packages/openc3-cosmos-tool-tablemanager/src/tools/TableManager/TableManager.vue index 6a127bdfb6..1f006bd61f 100644 --- a/openc3-cosmos-init/plugins/packages/openc3-cosmos-tool-tablemanager/src/tools/TableManager/TableManager.vue +++ b/openc3-cosmos-init/plugins/packages/openc3-cosmos-tool-tablemanager/src/tools/TableManager/TableManager.vue @@ -311,8 +311,8 @@ export default { showError: false, errorTitle: '', errorText: '', - uploadScript: false, - downloadScript: false, + uploadScript: null, + downloadScript: null, scriptBackground: true, } }, @@ -388,11 +388,11 @@ export default { // Everytime the filename changes we figure out if there is an associated upload & download script filename: function (val) { let upload = - this.filename.split('/').slice(0, 2).join('/') + '/procedures/upload.rb' + this.filename.split('/').slice(0, 2).join('/') + '/procedures/upload' let download = - this.filename.split('/').slice(0, 2).join('/') + - '/procedures/download.rb' - Api.get(`/openc3-api/tables/${upload}`, { + this.filename.split('/').slice(0, 2).join('/') + '/procedures/download' + // First try Ruby + Api.get(`/openc3-api/tables/${upload}.rb`, { headers: { Accept: 'application/json', // Since we're just checking for existance, 404 is possible so ignore it @@ -400,12 +400,26 @@ export default { }, }) .then((response) => { - this.uploadScript = true + this.uploadScript = `${upload}.rb` }) .catch((error) => { - this.uploadScript = false + // Now try python + Api.get(`/openc3-api/tables/${upload}.py`, { + headers: { + Accept: 'application/json', + // Since we're just checking for existance, 404 is possible so ignore it + 'Ignore-Errors': '404', + }, + }) + .then((response) => { + this.uploadScript = `${upload}.py` + }) + .catch((error) => { + this.uploadScript = null + }) }) - Api.get(`/openc3-api/tables/${download}`, { + // First check Ruby + Api.get(`/openc3-api/tables/${download}.rb`, { headers: { Accept: 'application/json', // Since we're just checking for existance, 404 is possible so ignore it @@ -413,10 +427,23 @@ export default { }, }) .then((response) => { - this.downloadScript = true + this.downloadScript = `${download}.rb` }) .catch((error) => { - this.downloadScript = false + // Now try python + Api.get(`/openc3-api/tables/${download}.py`, { + headers: { + Accept: 'application/json', + // Since we're just checking for existance, 404 is possible so ignore it + 'Ignore-Errors': '404', + }, + }) + .then((response) => { + this.downloadScript = `${download}.py` + }) + .catch((error) => { + this.downloadScript = null + }) }) }, }, @@ -595,9 +622,7 @@ export default { }) }, upload() { - let upload = - this.filename.split('/').slice(0, 2).join('/') + '/procedures/upload.rb' - Api.post(`/script-api/scripts/${upload}/run`, { + Api.post(`/script-api/scripts/${this.uploadScript}/run`, { data: { environment: [{ key: 'TBL_FILENAME', value: this.filename }], }, @@ -619,10 +644,7 @@ export default { }, ) .then(() => { - let download = - this.filename.split('/').slice(0, 2).join('/') + - '/procedures/download.rb' - Api.post(`/script-api/scripts/${download}/run`, { + Api.post(`/script-api/scripts/${this.downloadScript}/run`, { data: { environment: [{ key: 'TBL_FILENAME', value: this.filename }], }, diff --git a/openc3/python/openc3/script/storage.py b/openc3/python/openc3/script/storage.py index 0ed30f231f..5241d9276e 100644 --- a/openc3/python/openc3/script/storage.py +++ b/openc3/python/openc3/script/storage.py @@ -96,7 +96,7 @@ def put_target_file(path, io_or_string, scope=OPENC3_SCOPE): # @return [File|None] def get_target_file(path, original=False, scope=OPENC3_SCOPE): part = "targets" - if not original: + if original == False: part += "_modified" # Loop to allow redo when switching from modified to original while True: @@ -134,6 +134,8 @@ def _get_storage_file(path, scope=OPENC3_SCOPE): # Try to get the file uri = _get_uri(result["url"]) response = requests.get(uri) + if response.status_code == 404: + raise RuntimeError(f"File not found: {scope}/{path}") file.write(response.text) file.seek(0) return file From 93e91cd9d7697ee1efcd132476eccdb953aa17c9 Mon Sep 17 00:00:00 2001 From: Jason Thomas Date: Sun, 14 Jan 2024 09:07:17 -0700 Subject: [PATCH 2/2] Fix ruff --- openc3/python/openc3/script/storage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openc3/python/openc3/script/storage.py b/openc3/python/openc3/script/storage.py index 5241d9276e..e054c42295 100644 --- a/openc3/python/openc3/script/storage.py +++ b/openc3/python/openc3/script/storage.py @@ -96,7 +96,7 @@ def put_target_file(path, io_or_string, scope=OPENC3_SCOPE): # @return [File|None] def get_target_file(path, original=False, scope=OPENC3_SCOPE): part = "targets" - if original == False: + if original is False: part += "_modified" # Loop to allow redo when switching from modified to original while True: