view dash.py @ 12:50f4c64834cb

delegation of dynamic charts works now
author sean
date Tue, 04 Aug 2015 16:00:07 +0200
parents d71f39618d6f
children 3a9cb396905f
line wrap: on
line source
"""
    This software is part of "Bottledash"
    author: sean engelhardt > sean.engelhardt@intevation.de
    license: GNU >= V2. See LICENSE for details
"""

from bottle import get, post, request, view, response, route
from bottle import template, run, static_file, error
import os.path,sys,configparser,functools,bottle

CONFIG_FILE = "dash.conf"
PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
CONFIG_PATH = PATH + "/" + CONFIG_FILE

# Create a new list with absolute paths
MY_TEMPLATE_PATH = [
   os.path.abspath(os.path.join(os.path.dirname(__file__), './views')),
]

# Patch @view() so it uses the customized path list instead of the global one
view = functools.partial(bottle.view, template_lookup=MY_TEMPLATE_PATH)

tiles = []
settings = {}

default_settings = configparser.ConfigParser()
default_settings['settings'] = {'show_top_bar': False}

##read the config file. usually "dash.conf"

def read_config():

    # print(PATH)
    global settings

    if os.path.isfile(CONFIG_PATH) == False:
        write_default_config()

    print("read existing config file...")
    config = configparser.ConfigParser()
    config.read(CONFIG_PATH)

    for section in config.items():
        if "tile" in section[0]:
            tiles.append(section[1])

    try:
        if config["settings"]:
            settings = config["settings"]
        else:
            settings = default_settings["settings"]
    except KeyError:
        settings = default_settings["settings"]

###write the default condfig file if there is none
def write_default_config():
    print("there is no Config file! Creating one...")
    file = open(CONFIG_PATH, "w")
    file.write("""
### bottledash default configuration
### created by sean engelhardt >sean.engelhardt@intevation.de
### license: GNU GPL >= v2. See LICENSE for details
###
### Usage:
### define the tiles for the dashboard in sections

# Settings:
# example:
# --------------------
# [settings]
# show_top_bar=True
# --------------------
# options:
#   show_top_bar (True / False) [optional]
#     If True, the dashboard will show the current date and the time on the top
#     if False, the dashboard will not show a top-bar
#   default: True
#   hint: the top-bar has got a height of 5%!

[settings]
show_top_bar=True

# Tiles:
# example:
# --------------------
# [tile1]
# type=mon
# source=192.168.0.2
# status=up
# --------------------
# options:
#   type (mon / d3js) [required]
#     tells the program what kind of tile you need.
#     a "mon" tile can be used for IT infrastructure monitoring purposes
#     a d3js tile can be used to display a chart
#   default: -
#
#   source (<IP> or <FQDN>) [required for mon-types]
#     ONLY FOR MON-Type tiles!
#     tells the tile which resource to watch
#   default: none
#
#   status: (up / down) [required]
#     ONLY FOR DEBUGGING PURPOSE - WILL BE REMOVED LATER
#     simulates up and down events for mon-type-tiles

[tile1]
type=mon
source=192.168.0.2
status=up

[tile2]
type=mon
source=192.168.2.3
status=down

[tile3]
type=mon
source=192.168.4.3
status=up

[tile4]
type=mon
source=192.168.4.3
status=down

""")
    file.close()

##Bottle
@route('/')
@view('hello_template')
def call_dashboard():
    return dict(tiles=tiles, settings=settings)

@route('/config')
def call_config():
    return 'Not implemented yet'


@route('/static/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='./static_files/')

@error(404)
def error404(error):
    return 'Nothing here, sorry <br /> 404'

read_config()
# print(tiles)
run(host='localhost', port=8080, debug=True)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)