示例#1
0
    def handleJob(self, job):
        # Render the segments for the first sub-page of this page.
        fac = load_factory(self.app, job)

        # These things should be OK as they're checked upstream by the baker.
        route = self.app.getRoute(fac.source.name, fac.metadata,
                                  skip_taxonomies=True)
        assert route is not None

        page = fac.buildPage()
        route_metadata = create_route_metadata(page)
        qp = QualifiedPage(page, route, route_metadata)
        ctx = PageRenderingContext(qp)
        self.app.env.abort_source_use = True

        result = {
                'path': fac.path,
                'aborted': False,
                'errors': None}
        logger.debug("Preparing page: %s" % fac.ref_spec)
        try:
            render_page_segments(ctx)
        except AbortedSourceUseError:
            logger.debug("Page %s was aborted." % fac.ref_spec)
            result['aborted'] = True
        except Exception as ex:
            logger.debug("Got rendering error. Sending it to master.")
            result['errors'] = _get_errors(ex)
            if self.ctx.debug:
                logger.exception(ex)
        finally:
            self.app.env.abort_source_use = False
        return result
示例#2
0
    def handleJob(self, job):
        # Render the segments for the first sub-page of this page.
        fac = load_factory(self.app, job)

        # These things should be OK as they're checked upstream by the baker.
        route = self.app.getRoute(fac.source.name, fac.metadata,
                                  skip_taxonomies=True)
        assert route is not None

        page = fac.buildPage()
        route_metadata = create_route_metadata(page)
        qp = QualifiedPage(page, route, route_metadata)
        ctx = PageRenderingContext(qp)
        self.app.env.abort_source_use = True

        result = {
                'path': fac.path,
                'aborted': False,
                'errors': None}
        logger.debug("Preparing page: %s" % fac.ref_spec)
        try:
            render_page_segments(ctx)
        except AbortedSourceUseError:
            logger.debug("Page %s was aborted." % fac.ref_spec)
            result['aborted'] = True
        except Exception as ex:
            logger.debug("Got rendering error. Sending it to master.")
            result['errors'] = _get_errors(ex)
            if self.ctx.debug:
                logger.exception(ex)
        finally:
            self.app.env.abort_source_use = False
        return result
示例#3
0
    def handleJob(self, job):
        # Render the segments for the first sub-page of this page.
        fac = load_factory(self.app, job['factory_info'])
        self.app.env.addManifestEntry('RenderJobs', fac.ref_spec)

        route_index = job['route_index']
        route = self.app.routes[route_index]

        page = fac.buildPage()
        route_metadata = create_route_metadata(page)
        qp = QualifiedPage(page, route, route_metadata)
        ctx = PageRenderingContext(qp)
        self.app.env.abort_source_use = True

        result = {
                'path': fac.path,
                'aborted': False,
                'errors': None}
        logger.debug("Preparing page: %s" % fac.ref_spec)
        try:
            render_page_segments(ctx)
        except AbortedSourceUseError:
            logger.debug("Page %s was aborted." % fac.ref_spec)
            self.app.env.stepCounter("SourceUseAbortions")
            result['aborted'] = True
        except Exception as ex:
            logger.debug("Got rendering error. Sending it to master.")
            result['errors'] = _get_errors(ex)
            if self.ctx.app.debug:
                logger.exception(ex)
        finally:
            self.app.env.abort_source_use = False
        return result
