-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
372 lines (337 loc) · 12.7 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""
MIT License
Copyright (c) 2017-2020 Ioan Coman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import os
import sys
import string
import json
import bottle
py = sys.version_info
py3k = py >= (3, 0, 0)
def html_redirect(location):
return '<!-- Redirect to "{0}" -->\n<html><meta http-equiv="refresh" content="0;url={0}"><body>Redirecting...</body></html>'.format(
location)
def get_props(configFile):
"""
Load webserver config
"""
DATAFOLDER = os.getcwd()
configFilename = os.path.sep.join((DATAFOLDER, configFile))
if not os.path.isfile(configFilename):
DATAFOLDER = os.path.sep.join((os.getcwd(), '..'))
configFilename = os.path.sep.join((DATAFOLDER, configFile))
ret = _get_props(configFilename, '=')
ret['DATAFOLDER'] = DATAFOLDER
return ret
def _get_props(filename, sep, comment=";#"):
#
# sep = separator and may be :, =, etc.
# comment = a list of chars which define starting of comment
#
ret = {}
overWriteProp = True
# overWriteProp = False #props with same name are grouped into a list
try:
accumulator = ''
flag = False
with open(filename, "rt") as f:
while True:
line = f.readline()
if not line:
# line must have at least '\r\n' (Windows) or '\n' (Unix)
break
line = line.replace('\r', '')
line = line.replace('\n', '')
line = line.lstrip()
if not line:
# line is empty
continue
if line[0] in comment:
# line starts with a comment
continue
for c in comment:
# remove commented section
ix = line.find(c)
if -1 != ix:
line = line[:line.find(c)]
if line[-1] == '\\':
# current line will continue on next line
line = line[:-1]
if flag:
# add to accumulator
accumulator = ' '.join((accumulator, line))
else:
# set
accumulator = line
flag = True
continue
if flag:
# add last part of line to accumulator
accumulator = ' '.join((accumulator, line))
line = accumulator
accumulator = ''
flag = False
# split line to key and value
ix = line.find(sep)
if ix > 0:
key = line[:ix].rstrip()
value = line[ix + 1:].lstrip().rstrip()
try:
# if value might be a number
value = int(value)
except:
pass
if overWriteProp:
ret[key] = value
else:
# append
old = ret.get(key)
if old:
if type(old) is list:
# already a list
ret[key].append(value)
else:
ret[key] = [old, value]
else:
ret[key] = value
except Exception as ex:
print(ex)
return ret
def _make_login_logout(user, icon, loginurl, logouturl):
if user:
title = 'Logout {}'.format(user)
link = logouturl
else:
title = 'Login'
link = loginurl
if icon:
icontitle = '<span class="glyphicon glyphicon-{}"></span> {}'.format(
icon, title)
else:
icontitle = title
return icontitle, link
def make_bootstrap_navbar(url, bs, menufile, loginurl, logouturl, brandurl, brandtitle):
userfullname = bs.get('userfullname') or bs.get('username')
if not py3k and userfullname:
userfullname = userfullname.encode('utf8')
user_groups = bs.get('groups') or [0]
navcode = ''
L = []
menuitem = []
with open(menufile, 'rt') as f:
for line in f.read().split('\n'):
line = line.replace('\r', '')
if line.startswith('#'):
continue
if not line.lstrip():
continue
link, title, icon, groups, description = line.split('|')
if not groups:
groups = '[-1]' # Default is all authenticated users
groups = json.loads(groups)
if line[0] in string.whitespace:
if menuitem:
menuitem[5].append(
(link.strip(), title, icon, groups, description))
else:
if menuitem:
L.append(menuitem)
menuitem = [link, title, icon, groups, description, []]
if menuitem:
L.append(menuitem)
for i in L:
link, title, icon, groups, description, childs = i
if icon:
icontitle = '<span class="glyphicon glyphicon-{}"></span> {}'.format(
icon, title)
else:
icontitle = title
hasAccess = False
for gr in user_groups:
if gr in groups:
hasAccess = True
break
if title == 'loginlogout':
hasAccess = True
if not hasAccess:
continue
if len(childs) > 0:
menuchilds = ''
for ch in childs:
ch_link, ch_title, ch_icon, ch_groups, ch_description = ch
hasAccess = False
for gr in user_groups:
if gr in ch_groups:
hasAccess = True
break
if title == 'loginlogout':
hasAccess = True
if not hasAccess:
continue
if ch_icon:
ch_icontitle = '<span class="glyphicon glyphicon-{}"></span> {}'.format(
ch_icon, ch_title)
else:
ch_icontitle = ch_title
if ch_title == 'loginlogout':
ch_icontitle, ch_link = _make_login_logout(
userfullname, ch_icon, loginurl, logouturl)
if ch_description:
ch_descr = ' title="{}"'.format(ch_description)
else:
ch_descr = ''
if ch_link == url:
active = ' class="active"'
else:
active = ''
menuchilds += '\n<li{}><a href="{}"{}>{}</a></li>'.format(
active, ch_link, ch_descr, ch_icontitle)
if link == url:
active = ' active'
else:
active = ''
navcode += '''
<li class="dropdown{}">
<a href="#" title="{}" class="dropdown-toggle" data-toggle="dropdown">{}<b class="caret"></b></a>
<ul class="dropdown-menu">{}</ul>
</li>'''.format(active, description, icontitle, menuchilds)
else:
# no childs
if description:
descr = ' title="{}"'.format(description)
else:
descr = ''
if title == 'loginlogout':
icontitle, link = _make_login_logout(
userfullname, icon, loginurl, logouturl)
if link == url:
active = ' active'
else:
active = ''
navcode += '\n<li{}><a href="{}"{}>{}</a></li>'.format(
active, link, descr, icontitle)
if brandurl and brandtitle:
brand = '<a href="{}" class="navbar-brand">{}</a>'.format(
brandurl, brandtitle)
else:
brand = ''
ret = '''
<header class="navbar navbar-inverse navbar-fixed-top bs-docs-nav" role="banner">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
{brand}
</div>
<nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation">
<ul class="nav navbar-nav navbar-right">
{navcode}
</ul>
</nav>
</div>
</header>
'''.format(**dict(navcode=navcode, brand=brand))
return ret
def load_config(config_file):
"""
Load a webserver module config
Use app from
https://github.com/icoman/PropertiesEditor_v1
to generate and edit config files
"""
config = {}
if os.path.isfile(config_file):
with open(config_file, 'rt') as f:
jsdoc = json.loads(f.read())
data = jsdoc.get('data', {})
for x in data:
ob = data[x]
name = ob.get('name')
value = ob.get('value')
if ob.get('type') in ('mc',):
# mc = multiple checks = a list read by ajax post
try:
_e = eval(str(value or "[]"))
if type(_e) is not type([]):
_e = [_e]
value = [int(x) for x in _e]
except:
value = []
config[name] = value
return config
class SSLWSGIRefServer(bottle.ServerAdapter):
def run(self, handler):
from wsgiref.simple_server import make_server, WSGIRequestHandler
import ssl
if self.quiet:
class QuietHandler(WSGIRequestHandler):
def log_request(*args, **kw):
# pylint: disable=E0211
pass
self.options['handler_class'] = QuietHandler
srv = make_server(self.host, self.port, handler, **self.options)
srv.socket = ssl.wrap_socket(
srv.socket,
certfile='server.pem', # path to certificate
server_side=True)
srv.serve_forever()
class myWaitressServer(bottle.ServerAdapter):
# Multi-threaded, poweres Pyramid
def run(self, handler):
from waitress import serve
serve(handler, host=self.host, port=self.port, threads=10)
class myPasteServer(bottle.ServerAdapter):
# Multi-threaded, stable, tried and tested
def run(self, handler): # pragma: no cover
from paste import httpserver
httpserver.serve(handler, host=self.host, port=str(self.port),
**self.options)
class myCherryPyServer(bottle.ServerAdapter):
# Multi-threaded and very stable
def run(self, handler): # pragma: no cover
import wsgiserver
self.options['bind_addr'] = (self.host, self.port)
self.options['wsgi_app'] = handler
certfile = self.options.get('certfile')
if certfile:
del self.options['certfile']
keyfile = self.options.get('keyfile')
if keyfile:
del self.options['keyfile']
server = wsgiserver.CherryPyWSGIServer(**self.options)
if certfile:
server.ssl_certificate = certfile
if keyfile:
server.ssl_private_key = keyfile
try:
server.start()
finally:
server.stop()
class myGeventWebSocketServer(bottle.ServerAdapter):
# Use this for websocket support
def run(self, handler):
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
server = pywsgi.WSGIServer(
(self.host, self.port), handler, handler_class=WebSocketHandler)
server.serve_forever()