annotate globalneighbors/web.py @ 10:21ed15391e8a

add a placeholder view for a city based on geoid
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 12:28:36 -0700
parents 254195d0bac2
children d1b99c695511
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 web handler for GlobalNeighbors
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 # imports
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import argparse
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import json
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
10 import os
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import sys
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import time
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 from webob import Request, Response, exc
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 from wsgiref import simple_server
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 from .locations import locations
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 from .locations import city_dict
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 from .read import read_cities
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 from .read import read_city_list
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 from .schema import fields
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 from .schema import name
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
21 from .template import template_dir
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
22 from .template import TemplateLoader
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23
10
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
24 here = os.path.dirname(os.path.abspath(__file__))
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
25 static_dir = os.path.join(here, 'static')
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
26
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
27
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
28 class PassthroughFileserver(object):
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
29
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
30 """serve files if they exist"""
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
31
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
32
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
33 def __init__(self, app, directory=static_dir):
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
34 self.app = app
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
35 self.directory = directory
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
36 self.fileserver = StaticURLParser(self.directory)
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
37
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
38 def __call__(self, environ, start_response):
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
39
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
40 path = environ['PATH_INFO'].strip('/')
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
41
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
42 if path and os.path.exists(os.path.join(self.directory, path)) and '..' not in path:
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
43
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
44 return self.fileserver(environ, start_response)
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
45 return self.app(environ, start_response)
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
46
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
7
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
48 def autocomplete(cities, startswith=None, limit=None):
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
49 """autocomplete function for city names"""
7
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
50 ### TODO:
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
51 # - sort once, ahead of time
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
52 # - return most populous cities
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
53
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
54 if startswith:
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
55 retval = []
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
56 for i in cities:
7
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
57 if i[name].startswith(startswith):
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
58 retval.append(i[name])
7
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
59 return sorted(retval)[:limit]
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
60 else:
7
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
61 return sorted([i[name] for i in cities])[:limit]
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
62
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
63
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 class Handler(object):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 """base class for HTTP handler"""
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 def __call__(self, environ, start_response):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68 request = Request(environ)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69 method = getattr(self, request.method, None)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 response = None
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 if method is not None:
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 response = method(request)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 if response is None:
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 response = exc.HTTPNotFound()
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 return response(environ, start_response)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78 class CitiesHandler(Handler):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79 """cities ReST API"""
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81 content_type = 'application/json'
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83 def __init__(self, locations, cities):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 self.locations = locations
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85 self._cities = cities
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
87
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
88 def cities(self, startswith=None):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
89 """return list of cities"""
10
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
90 return autocomplete(self._cities.values(),
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
91 startswith=startswith)
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
92
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
93 def GET(self, request):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
94 return Response(content_type=self.content_type,
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
95 body=json.dumps(self.cities(
7
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
96 startswith=request.GET.get('term'))))
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
97
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
98
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
99 class GlobalHandler(Handler):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
100 """WSGI HTTP Handler"""
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
101
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
102 content_type = 'text/html'
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
103
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
104 def __init__(self, datafile, template_dir=template_dir):
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
105
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
106
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
107 # parse data
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
108 self.datafile = datafile
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
109 self.cities = read_city_list(self.datafile,
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
110 fields=fields)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
111 self.locations = locations(self.cities)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
112
10
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
113 # convert cities to a dict for lookup
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
114 self.cities = {city['geonameid'] : city
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
115 for city in self.cities}
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
116
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
117 # declare handlers
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
118 self.handlers = {'/cities':
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
119 CitiesHandler(self.locations,
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
120 self.cities)}
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
121
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
122 # template loader
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
123 self.loader = TemplateLoader(template_dir)
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
124 self.index = self.loader.load('index.html')
10
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
125 self.citypage = self.loader.load('city.html')
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
126
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
127
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
128 def GET(self, request):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
129 if request.path_info in ('', '/'):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
130 # Landing page
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
131 return Response(content_type=self.content_type,
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
132 body=self.index.render(n_cities=len(self.cities)))
10
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
133 elif request.path_info in self.handlers:
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
134 return request.get_response(self.handlers[request.path_info])
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
135 else:
10
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
136 try:
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
137 geoid = int(request.path.strip('/'))
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
138 city = self.cities.get(geoid)
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
139 if not city:
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
140 return
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
141 return Response(content_type=self.content_type,
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
142 body=self.citypage.render(**city))
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
143 except ValueError:
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
144 pass
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
145
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
146
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
147
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
148 def main(args=sys.argv[1:]):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
149 """CLI"""
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
150
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
151 # parse command line
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
152 parser = argparse.ArgumentParser(description=__doc__)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
153 parser.add_argument('cities',
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
154 help="cities1000 data file")
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
155 parser.add_argument('-p', '--port', dest='port',
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
156 type=int, default=8080,
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
157 help="port to serve on")
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
158 parser.add_argument('--hostname', dest='hostname',
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
159 default='localhost',
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
160 help="host name [DEFAULT: %(default)s]")
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
161 options = parser.parse_args(args)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
162
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
163 # instantiate WSGI handler
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
164 app = GlobalHandler(datafile=options.cities)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
165
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
166 # serve it (Warning! Single threaded!)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
167 server = simple_server.make_server(host='0.0.0.0',
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
168 port=options.port,
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
169 app=app)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
170 try:
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
171 print ("Serving on http://{hostname}:{port}/".format(**options.__dict__))
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
172 server.serve_forever()
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
173 except KeyboardInterrupt:
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
174 pass
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
175
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
176
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
177 if __name__ == '__main__':
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
178 main()
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
179