示例#4
0
文件: baker.py 项目: thhgcn/PieCrust2
    def _makeBakeJob(self, record, fac, tax_info=None):
        # Get the previous (if any) and current entry for this page.
        pair = record.getPreviousAndCurrentEntries(fac.path, tax_info)
        assert pair is not None
        prev_entry, cur_entry = pair
        assert cur_entry is not None

        # Ignore if there were errors in the previous passes.
        if cur_entry.errors:
            logger.debug("Ignoring %s because it had previous "
                         "errors." % fac.ref_spec)
            return None

        # Build the route metadata and find the appropriate route.
        page = fac.buildPage()
        route_metadata = create_route_metadata(page)
        if tax_info is not None:
            tax = self.app.getTaxonomy(tax_info.taxonomy_name)
            route = self.app.getTaxonomyRoute(tax_info.taxonomy_name,
                                              tax_info.source_name)

            slugified_term = route.slugifyTaxonomyTerm(tax_info.term)
            route_metadata[tax.term_name] = slugified_term
        else:
            route = self.app.getRoute(fac.source.name, route_metadata,
                                      skip_taxonomies=True)
        assert route is not None

        # Figure out if this page is overriden by another previously
        # baked page. This happens for example when the user has
        # made a page that has the same page/URL as a theme page.
        uri = route.getUri(route_metadata)
        override_entry = record.getOverrideEntry(page.path, uri)
        if override_entry is not None:
            override_source = self.app.getSource(
                    override_entry.source_name)
            if override_source.realm == fac.source.realm:
                cur_entry.errors.append(
                        "Page '%s' maps to URL '%s' but is overriden "
                        "by page '%s'." %
                        (fac.ref_spec, uri, override_entry.path))
                logger.error(cur_entry.errors[-1])
            cur_entry.flags |= BakeRecordEntry.FLAG_OVERRIDEN
            return None

        job = {
                'type': JOB_BAKE,
                'job': {
                        'factory_info': save_factory(fac),
                        'taxonomy_info': tax_info,
                        'route_metadata': route_metadata,
                        'dirty_source_names': record.dirty_source_names
                        }
                }
        return job
示例#5
0
文件: baker.py 项目: kinow/PieCrust2
    def _makeBakeJob(self, record, fac, tax_info=None):
        # Get the previous (if any) and current entry for this page.
        pair = record.getPreviousAndCurrentEntries(fac.path, tax_info)
        assert pair is not None
        prev_entry, cur_entry = pair
        assert cur_entry is not None

        # Ignore if there were errors in the previous passes.
        if cur_entry.errors:
            logger.debug("Ignoring %s because it had previous "
                         "errors." % fac.ref_spec)
            return None

        # Build the route metadata and find the appropriate route.
        page = fac.buildPage()
        route_metadata = create_route_metadata(page)
        if tax_info is not None:
            tax = self.app.getTaxonomy(tax_info.taxonomy_name)
            route = self.app.getTaxonomyRoute(tax_info.taxonomy_name,
                                              tax_info.source_name)

            slugified_term = route.slugifyTaxonomyTerm(tax_info.term)
            route_metadata[tax.term_name] = slugified_term
        else:
            route = self.app.getRoute(fac.source.name,
                                      route_metadata,
                                      skip_taxonomies=True)
        assert route is not None

        # Figure out if this page is overriden by another previously
        # baked page. This happens for example when the user has
        # made a page that has the same page/URL as a theme page.
        uri = route.getUri(route_metadata)
        override_entry = record.getOverrideEntry(page.path, uri)
        if override_entry is not None:
            override_source = self.app.getSource(override_entry.source_name)
            if override_source.realm == fac.source.realm:
                cur_entry.errors.append(
                    "Page '%s' maps to URL '%s' but is overriden "
                    "by page '%s'." % (fac.ref_spec, uri, override_entry.path))
                logger.error(cur_entry.errors[-1])
            cur_entry.flags |= BakeRecordEntry.FLAG_OVERRIDEN
            return None

        job = {
            'type': JOB_BAKE,
            'job': {
                'factory_info': save_factory(fac),
                'taxonomy_info': tax_info,
                'route_metadata': route_metadata,
                'dirty_source_names': record.dirty_source_names
            }
        }
        return job
示例#6
0
 def _get_uri(self):
     page = self._page
     if self._route is None:
         # TODO: this is not quite correct, as we're missing parts of the
         #       route metadata if the current page is a taxonomy page.
         route_metadata = create_route_metadata(page)
         self._route = page.app.getRoute(page.source.name, route_metadata)
         self._route_metadata = route_metadata
         if self._route is None:
             raise Exception("Can't get route for page: %s" % page.path)
     return self._route.getUri(self._route_metadata)
 def _get_uri(self):
     page = self._page
     if self._route is None:
         # TODO: this is not quite correct, as we're missing parts of the
         #       route metadata if the current page is a taxonomy page.
         route_metadata = create_route_metadata(page)
         self._route = page.app.getSourceRoute(page.source.name, route_metadata)
         self._route_metadata = route_metadata
         if self._route is None:
             raise Exception("Can't get route for page: %s" % page.path)
     return self._route.getUri(self._route_metadata)
