annotate globalneighbors/web.py @ 13:94af113e498a

we have links
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 14:04:49 -0700
parents d1b99c695511
children 27925261c137
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
11
d1b99c695511 remove obselete data
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
13 from paste import httpserver
d1b99c695511 remove obselete data
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
14 from paste.urlparser import StaticURLParser
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 from webob import Request, Response, exc
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 from wsgiref import simple_server
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 from .locations import locations
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 from .locations import city_dict
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 from .read import read_cities
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 from .read import read_city_list
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 from .schema import fields
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 from .schema import name
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
23 from .template import template_dir
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
24 from .template import TemplateLoader
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
10
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
26 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
27 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
28
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 class PassthroughFileserver(object):
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 """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
33
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
34
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
35 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
36 self.app = app
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
37 self.directory = directory
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
38 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
39
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
40 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
41
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
42 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
43
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
44 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
45
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
46 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
47 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
48
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
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 def autocomplete(cities, startswith=None, limit=None):
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
51 """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
52 ### 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
53 # - 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
54 # - return most populous cities
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
55
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
56 if startswith:
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
57 retval = []
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
58 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
59 if i[name].startswith(startswith):
13
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
60 retval.append({"name": i[name],
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
61 "geonameid": i['geonameid']})
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
62 else:
13
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
63 retval = [{"name": i[name],
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
64 "geonameid": i['geonameid']}
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
65 for i in cities]
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
66 return sorted(retval)[:limit]
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
67
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
68
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69 class Handler(object):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 """base class for HTTP handler"""
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 def __call__(self, environ, start_response):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 request = Request(environ)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 method = getattr(self, request.method, None)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 response = None
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76 if method is not None:
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77 response = method(request)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78 if response is None:
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79 response = exc.HTTPNotFound()
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80 return response(environ, start_response)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81
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 class CitiesHandler(Handler):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 """cities ReST API"""
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86 content_type = 'application/json'
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 __init__(self, locations, cities):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
89 self.locations = locations
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
90 self._cities = cities
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
91
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 cities(self, startswith=None):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
94 """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
95 return autocomplete(self._cities.values(),
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
96 startswith=startswith)
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 def GET(self, request):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
99 return Response(content_type=self.content_type,
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
100 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
101 startswith=request.GET.get('term'))))
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
102
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
103
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
104 class GlobalHandler(Handler):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
105 """WSGI HTTP Handler"""
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
106
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
107 content_type = 'text/html'
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
108
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
109 def __init__(self, datafile, template_dir=template_dir):
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
110
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
111
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
112 # parse data
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
113 self.datafile = datafile
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
114 self.cities = read_city_list(self.datafile,
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
115 fields=fields)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
116 self.locations = locations(self.cities)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
117
13
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
118 # get country codes
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
119 self.country_codes = sorted(set([city['country code']
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
120 for city in self.cities
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
121 if city['country code']]))
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
122
10
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
123 # 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
124 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
125 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
126
13
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
127
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
128 # declare handlers
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
129 self.handlers = {'/cities':
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
130 CitiesHandler(self.locations,
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
131 self.cities)}
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
132
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
133 # template loader
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
134 self.loader = TemplateLoader(template_dir)
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
135 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
136 self.citypage = self.loader.load('city.html')
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
137
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
138
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
139 def GET(self, request):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
140 if request.path_info in ('', '/'):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
141 # Landing page
13
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
142 body = self.index.render(n_cities=len(self.cities),
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
143 country_codes=self.country_codes)
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
144 return Response(content_type=self.content_type,
13
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
145 body=body)
94af113e498a we have links
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
146
10
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
147 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
148 return request.get_response(self.handlers[request.path_info])
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
149 else:
10
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
150 try:
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
151 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
152 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
153 if not city:
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
154 return
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
155 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
156 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
157 except ValueError:
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
158 pass
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
159
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
160
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
161
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
162 def main(args=sys.argv[1:]):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
163 """CLI"""
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
164
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
165 # parse command line
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
166 parser = argparse.ArgumentParser(description=__doc__)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
167 parser.add_argument('cities',
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
168 help="cities1000 data file")
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
169 parser.add_argument('-p', '--port', dest='port',
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
170 type=int, default=8080,
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
171 help="port to serve on")
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
172 parser.add_argument('--hostname', dest='hostname',
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
173 default='localhost',
6
316e1d54ffd4 move to jinja templates
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
174 help="host name [DEFAULT: %(default)s]")
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
175 options = parser.parse_args(args)
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 # instantiate WSGI handler
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
178 app = GlobalHandler(datafile=options.cities)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
179
11
d1b99c695511 remove obselete data
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
180 # wrap it in a static file server
d1b99c695511 remove obselete data
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
181 app = PassthroughFileserver(app)
d1b99c695511 remove obselete data
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
182
d1b99c695511 remove obselete data
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
183 print ("Serving on http://{hostname}:{port}/".format(**options.__dict__))
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
184 try:
11
d1b99c695511 remove obselete data
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
185 httpserver.serve(app, host='0.0.0.0', port=options.port)
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
186 except KeyboardInterrupt:
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
187 pass
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
188
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
189
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
190 if __name__ == '__main__':
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
191 main()