Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: cms/bin/test_server.py

Issue 6451712864813056: Issue 2196 - [cms] Drop depencency on Flask for test server (Closed)
Patch Set: More test server improvements Created March 23, 2015, 11:50 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « README.md ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import sys, os, flask 18 import mimetypes
19 import os
20 import sys
21
22 import jinja2
23
19 from ..utils import process_page 24 from ..utils import process_page
20 from ..sources import FileSource 25 from ..sources import FileSource
21 from ..converters import converters 26 from ..converters import converters
22 27
23 app = flask.Flask("cms.bin.test_server")
24 source = None 28 source = None
25 29
26 mime_types = { 30 ERROR_404 = """
27 "": "text/html; charset=utf-8", 31 <html>
28 ".htm": "text/html; charset=utf-8", 32 <head>
29 ".html": "text/html; charset=utf-8", 33 <title>404 Not Found</title>
30 ".js": "application/javascript; charset=utf-8", 34 </head>
31 ".css": "text/css; charset=utf-8", 35 <body>
32 ".xml": "text/xml; charset=utf-8", 36 <h1>404 Not Found</h1>
33 ".png": "image/png", 37 <p>No page found for the address {{uri}}.</p>
34 ".jpg": "image/jpeg", 38 </body>
35 ".jpeg": "image/jpeg", 39 </html>"""
36 } 40
41 ERROR_500 = """
Sebastian Noack 2015/03/23 12:06:47 How about using a common template for both error p
Wladimir Palant 2015/03/23 12:27:26 Done.
42 <html>
43 <head>
44 <title>500 Internal Server Error</title>
45 </head>
46 <body>
47 <h1>500 Internal Server Error</h1>
48 <p>An error occurred while processing the request for {{uri}}:</p>
49 <pre>{{error}}</pre>
50 </body>
51 </html>"""
52
53 mime_types = mimetypes.MimeTypes()
54 mime_types.add_type("text/html", "")
37 55
38 def get_data(path): 56 def get_data(path):
39 if source.has_static(path): 57 if source.has_static(path):
40 return source.read_static(path) 58 return source.read_static(path)
41 59
42 path = path.rstrip("/") 60 path = path.strip("/")
43 if path == "": 61 if path == "":
44 path = source.read_config().get("general", "defaultlocale") 62 path = source.read_config().get("general", "defaultlocale")
45 if "/" in path: 63 if "/" in path:
46 locale, page = path.split("/", 1) 64 locale, page = path.split("/", 1)
47 else: 65 else:
48 locale, page = path, "" 66 locale, page = path, ""
49 67
50 default_page = source.read_config().get("general", "defaultpage") 68 default_page = source.read_config().get("general", "defaultpage")
51 alternative_page = "/".join([page, default_page]).lstrip("/") 69 alternative_page = "/".join([page, default_page]).lstrip("/")
52 for format in converters.iterkeys(): 70 for format in converters.iterkeys():
53 for p in (page, alternative_page): 71 for p in (page, alternative_page):
54 if source.has_page(p, format): 72 if source.has_page(p, format):
55 return process_page(source, locale, p, format, "http://127.0.0.1:5000"). encode("utf-8") 73 return process_page(source, locale, p, format, "http://127.0.0.1:5000")
56 if source.has_localizable_file(locale, page): 74 if source.has_localizable_file(locale, page):
57 return source.read_localizable_file(locale, page) 75 return source.read_localizable_file(locale, page)
58 76
59 return None 77 return None
60 78
61 @app.route("/", methods = ["GET"]) 79 def show_template(start_response, status, template_data, **kwargs):
62 @app.route("/<path:path>", methods = ["GET"]) 80 env = jinja2.Environment(autoescape=True)
63 def show(path=""): 81 template = env.from_string(template_data)
82 start_response(status, [("Content-Type", "text/html; encoding=utf-8")])
83 return [template.render(**kwargs).encode("utf-8")]
Sebastian Noack 2015/03/23 12:06:47 How about this? for fragment in template.stream
Wladimir Palant 2015/03/23 12:27:26 Done.
84
85 def handler(environ, start_response):
86 path = environ.get("PATH_INFO")
87
64 data = get_data(path) 88 data = get_data(path)
65 if data == None: 89 if data == None:
66 flask.abort(404) 90 return show_template(start_response, "404 Not Found", ERROR_404, uri=path)
67 91
68 root, ext = os.path.splitext(path) 92 mime = mime_types.guess_type(path)[0] or "application/octet-stream"
69 mime = mime_types.get(ext.lower(), "application/octet-stream") 93
70 return data, 200, {"Content-Type": mime} 94 if isinstance(data, unicode):
95 data = data.encode("utf-8")
96 mime += "; charset=utf-8"
Sebastian Noack 2015/03/23 12:06:47 Maybe we should add a (global) variable for the en
Wladimir Palant 2015/03/23 12:27:26 Done.
97
98 start_response("200 OK", [("Content-Type", mime)])
99 return [data]
71 100
72 if __name__ == "__main__": 101 if __name__ == "__main__":
73 if len(sys.argv) < 2: 102 if len(sys.argv) < 2:
74 print >>sys.stderr, "Usage: %s source_dir" % sys.argv[0] 103 print >>sys.stderr, "Usage: %s source_dir" % sys.argv[0]
75 sys.exit(1) 104 sys.exit(1)
76 105
77 source = FileSource(sys.argv[1]) 106 source = FileSource(sys.argv[1])
78 107
79 app.run(debug=True) 108 try:
109 from werkzeug.serving import run_simple
110 except ImportError:
111 from wsgiref.simple_server import make_server
112 def run_simple(host, port, app, **kwargs):
113 def wrapper(environ, start_response):
114 try:
115 return app(environ, start_response)
116 except Exception, e:
117 return show_template(start_response, "500 Internal Server Error",
118 ERROR_500, uri=environ.get("PATH_INFO"), error=e)
119
120 server = make_server(host, port, wrapper)
121 print " * Running on http://%s:%i/" % server.server_address
122 server.serve_forever()
123
124 run_simple("localhost", 5000, handler, use_reloader=True, use_debugger=True)
OLDNEW
« no previous file with comments | « README.md ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld