Skip to content

Commit

Permalink
Improve examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthomas committed Dec 28, 2023
1 parent d5d743e commit 9066965
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 16 deletions.
5 changes: 5 additions & 0 deletions openc3/lib/openc3/accessors/accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.

require 'json'

module OpenC3
class Accessor
attr_accessor :packet
Expand Down Expand Up @@ -97,18 +99,21 @@ def self.convert_to_type(value, item)
# Do nothing for complex object types
when :STRING, :BLOCK
if item.array_size
value = JSON.parse(value) if value.is_a? String
value = value.map(&:to_s)
else
value = value.to_s
end
when :UINT, :INT
if item.array_size
value = JSON.parse(value) if value.is_a? String
value = value.map(&:to_i)
else
value = value.to_i
end
when :FLOAT
if item.array_size
value = JSON.parse(value) if value.is_a? String
value = value.map(&:to_f)
else
value = value.to_f
Expand Down
3 changes: 3 additions & 0 deletions openc3/python/openc3/accessors/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ def convert_to_type(cls, value, item):
case "STRING" | "BLOCK":
if item.array_size:
if isinstance(value, str):
# Thought about using json.loads here but it doesn't
# support basic examples like "[2.2, '3', 4]"
# Seems it's pretty strict about quotes and escaping
value = literal_eval(value)
value = [str(x) for x in value]
else:
Expand Down
27 changes: 23 additions & 4 deletions openc3/python/test/accessors/test_html_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,17 @@ def test_should_read_a_collection_of_items(self):
item3 = self.Html("ITEM3", "/html/body/img/@src", "STRING", None)
item4 = self.Html("ITEM4", "/html/body/ul/li[1]/text()", "UINT", None)
item5 = self.Html("ITEM5", "/html/body/ul/li[2]/text()", "FLOAT", None)
item6 = self.Html("ITEM6", "/html/body/ul/li[3]/text()", "FLOAT", 24)

items = [item1, item2, item3, item4, item5]
items = [item1, item2, item3, item4, item5, item6]

results = HtmlAccessor.class_read_items(items, self.data1)
self.assertEqual(results["ITEM1"], "test.js")
self.assertEqual(results["ITEM2"], "No Script Detected")
self.assertEqual(results["ITEM3"], "test.jpg")
self.assertEqual(results["ITEM4"], 1)
self.assertEqual(results["ITEM5"], 3.14)
self.assertEqual(results["ITEM6"], [1.1, 2.2, 3.3])

def test_should_write_different_types(self):
item = self.Html("ITEM", "/html/head/script/@src", "STRING", None)
Expand All @@ -103,15 +105,29 @@ def test_should_write_different_types(self):
HtmlAccessor.class_write_item(item, 1.234, self.data1)
self.assertEqual(HtmlAccessor.class_read_item(item, self.data1), 1.234)

item = self.Html("ITEM", "/html/body/ul/li[3]/text()", "FLOAT", 24)
HtmlAccessor.class_write_item(item, ["2.2", 3, 4.4], self.data1)
self.assertEqual(
HtmlAccessor.class_read_item(item, self.data1), [2.2, 3.0, 4.4]
)

def test_should_write_multiple_items(self):
item1 = self.Html("ITEM1", "/html/head/script/@src", "STRING", None)
item2 = self.Html("ITEM2", "/html/body/noscript/text()", "STRING", None)
item3 = self.Html("ITEM3", "/html/body/img/@src", "STRING", None)
item4 = self.Html("ITEM4", "/html/body/ul/li[1]/text()", "UINT", None)
item5 = self.Html("ITEM5", "/html/body/ul/li[2]/text()", "FLOAT", None)

items = [item1, item2, item3, item4, item5]
values = ["different.js", "Nothing Here", "other.png", 15, 1.234]
item6 = self.Html("ITEM6", "/html/body/ul/li[3]/text()", "FLOAT", 24)

items = [item1, item2, item3, item4, item5, item6]
values = [
"different.js",
"Nothing Here",
"other.png",
15,
1.234,
[2.2, 3.3, 4.4],
]
HtmlAccessor.class_write_items(items, values, self.data1)

