annotate dash.py @ 14:3a9cb396905f

made a template how to use the web_view module
author sean
date Wed, 05 Aug 2015 13:24:25 +0200
parents d71f39618d6f
children 82d66f4488cd
rev   line source
4
3e66e2f92770 added LICENSE and license headers in the sourcecode. Added readme
sean
parents: 3
diff changeset
1 """
3e66e2f92770 added LICENSE and license headers in the sourcecode. Added readme
sean
parents: 3
diff changeset
2 This software is part of "Bottledash"
3e66e2f92770 added LICENSE and license headers in the sourcecode. Added readme
sean
parents: 3
diff changeset
3 author: sean engelhardt > sean.engelhardt@intevation.de
3e66e2f92770 added LICENSE and license headers in the sourcecode. Added readme
sean
parents: 3
diff changeset
4 license: GNU >= V2. See LICENSE for details
3e66e2f92770 added LICENSE and license headers in the sourcecode. Added readme
sean
parents: 3
diff changeset
5 """
3e66e2f92770 added LICENSE and license headers in the sourcecode. Added readme
sean
parents: 3
diff changeset
6
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
7 from bottle import get, post, request, view, response, route
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
8 from bottle import template, run, static_file, error
10
d71f39618d6f used another workaround to locate the correct template
sean
parents: 9
diff changeset
9 import os.path,sys,configparser,functools,bottle
0
e1264e58154c initial commpit
sean
parents:
diff changeset
10
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
11 CONFIG_FILE = "dash.conf"
8
8fc4db85f2f5 fixed a path error
sean
parents: 7
diff changeset
12 PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
8fc4db85f2f5 fixed a path error
sean
parents: 7
diff changeset
13 CONFIG_PATH = PATH + "/" + CONFIG_FILE
8fc4db85f2f5 fixed a path error
sean
parents: 7
diff changeset
14
9
b06cb7fbf7f8 updated template paths for daemon usage
sean
parents: 8
diff changeset
15 # Create a new list with absolute paths
b06cb7fbf7f8 updated template paths for daemon usage
sean
parents: 8
diff changeset
16 MY_TEMPLATE_PATH = [
10
d71f39618d6f used another workaround to locate the correct template
sean
parents: 9
diff changeset
17 os.path.abspath(os.path.join(os.path.dirname(__file__), './views')),
9
b06cb7fbf7f8 updated template paths for daemon usage
sean
parents: 8
diff changeset
18 ]
b06cb7fbf7f8 updated template paths for daemon usage
sean
parents: 8
diff changeset
19
b06cb7fbf7f8 updated template paths for daemon usage
sean
parents: 8
diff changeset
20 # Patch @view() so it uses the customized path list instead of the global one
b06cb7fbf7f8 updated template paths for daemon usage
sean
parents: 8
diff changeset
21 view = functools.partial(bottle.view, template_lookup=MY_TEMPLATE_PATH)
b06cb7fbf7f8 updated template paths for daemon usage
sean
parents: 8
diff changeset
22
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
23 tiles = []
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
24 settings = {}
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
25
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
26 default_settings = configparser.ConfigParser()
10
d71f39618d6f used another workaround to locate the correct template
sean
parents: 9
diff changeset
27 default_settings['settings'] = {'show_top_bar': False}
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
28
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
29 ##read the config file. usually "dash.conf"
8
8fc4db85f2f5 fixed a path error
sean
parents: 7
diff changeset
30
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
31 def read_config():
8
8fc4db85f2f5 fixed a path error
sean
parents: 7
diff changeset
32
8fc4db85f2f5 fixed a path error
sean
parents: 7
diff changeset
33 # print(PATH)
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
34 global settings
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
35
8
8fc4db85f2f5 fixed a path error
sean
parents: 7
diff changeset
36 if os.path.isfile(CONFIG_PATH) == False:
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
37 write_default_config()
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
38
8
8fc4db85f2f5 fixed a path error
sean
parents: 7
diff changeset
39 print("read existing config file...")
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
40 config = configparser.ConfigParser()
8
8fc4db85f2f5 fixed a path error
sean
parents: 7
diff changeset
41 config.read(CONFIG_PATH)
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
42
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
43 for section in config.items():
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
44 if "tile" in section[0]:
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
45 tiles.append(section[1])
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
46
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
47 try:
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
48 if config["settings"]:
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
49 settings = config["settings"]
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
50 else:
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
51 settings = default_settings["settings"]
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
52 except KeyError:
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
53 settings = default_settings["settings"]
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
54
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
55 ###write the default condfig file if there is none
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
56 def write_default_config():
8
8fc4db85f2f5 fixed a path error
sean
parents: 7
diff changeset
57 print("there is no Config file! Creating one...")
8fc4db85f2f5 fixed a path error
sean
parents: 7
diff changeset
58 file = open(CONFIG_PATH, "w")
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
59 file.write("""
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
60 ### bottledash default configuration
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
61 ### created by sean engelhardt >sean.engelhardt@intevation.de
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
62 ### license: GNU GPL >= v2. See LICENSE for details
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
63 ###
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
64 ### Usage:
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
65 ### define the tiles for the dashboard in sections
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
66
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
67 # Settings:
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
68 # example:
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
69 # --------------------
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
70 # [settings]
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
71 # show_top_bar=True
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
72 # --------------------
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
73 # options:
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
74 # show_top_bar (True / False) [optional]
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
75 # If True, the dashboard will show the current date and the time on the top
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
76 # if False, the dashboard will not show a top-bar
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
77 # default: True
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
78 # hint: the top-bar has got a height of 5%!
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
79
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
80 [settings]
14
3a9cb396905f made a template how to use the web_view module
sean
parents: 10
diff changeset
81 show_top_bar=False
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
82
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
83 # Tiles:
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
84 # example:
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
85 # --------------------
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
86 # [tile1]
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
87 # type=mon
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
88 # source=192.168.0.2
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
89 # status=up
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
90 # --------------------
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
91 # options:
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
92 # type (mon / d3js) [required]
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
93 # tells the program what kind of tile you need.
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
94 # a "mon" tile can be used for IT infrastructure monitoring purposes
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
95 # a d3js tile can be used to display a chart
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
96 # default: -
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
97 #
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
98 # source (<IP> or <FQDN>) [required for mon-types]
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
99 # ONLY FOR MON-Type tiles!
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
100 # tells the tile which resource to watch
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
101 # default: none
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
102 #
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
103 # status: (up / down) [required]
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
104 # ONLY FOR DEBUGGING PURPOSE - WILL BE REMOVED LATER
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
105 # simulates up and down events for mon-type-tiles
14
3a9cb396905f made a template how to use the web_view module
sean
parents: 10
diff changeset
106 #
3a9cb396905f made a template how to use the web_view module
sean
parents: 10
diff changeset
107 # div_name: (identifier) [required for d3js-types]
3a9cb396905f made a template how to use the web_view module
sean
parents: 10
diff changeset
108 #
3a9cb396905f made a template how to use the web_view module
sean
parents: 10
diff changeset
109 # script: (name of a script without extension) [required for d3js-types0]
3a9cb396905f made a template how to use the web_view module
sean
parents: 10
diff changeset
110 #
3a9cb396905f made a template how to use the web_view module
sean
parents: 10
diff changeset
111
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
112
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
113 [tile1]
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
114 type=mon
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
115 source=192.168.0.2
14
3a9cb396905f made a template how to use the web_view module
sean
parents: 10
diff changeset
116 status=down
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
117
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
118 [tile2]
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
119 type=mon
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
120 source=192.168.2.3
14
3a9cb396905f made a template how to use the web_view module
sean
parents: 10
diff changeset
121 status=up
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
122
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
123 [tile3]
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
124 type=mon
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
125 source=192.168.4.3
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
126 status=up
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
127
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
128 [tile4]
14
3a9cb396905f made a template how to use the web_view module
sean
parents: 10
diff changeset
129 type=d3js
3a9cb396905f made a template how to use the web_view module
sean
parents: 10
diff changeset
130 div_name=techintern
3a9cb396905f made a template how to use the web_view module
sean
parents: 10
diff changeset
131 script=display_issues_techintern
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
132
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
133 """)
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
134 file.close()
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
135
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
136 ##Bottle
0
e1264e58154c initial commpit
sean
parents:
diff changeset
137 @route('/')
e1264e58154c initial commpit
sean
parents:
diff changeset
138 @view('hello_template')
e1264e58154c initial commpit
sean
parents:
diff changeset
139 def call_dashboard():
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
140 return dict(tiles=tiles, settings=settings)
0
e1264e58154c initial commpit
sean
parents:
diff changeset
141
e1264e58154c initial commpit
sean
parents:
diff changeset
142 @route('/config')
e1264e58154c initial commpit
sean
parents:
diff changeset
143 def call_config():
e1264e58154c initial commpit
sean
parents:
diff changeset
144 return 'Not implemented yet'
e1264e58154c initial commpit
sean
parents:
diff changeset
145
e1264e58154c initial commpit
sean
parents:
diff changeset
146
e1264e58154c initial commpit
sean
parents:
diff changeset
147 @route('/static/<filepath:path>')
e1264e58154c initial commpit
sean
parents:
diff changeset
148 def server_static(filepath):
e1264e58154c initial commpit
sean
parents:
diff changeset
149 return static_file(filepath, root='./static_files/')
e1264e58154c initial commpit
sean
parents:
diff changeset
150
e1264e58154c initial commpit
sean
parents:
diff changeset
151 @error(404)
e1264e58154c initial commpit
sean
parents:
diff changeset
152 def error404(error):
e1264e58154c initial commpit
sean
parents:
diff changeset
153 return 'Nothing here, sorry <br /> 404'
e1264e58154c initial commpit
sean
parents:
diff changeset
154
3
3f5bcad45756 pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents: 0
diff changeset
155 read_config()
7
c8cb2aa0b72c fixed default condifuration
sean
parents: 5
diff changeset
156 # print(tiles)
9
b06cb7fbf7f8 updated template paths for daemon usage
sean
parents: 8
diff changeset
157 run(host='localhost', port=8080, debug=True)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)