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: Created March 20, 2015, 9:12 p.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 sys, os, flask
19 from ..utils import process_page 19 from ..utils import process_page
20 from ..sources import FileSource 20 from ..sources import FileSource
21 from ..converters import converters 21 from ..converters import converters
22 22
23 app = flask.Flask("cms.bin.test_server")
24 source = None 23 source = None
25 24
26 mime_types = { 25 mime_types = {
27 "": "text/html; charset=utf-8", 26 "": "text/html; charset=utf-8",
28 ".htm": "text/html; charset=utf-8", 27 ".htm": "text/html; charset=utf-8",
29 ".html": "text/html; charset=utf-8", 28 ".html": "text/html; charset=utf-8",
30 ".js": "application/javascript; charset=utf-8", 29 ".js": "application/javascript; charset=utf-8",
31 ".css": "text/css; charset=utf-8", 30 ".css": "text/css; charset=utf-8",
32 ".xml": "text/xml; charset=utf-8", 31 ".xml": "text/xml; charset=utf-8",
33 ".png": "image/png", 32 ".png": "image/png",
34 ".jpg": "image/jpeg", 33 ".jpg": "image/jpeg",
35 ".jpeg": "image/jpeg", 34 ".jpeg": "image/jpeg",
36 } 35 }
37 36
38 def get_data(path): 37 def get_data(path):
39 if source.has_static(path): 38 if source.has_static(path):
40 return source.read_static(path) 39 return source.read_static(path)
41 40
42 path = path.rstrip("/") 41 path = path.strip("/")
43 if path == "": 42 if path == "":
44 path = source.read_config().get("general", "defaultlocale") 43 path = source.read_config().get("general", "defaultlocale")
45 if "/" in path: 44 if "/" in path:
46 locale, page = path.split("/", 1) 45 locale, page = path.split("/", 1)
47 else: 46 else:
48 locale, page = path, "" 47 locale, page = path, ""
49 48
50 default_page = source.read_config().get("general", "defaultpage") 49 default_page = source.read_config().get("general", "defaultpage")
51 alternative_page = "/".join([page, default_page]).lstrip("/") 50 alternative_page = "/".join([page, default_page]).lstrip("/")
52 for format in converters.iterkeys(): 51 for format in converters.iterkeys():
53 for p in (page, alternative_page): 52 for p in (page, alternative_page):
54 if source.has_page(p, format): 53 if source.has_page(p, format):
55 return process_page(source, locale, p, format, "http://127.0.0.1:5000"). encode("utf-8") 54 return process_page(source, locale, p, format, "http://127.0.0.1:5000"). encode("utf-8")
56 if source.has_localizable_file(locale, page): 55 if source.has_localizable_file(locale, page):
57 return source.read_localizable_file(locale, page) 56 return source.read_localizable_file(locale, page)
58 57
59 return None 58 return None
60 59
61 @app.route("/", methods = ["GET"]) 60 def handler(environ, start_response):
62 @app.route("/<path:path>", methods = ["GET"]) 61 path = environ.get("REQUEST_URI") or environ.get("PATH_INFO")
63 def show(path=""):
64 data = get_data(path) 62 data = get_data(path)
65 if data == None: 63 if data == None:
66 flask.abort(404) 64 flask.abort(404)
67 65
68 root, ext = os.path.splitext(path) 66 root, ext = os.path.splitext(path)
69 mime = mime_types.get(ext.lower(), "application/octet-stream") 67 mime = mime_types.get(ext.lower(), "application/octet-stream")
70 return data, 200, {"Content-Type": mime} 68
69 if isinstance(data, unicode):
70 data = data.encode("utf-8")
71
72 start_response("200 OK", [("Content-Type", mime)])
73 return [data]
71 74
72 if __name__ == "__main__": 75 if __name__ == "__main__":
73 if len(sys.argv) < 2: 76 if len(sys.argv) < 2:
74 print >>sys.stderr, "Usage: %s source_dir" % sys.argv[0] 77 print >>sys.stderr, "Usage: %s source_dir" % sys.argv[0]
75 sys.exit(1) 78 sys.exit(1)
76 79
77 source = FileSource(sys.argv[1]) 80 source = FileSource(sys.argv[1])
78 81
79 app.run(debug=True) 82 try:
83 from werkzeug.serving import run_simple
84 except ImportError:
85 from wsgiref.simple_server import make_server
86 def run_simple(host, port, app, **kwargs):
87 server = make_server(host, port, app)
88 print " * Running on http://%s:%i/" % server.server_address
89 server.serve_forever()
90
91 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