self.assertEqual(
Expand All @@ -123,3 +139,6 @@ def test_should_write_multiple_items(self):
self.assertEqual(HtmlAccessor.class_read_item(item3, self.data1), "other.png")
self.assertEqual(HtmlAccessor.class_read_item(item4, self.data1), 15)
self.assertEqual(HtmlAccessor.class_read_item(item5, self.data1), 1.234)
self.assertEqual(
HtmlAccessor.class_read_item(item6, self.data1), [2.2, 3.3, 4.4]
)
14 changes: 11 additions & 3 deletions openc3/python/test/accessors/test_xml_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ def test_should_read_a_collection_of_items(self):
item3 = self.Xml("ITEM3", "/html/body/img/@src", "STRING", None)
item4 = self.Xml("ITEM4", "/html/body/p/ul/li[1]/text()", "UINT", None)
item5 = self.Xml("ITEM5", "/html/body/p/ul/li[2]/text()", "FLOAT", None)
item6 = self.Xml("ITEM6", "/html/body/p/ul/li[3]/text()", "FLOAT", 24)

items = [item1, item2, item3, item4, item5]
items = [item1, item2, item3, item4, item5, item6]

results = XmlAccessor.class_read_items(items, self.data1)
self.assertEqual(results["ITEM1"], "test.js")
self.assertEqual(results["ITEM2"], "No Script Detected")
self.assertEqual(results["ITEM3"], "test.jpg")
self.assertEqual(results["ITEM4"], 1)
self.assertEqual(results["ITEM5"], 3.14)
self.assertEqual(results["ITEM6"], [1, 2, 3])

def test_should_write_different_types(self):
item = self.Xml("ITEM", "/html/head/script/@src", "STRING", None)
Expand All @@ -87,19 +89,25 @@ def test_should_write_different_types(self):
XmlAccessor.class_write_item(item, 1.234, self.data1)
self.assertEqual(XmlAccessor.class_read_item(item, self.data1), 1.234)

item = self.Xml("ITEM", "/html/body/p/ul/li[3]/text()", "INT", 24)
XmlAccessor.class_write_item(item, [2.2, "3", 4], self.data1)
self.assertEqual(XmlAccessor.class_read_item(item, self.data1), [2, 3, 4])

def test_should_write_multiple_items(self):
item1 = self.Xml("ITEM1", "/html/head/script/@src", "STRING", None)
item2 = self.Xml("ITEM2", "/html/head/noscript/text()", "STRING", None)
item3 = self.Xml("ITEM3", "/html/body/img/@src", "STRING", None)
item4 = self.Xml("ITEM4", "/html/body/p/ul/li[1]/text()", "UINT", None)
item5 = self.Xml("ITEM5", "/html/body/p/ul/li[2]/text()", "FLOAT", None)
item6 = self.Xml("ITEM6", "/html/body/p/ul/li[3]/text()", "INT", 24)

items = [item1, item2, item3, item4, item5]
values = ["different.js", "Nothing Here", "other.png", 15, 1.234]
items = [item1, item2, item3, item4, item5, item6]
values = ["different.js", "Nothing Here", "other.png", 15, 1.234, [2, 3, 4]]
XmlAccessor.class_write_items(items, values, self.data1)

self.assertEqual(XmlAccessor.class_read_item(item1, self.data1), "different.js")
self.assertEqual(XmlAccessor.class_read_item(item2, self.data1), "Nothing Here")
self.assertEqual(XmlAccessor.class_read_item(item3, self.data1), "other.png")
self.assertEqual(XmlAccessor.class_read_item(item4, self.data1), 15)
self.assertEqual(XmlAccessor.class_read_item(item5, self.data1), 1.234)
self.assertEqual(XmlAccessor.class_read_item(item6, self.data1), [2, 3, 4])
56 changes: 50 additions & 6 deletions openc3/spec/accessors/html_accessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# This file may also be used under the terms of a commercial license
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.

require 'spec_helper'
Expand All @@ -23,7 +23,7 @@
module OpenC3
describe HtmlAccessor do
before(:each) do
@data1 = '<!DOCTYPE html><html lang="en"><head><title>My Title</title><script src="test.js"></script></head><body><noscript>No Script Detected</noscript><img src="test.jpg" alt="An Image"/><p>Paragraph</p><ul><li>1</li><li>3.14</li></ul></body></html>'
@data1 = '<!DOCTYPE html><html lang="en"><head><title>My Title</title><script src="test.js"></script></head><body><noscript>No Script Detected</noscript><img src="test.jpg" alt="An Image"/><p>Paragraph</p><ul><li>1</li><li>3.14</li><li>[1.1,2.2,3.3]</li></ul></body></html>'
end

describe "read_item" do
Expand All @@ -32,27 +32,38 @@ module OpenC3
item = OpenStruct.new
item.key = '/html/head/script/@src'
item.data_type = :STRING
item.array_size = nil
expect(HtmlAccessor.read_item(item, @data1)).to eq "test.js"

item = OpenStruct.new
item.key = '/html/body/noscript/text()'
item.data_type = :STRING
item.array_size = nil
expect(HtmlAccessor.read_item(item, @data1)).to eq "No Script Detected"

item = OpenStruct.new
item.key = '/html/body/img/@src'
item.data_type = :STRING
item.array_size = nil
expect(HtmlAccessor.read_item(item, @data1)).to eq "test.jpg"

item = OpenStruct.new
item.key = '/html/body/ul/li[1]/text()'
item.data_type = :UINT
item.array_size = nil
expect(HtmlAccessor.read_item(item, @data1)).to eq 1

item = OpenStruct.new
item.key = '/html/body/ul/li[2]/text()'
item.data_type = :FLOAT
item.array_size = nil
expect(HtmlAccessor.read_item(item, @data1)).to eq 3.14

item = OpenStruct.new
item.key = '/html/body/ul/li[3]/text()'
item.data_type = :FLOAT
item.array_size = 24
expect(HtmlAccessor.read_item(item, @data1)).to eq [1.1, 2.2, 3.3]
end
end

Expand All @@ -62,31 +73,42 @@ module OpenC3
item1.name = 'ITEM1'
item1.key = '/html/head/script/@src'
item1.data_type = :STRING
item1.array_size = nil
item2 = OpenStruct.new
item2.name = 'ITEM2'
item2.key = '/html/body/noscript/text()'
item2.data_type = :STRING
item2.array_size = nil
item3 = OpenStruct.new
item3.name = 'ITEM3'
item3.key = '/html/body/img/@src'
item3.data_type = :STRING
item3.array_size = nil
item4 = OpenStruct.new
item4.name = 'ITEM4'
item4.key = '/html/body/ul/li[1]/text()'
item4.data_type = :UINT
item4.array_size = nil
item5 = OpenStruct.new
item5.name = 'ITEM5'
item5.key = '/html/body/ul/li[2]/text()'
item5.data_type = :FLOAT
item5.array_size = nil
item6 = OpenStruct.new
item6.name = 'ITEM6'
item6.key = '/html/body/ul/li[3]/text()'
item6.data_type = :FLOAT
item6.array_size = 24

items = [item1, item2, item3, item4, item5]
items = [item1, item2, item3, item4, item5, item6]

results = HtmlAccessor.read_items(items, @data1)
expect(results['ITEM1']).to eq "test.js"
expect(results['ITEM2']).to eq "No Script Detected"
expect(results['ITEM3']).to eq "test.jpg"
expect(results['ITEM4']).to eq 1
expect(results['ITEM5']).to eq 3.14
expect(results['ITEM6']).to eq [1.1, 2.2, 3.3]
end
end

Expand All @@ -95,32 +117,44 @@ module OpenC3
item = OpenStruct.new
item.key = '/html/head/script/@src'
item.data_type = :STRING
item.array_size = nil
HtmlAccessor.write_item(item, "different.js", @data1)
expect(HtmlAccessor.read_item(item, @data1)).to eq "different.js"

item = OpenStruct.new
item.key = '/html/body/noscript/text()'
item.data_type = :STRING
item.array_size = nil
HtmlAccessor.write_item(item, "Nothing Here", @data1)
expect(HtmlAccessor.read_item(item, @data1)).to eq "Nothing Here"

item = OpenStruct.new
item.key = '/html/body/img/@src'
item.data_type = :STRING
item.array_size = nil
HtmlAccessor.write_item(item, "other.png", @data1)
expect(HtmlAccessor.read_item(item, @data1)).to eq "other.png"

item = OpenStruct.new
item.key = '/html/body/ul/li[1]/text()'
item.data_type = :UINT
item.array_size = nil
HtmlAccessor.write_item(item, 15, @data1)
expect(HtmlAccessor.read_item(item, @data1)).to eq 15

item = OpenStruct.new
item.key = '/html/body/ul/li[2]/text()'
item.data_type = :FLOAT
item.array_size = nil
HtmlAccessor.write_item(item, 1.234, @data1)
expect(HtmlAccessor.read_item(item, @data1)).to eq 1.234

item = OpenStruct.new
item.key = '/html/body/ul/li[3]/text()'
item.data_type = :FLOAT
item.array_size = 24
HtmlAccessor.write_item(item, ["2", 3.3, 4], @data1)
expect(HtmlAccessor.read_item(item, @data1)).to eq [2.0, 3.3, 4.0]
end
end

Expand All @@ -129,28 +163,38 @@ module OpenC3
item1 = OpenStruct.new
item1.key = '/html/head/script/@src'
item1.data_type = :STRING
item1.array_size = nil
item2 = OpenStruct.new
item2.key = '/html/body/noscript/text()'
item2.data_type = :STRING
item2.array_size = nil
item3 = OpenStruct.new
item3.key = '/html/body/img/@src'
item3.data_type = :STRING
item3.array_size = nil
item4 = OpenStruct.new
item4.key = '/html/body/ul/li[1]/text()'
item4.data_type = :UINT
item4.array_size = nil
item5 = OpenStruct.new
item5.key = '/html/body/ul/li[2]/text()'
item5.data_type = :FLOAT

items = [item1, item2, item3, item4, item5]
values = ["different.js", "Nothing Here", "other.png", 15, 1.234]
item5.array_size = nil
item6 = OpenStruct.new
item6.key = '/html/body/ul/li[3]/text()'
item6.data_type = :FLOAT
item6.array_size = 24

items = [item1, item2, item3, item4, item5, item6]
values = ["different.js", "Nothing Here", "other.png", 15, 1.234, [2.2, 3.3, 4.4]]
HtmlAccessor.write_items(items, values, @data1)

expect(HtmlAccessor.read_item(item1, @data1)).to eq "different.js"
expect(HtmlAccessor.read_item(item2, @data1)).to eq "Nothing Here"
expect(HtmlAccessor.read_item(item3, @data1)).to eq "other.png"
expect(HtmlAccessor.read_item(item4, @data1)).to eq 15
expect(HtmlAccessor.read_item(item5, @data1)).to eq 1.234
expect(HtmlAccessor.read_item(item6, @data1)).to eq [2.2, 3.3, 4.4]
end
end
end
Expand Down
Loading

0 comments on commit 9066965

Please sign in to comment.