Skip to content

Commit

Permalink
Fix small differences
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <michael@openrobotics.org>
  • Loading branch information
mjcarroll committed Feb 8, 2023
1 parent 49e7526 commit 7c37a93
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 30 deletions.
2 changes: 1 addition & 1 deletion sdf/1.10/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ foreach(FIL ${sdfs})

add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.xsd"
COMMAND ${RUBY} ${CMAKE_SOURCE_DIR}/tools/xmlschema.py
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/xmlschema.py
ARGS --sdf-dir ${CMAKE_CURRENT_SOURCE_DIR} --input-file ${ABS_FIL} --output-dir ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${ABS_FIL}
COMMENT "Running xml schema compiler on ${FIL}"
Expand Down
2 changes: 1 addition & 1 deletion sdf/1.5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ foreach(FIL ${sdfs})

add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.xsd"
COMMAND ${RUBY} ${CMAKE_SOURCE_DIR}/tools/xmlschema.py
COMMAND ${Pyhton3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/xmlschema.py
ARGS --sdf-dir ${CMAKE_CURRENT_SOURCE_DIR} --input-file ${ABS_FIL} --output-dir ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${ABS_FIL}
COMMENT "Running xml schema compiler on ${FIL}"
Expand Down
2 changes: 1 addition & 1 deletion sdf/1.6/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ foreach(FIL ${sdfs})

add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.xsd"
COMMAND ${RUBY} ${CMAKE_SOURCE_DIR}/tools/xmlschema.py
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/xmlschema.py
ARGS --sdf-dir ${CMAKE_CURRENT_SOURCE_DIR} --input-file ${ABS_FIL} --output-dir ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${ABS_FIL}
COMMENT "Running xml schema compiler on ${FIL}"
Expand Down
2 changes: 1 addition & 1 deletion sdf/1.7/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ foreach(FIL ${sdfs})

add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.xsd"
COMMAND ${RUBY} ${CMAKE_SOURCE_DIR}/tools/xmlschema.py
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/xmlschema.py
ARGS --sdf-dir ${CMAKE_CURRENT_SOURCE_DIR} --input-file ${ABS_FIL} --output-dir ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${ABS_FIL}
COMMENT "Running xml schema compiler on ${FIL}"
Expand Down
2 changes: 1 addition & 1 deletion sdf/1.8/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ foreach(FIL ${sdfs})

add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.xsd"
COMMAND ${RUBY} ${CMAKE_SOURCE_DIR}/tools/xmlschema.py
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/xmlschema.py
ARGS --sdf-dir ${CMAKE_CURRENT_SOURCE_DIR} --input-file ${ABS_FIL} --output-dir ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${ABS_FIL}
COMMENT "Running xml schema compiler on ${FIL}"
Expand Down
2 changes: 1 addition & 1 deletion sdf/1.9/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ foreach(FIL ${sdfs})

add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.xsd"
COMMAND ${RUBY} ${CMAKE_SOURCE_DIR}/tools/xmlschema.py
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/xmlschema.py
ARGS --sdf-dir ${CMAKE_CURRENT_SOURCE_DIR} --input-file ${ABS_FIL} --output-dir ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${ABS_FIL}
COMMENT "Running xml schema compiler on ${FIL}"
Expand Down
64 changes: 40 additions & 24 deletions tools/xmlschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class XmlSchema:
'1': ('1', '1'),
'+': ('1', 'unbounded'),
'*': ('0', 'unbounded'),
'-1': ('0', 'unbounded'),
}

def __init__(self, sdf_root_dir: str):
Expand All @@ -38,16 +39,15 @@ def _indent_lines(self, lines: List[str], indent) -> List[str]:
def _get_attribute(self, element: ElementTree.Element, attrib: str) -> Optional[str]:
return element.attrib[attrib] if attrib in element.attrib else None

def _is_std_type(self, element: ElementTree.Element) -> bool:
return self._get_attribute(element, 'type') in self.SDF_TYPES_TO_XSD_STD_TYPES.keys()
def _is_std_type(self, sdf_type: str) -> bool:
return sdf_type in self.SDF_TYPES_TO_XSD_STD_TYPES.keys()

def _xsd_type_string(self, element: ElementTree.Element) -> Optional[str]:
if self._is_std_type(element):
sdf_type = self._get_attribute(element, 'type')
if sdf_type:
xsd_type = self.SDF_TYPES_TO_XSD_STD_TYPES[sdf_type]
return 'xsd:' + xsd_type
return None
def _xsd_type_string(self, sdf_type: str) -> Optional[str]:
if self._is_std_type(sdf_type):
xsd_type = self.SDF_TYPES_TO_XSD_STD_TYPES[sdf_type]
return 'xsd:' + xsd_type
else:
return None

