added ability for xml_reader to reload after upload

This commit is contained in:
p.loedige 2021-01-04 10:12:41 +01:00
parent eb8ce9ee87
commit 3fdf60f866
2 changed files with 11 additions and 7 deletions

15
app.py
View File

@ -25,11 +25,11 @@ def api_root():
} }
@app.route('/XML/', methods=["GET","POST"]) @app.route('/XML/', methods=["GET","POST"])
def xml_transfer(self): def xml_transfer():
try: try:
#return the current config.xml #return the current config.xml
if request.method == "GET": if request.method == "GET":
return send_from_directory(str(dir + 'XML'), 'config.xml') return send_from_directory(str(dir + '/XML'), 'config.xml')
# set a new config.xml # set a new config.xml
if request.method == "POST": if request.method == "POST":
@ -40,10 +40,11 @@ def xml_transfer(self):
tmp_file.write(data) tmp_file.write(data)
tmp_file.close() tmp_file.close()
#check config.xml against the XSD file #check config.xml against the XSD file
if xml_reader.validate('/tmp/aped_device/config.xml',str(dir + 'XML/config.xsd')): if xml_reader.validate('/tmp/aped_device/config.xml',str(dir + '/XML/config.xsd')):
shutil.move(str(dir + 'XML/config.xml'),str(dir + 'XML/config_old.xml')) shutil.move(str(dir + '/XML/config.xml'),str(dir + '/XML/config_old.xml'))
shutil.move('/tmp/aped_device/config.xml',str(dir + 'XML/config.xml')) shutil.move('/tmp/aped_device/config.xml',str(dir + '/XML/config.xml'))
self.interface_handler = Interface_handler(self.xml_reader) xml_reader.set_root(str(dir + '/XML/config.xml'))
interface_handler = Interface_handler(xml_reader)
else: else:
SyntaxError('the config.xml has invalid content') SyntaxError('the config.xml has invalid content')
return {'error': ''} return {'error': ''}
@ -51,7 +52,7 @@ def xml_transfer(self):
return {'error' : str(e)} return {'error' : str(e)}
@app.route('/device/<device_name>/', methods=["GET", "POST"]) @app.route('/device/<device_name>/', methods=["GET", "POST"])
def api_leds_control(device_name): def api_devices(device_name):
try: try:
if request.method == "POST": if request.method == "POST":
response = interface_handler.write(device_name,request.data.get("output")) response = interface_handler.write(device_name,request.data.get("output"))

View File

@ -15,6 +15,9 @@ class Xml_reader:
#check the XML for validity using the XSD file #check the XML for validity using the XSD file
if not self.validate(xml_path,xsd_path): if not self.validate(xml_path,xsd_path):
raise SyntaxError('the XML config file has an invalid syntax') raise SyntaxError('the XML config file has an invalid syntax')
self.set_root(xml_path)
def set_root(self, xml_path: str):
self.root = etree.parse(xml_path).getroot() self.root = etree.parse(xml_path).getroot()
def validate(self, xml_path: str, xsd_path: str) -> bool: def validate(self, xml_path: str, xsd_path: str) -> bool: