From 5f213676220a9dfe6ef10dac5a856be5acf27a20 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 24 Jul 2017 15:49:07 +0200 Subject: [PATCH] stuff --- .gitignore | 7 +- config.cfg.sample | 1 + flaskapp/__init__.py | 50 ++++++++++ flaskapp/functions.py | 164 +++++++++++++++++++++++++++++++++ flaskapp/templates/layout.html | 24 +++++ flaskapp/templates/main.html | 28 ++++++ requirements.txt | 3 + run.py | 11 +++ services.yml.sample | 18 ++++ 9 files changed, 305 insertions(+), 1 deletion(-) create mode 100644 config.cfg.sample create mode 100644 flaskapp/__init__.py create mode 100644 flaskapp/functions.py create mode 100644 flaskapp/templates/layout.html create mode 100644 flaskapp/templates/main.html create mode 100644 requirements.txt create mode 100644 run.py create mode 100644 services.yml.sample diff --git a/.gitignore b/.gitignore index a5abce0..64844f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ *~ -*# \ No newline at end of file +*# +*.pyc +build/* +config.cfg +services.yml +env/* \ No newline at end of file diff --git a/config.cfg.sample b/config.cfg.sample new file mode 100644 index 0000000..02bc328 --- /dev/null +++ b/config.cfg.sample @@ -0,0 +1 @@ +title: "triton welcome" \ No newline at end of file diff --git a/flaskapp/__init__.py b/flaskapp/__init__.py new file mode 100644 index 0000000..0328c17 --- /dev/null +++ b/flaskapp/__init__.py @@ -0,0 +1,50 @@ +# ************************************************** +# Static / Frozen Website with bootstrap & flaticons +# ************************************************** +import sys +from flask import Flask, render_template, send_from_directory,jsonify, redirect +#from flask_flatpages import FlatPages, pygments_style_defs +from flask_frozen import Freezer +from config import Config +import os +import re +from os.path import isfile, abspath +import yaml + +package_directory = os.path.dirname(os.path.abspath(__file__)) + +#cfg = Config(file('./config.cfg')) +cfg = Config(file(os.path.join(package_directory, '../config.cfg'))) + +f=open(os.path.join(package_directory, '../services.yml'),"r") +services=yaml.safe_load(f) +f.close() + + +# Loading constants from config file +#FLATPAGES_AUTO_RELOAD = cfg.pages_reload +#FLATPAGES_EXTENSION = '.md' +#FLATPAGES_ROOT = abspath(cfg.pages_root) +#FLATPAGES_FP_ROOT = abspath(cfg.pages_root) + +# Initialize application +app = Flask(__name__, static_folder= "../static") +#flatpages = FlatPages(app) +#flatpages.get('index') + +app.config.from_object(__name__) +app.config["FREEZER_RELATIVE_URLS"]=True +#app.config["FREEZER_DESTINATION"]="/home/andis/www/triton_welcome/build/" +app.config["FREEZER_DESTINATION"]="../build" +freezer = Freezer(app) + +@app.route('/') +def index(): + return render_template('main.html', title=cfg.title, services=services["services"]) +@app.route('/reload.html') +def reload(): + f=open(os.path.join(package_directory, '../services.yml'),"r") + global services + services=yaml.safe_load(f) + f.close() + return redirect('/') diff --git a/flaskapp/functions.py b/flaskapp/functions.py new file mode 100644 index 0000000..a52ffe2 --- /dev/null +++ b/flaskapp/functions.py @@ -0,0 +1,164 @@ +# List all non *.md files within the directory +def list_dir_md(mypath): + return [f for f in os.listdir(mypath) if isfile(os.path.join(mypath, f)) and re.match('.*\%s.*' % FLATPAGES_EXTENSION,f) is None] + +# List all jpg images within the directory +def list_dir_img(mypath): + return [f for f in os.listdir(mypath) if isfile(os.path.join(mypath, f)) and re.match('.*\.jpg',f) is not None] + +def path_depth(path): + p_split=path.split('/') + if path =="": + cc=0 + else: + cc=len(path.split('/')) + if p_split[-1]=='index' or p_split[-1]=='index.json' : + cc=cc-1 + return cc + +# List all subpages of the current page +def get_sub_pages(path): + return [p for p in flatpages + if (p.path.startswith(path) and # is a subpage + ( re.match('.*index',p.path) is None) and # is no index + path_depth(p.path) [a/index a/b/index] +def get_breadcrumb_paths(path): + elements = path.split('/') + elements2 = ['index'] + for i in range(1,len(elements)): + elements2.append(pjoin2(elements[0:i])+u'/index') + return elements2 + +def get_breadcrumb_pages(paths): + pages=[flatpages.get(p) for p in paths] + return pages + +def get_paths(pages): + paths=[p.path for p in pages] + return paths + +def get_bc(path): + return get_breadcrumb_pages(get_breadcrumb_paths(path)) + +# load a page from a path information +def get_flatpage(path): + is_index=False + # root index + if path == 'index' or path=='': + is_index=True + path='' + pathi='index' + else: + if path.split('/')[-1]=='index': # last element is "index" ? + is_index=True + path='/'.join(path.split('/')[0:-1]) # remove index from path + pathi = u'{}/{}'.format(path, 'index') # try to add index to the path + page = flatpages.get(pathi) # try to load index page + if not page is None: # if successful it is a index + is_index=True + else: + page=flatpages.get(path) + return (is_index, path, page) + + +def pjoin (rt,pth): + return u'{}/{}'.format(rt,pth) + +def pjoin2 (pth): + return u'/'.join(pth) or u'' + +def misskey(a,key): + if not a.has_key(key): + return True + return a[key] is None + +def page_defaults(page, is_index, path): + if misskey(page.meta,"title"): + page.meta["title"]=path.split('/')[-1] + if misskey(page.meta, "template"): + page.meta["template"]='page.html' + return page + +@app.route('//') +def post(name='index'): + is_index, path, page = get_flatpage(name) + app.logger.info('render '+name) + path2 = pjoin(FLATPAGES_ROOT,path) + + if is_index == True and not page is None: + ld=list_dir_md(path2) + il=list_dir_img(path2) + sp=get_sub_pages(path) + spi=get_sub_ipages(path) + else: + ld=[] + sp=[] + spi=[] + il=[] + if not page is None: + page_defaults(page,is_index,path) + app.logger.info("Render Template"+page["template"] +"for "+path) + return render_template(page.meta["template"], ld=ld, post=page, sp=sp, spi=spi, il=il, pth=path, pagebreadcrumbs=get_bc(path)) + + if os.path.exists(u'{}/{}'.format(FLATPAGES_ROOT,path)): + return send_from_directory(FLATPAGES_ROOT,path) + else: + return send_from_directory('static',path) + + + +@app.route('/.json/') +def postjson(name='index'): + is_index, path, page = get_flatpage(name) + app.logger.info('render '+name) + path2 = pjoin(FLATPAGES_ROOT,path) + + if is_index == True and not page is None: + ld=list_dir_md(path2) + il=list_dir_img(path2) + sp=get_sub_pages(path) + spi=get_sub_ipages(path) + else: + ld=[] + sp=[] + spi=[] + il=[] + if not page is None: + page_defaults(page,is_index,path) + app.logger.info("Render Template"+page["template"] +"for "+path) + p=page.meta + bc=get_breadcrumb_paths(path) + p["depth"]=path_depth(path) + p["path"]=path + p["breadcrumbs"]=bc + p["subfolders"]=get_paths(spi) + p["subpages"]= [u'%s.json' % p1 for p1 in get_paths(sp)] + p["text"]=page.html + return jsonify(page=p) + # return render_template(page.meta["template"], ld=ld, post=page, sp=sp, spi=spi, il=il, pth=path, pagebreadcrumbs=get_bc(path,page)) + + + if os.path.exists(u'{}/{}'.format(FLATPAGES_ROOT,path)): + return send_from_directory(FLATPAGES_ROOT,path) + else: + return send_from_directory('static',path) + diff --git a/flaskapp/templates/layout.html b/flaskapp/templates/layout.html new file mode 100644 index 0000000..e315654 --- /dev/null +++ b/flaskapp/templates/layout.html @@ -0,0 +1,24 @@ + + + +Triton - Home + + + + + +
+
+
+

