Mercurial > piecrust2
comparison piecrust/serving/util.py @ 555:daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Wed, 12 Aug 2015 23:04:46 -0700 |
| parents | cc6f3dbe3048 |
| children | 657384f08ca3 |
comparison
equal
deleted
inserted
replaced
| 554:155c7e20414f | 555:daf8df5ade7d |
|---|---|
| 3 import hashlib | 3 import hashlib |
| 4 import logging | 4 import logging |
| 5 import datetime | 5 import datetime |
| 6 from werkzeug.wrappers import Response | 6 from werkzeug.wrappers import Response |
| 7 from werkzeug.wsgi import wrap_file | 7 from werkzeug.wsgi import wrap_file |
| 8 from piecrust.app import PieCrust | |
| 9 from piecrust.rendering import QualifiedPage | |
| 10 from piecrust.routing import RouteNotFoundError | |
| 11 from piecrust.sources.base import MODE_PARSING | |
| 12 from piecrust.sources.pageref import PageNotFoundError | |
| 13 from piecrust.uriutil import split_sub_uri | |
| 8 | 14 |
| 9 | 15 |
| 10 logger = logging.getLogger(__name__) | 16 logger = logging.getLogger(__name__) |
| 17 | |
| 18 | |
| 19 def get_app_for_server(root_dir, debug=False, sub_cache_dir=None): | |
| 20 app = PieCrust(root_dir=root_dir, debug=debug) | |
| 21 if sub_cache_dir: | |
| 22 app._useSubCacheDir(sub_cache_dir) | |
| 23 app.config.set('site/root', '/') | |
| 24 app.config.set('server/is_serving', True) | |
| 25 return app | |
| 26 | |
| 27 | |
| 28 class RequestedPage(object): | |
| 29 def __init__(self, qualified_page): | |
| 30 self.qualified_page = qualified_page | |
| 31 self.req_path = None | |
| 32 self.page_num = 1 | |
| 33 self.not_found_errors = [] | |
| 34 | |
| 35 | |
| 36 def find_routes(routes, uri): | |
| 37 res = [] | |
| 38 tax_res = [] | |
| 39 for route in routes: | |
| 40 metadata = route.matchUri(uri) | |
| 41 if metadata is not None: | |
| 42 if route.is_taxonomy_route: | |
| 43 tax_res.append((route, metadata)) | |
| 44 else: | |
| 45 res.append((route, metadata)) | |
| 46 return res + tax_res | |
| 47 | |
| 48 | |
| 49 def get_requested_page(app, req_path): | |
| 50 # Try to find what matches the requested URL. | |
| 51 req_path, page_num = split_sub_uri(app, req_path) | |
| 52 | |
| 53 routes = find_routes(app.routes, req_path) | |
| 54 if len(routes) == 0: | |
| 55 raise RouteNotFoundError("Can't find route for: %s" % req_path) | |
| 56 | |
| 57 qp = None | |
| 58 not_found_errors = [] | |
| 59 for route, route_metadata in routes: | |
| 60 try: | |
| 61 qp = _get_requested_page_for_route( | |
| 62 app, route, route_metadata, req_path) | |
| 63 if qp is not None: | |
| 64 break | |
| 65 except PageNotFoundError as nfe: | |
| 66 not_found_errors.append(nfe) | |
| 67 | |
| 68 req_page = RequestedPage(qp) | |
| 69 req_page.req_path = req_path | |
| 70 req_page.page_num = page_num | |
| 71 req_page.not_found_errors = not_found_errors | |
| 72 return req_page | |
| 73 | |
| 74 | |
| 75 def _get_requested_page_for_route(app, route, route_metadata, req_path): | |
| 76 taxonomy = None | |
| 77 source = app.getSource(route.source_name) | |
| 78 if route.taxonomy_name is None: | |
| 79 factory = source.findPageFactory(route_metadata, MODE_PARSING) | |
| 80 if factory is None: | |
| 81 raise PageNotFoundError("No path found for '%s' in source '%s'." % | |
| 82 (req_path, source.name)) | |
| 83 else: | |
| 84 taxonomy = app.getTaxonomy(route.taxonomy_name) | |
| 85 | |
| 86 # This will raise `PageNotFoundError` naturally if not found. | |
| 87 tax_page_ref = taxonomy.getPageRef(source) | |
| 88 factory = tax_page_ref.getFactory() | |
| 89 | |
| 90 # Build the page. | |
| 91 page = factory.buildPage() | |
| 92 qp = QualifiedPage(page, route, route_metadata) | |
| 93 return qp | |
| 11 | 94 |
| 12 | 95 |
| 13 def load_mimetype_map(): | 96 def load_mimetype_map(): |
| 14 mimetype_map = {} | 97 mimetype_map = {} |
| 15 sep_re = re.compile(r'\s+') | 98 sep_re = re.compile(r'\s+') |
