added xml validation

This commit is contained in:
paul-loedige 2020-12-25 21:17:54 +01:00
parent b19cef6c18
commit d5f917f1c3

View File

@ -1,15 +1,30 @@
import xml.etree.ElementTree as ET
from lxml import etree
class Xml_reader:
""" reader for the XML configuration """
def __init__(self, path):
def __init__(self, xml_path, xsd_path):
"""inits the reader
Args:
path (string): path to the XML config file
"""
self.root = ET.parse(path).getroot()
#check the XML for validity using the XSD file
if not self.validate(xml_path,xsd_path):
raise SyntaxError('the XML config file has an invalid syntax')
self.root = etree.parse(xml_path).getroot()
def validate(self, xml_path:str, xsd_path: str) -> bool:
"""validates a xml by using a xsd file
Args:
xml_path (str): the path to the xml file
xsd_path (str): the path to the xsd file
Returns:
bool: result of the validation
"""
xml_schema = etree.XMLSchema(etree.parse(xsd_path))
return xml_schema.validate(etree.parse(xml_path))
def get_device_names(self):
"""returns the names of all devices in the config