# Copyright 2018 AT&T Intellectual Property. All other rights reserved. # # Licensed under the Apache License, Version 2.0 (the 'License'); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an 'AS IS' BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import json import logging import os import sys import click import yaml from flask import Flask, request, render_template, send_from_directory from flask_bootstrap import Bootstrap app_path = os.path.dirname(os.path.abspath(__file__)) app = Flask('Yaml Editor!', template_folder=os.path.join(app_path, 'templates'), static_folder=os.path.join(app_path, 'static')) Bootstrap(app) logging.getLogger('werkzeug').setLevel(logging.ERROR) LOG = app.logger @app.route('/favicon.ico') def favicon(): return send_from_directory(app.static_folder, 'favicon.ico') @app.route('/', methods=['GET', 'POST']) def index(): """Renders index page to edit provided yaml file.""" LOG.info('Rendering yaml file for editing') with open(app.config['YAML_FILE']) as file_obj: data = yaml.safe_load(file_obj) return render_template('yaml.html', data=json.dumps(data), change_str=app.config['STRING_TO_CHANGE']) @app.route('/save', methods=['POST']) def save(): """Save current progress on file.""" LOG.info('Saving edited inputs from user to yaml file') out = request.json.get('yaml_data') with open(app.config['YAML_FILE'], 'w') as file_obj: yaml.safe_dump(out, file_obj, default_flow_style=False) return "Data saved successfully!" @app.route('/saveExit', methods=['POST']) def save_exit(): """Save current progress on file and shuts down the server.""" LOG.info('Saving edited inputs from user to yaml file and shutting' ' down server') out = request.json.get('yaml_data') with open(app.config['YAML_FILE'], 'w') as file_obj: yaml.safe_dump(out, file_obj, default_flow_style=False) func = request.environ.get('werkzeug.server.shutdown') if func: func() return "Saved successfully, Shutting down app! You may close the tab!" @app.errorhandler(404) def page_not_found(e): """Serves 404 error.""" LOG.info('User tried to access unavailable page.') return '