diff --git a/__init__.py b/__init__.py deleted file mode 100644 index cc44c99..0000000 --- a/__init__.py +++ /dev/null @@ -1,35 +0,0 @@ -# -*- coding: utf-8 -*- -from flask import Flask,jsonify,send_from_directory, render_template -import subprocess -import os -from config import Config - - -app = Flask(__name__, template_folder="templates", static_folder="static") -def run_cmd(cmd): - p = subprocess.Popen(cmd, stdout = subprocess.PIPE, - stderr=subprocess.PIPE, - stdin=subprocess.PIPE) - out,err = p.communicate() - return render_template("index.html",out=out+"\nErrorLog\n"+err,keys=keys, title=cfg.title.decode("utf8")) - -package_directory = os.path.dirname(os.path.abspath(__file__)) -cfg = Config(file(os.path.join(package_directory, 'config.cfg'))) -cmds = Config(file(os.path.join(package_directory, 'cmds.cfg'))) -keys=cmds.keys() -@app.route("/") -def hello(): - return render_template("index.html",out="Hello",keys=keys, title=cfg.title.decode("utf8")) - -@app.route("/reload") -def reload(): - cmds = Config(file(os.path.join(package_directory, 'cmds.cfg'))) - return render_template("index.html",out="",keys=keys, title=cfg.title.decode("utf8")) - -@app.route("/") -def cm(cmd): - if cmd in cmds.keys(): - c=cmds[cmd] - return run_cmd(c) - else: - return render_template("index.html", out="Command not found",keys=keys, title=cfg.title.decode("utf8")), 404 diff --git a/flaskapp/__init__.py b/flaskapp/__init__.py new file mode 100644 index 0000000..4c33ac1 --- /dev/null +++ b/flaskapp/__init__.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +from flask import Flask,jsonify,send_from_directory, render_template +import subprocess +import os +from config import Config +import yaml + +app = Flask(__name__, template_folder="templates", static_folder="static") +package_directory = os.path.dirname(os.path.abspath(__file__)) +cfg = Config(file(os.path.join(package_directory, 'config.cfg'))) +if not "vers" in cfg: + cfg.vers=1 + +global cmds +if "vers" in cfg and cfg.vers == 2: + f=open(os.path.join(package_directory, 'cmds.cfg'),"r") + cmds=yaml.safe_load(f) + f.close() + keys=cmds.keys() +else: + cmds = Config(file(os.path.join(package_directory, 'cmds.cfg'))) + keys=cmds.keys() + + + +def render_index(out,code=200): + return render_template("index.html", + out=out,keys=keys, + title=cfg.title.decode("utf8"), + cmds=cmds, + vers=cfg.vers), code + + +def run_cmd(cmd): + p = subprocess.Popen(cmd, stdout = subprocess.PIPE, + stderr=subprocess.PIPE, + stdin=subprocess.PIPE) + out,err = p.communicate() + return render_index(out+"\nErrorLog\n"+err) + + +@app.route("/") +def hello(): + return render_index(str(cmds)) + +@app.route("/reload") +def reload(): + global cmds + if "vers" in cfg and cfg.vers == 2: + f=open(os.path.join(package_directory, 'cmds.cfg'),"r") + cmds=yaml.safe_load(f) + f.close() + keys=cmds.keys() + else: + cmds = Config(file(os.path.join(package_directory, 'cmds.cfg'))) + keys=cmds.keys() + + return render_index("Commands in cmds.cfg reloaded") + +@app.route("/") +def cm(cmd): + if cmd in keys: + if "vers" in cfg and cfg.vers == 2: + c=cmds[cmd].cmd + else: + c=cmds[cmd] + return run_cmd(c) + else: + return render_template("index.html", out="Command not found", cmds=cmds, keys=keys, title=cfg.title.decode("utf8"), vers=cfg.vers), 404 diff --git a/run b/run index 4a0e56a..7f49bf6 100755 --- a/run +++ b/run @@ -1,7 +1,6 @@ #!./env/bin/python -from management import app +from flaskapp import app -#print("sdf2") #app.run(debug=True) if __name__ == "__main__": # if len(sys.argv) > 1 and sys.argv[1] == "build":