| OLD | NEW |
| 1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
| 2 # Copyright (C) 2006-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 Eyeo GmbH |
| 3 # | 3 # |
| 4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
| 5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
| 6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
| 7 # | 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 import jinja2 | 21 import jinja2 |
| 22 | 22 |
| 23 from cms.utils import process_page | 23 from cms.utils import process_page |
| 24 from cms.sources import FileSource | 24 from cms.sources import FileSource |
| 25 from cms.converters import converters | 25 from cms.converters import converters |
| 26 | 26 |
| 27 source = None | 27 source = None |
| 28 address = None | 28 address = None |
| 29 port = None | 29 port = None |
| 30 | 30 |
| 31 UNICODE_ENCODING = "utf-8" | 31 UNICODE_ENCODING = 'utf-8' |
| 32 | 32 |
| 33 ERROR_TEMPLATE = """ | 33 ERROR_TEMPLATE = ''' |
| 34 <html> | 34 <html> |
| 35 <head> | 35 <head> |
| 36 <title>{{status}}</title> | 36 <title>{{status}}</title> |
| 37 </head> | 37 </head> |
| 38 <body> | 38 <body> |
| 39 <h1>{{status}}</h1> | 39 <h1>{{status}}</h1> |
| 40 {% set code = status.split()|first|int %} | 40 {% set code = status.split()|first|int %} |
| 41 {% if code == 404 %} | 41 {% if code == 404 %} |
| 42 <p>No page found for the address {{uri}}.</p> | 42 <p>No page found for the address {{uri}}.</p> |
| 43 {% elif code == 500 %} | 43 {% elif code == 500 %} |
| 44 <p>An error occurred while processing the request for {{uri}}:</p> | 44 <p>An error occurred while processing the request for {{uri}}:</p> |
| 45 <pre>{{error}}</pre> | 45 <pre>{{error}}</pre> |
| 46 {% endif %} | 46 {% endif %} |
| 47 </body> | 47 </body> |
| 48 </html>""" | 48 </html>''' |
| 49 | 49 |
| 50 # Initilize the mimetypes modules manually for consistent behavior, | 50 # Initilize the mimetypes modules manually for consistent behavior, |
| 51 # ignoring local files and Windows Registry. | 51 # ignoring local files and Windows Registry. |
| 52 mimetypes.init([]) | 52 mimetypes.init([]) |
| 53 | 53 |
| 54 | 54 |
| 55 def get_page(path): | 55 def get_page(path): |
| 56 path = path.strip("/") | 56 path = path.strip('/') |
| 57 if path == "": | 57 if path == '': |
| 58 path = source.read_config().get("general", "defaultlocale") | 58 path = source.read_config().get('general', 'defaultlocale') |
| 59 if "/" in path: | 59 if '/' in path: |
| 60 locale, page = path.split("/", 1) | 60 locale, page = path.split('/', 1) |
| 61 else: | 61 else: |
| 62 locale, page = path, "" | 62 locale, page = path, '' |
| 63 | 63 |
| 64 default_page = source.read_config().get("general", "defaultpage") | 64 default_page = source.read_config().get('general', 'defaultpage') |
| 65 alternative_page = "/".join([page, default_page]).lstrip("/") | 65 alternative_page = '/'.join([page, default_page]).lstrip('/') |
| 66 | 66 |
| 67 for format in converters.iterkeys(): | 67 for format in converters.iterkeys(): |
| 68 for p in (page, alternative_page): | 68 for p in (page, alternative_page): |
| 69 if source.has_page(p, format): | 69 if source.has_page(p, format): |
| 70 return (p, process_page(source, locale, p, format, "http://%s:%d
" % (address, port))) | 70 return (p, process_page(source, locale, p, format, 'http://%s:%d
' % (address, port))) |
| 71 if source.has_localizable_file(locale, page): | 71 if source.has_localizable_file(locale, page): |
| 72 return (page, source.read_localizable_file(locale, page)) | 72 return (page, source.read_localizable_file(locale, page)) |
| 73 | 73 |
| 74 return (None, None) | 74 return (None, None) |
| 75 | 75 |
| 76 | 76 |
| 77 def has_conflicting_pages(page): | 77 def has_conflicting_pages(page): |
| 78 pages = [p for p, _ in source.list_pages()] | 78 pages = [p for p, _ in source.list_pages()] |
| 79 pages.extend(source.list_localizable_files()) | 79 pages.extend(source.list_localizable_files()) |
| 80 | 80 |
| 81 if pages.count(page) > 1: | 81 if pages.count(page) > 1: |
| 82 return True | 82 return True |
| 83 if any(p.startswith(page + "/") or page.startswith(p + "/") for p in pages): | 83 if any(p.startswith(page + '/') or page.startswith(p + '/') for p in pages): |
| 84 return True | 84 return True |
| 85 return False | 85 return False |
| 86 | 86 |
| 87 | 87 |
| 88 def get_data(path): | 88 def get_data(path): |
| 89 if source.has_static(path): | 89 if source.has_static(path): |
| 90 return source.read_static(path) | 90 return source.read_static(path) |
| 91 | 91 |
| 92 page, data = get_page(path) | 92 page, data = get_page(path) |
| 93 if page and has_conflicting_pages(page): | 93 if page and has_conflicting_pages(page): |
| 94 raise Exception("The requested page conflicts with another page") | 94 raise Exception('The requested page conflicts with another page') |
| 95 return data | 95 return data |
| 96 | 96 |
| 97 | 97 |
| 98 def show_error(start_response, status, **kwargs): | 98 def show_error(start_response, status, **kwargs): |
| 99 env = jinja2.Environment(autoescape=True) | 99 env = jinja2.Environment(autoescape=True) |
| 100 template = env.from_string(ERROR_TEMPLATE) | 100 template = env.from_string(ERROR_TEMPLATE) |
| 101 mime = "text/html; encoding=%s" % UNICODE_ENCODING | 101 mime = 'text/html; encoding=%s' % UNICODE_ENCODING |
| 102 start_response(status, [("Content-Type", mime)]) | 102 start_response(status, [('Content-Type', mime)]) |
| 103 for fragment in template.stream(status=status, **kwargs): | 103 for fragment in template.stream(status=status, **kwargs): |
| 104 yield fragment.encode(UNICODE_ENCODING) | 104 yield fragment.encode(UNICODE_ENCODING) |
| 105 | 105 |
| 106 | 106 |
| 107 def handler(environ, start_response): | 107 def handler(environ, start_response): |
| 108 path = environ.get("PATH_INFO") | 108 path = environ.get('PATH_INFO') |
| 109 | 109 |
| 110 data = get_data(path) | 110 data = get_data(path) |
| 111 if data is None: | 111 if data is None: |
| 112 return show_error(start_response, "404 Not Found", uri=path) | 112 return show_error(start_response, '404 Not Found', uri=path) |
| 113 | 113 |
| 114 mime = mimetypes.guess_type(path)[0] or "text/html" | 114 mime = mimetypes.guess_type(path)[0] or 'text/html' |
| 115 | 115 |
| 116 if isinstance(data, unicode): | 116 if isinstance(data, unicode): |
| 117 data = data.encode(UNICODE_ENCODING) | 117 data = data.encode(UNICODE_ENCODING) |
| 118 mime = "%s; charset=%s" % (mime, UNICODE_ENCODING) | 118 mime = '%s; charset=%s' % (mime, UNICODE_ENCODING) |
| 119 | 119 |
| 120 start_response("200 OK", [("Content-Type", mime)]) | 120 start_response('200 OK', [('Content-Type', mime)]) |
| 121 return [data] | 121 return [data] |
| 122 | 122 |
| 123 if __name__ == "__main__": | 123 if __name__ == '__main__': |
| 124 | 124 |
| 125 parser = argparse.ArgumentParser(description='CMS development server created
to test pages locally and on-the-fly') | 125 parser = argparse.ArgumentParser(description='CMS development server created
to test pages locally and on-the-fly') |
| 126 parser.add_argument('path', nargs='?', default=os.curdir) | 126 parser.add_argument('path', nargs='?', default=os.curdir) |
| 127 parser.add_argument('-a', '--address', default='localhost', help='Address of
the interface the server will listen on') | 127 parser.add_argument('-a', '--address', default='localhost', help='Address of
the interface the server will listen on') |
| 128 parser.add_argument('-p', '--port', type=int, default=5000, help='TCP port t
he server will listen on') | 128 parser.add_argument('-p', '--port', type=int, default=5000, help='TCP port t
he server will listen on') |
| 129 args = parser.parse_args() | 129 args = parser.parse_args() |
| 130 | 130 |
| 131 source = FileSource(args.path) | 131 source = FileSource(args.path) |
| 132 address = args.address | 132 address = args.address |
| 133 port = args.port | 133 port = args.port |
| 134 | 134 |
| 135 try: | 135 try: |
| 136 from werkzeug.serving import ThreadedWSGIServer, run_simple | 136 from werkzeug.serving import ThreadedWSGIServer, run_simple |
| 137 | 137 |
| 138 # see https://github.com/mitsuhiko/werkzeug/pull/770 | 138 # see https://github.com/mitsuhiko/werkzeug/pull/770 |
| 139 ThreadedWSGIServer.daemon_threads = True | 139 ThreadedWSGIServer.daemon_threads = True |
| 140 | 140 |
| 141 def run(*args, **kwargs): | 141 def run(*args, **kwargs): |
| 142 # The werkzeug logger must be configured before the | 142 # The werkzeug logger must be configured before the |
| 143 # root logger. Also we must prevent it from propagating | 143 # root logger. Also we must prevent it from propagating |
| 144 # messages, otherwise messages are logged twice. | 144 # messages, otherwise messages are logged twice. |
| 145 import logging | 145 import logging |
| 146 logger = logging.getLogger("werkzeug") | 146 logger = logging.getLogger('werkzeug') |
| 147 logger.propagate = False | 147 logger.propagate = False |
| 148 logger.setLevel(logging.INFO) | 148 logger.setLevel(logging.INFO) |
| 149 logger.addHandler(logging.StreamHandler()) | 149 logger.addHandler(logging.StreamHandler()) |
| 150 | 150 |
| 151 run_simple(threaded=True, *args, **kwargs) | 151 run_simple(threaded=True, *args, **kwargs) |
| 152 except ImportError: | 152 except ImportError: |
| 153 from SocketServer import ThreadingMixIn | 153 from SocketServer import ThreadingMixIn |
| 154 from wsgiref.simple_server import WSGIServer, make_server | 154 from wsgiref.simple_server import WSGIServer, make_server |
| 155 | 155 |
| 156 class ThreadedWSGIServer(ThreadingMixIn, WSGIServer): | 156 class ThreadedWSGIServer(ThreadingMixIn, WSGIServer): |
| 157 daemon_threads = True | 157 daemon_threads = True |
| 158 | 158 |
| 159 def run(host, port, app, **kwargs): | 159 def run(host, port, app, **kwargs): |
| 160 def wrapper(environ, start_response): | 160 def wrapper(environ, start_response): |
| 161 try: | 161 try: |
| 162 return app(environ, start_response) | 162 return app(environ, start_response) |
| 163 except Exception, e: | 163 except Exception, e: |
| 164 return show_error(start_response, "500 Internal Server Error
", | 164 return show_error(start_response, '500 Internal Server Error
', |
| 165 uri=environ.get("PATH_INFO"), error=e) | 165 uri=environ.get('PATH_INFO'), error=e) |
| 166 | 166 |
| 167 server = make_server(host, port, wrapper, ThreadedWSGIServer) | 167 server = make_server(host, port, wrapper, ThreadedWSGIServer) |
| 168 print " * Running on http://%s:%i/" % server.server_address | 168 print ' * Running on http://%s:%i/' % server.server_address |
| 169 server.serve_forever() | 169 server.serve_forever() |
| 170 | 170 |
| 171 run(address, port, handler, use_reloader=True, use_debugger=True) | 171 run(address, port, handler, use_reloader=True, use_debugger=True) |
| OLD | NEW |