Added the YAML editor to DeTT&CT

master
Ruben Bouman 2020-02-20 12:02:27 +01:00
parent f834638214
commit 61b30b2fff
2 changed files with 79 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import argparse
import os
import signal
from interactive_menu import *
from yaml_editor import YamlEditor
def _init_menu():
@ -18,8 +19,12 @@ def _init_menu():
# add subparsers
subparsers = menu_parser.add_subparsers(title='MODE',
description='Select the mode to use. Every mode has its own arguments and '
'help info displayed using: {visibility, detection, group, '
'generic} --help', metavar='', dest='subparser')
'help info displayed using: {editor, datasource, visibility, detection, '
'group, generic} --help', metavar='', dest='subparser')
parser_editor = subparsers.add_parser('editor', aliases=['e'], help='YAML editor',
description='Start the YAML editor for easy editing the YAML administration files.')
parser_editor.add_argument('-p', '--port', help='port where the webserver listens on (default is 8080)', required=False, default=8080)
# create the data source parser
parser_data_sources = subparsers.add_parser('datasource', help='data source mapping and quality',
@ -183,6 +188,9 @@ def _menu(menu_parser):
if args.interactive:
interactive_menu()
elif args.subparser in ['editor', 'e']:
YamlEditor(int(args.port)).start()
elif args.subparser in ['datasource', 'ds']:
if check_file(args.file_ds, FILE_TYPE_DATA_SOURCE_ADMINISTRATION, args.health):
file_ds = args.file_ds
@ -283,6 +291,8 @@ def _prepare_folders():
os.mkdir('output')
# pylint: disable=unused-argument
def _signal_handler(signum, frame):
"""
Function to handles exiting via Ctrl+C.

67
yaml_editor.py Normal file
View File

@ -0,0 +1,67 @@
import webbrowser
import os
import sys
import signal
from http.server import HTTPServer, SimpleHTTPRequestHandler
from socketserver import TCPServer
import threading
class QuietHTTPRequestHandler(SimpleHTTPRequestHandler):
def log_message(self, format, *args):
pass
def log_request(self, code='-', size='-'):
pass
class YamlEditor:
def __init__(self, port):
"""
Constructor of the YamlEditor class. Sets the SIGTERM (clean quit) en SIGINT (Ctrl+C) handlers and the default variables.
:param port: The port for the webserver to listen on
"""
signal.signal(signal.SIGTERM, self._signal_handler)
signal.signal(signal.SIGINT, self._signal_handler)
self.port = port
self.httpd = None
def _signal_handler(self, signal, frame):
"""
Handles the termination of the application.
:param signum: Indicator of the termination signal
:param frame:
"""
print("Shutting down webserver")
self.httpd.server_close()
self.httpd.shutdown()
def _run_webserver(self):
"""
Starts the webserver on the given port.
"""
try:
os.chdir('./YAML-editor/dist')
self.httpd = TCPServer(('', self.port), QuietHTTPRequestHandler)
print("YAML editor started at port", self.port)
url = 'http://localhost:' + str(self.port)
if not os.getenv('DeTTECT_DOCKER_CONTAINER'):
print("Opening webbrowser: " + url)
webbrowser.open_new_tab(url)
else:
print("You can open the YAML editor on: " + url)
self.httpd.serve_forever()
except Exception as e:
print("Could not start webserver: " + str(e))
def start(self):
"""
Starts the YAML editor by starting a thread where the webserver runs in.
"""
thread = threading.Thread(target=self._run_webserver)
thread.start()