def _print_documentation(self, element: ElementTree.Element) -> List[str]:
lines = []
Expand Down Expand Up @@ -85,31 +85,37 @@ def _print_include_ref(self, element: ElementTree.Element) -> List[str]:
print(f'Invalid sdf included {filename}')
return lines

def _print_element(self, element: ElementTree.Element) -> List[str]:
def _print_plugin_element(self, element: ElementTree.Element) -> List[str]:
lines = []

# Short circuit for plugin.sdf copy_data element
if 'copy_data' in element.attrib:
lines.append('<xsd:sequence>')
lines.append(" <xsd:any minOccurs='0' maxOccurs='unbounded' processContents='lax'/>")
lines.append('</xsd:sequence>')
return lines
return lines

def _print_element(self, element: ElementTree.Element) -> List[str]:
lines = []

elem_name = self._get_attribute(element, 'name')
elem_type = self._xsd_type_string(element)
elem_type = self._get_attribute(element, 'type')
elem_reqd = self._get_attribute(element, 'required')

minOccurs, maxOccurs = self.SDF_REQUIRED_TO_MIN_MAX_OCCURS[elem_reqd]
lines.append(f"<xsd:choice minOccurs='{minOccurs}' maxOccurs='{maxOccurs}'>")
if elem_type and self._is_std_type(elem_type):
elem_type = self._xsd_type_string(elem_type)

if elem_type:
if elem_reqd:
minOccurs, maxOccurs = self.SDF_REQUIRED_TO_MIN_MAX_OCCURS[elem_reqd]
lines.append(f"<xsd:choice minOccurs='{minOccurs}' maxOccurs='{maxOccurs}'>")

if elem_type is None:
lines.append(f"<xsd:element name='{elem_name}'>")
lines.extend(self._indent_lines(self._print_documentation(element), 2))
lines.append(" <xsd:complexType>")
lines.append(" <xsd:choice maxOccurs='unbounded'>")

for e in element.findall('element'):
lines.extend(self._indent_lines(self._print_element(e), 2))
lines.extend(self._indent_lines(self._print_element(e), 6))

lines.append(" </xsd:choice>")

Expand All @@ -129,19 +135,22 @@ def _print_attribute(self, element: ElementTree.Element) -> List[str]:
lines = []

elem_name = self._get_attribute(element, 'name')
elem_type = self._xsd_type_string(element)
elem_type = self._get_attribute(element, 'type')
elem_reqd = self._get_attribute(element, 'required')
elem_default = self._get_attribute(element, 'default')

if elem_type and self._is_std_type(elem_type):
elem_type = self._xsd_type_string(elem_type)

use = ""
default = ""

if elem_reqd == "1":
use = "use='required'"
elif elem_reqd == "0":
use = "use='optional'"
if elem_default:
default = f"default='{elem_default}';'"
if elem_default is not None:
default = f"default='{elem_default}'"

lines.append(f"<xsd:attribute name='{elem_name}' type='{elem_type}' {use} {default}>")
lines.extend(self._indent_lines(self._print_documentation(element), 2))
Expand All @@ -150,7 +159,10 @@ def _print_attribute(self, element: ElementTree.Element) -> List[str]:

def _print_xsd(self, element: ElementTree.Element) -> List[str]:
lines = []

elem_name = self._get_attribute(element, 'name')
elem_type = self._get_attribute(element, 'type')

elements = element.findall('element')
attributes = element.findall('attribute')
includes = element.findall('include')
Expand All @@ -170,7 +182,10 @@ def _print_xsd(self, element: ElementTree.Element) -> List[str]:
lines.append(" <xsd:choice maxOccurs='unbounded'>")

for child_element in elements:
lines.extend(self._indent_lines(self._print_element(child_element), 4))
if 'copy_data' in child_element.attrib:
lines.extend(self._indent_lines(self._print_plugin_element(child_element), 4))
else:
lines.extend(self._indent_lines(self._print_element(child_element), 6))

for include_element in includes:
lines.extend(self._indent_lines(self._print_include_ref(include_element), 6))
Expand All @@ -184,10 +199,11 @@ def _print_xsd(self, element: ElementTree.Element) -> List[str]:
lines.append(" </xsd:complexType>")
lines.append(f"</xsd:element>")
else:
elem_type = self._xsd_type_string(element)

if elem_type is None:
if elem_type and self._is_std_type(elem_type):
elem_type = self._xsd_type_string(elem_type)
else:
elem_type = ""

lines.append(f"<xsd:element name='{elem_name}'{elem_type} />")
return lines

Expand Down

0 comments on commit 7c37a93

Please sign in to comment.