Skip to content
Snippets Groups Projects
Commit 6b80344f authored by Thomas Bock's avatar Thomas Bock :speech_balloon:
Browse files

config links to dcc

parent 5df42f14
No related branches found
No related tags found
No related merge requests found
...@@ -83,6 +83,14 @@ Returns the version of the validation server: ...@@ -83,6 +83,14 @@ Returns the version of the validation server:
> <version>0.2.0</version> > <version>0.2.0</version>
``` ```
### /version_xsd [GET]
Returns the version of the xsd-definition:
```
> curl http://localhost:5005/version
> <version>0.2.0</version>
``
### /update [POST] ### /update [POST]
......
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
"dir":"." "dir":"."
}, },
"xsd":{ "xsd":{
"dir":"./xsd", "dir":"../DCC/schemata",
"file": "DCC_v1.9.xsd",
"url": "https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC" "url": "https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC"
} }
} }
...@@ -5,9 +5,6 @@ import utils as utils ...@@ -5,9 +5,6 @@ import utils as utils
config = utils.get_config_dict() config = utils.get_config_dict()
git_cmd = utils.git_cmd(config) git_cmd = utils.git_cmd(config)
## kommt später vom server
xsd_file_name = 'DCC_v1.8.3.xsd'
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app)
...@@ -18,6 +15,14 @@ def version(): ...@@ -18,6 +15,14 @@ def version():
return utils.xml_response(ret) return utils.xml_response(ret)
@app.route('/version_xsd', methods=['get'])
def version_xsd():
app.logger.debug('hit version_xsd')
xsd_str = utils.get_xsd(config, from_server=False)
version_str = utils.get_xsd_version(xsd_str)
return utils.xml_response(utils.return_version(version_str))
@app.route('/update', methods=['post']) @app.route('/update', methods=['post'])
def update(): def update():
app.logger.debug('hit update') app.logger.debug('hit update')
...@@ -31,7 +36,7 @@ def update(): ...@@ -31,7 +36,7 @@ def update():
@app.route('/update_xsd', methods=['post']) @app.route('/update_xsd', methods=['post'])
def update_xsd(): def update_xsd():
app.logger.debug('hit update xsd') app.logger.debug('hit update xsd')
xsd_str = utils.get_xsd(config, xsd_file_name, from_server=True) xsd_str = utils.get_xsd(config, from_server=True)
xsd_xml = utils.parse(xsd_str) xsd_xml = utils.parse(xsd_str)
if xsd_xml: if xsd_xml:
ret = utils.save_xsd(config, xsd_str, xsd_file_name) ret = utils.save_xsd(config, xsd_str, xsd_file_name)
...@@ -40,14 +45,13 @@ def update_xsd(): ...@@ -40,14 +45,13 @@ def update_xsd():
return utils.xml_response(ret) return utils.xml_response(ret)
@app.route('/validate', methods=['post']) @app.route('/validate', methods=['post'])
def validate(): def validate():
app.logger.debug('hit validate') app.logger.debug('hit validate')
xml_str = request.data xml_str = request.data
xml_tree = utils.parse(xml_str) xml_tree = utils.parse(xml_str)
if xml_tree: if xml_tree:
xsd_str = utils.get_xsd(config, xsd_file_name, from_server=False) xsd_str = utils.get_xsd(config, from_server=False)
ret = utils.validate(xml_str=xml_str, xsd_str=xsd_str) ret = utils.validate(xml_str=xml_str, xsd_str=xsd_str)
else: else:
ret = utils.return_error(error='unvalid xml data') ret = utils.return_error(error='unvalid xml data')
......
...@@ -5,8 +5,9 @@ from flask import Response ...@@ -5,8 +5,9 @@ from flask import Response
from xml.etree import ElementTree as ET from xml.etree import ElementTree as ET
import xmlschema import xmlschema
ns = {"w3":"http://www.w3.org/2001/XMLSchema"}
def get_config_dict(): def get_config_dict():
## sollte vielleicht doch xml-datei werden
with open('./config.json') as json_config_file: with open('./config.json') as json_config_file:
config = json.load(json_config_file) config = json.load(json_config_file)
...@@ -15,24 +16,25 @@ def get_config_dict(): ...@@ -15,24 +16,25 @@ def get_config_dict():
def git_cmd(config): def git_cmd(config):
return git.cmd.Git(config['git']['dir']) return git.cmd.Git(config['git']['dir'])
def get_xsd_url(config, xsd_name): def get_xsd_url(config, file_name):
return "{uri}/{xsd_name}".format(uri=config['xsd']['url'], xsd_name=xsd_name) return "{uri}/{file_name}".format(uri=config['xsd']['url'], file_name=file_name)
def get_xsd_path_file(config, xsd_name): def get_xsd_path_file(config, file_name):
return "{dir}/{xsd_name}".format(dir=config['xsd']['dir'], xsd_name=xsd_name) return "{dir}/{file_name}".format(dir=config['xsd']['dir'], file_name=file_name)
def get_xsd(config, xsd_name, from_server=True): def get_xsd(config, from_server=True):
file_name = config['xsd']['file']
if from_server: if from_server:
url = get_xsd_url(config, xsd_name) url = get_xsd_url(config, file_name)
xsd_str = requests.get(url).text xsd_str = requests.get(url).text
else: else:
path_file = get_xsd_path_file(config, xsd_name) path_file = get_xsd_path_file(config, file_name)
xsd_str = open(path_file).read() xsd_str = open(path_file).read()
return xsd_str return xsd_str
def save_xsd(config, xsd_str, xsd_name): def save_xsd(config, xsd_str, file_name):
path_file = get_xsd_path_file(config, xsd_name) path_file = get_xsd_path_file(config, file_name)
file = open(path_file, "w") file = open(path_file, "w")
file.write(xsd_str) file.write(xsd_str)
...@@ -45,6 +47,13 @@ def parse(xml_str): ...@@ -45,6 +47,13 @@ def parse(xml_str):
tree = None tree = None
return tree return tree
def get_xsd_version(xsd_str):
tree = parse(xsd_str)
for annotation in tree.findall("w3:annotation", ns):
version_text = annotation.find("w3:documentation", ns).text
return version_text
def validate(xml_str, xsd_str): def validate(xml_str, xsd_str):
tree = parse(xml_str) tree = parse(xml_str)
schema = xmlschema.XMLSchema(xsd_str) schema = xmlschema.XMLSchema(xsd_str)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment