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

Delta Between Two Patch Sets: cms/bin/test_server.py

Issue 6451712864813056: Issue 2196 - [cms] Drop depencency on Flask for test server (Closed)
Left Patch Set: More test server improvements Created March 23, 2015, 11:50 a.m.
Right Patch Set: Addressed comments Created March 23, 2015, 12:26 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « README.md ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 mimetypes 18 import mimetypes
19 import os 19 import os
20 import sys 20 import sys
21 21
22 import jinja2 22 import jinja2
23 23
24 from ..utils import process_page 24 from ..utils import process_page
25 from ..sources import FileSource 25 from ..sources import FileSource
26 from ..converters import converters 26 from ..converters import converters
27 27
28 source = None 28 source = None
29 29
30 ERROR_404 = """ 30 UNICODE_ENCODING = "utf-8"
31
32 ERROR_TEMPLATE = """
31 <html> 33 <html>
32 <head> 34 <head>
33 <title>404 Not Found</title> 35 <title>{{status}}</title>
34 </head> 36 </head>
35 <body> 37 <body>
36 <h1>404 Not Found</h1> 38 <h1>{{status}}</h1>
37 <p>No page found for the address {{uri}}.</p> 39 {% set code = status.split()|first|int %}
40 {% if code == 404 %}
41 <p>No page found for the address {{uri}}.</p>
42 {% elif code == 500 %}
43 <p>An error occurred while processing the request for {{uri}}:</p>
44 <pre>{{error}}</pre>
45 {% endif %}
38 </body> 46 </body>
39 </html>""" 47 </html>"""
40 48
41 ERROR_500 = """ 49 # Create our own instance, the default one will introduce "random" host-specific
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> 50 # behavior by parsing local config files.
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() 51 mime_types = mimetypes.MimeTypes()
54 mime_types.add_type("text/html", "")
55 52
56 def get_data(path): 53 def get_data(path):
57 if source.has_static(path): 54 if source.has_static(path):
58 return source.read_static(path) 55 return source.read_static(path)
59 56
60 path = path.strip("/") 57 path = path.strip("/")
61 if path == "": 58 if path == "":
62 path = source.read_config().get("general", "defaultlocale") 59 path = source.read_config().get("general", "defaultlocale")
63 if "/" in path: 60 if "/" in path:
64 locale, page = path.split("/", 1) 61 locale, page = path.split("/", 1)
65 else: 62 else:
66 locale, page = path, "" 63 locale, page = path, ""
67 64
68 default_page = source.read_config().get("general", "defaultpage") 65 default_page = source.read_config().get("general", "defaultpage")
69 alternative_page = "/".join([page, default_page]).lstrip("/") 66 alternative_page = "/".join([page, default_page]).lstrip("/")
70 for format in converters.iterkeys(): 67 for format in converters.iterkeys():
71 for p in (page, alternative_page): 68 for p in (page, alternative_page):
72 if source.has_page(p, format): 69 if source.has_page(p, format):
73 return process_page(source, locale, p, format, "http://127.0.0.1:5000") 70 return process_page(source, locale, p, format, "http://127.0.0.1:5000")
74 if source.has_localizable_file(locale, page): 71 if source.has_localizable_file(locale, page):
75 return source.read_localizable_file(locale, page) 72 return source.read_localizable_file(locale, page)
76 73
77 return None 74 return None
78 75
79 def show_template(start_response, status, template_data, **kwargs): 76 def show_error(start_response, status, **kwargs):
80 env = jinja2.Environment(autoescape=True) 77 env = jinja2.Environment(autoescape=True)
81 template = env.from_string(template_data) 78 template = env.from_string(ERROR_TEMPLATE)
82 start_response(status, [("Content-Type", "text/html; encoding=utf-8")]) 79 mime = "text/html; encoding=%s" % UNICODE_ENCODING
83 return [template.render(**kwargs).encode("utf-8")] 80 start_response(status, [("Content-Type", mime)])
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.
81 for fragment in template.stream(status=status, **kwargs):
82 yield fragment.encode(UNICODE_ENCODING)
84 83
85 def handler(environ, start_response): 84 def handler(environ, start_response):
86 path = environ.get("PATH_INFO") 85 path = environ.get("PATH_INFO")
87 86
88 data = get_data(path) 87 data = get_data(path)
89 if data == None: 88 if data == None:
90 return show_template(start_response, "404 Not Found", ERROR_404, uri=path) 89 return show_error(start_response, "404 Not Found", uri=path)
91 90
92 mime = mime_types.guess_type(path)[0] or "application/octet-stream" 91 mime = mime_types.guess_type(path)[0] or "text/html"
93 92
94 if isinstance(data, unicode): 93 if isinstance(data, unicode):
95 data = data.encode("utf-8") 94 data = data.encode(UNICODE_ENCODING)
96 mime += "; charset=utf-8" 95 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING)
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 96
98 start_response("200 OK", [("Content-Type", mime)]) 97 start_response("200 OK", [("Content-Type", mime)])
99 return [data] 98 return [data]
100 99
101 if __name__ == "__main__": 100 if __name__ == "__main__":
102 if len(sys.argv) < 2: 101 if len(sys.argv) < 2:
103 print >>sys.stderr, "Usage: %s source_dir" % sys.argv[0] 102 print >>sys.stderr, "Usage: %s source_dir" % sys.argv[0]
104 sys.exit(1) 103 sys.exit(1)
105 104
106 source = FileSource(sys.argv[1]) 105 source = FileSource(sys.argv[1])
107 106
108 try: 107 try:
109 from werkzeug.serving import run_simple 108 from werkzeug.serving import run_simple
110 except ImportError: 109 except ImportError:
111 from wsgiref.simple_server import make_server 110 from wsgiref.simple_server import make_server
112 def run_simple(host, port, app, **kwargs): 111 def run_simple(host, port, app, **kwargs):
113 def wrapper(environ, start_response): 112 def wrapper(environ, start_response):
114 try: 113 try:
115 return app(environ, start_response) 114 return app(environ, start_response)
116 except Exception, e: 115 except Exception, e:
117 return show_template(start_response, "500 Internal Server Error", 116 return show_error(start_response, "500 Internal Server Error",
118 ERROR_500, uri=environ.get("PATH_INFO"), error=e) 117 uri=environ.get("PATH_INFO"), error=e)
119 118
120 server = make_server(host, port, wrapper) 119 server = make_server(host, port, wrapper)
121 print " * Running on http://%s:%i/" % server.server_address 120 print " * Running on http://%s:%i/" % server.server_address
122 server.serve_forever() 121 server.serve_forever()
123 122
124 run_simple("localhost", 5000, handler, use_reloader=True, use_debugger=True) 123 run_simple("localhost", 5000, handler, use_reloader=True, use_debugger=True)
LEFTRIGHT
« README.md ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld