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

Allow python upload/download in TableManager #1052

Merged
merged 2 commits into from
Jan 17, 2024
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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -16,4 +18,4 @@
# cmd("TGT", "UPLOAD", "DATA" => buffer[i...(i + buf_size)])
# i += buf_size
# end
file.delete
file.close()
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ export default {
showError: false,
errorTitle: '',
errorText: '',
uploadScript: false,
downloadScript: false,
uploadScript: null,
downloadScript: null,
scriptBackground: true,
}
},
Expand Down Expand Up @@ -388,35 +388,62 @@ 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
'Ignore-Errors': '404',
},
})
.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
'Ignore-Errors': '404',
},
})
.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
})
})
},
},
Expand Down Expand Up @@ -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 }],
},
Expand All @@ -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 }],
},
Expand Down
4 changes: 3 additions & 1 deletion openc3/python/openc3/script/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 is False:
part += "_modified"
# Loop to allow redo when switching from modified to original
while True:
Expand Down Expand Up @@ -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
Expand Down
Loading