forked from OpenGeoscience/geoweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoweb.py
executable file
·118 lines (90 loc) · 3.28 KB
/
geoweb.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
#!/usr/bin/python
'''
cherryd -i py -c py/app.conf
'''
import cherrypy
import imp
import json
import os
import re
import sys
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(current_dir, "modules"))
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
import modules.geowebsocket #used by config
from mako.template import Template
from mako.lookup import TemplateLookup
template_dir = os.path.dirname(os.path.abspath(__file__))
lookup = TemplateLookup(directories=[template_dir])
#render demo
from ogsvtk import VTKRoot
#make sure WebSocketPlugin runs after daemonizer plugin (priority 65)
#see cherrypy plugin documentation for default plugin priorities
WebSocketPlugin.start.__func__.priority = 66
#init websocket plugin
WebSocketPlugin(cherrypy.engine).subscribe()
cherrypy.tools.websocket = WebSocketTool()
# Utility functions
def empty_response():
return {'result': None, 'error': None}
def empty_result():
return {}
services = dict()
class Root(object):
vtk = VTKRoot(host='127.0.0.1', port=8080, ssl=False)
@cherrypy.expose
def mongo(self, *args, **kwargs):
import mongo
pargs = list(args)
return mongo.run(*pargs, **kwargs)
@cherrypy.expose
def data(self, *args, **kwargs):
import geodata
pargs = list(args)
return geodata.run(*pargs, **kwargs);
@cherrypy.expose
def ws(self):
cherrypy.log("Handler created: %s" % repr(cherrypy.request.ws_handler))
@cherrypy.expose
def default(self, *args, **kwargs):
# If there are no positional arguments, abort
if len(args) == 0:
raise cherrypy.HTTPError(404)
# Convert the args into a list (from a tuple).
path = list(args)
runningPath = current_dir
response = {'result': None, 'error': None}
def stripAllSpecial(str):
pattern = re.compile('[\W]')
return pattern.sub('', str)
for i in range(len(path)):
service = None
runningPath = os.path.join(runningPath, path[i])
if runningPath in services:
service = services[runningPath]
elif os.path.exists(runningPath + '.py'):
try:
service = services[runningPath] = \
imp.load_source("__imp_%s__" %
stripAllSpecial(runningPath),
runningPath + '.py')
except IOError as e:
error = "IOError: %s" % e
cherrypy.log(error)
response['error'] = error
return json.dumps(response)
if service is not None:
if hasattr(service, 'run'):
pathArgs = path[i+1:]
response['result'] = service.run(*pathArgs, **kwargs)
return json.dumps(response)
else:
error = "`run` not defined in service %s" % runningPath
cherrypy.log(error)
response['error'] = error
return json.dumps(response)
raise cherrypy.HTTPError(404)
if __name__ == '__main__':
import os.path
cherrypy.engine.start()
cherrypy.engine.block()