annotate example/example.py @ 26:ccd73b01ba79 default tip

py3
author Jeff Hammel <k0scist@gmail.com>
date Tue, 03 Nov 2020 10:00:32 -0800
parents 099f270654ae
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
1 #!/usr/bin/env python
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
2
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
3 """
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
4 site map example application
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
5 """
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
6
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
7 # imports
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
8 import argparse
1
084088505eea almost working
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
9 import os
22
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
10 import sys
0
7a60bacc6a22 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 from svgsitemap import *
7a60bacc6a22 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 from webob import Request, Response
22
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
13 from wsgiref import simple_server
0
7a60bacc6a22 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
7a60bacc6a22 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 def example(environ, start_response):
22
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
16 """example WSGI app"""
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
17
0
7a60bacc6a22 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 link = '<a href="/%s">%s</a>'
7a60bacc6a22 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 body = '<br/>'.join([link % (i,i) for i in range(30)])
2
30d03e830354 compute line widths
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
20 body = '<html><body><a href="/map">map</a><br/>%s</body></html>' % body
1
084088505eea almost working
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
21 response = Response(content_type='text/html', body=body)
0
7a60bacc6a22 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 return response(environ, start_response)
7a60bacc6a22 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23
22
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
24
0
7a60bacc6a22 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 def factory():
22
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
26 """WSGI app factory"""
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
27
1
084088505eea almost working
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
28 dirname = os.path.dirname(os.path.abspath(__file__))
084088505eea almost working
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
29 inifile = os.path.join(dirname, 'example.gv.txt')
084088505eea almost working
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
30 svgfile = os.path.join(dirname, 'example.svg')
084088505eea almost working
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
31 app = MapserverMiddleware(example, svgfile)
9
aa4eab6dc994 * dont set node width, height; * move save() to its own function
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
32 app = SVGSiteMap(app, file=inifile, output=svgfile)
1
084088505eea almost working
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
33 return app
0
7a60bacc6a22 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34
23
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
35 def main(args=sys.argv[1:]):
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
36 """CLI"""
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
37
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
38 # parse command line options
Jeff Hammel <k0scist@gmail.com>
parents: 22
diff changeset
39 parser = argparse.ArgumentParser(description=__doc__)
24
099f270654ae more wip
Jeff Hammel <k0scist@gmail.com>
parents: 23
diff changeset
40 options = parser.parse_args(args)
22
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
41
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
42 server = simple_server.make_server(host='0.0.0.0', port=int(54321), app=factory())
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
43 server.serve_forever()
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
44
0
7a60bacc6a22 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 if __name__ == '__main__':
22
1b077cb84be5 make the example more of a real program
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
46 main()