From 3fdf60f86623a6f5869ee90c35c0328f82b7382c Mon Sep 17 00:00:00 2001 From: "p.loedige" Date: Mon, 4 Jan 2021 10:12:41 +0100 Subject: [PATCH] added ability for xml_reader to reload after upload --- app.py | 15 ++++++++------- xml_reader.py | 3 +++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index 81f8759..58d2aef 100644 --- a/app.py +++ b/app.py @@ -25,11 +25,11 @@ def api_root(): } @app.route('/XML/', methods=["GET","POST"]) -def xml_transfer(self): +def xml_transfer(): try: #return the current config.xml 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 if request.method == "POST": @@ -40,10 +40,11 @@ def xml_transfer(self): tmp_file.write(data) tmp_file.close() #check config.xml against the XSD file - 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('/tmp/aped_device/config.xml',str(dir + 'XML/config.xml')) - self.interface_handler = Interface_handler(self.xml_reader) + 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('/tmp/aped_device/config.xml',str(dir + '/XML/config.xml')) + xml_reader.set_root(str(dir + '/XML/config.xml')) + interface_handler = Interface_handler(xml_reader) else: SyntaxError('the config.xml has invalid content') return {'error': ''} @@ -51,7 +52,7 @@ def xml_transfer(self): return {'error' : str(e)} @app.route('/device//', methods=["GET", "POST"]) -def api_leds_control(device_name): +def api_devices(device_name): try: if request.method == "POST": response = interface_handler.write(device_name,request.data.get("output")) diff --git a/xml_reader.py b/xml_reader.py index bfdcab1..7f03323 100644 --- a/xml_reader.py +++ b/xml_reader.py @@ -15,6 +15,9 @@ class Xml_reader: #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.set_root(xml_path) + + def set_root(self, xml_path: str): self.root = etree.parse(xml_path).getroot() def validate(self, xml_path: str, xsd_path: str) -> bool: