示例#1
0
	def test_dict_parsing(self):
		global SomeClass
		global SomeOtherClass

		SomeOtherClass = Parsable(name=Bind(to="name"), content=Bind(to="value", type=int))(SomeOtherClass)
		SomeClass = Parsable(name="box", children=[ BindDict(to="dict_attr", key="name", type=SomeOtherClass) ])(SomeClass)
		
		parsed: SomeClass = SomeClass.parse_string("""
			<box>
				<oui>5</oui>
				<non>21</non>
				<maybe>37</maybe>
			</box>
		""")

		self.assertIsInstance(parsed, SomeClass, "right class parsed")
		self.assertEqual(len(parsed.dict_attr.keys()), 3, "right number of keys")

		for key in parsed.dict_attr:
			if key == "oui":
				self.assertEqual(parsed.dict_attr[key].value, 5)
			elif key == "non":
				self.assertEqual(parsed.dict_attr[key].value, 21)
			elif key == "maybe":
				self.assertEqual(parsed.dict_attr[key].value, 37)
示例#2
0
	def test_content_parsing(self):
		global SomeClass
		SomeClass = Parsable(name="two", content=Bind(type=int, to="int_attr"))(SomeClass)

		parsed = SomeClass.parse_string("<two>84</two>")

		self.assertIsInstance(parsed, SomeClass)
		self.assertEqual(parsed.int_attr, 84)
示例#3
0
	def test_property_call(self):
		"""Test that a class property is not override by parsed values"""
		global YetAnotherClass
		YetAnotherClass = Parsable(name=Bind(to="x", type=Enum(binding = {"zero": 0})))(YetAnotherClass)

		parsed = YetAnotherClass.parse_string('<zero />')

		self.assertIsInstance(parsed, YetAnotherClass)
		self.assertEqual(parsed.set_called, True, "setter called on parsing")
		self.assertEqual(parsed.x, 5, "property response unchanged")
		self.assertEqual(parsed.get_called, True, "getter called")
	def test_enum_values_parsing(self):
		global SomeClass
		enum = Enum(values=[5, 6, 7, 8, 9], cast=int)
		SomeClass = Parsable(name="three", attributes={"value": Bind(type=enum, to="int_attr") })(SomeClass)

		# Parsing success
		parsed = SomeClass.parse_string("<three value='7' />")

		self.assertIsInstance(parsed, SomeClass)
		self.assertEqual(parsed.int_attr, 7)

		# Parsing error
		with self.assertRaises(ParsingException):
			SomeClass.parse_string("<three value='3' />")
	def test_enum_binding_parsing(self):
		global SomeClass
		enum = Enum(binding={"one": 1, "two": 2, "three": 3, "four": 4, "five": 5})
		SomeClass = Parsable(name="two", content=Bind(type=enum, to="int_attr"))(SomeClass)

		# Parsing success
		parsed = SomeClass.parse_string("<two>one</two>")

		self.assertIsInstance(parsed, SomeClass)
		self.assertEqual(parsed.int_attr, 1)

		# Parsing error
		with self.assertRaises(ParsingException):
			SomeClass.parse_string("<two>zero</two>")
    def test_mandatory(self):
        global SomeClass

        SomeClass = Parsable(
            name="lowl",
            attributes={"name": Bind(mandatory=True,
                                     to="str_attr")})(SomeClass)

        parsed = SomeClass.parse_string("<lowl name='bob' />")

        self.assertIsInstance(parsed, SomeClass)
        self.assertEqual(parsed.str_attr, "bob")

        with self.assertRaises(ParsingException):
            SomeClass.parse_string("<lowl />")
示例#7
0
	def test_list_parsing(self):
		global SomeClass
		global SomeOtherClass

		SomeClass = Parsable(name="in", attributes={ "v": Bind(to="int_value", type=int) })(SomeClass)
		SomeOtherClass = Parsable(name="box", children = [ BindList(to="children", type=SomeClass) ])(SomeOtherClass)
		
		parsed: SomeOtherClass = SomeOtherClass.parse_string("""
			<box>
				<in v="1" />
				<in v="2" />
				<in v="3" />
			</box>
		""")

		self.assertIsInstance(parsed, SomeOtherClass, "class parsed")
		self.assertEqual(len(parsed.children), 3, "right number of children")

		for i in range(3):
			child = parsed.children[i]

			self.assertIsInstance(child, SomeClass, "children has right class")
			self.assertEqual(child.int_value, i + 1, "values are parsed in order")
from ai_msgs.msg import ActionPoint, ActionStatus
from args_lib.msg import Argument
from ai_msgs.srv import ComputeActionPoint

from xml_class_parser import Parsable, Bind, BindDict, BindList
from xml_class_parser.types import Bool, BlackList

class ActionChoice:
	"""
		Choice of an action
	"""
	def __init__(self, action=None, score = 0):
		self.action = action
		self.score = score

Argument = Parsable(name="arg", attributes={"name": str}, content=Bind(to="value"))(Argument)


@Parsable(
	name = Bind(to="name", type=BlackList("group")),
	attributes = {
		"native": Bool,
		"points": int,
	},
	children = [
		BindList(to="arguments", type=Argument)
	]
)
class Action:
	action_id = 0
class RectZone(Zone):
    def __init__(self):
        super().__init__()
        self.width = 0
        self.height = 0


class CircleZone(Zone):
    def __init__(self):
        super().__init__()
        self.radius = 0


@Parsable(name="symmetry",
          attributes={
              "offset": Bind(mandatory=True, type=int),
              "axis": Bind(mandatory=True),
              "source": str,
              "target": str
          })
class Symmetry:
    """
		Symmetry according to an axis (x or y).

		Having axis set to "x" means that objects on one side
		of the line of equation x = [offset] are cloned on the
		other side
	"""
    def __init__(self):
        self.axis: str = "x"
        self.offset: int = 0
示例#10
0
from .shape import Circle, Rect

from typing import List, Dict

from args_lib.argumentable import Argumentable
from copy import copy, deepcopy

from xml_class_parser import Parsable, Bind, BindDict, Context, Slice, ParsingException
from xml_class_parser.helper import AttrAttrTuple

@Parsable(
	name=Bind(to="name"),
	attributes = {
		"extends": str,
		"x": int,
		"y": int
	},
	children = [
		Bind(to="shape", type=Circle),
		Bind(to="shape", type=Rect),
		BindDict(to="values", type=AttrAttrTuple("arg", "name", "type"), key="name", post_cast=Slice("value"))
	]
)
class MapObject(Argumentable):
	"""
		Define an object on the field, with associated action
		to deal with and some properties
	"""
	def __init__(self):
		super().__init__()