import subprocess
import config
from flask import render_template, jsonify, request
from app import app


@app.route(config.PREFIX + '/', methods=['GET', 'POST'])
@app.route(config.PREFIX + '/index', methods=['GET', 'POST'])
def index():
    return render_template('index.html')


@app.route(config.PREFIX + '/about', methods=['GET'])
def about():
    return render_template('about.html')


@app.route(config.PREFIX + '/yangre', methods=['GET', 'POST'])
def yangre():
    # writing the test string to file, as required by w3cgrep
    with open("w3c_input", "w") as testfile:
        testfile.write(request.form['content'])
        testfile.write("\n")

    # An unquoted string is any sequence of characters that does not
    # contain any space, tab, carriage return, or line feed characters, a
    # single or double quote character, a semicolon (";"), braces ("{" or
    # "}"), or comment sequences ("//", "/*", or "*/").

    # python 3.5 dependency. To get stdout as a string we need the universal_newlines=True parameter
    # in python 3.6 this changes to encoding='utf8'
    w3c_input_obj = subprocess.run(
        [config.W3CGREP_PATH,
         str(request.form['pattern']), "w3c_input"],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        universal_newlines=True)
    if not w3c_input_obj.stdout:
        w3c_input_result = 1
    else:
        w3c_input_result = 0

    yangre_input_obj = {}
    if request.form['inverted'] == "true":
        w3c_input_result = int(not w3c_input_result)

        # python 3.5 dependency. To get stdout as a string we need the universal_newlines=True parameter
        # in python 3.6 this changes to encoding='utf8'
        yangre_input_obj = subprocess.run(
            [
                config.YANGGRE_PATH, "-p",
                str(request.form['pattern']), "-i",
                str(request.form['content'])
            ],
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            universal_newlines=True)
    else:
        # python 3.5 dependency. To get stdout as a string we need the universal_newlines=True parameter
        # in python 3.6 this changes to encoding='utf8'
        print("Passing", config.YANGGRE_PATH, "-p",
              str(request.form['pattern']), str(request.form['content']))
        yangre_input_obj = subprocess.run(
            [
                config.YANGGRE_PATH, "-p",
                str(request.form['pattern']),
                str(request.form['content'])
            ],
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            universal_newlines=True)

    if w3c_input_obj.returncode == 1:
        w3c_input_result = -1  # I used -1 as error code

    return jsonify({
        'pattern_nb': request.form['pattern_nb'],
        'w3cgrep_result': w3c_input_result,
        'w3cgrep_output': w3c_input_obj.stdout,
        'yangre_result': yangre_input_obj.returncode,
        'yangre_output': yangre_input_obj.stdout
    })

@@ -11,16 +11,26 @@
 process%0A
+import os%0A
 import c
@@ -533,16 +533,81 @@
 w3cgrep%0A
+    w3cinput_filename = %22w3c_input%22 + request.form%5B'pattern_nb'%5D%0A
     with
@@ -612,27 +612,33 @@
 th open(
-%22
 w3c
-_
 input
-%22
+_filename
 , %22w%22) a
@@ -737,27 +737,32 @@
   # 
-An unquoted
+writing the test
  string 
 is a
@@ -761,255 +761,292 @@
 ing 
-is any sequence of characters that does not%0A    # contain any space, tab, carriage return, or line feed characters, a%0A    # single or double quote character, a semicolon (%22;%22), braces (%22%7B%22 or%0A    # %22%7D%22), or comment sequences (%22//%22, %22/*%22, or %22*/%22).
+to another file for yangre%0A    yangreinput_filename = %22yangre_input%22 + request.form%5B'pattern_nb'%5D%0A    with open(yangreinput_filename, %22w%22) as yangrefile:%0A        yangrefile.write(request.form%5B'pattern'%5D)%0A        yangrefile.write(%22%5Cn%22)%0A        yangrefile.write(request.form%5B'content'%5D)
 %0A%0A  
@@ -1301,27 +1301,33 @@
 ern'%5D), 
-%22
 w3c
-_
 input
-%22
+_filename
 %5D,%0A     
@@ -1862,33 +1862,16 @@
        %5B
-%0A                
 config.Y
@@ -1889,121 +1889,38 @@
 , %22-
-p%22,%0A                str(request.form%5B'pattern'%5D), %22-i%22,%0A                str(request.form%5B'content'%5D)%0A            
+f%22, yangreinput_filename, %22-i%22
 %5D,%0A 
@@ -2204,134 +2204,8 @@
 f8'%0A
-        print(%22Passing%22, config.YANGGRE_PATH, %22-p%22,%0A              str(request.form%5B'pattern'%5D), str(request.form%5B'content'%5D))%0A
     
@@ -2256,33 +2256,16 @@
        %5B
-%0A                
 config.Y
@@ -2283,115 +2283,32 @@
 , %22-
-p%22,%0A                str(request.form%5B'pattern'%5D),%0A                str(request.form%5B'content'%5D)%0A            
+f%22, yangreinput_filename
 %5D,%0A 
@@ -2514,16 +2514,107 @@
 r code%0A%0A
+    # clean up files%0A    os.remove(w3cinput_filename)%0A    os.remove(yangreinput_filename)%0A%0A
     retu

test using yangre with file