Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 import argparse | 21 import argparse |
22 | 22 |
23 import jinja2 | 23 import jinja2 |
24 | 24 |
25 from cms.utils import process_page | 25 from cms.utils import process_page |
26 from cms.sources import FileSource | 26 from cms.sources import FileSource |
27 from cms.converters import converters | 27 from cms.converters import converters |
28 | 28 |
29 source = None | 29 source = None |
30 address = None | |
31 port = None | |
30 | 32 |
31 UNICODE_ENCODING = "utf-8" | 33 UNICODE_ENCODING = "utf-8" |
32 | 34 |
33 ERROR_TEMPLATE = """ | 35 ERROR_TEMPLATE = """ |
34 <html> | 36 <html> |
35 <head> | 37 <head> |
36 <title>{{status}}</title> | 38 <title>{{status}}</title> |
37 </head> | 39 </head> |
38 <body> | 40 <body> |
39 <h1>{{status}}</h1> | 41 <h1>{{status}}</h1> |
(...skipping 19 matching lines...) Expand all Loading... | |
59 locale, page = path.split("/", 1) | 61 locale, page = path.split("/", 1) |
60 else: | 62 else: |
61 locale, page = path, "" | 63 locale, page = path, "" |
62 | 64 |
63 default_page = source.read_config().get("general", "defaultpage") | 65 default_page = source.read_config().get("general", "defaultpage") |
64 alternative_page = "/".join([page, default_page]).lstrip("/") | 66 alternative_page = "/".join([page, default_page]).lstrip("/") |
65 | 67 |
66 for format in converters.iterkeys(): | 68 for format in converters.iterkeys(): |
67 for p in (page, alternative_page): | 69 for p in (page, alternative_page): |
68 if source.has_page(p, format): | 70 if source.has_page(p, format): |
69 return (p, process_page(source, locale, p, format, "http://127.0.0.1:500 0")) | 71 return (p, process_page(source, locale, p, format, "http://%s:%d" % (add ress, port))) |
70 if source.has_localizable_file(locale, page): | 72 if source.has_localizable_file(locale, page): |
71 return (page, source.read_localizable_file(locale, page)) | 73 return (page, source.read_localizable_file(locale, page)) |
72 | 74 |
73 return (None, None) | 75 return (None, None) |
74 | 76 |
75 def has_conflicting_pages(page): | 77 def has_conflicting_pages(page): |
76 pages = [p for p, _ in source.list_pages()] | 78 pages = [p for p, _ in source.list_pages()] |
77 pages.extend(source.list_localizable_files()) | 79 pages.extend(source.list_localizable_files()) |
78 | 80 |
79 if pages.count(page) > 1: | 81 if pages.count(page) > 1: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 | 113 |
112 if isinstance(data, unicode): | 114 if isinstance(data, unicode): |
113 data = data.encode(UNICODE_ENCODING) | 115 data = data.encode(UNICODE_ENCODING) |
114 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING) | 116 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING) |
115 | 117 |
116 start_response("200 OK", [("Content-Type", mime)]) | 118 start_response("200 OK", [("Content-Type", mime)]) |
117 return [data] | 119 return [data] |
118 | 120 |
119 if __name__ == "__main__": | 121 if __name__ == "__main__": |
120 | 122 |
121 parser = argparse.ArgumentParser(description='CMS testing server.') | 123 parser = argparse.ArgumentParser(description='CMS development server created t o test pages locally and on-the-fly') |
122 parser.add_argument('basepath', nargs='?', default=os.curdir) | 124 parser.add_argument('path', nargs='?', default=os.curdir) |
Sebastian Noack
2016/01/20 17:46:25
Nit: I would simply call it "path".
juliandoucette
2016/01/21 13:46:24
Done.
| |
123 parser.add_argument('-n', '--hostname', default='localhost', type=str, help='S erver hostname (EG: localhost, example.com, 0.0.0.0)') | 125 parser.add_argument('-a', '--address', default='localhost', help='Address of t he interface the server will listen on') |
Sebastian Noack
2016/01/20 17:46:25
Nit: "Address of the interface the server will lis
Sebastian Noack
2016/01/20 17:46:25
type=str is the default. How about omitting it, if
Sebastian Noack
2016/01/20 18:00:55
--address would be a better name for the option, a
juliandoucette
2016/01/21 13:46:24
Done.
juliandoucette
2016/01/21 13:46:24
Acknowledged.
juliandoucette
2016/01/21 13:46:24
Done.
| |
124 parser.add_argument('-p', '--port', default='5000', type=str, help='Port numbe r (EG: 8080, 5000)') | 126 parser.add_argument('-p', '--port', type=int, default=5000, help='TCP port the server will listen on') |
Sebastian Noack
2016/01/20 17:46:25
How about type=int? Then you don't need to convert
juliandoucette
2016/01/21 13:46:23
Done.
| |
125 args = parser.parse_args() | 127 args = parser.parse_args() |
126 | 128 |
127 source = FileSource(args.basepath) | 129 source = FileSource(args.path) |
128 hostname = args.hostname | 130 address = args.address |
129 port = int(args.port) | 131 port = args.port |
130 | 132 |
131 try: | 133 try: |
132 from werkzeug.serving import ThreadedWSGIServer, run_simple | 134 from werkzeug.serving import ThreadedWSGIServer, run_simple |
133 | 135 |
134 # see https://github.com/mitsuhiko/werkzeug/pull/770 | 136 # see https://github.com/mitsuhiko/werkzeug/pull/770 |
135 ThreadedWSGIServer.daemon_threads = True | 137 ThreadedWSGIServer.daemon_threads = True |
136 | 138 |
137 def run(*args, **kwargs): | 139 def run(*args, **kwargs): |
138 # The werkzeug logger must be configured before the | 140 # The werkzeug logger must be configured before the |
139 # root logger. Also we must prevent it from propagating | 141 # root logger. Also we must prevent it from propagating |
(...skipping 17 matching lines...) Expand all Loading... | |
157 try: | 159 try: |
158 return app(environ, start_response) | 160 return app(environ, start_response) |
159 except Exception, e: | 161 except Exception, e: |
160 return show_error(start_response, "500 Internal Server Error", | 162 return show_error(start_response, "500 Internal Server Error", |
161 uri=environ.get("PATH_INFO"), error=e) | 163 uri=environ.get("PATH_INFO"), error=e) |
162 | 164 |
163 server = make_server(host, port, wrapper, ThreadedWSGIServer) | 165 server = make_server(host, port, wrapper, ThreadedWSGIServer) |
164 print " * Running on http://%s:%i/" % server.server_address | 166 print " * Running on http://%s:%i/" % server.server_address |
165 server.serve_forever() | 167 server.serve_forever() |
166 | 168 |
167 run(hostname, port, handler, use_reloader=True, use_debugger=True) | 169 run(address, port, handler, use_reloader=True, use_debugger=True) |
LEFT | RIGHT |