FET Webservices - triton.fet.at


+

Dieser Server ist der Proxy Server und Host für alle FET Web Services

+
+
+
+ {% block main %} + {% endblock %} +
+ + + +
sdf
diff --git a/flaskapp/templates/main.html b/flaskapp/templates/main.html new file mode 100644 index 0000000..c0bcac0 --- /dev/null +++ b/flaskapp/templates/main.html @@ -0,0 +1,28 @@ +{% extends "layout.html" %} +{% block main %} +{% for services_row in services | batch (3) %} +
+ {% for s in services_row %} + + {% endfor %} +
+{% endfor %} + + +{% endblock %} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1d3e7b7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +Frozen-Flask +Flask +PyYAML diff --git a/run.py b/run.py new file mode 100644 index 0000000..bc6806a --- /dev/null +++ b/run.py @@ -0,0 +1,11 @@ +from flaskapp import app,freezer +import sys + +if __name__ == "__main__": + if len(sys.argv) > 1 and sys.argv[1] == "build": + freezer.freeze() + elif len(sys.argv) > 1 and sys.argv[1] == "freezer": + freezer.run(host='0.0.0.0',port=4444, debug=True) + else: + app.run(host='0.0.0.0',port=4444, debug=True) + diff --git a/services.yml.sample b/services.yml.sample new file mode 100644 index 0000000..257c6d9 --- /dev/null +++ b/services.yml.sample @@ -0,0 +1,18 @@ +services: + - name: "fet.at" + desc: "FET Website" + url: "https://www.fet.at" + - name: "cloud.fet" + url: "https://cloud.fet.at" + desc: "nextcloud Instanz" + icon: "fa-cloud" + - name: "Interne Dokumentation" + desc: "dynamisch generierte Doku in der fet cloud" + url: "https://intern.triton.fet.at/index/" + - name: "Ruby Test" + desc: "Testserver für Ruby Projekte (mogok)" + url: "https://testrby.triton.fet.at/" + - name: "Konfiguration und Management triton" + desc: "Konfiguration für triton" + icon: "fa-wrench" + url: "https://triton.fet.at/conf" \ No newline at end of file