示例#8
0
def write_page(source_name):
    site = g.site.piecrust_app
    source = site.getSource(source_name)
    if source is None:
        abort(400)
    if not isinstance(source, IInteractiveSource):
        abort(400)

    if request.method == 'POST':
        if 'do_save' in request.form:
            metadata = {}
            for f in source.getInteractiveFields():
                metadata[f.name] = f.default_value
            for fk, fv in request.form.items():
                if fk.startswith('meta-'):
                    metadata[fk[5:]] = fv

            logger.debug("Searching for page with metadata: %s" % metadata)
            fac = source.findPageFactory(metadata, MODE_CREATING)
            if fac is None:
                logger.error("Can't find page for %s" % metadata)
                abort(500)

            logger.debug("Creating page: %s" % fac.path)
            os.makedirs(os.path.dirname(fac.path), exist_ok=True)
            with open(fac.path, 'w', encoding='utf8') as fp:
                fp.write('')
            flash("%s was created." % os.path.relpath(fac.path, site.root_dir))

            route = site.getSourceRoute(source.name, fac.metadata)
            if route is None:
                logger.error("Can't find route for page: %s" % fac.path)
                abort(500)

            dummy = _DummyPage(fac)
            route_metadata = create_route_metadata(dummy)
            uri = route.getUri(route_metadata)
            uri_root = '/site/%s/' % g.site.name
            uri = uri[len(uri_root):]
            logger.debug("Redirecting to: %s" % uri)

            return redirect(url_for('.edit_page', slug=uri))

        abort(400)

    return _write_page_form(source)
示例#9
0
def write_page(source_name):
    site = g.site.piecrust_app
    source = site.getSource(source_name)
    if source is None:
        abort(400)
    if not isinstance(source, IInteractiveSource):
        abort(400)

    if request.method == "POST":
        if "do_save" in request.form:
            metadata = {}
            for fk, fv in request.form.items():
                if fk.startswith("meta-"):
                    metadata[fk[5:]] = fv

            logger.debug("Searching for page with metadata: %s" % metadata)
            fac = source.findPageFactory(metadata, MODE_CREATING)
            if fac is None:
                logger.error("Can't find page for %s" % metadata)
                abort(500)

            logger.debug("Creating page: %s" % fac.path)
            with open(fac.path, "w", encoding="utf8") as fp:
                fp.write("")
            flash("%s was created." % os.path.relpath(fac.path, site.root_dir))

            route = site.getRoute(source.name, fac.metadata, skip_taxonomies=True)
            if route is None:
                logger.error("Can't find route for page: %s" % fac.path)
                abort(500)

            dummy = _DummyPage(fac)
            route_metadata = create_route_metadata(dummy)
            uri = route.getUri(route_metadata)
            uri_root = "/site/%s/" % g.site.name
            uri = uri[len(uri_root) :]
            logger.debug("Redirecting to: %s" % uri)

            return redirect(url_for("edit_page", slug=uri))

        abort(400)

    return _write_page_form(source)
示例#10
0
    def queueBakeJob(self, page_fac, route, extra_route_metadata, seed):
        if self._is_running:
            raise Exception("The job queue is running.")

        extra_key = self.getRecordExtraKey(seed)
        entry = BakeRecordEntry(
                page_fac.source.name,
                page_fac.path,
                extra_key)
        self._record.addEntry(entry)

        page = page_fac.buildPage()
        route_metadata = create_route_metadata(page)
        route_metadata.update(extra_route_metadata)
        uri = route.getUri(route_metadata)
        override_entry = self._record.getOverrideEntry(page.path, uri)
        if override_entry is not None:
            override_source = self.app.getSource(
                    override_entry.source_name)
            if override_source.realm == fac.source.realm:
                cur_entry.errors.append(
                        "Page '%s' maps to URL '%s' but is overriden "
                        "by page '%s'." %
                        (fac.ref_spec, uri, override_entry.path))
                logger.error(cur_entry.errors[-1])
            cur_entry.flags |= BakeRecordEntry.FLAG_OVERRIDEN
            return

        route_index = self._app.routes.index(route)
        job = {
                'type': JOB_BAKE,
                'job': {
                        'factory_info': save_factory(page_fac),
                        'generator_name': self._generator.name,
                        'generator_record_key': extra_key,
                        'route_index': route_index,
                        'route_metadata': route_metadata,
                        'dirty_source_names': self._record.dirty_source_names,
                        'needs_config': True
                        }
                }
        self._job_queue.append(job)