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

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

Issue 4921490642305024: Issue 2129 - Remove CMS implementation from sitescripts repository (Closed)
Patch Set: Created March 13, 2015, 5:05 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 | « sitescripts/cms/bin/runserver.py ('k') | sitescripts/cms/converters.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH
5 #
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
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
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/>.
17
18 import sys, os, flask
19 from ...utils import setupStderr
20 from ..utils import process_page
21 from ..sources import FileSource
22 from ..converters import converters
23
24 app = flask.Flask("sitescripts.cms.bin.test_server")
25 source = None
26
27 mime_types = {
28 "": "text/html; charset=utf-8",
29 ".htm": "text/html; charset=utf-8",
30 ".html": "text/html; charset=utf-8",
31 ".js": "application/javascript; charset=utf-8",
32 ".css": "text/css; charset=utf-8",
33 ".xml": "text/xml; charset=utf-8",
34 ".png": "image/png",
35 ".jpg": "image/jpeg",
36 ".jpeg": "image/jpeg",
37 }
38
39 def get_data(path):
40 if source.has_static(path):
41 return source.read_static(path)
42
43 path = path.rstrip("/")
44 if path == "":
45 path = source.read_config().get("general", "defaultlocale")
46 if "/" not in path:
47 path = "%s/%s" % (path, source.read_config().get("general", "defaultpage"))
48
49 locale, page = path.split("/", 1)
50 for format in converters.iterkeys():
51 if source.has_page(page, format):
52 return process_page(source, locale, page, format, "http://127.0.0.1:5000") .encode("utf-8")
53 if source.has_localizable_file(locale, page):
54 return source.read_localizable_file(locale, page)
55
56 return None
57
58 @app.route("/", methods = ["GET"])
59 @app.route("/<path:path>", methods = ["GET"])
60 def show(path=""):
61 data = get_data(path)
62 if data == None:
63 flask.abort(404)
64
65 root, ext = os.path.splitext(path)
66 mime = mime_types.get(ext.lower(), "application/octet-stream")
67 return data, 200, {"Content-Type": mime}
68
69 if __name__ == "__main__":
70 setupStderr()
71 if len(sys.argv) < 2:
72 print >>sys.stderr, "Usage: %s source_dir" % sys.argv[0]
73 sys.exit(1)
74
75 source = FileSource(sys.argv[1])
76
77 # Make sure to "fix" argv to ensure that restart can succeed
78 sys.argv[0:1] = ["-m", "sitescripts.cms.bin.test_server"]
79
80 app.run(debug=True)
OLDNEW
« no previous file with comments | « sitescripts/cms/bin/runserver.py ('k') | sitescripts/cms/converters.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld