Mercurial > bottledash
annotate dash.py @ 10:d71f39618d6f
used another workaround to locate the correct template
author | sean |
---|---|
date | Wed, 29 Jul 2015 12:27:26 +0200 |
parents | b06cb7fbf7f8 |
children | 3a9cb396905f |
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 | 9 import os.path,sys,configparser,functools,bottle |
0 | 10 |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
11 CONFIG_FILE = "dash.conf" |
8 | 12 PATH = os.path.abspath(os.path.dirname(sys.argv[0])) |
13 CONFIG_PATH = PATH + "/" + CONFIG_FILE | |
14 | |
9 | 15 # Create a new list with absolute paths |
16 MY_TEMPLATE_PATH = [ | |
10 | 17 os.path.abspath(os.path.join(os.path.dirname(__file__), './views')), |
9 | 18 ] |
19 | |
20 # Patch @view() so it uses the customized path list instead of the global one | |
21 view = functools.partial(bottle.view, template_lookup=MY_TEMPLATE_PATH) | |
22 | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
23 tiles = [] |
7 | 24 settings = {} |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
25 |
7 | 26 default_settings = configparser.ConfigParser() |
10 | 27 default_settings['settings'] = {'show_top_bar': False} |
7 | 28 |
29 ##read the config file. usually "dash.conf" | |
8 | 30 |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
31 def read_config(): |
8 | 32 |
33 # print(PATH) | |
7 | 34 global settings |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
35 |
8 | 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 | 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 | 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 | 47 try: |
48 if config["settings"]: | |
49 settings = config["settings"] | |
50 else: | |
51 settings = default_settings["settings"] | |
52 except KeyError: | |
53 settings = default_settings["settings"] | |
54 | |
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 | 57 print("there is no Config file! Creating one...") |
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 | 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 | 66 |
67 # Settings: | |
68 # example: | |
69 # -------------------- | |
70 # [settings] | |
71 # show_top_bar=True | |
72 # -------------------- | |
73 # options: | |
74 # show_top_bar (True / False) [optional] | |
75 # If True, the dashboard will show the current date and the time on the top | |
76 # if False, the dashboard will not show a top-bar | |
77 # default: True | |
78 # hint: the top-bar has got a height of 5%! | |
79 | |
80 [settings] | |
81 show_top_bar=True | |
82 | |
83 # Tiles: | |
84 # example: | |
85 # -------------------- | |
86 # [tile1] | |
87 # type=mon | |
88 # source=192.168.0.2 | |
89 # status=up | |
90 # -------------------- | |
91 # options: | |
92 # type (mon / d3js) [required] | |
93 # tells the program what kind of tile you need. | |
94 # a "mon" tile can be used for IT infrastructure monitoring purposes | |
95 # a d3js tile can be used to display a chart | |
96 # default: - | |
97 # | |
98 # source (<IP> or <FQDN>) [required for mon-types] | |
99 # ONLY FOR MON-Type tiles! | |
100 # tells the tile which resource to watch | |
101 # default: none | |
102 # | |
103 # status: (up / down) [required] | |
104 # ONLY FOR DEBUGGING PURPOSE - WILL BE REMOVED LATER | |
105 # simulates up and down events for mon-type-tiles | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
106 |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
107 [tile1] |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
108 type=mon |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
109 source=192.168.0.2 |
7 | 110 status=up |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
111 |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
112 [tile2] |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
113 type=mon |
7 | 114 source=192.168.2.3 |
115 status=down | |
116 | |
117 [tile3] | |
118 type=mon | |
119 source=192.168.4.3 | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
120 status=up |
7 | 121 |
122 [tile4] | |
123 type=mon | |
124 source=192.168.4.3 | |
125 status=down | |
126 | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
127 """) |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
128 file.close() |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
129 |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
130 ##Bottle |
0 | 131 @route('/') |
132 @view('hello_template') | |
133 def call_dashboard(): | |
7 | 134 return dict(tiles=tiles, settings=settings) |
0 | 135 |
136 @route('/config') | |
137 def call_config(): | |
138 return 'Not implemented yet' | |
139 | |
140 | |
141 @route('/static/<filepath:path>') | |
142 def server_static(filepath): | |
143 return static_file(filepath, root='./static_files/') | |
144 | |
145 @error(404) | |
146 def error404(error): | |
147 return 'Nothing here, sorry <br /> 404' | |
148 | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
149 read_config() |
7 | 150 # print(tiles) |
9 | 151 run(host='localhost', port=8080, debug=True) |