Mercurial > piecrust2
annotate piecrust/processing/sitemap.py @ 435:5ceb86818dc5
bug: Fix a crash when errors occur while processing an asset.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 27 Jun 2015 21:48:12 -0700 |
parents | 21e26ed867b6 |
children | 62274d805a6e |
rev | line source |
---|---|
34 | 1 import time |
2 import logging | |
3 import yaml | |
4 from piecrust.processing.base import SimpleFileProcessor | |
430
21e26ed867b6
internal: Create full route metadata in one place.
Ludovic Chabant <ludovic@chabant.com>
parents:
287
diff
changeset
|
5 from piecrust.routing import create_route_metadata |
34 | 6 |
7 | |
8 logger = logging.getLogger(__name__) | |
9 | |
10 | |
11 SITEMAP_HEADER = \ | |
12 """<?xml version="1.0" encoding="utf-8"?> | |
13 <urlset | |
14 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | |
15 """ | |
16 SITEMAP_FOOTER = "</urlset>\n" | |
17 | |
18 SITEURL_HEADER = " <url>\n" | |
19 SITEURL_LOC = " <loc>%s</loc>\n" | |
20 SITEURL_LASTMOD = " <lastmod>%s</lastmod>\n" | |
21 SITEURL_CHANGEFREQ = " <changefreq>%s</changefreq>\n" | |
22 SITEURL_PRIORITY = " <priority>%f</priority>\n" | |
23 SITEURL_FOOTER = " </url>\n" | |
24 | |
25 | |
26 class SitemapProcessor(SimpleFileProcessor): | |
27 PROCESSOR_NAME = 'sitemap' | |
28 | |
29 def __init__(self): | |
30 super(SitemapProcessor, self).__init__({'sitemap': 'xml'}) | |
31 self._start_time = None | |
32 | |
33 def onPipelineStart(self, pipeline): | |
34 self._start_time = time.time() | |
35 | |
36 def _doProcess(self, in_path, out_path): | |
37 with open(in_path, 'r') as fp: | |
38 sitemap = yaml.load(fp) | |
39 | |
40 with open(out_path, 'w') as fp: | |
41 fp.write(SITEMAP_HEADER) | |
42 self._writeManualLocs(sitemap, fp) | |
43 self._writeAutoLocs(sitemap, fp) | |
44 fp.write(SITEMAP_FOOTER) | |
45 | |
46 return True | |
47 | |
48 def _writeManualLocs(self, sitemap, fp): | |
49 locs = sitemap.setdefault('locations', None) | |
50 if not locs: | |
51 return | |
52 | |
53 logger.debug("Generating manual sitemap entries.") | |
54 for loc in locs: | |
55 self._writeEntry(loc, fp) | |
56 | |
57 def _writeAutoLocs(self, sitemap, fp): | |
58 source_names = sitemap.setdefault('autogen', None) | |
59 if not source_names: | |
60 return | |
61 | |
62 for name in source_names: | |
63 logger.debug("Generating automatic sitemap entries for '%s'." % | |
430
21e26ed867b6
internal: Create full route metadata in one place.
Ludovic Chabant <ludovic@chabant.com>
parents:
287
diff
changeset
|
64 name) |
34 | 65 source = self.app.getSource(name) |
66 if source is None: | |
67 raise Exception("No such source: %s" % name) | |
68 | |
114
371a6c879ab9
When possible, try and batch-load pages so we only lock once.
Ludovic Chabant <ludovic@chabant.com>
parents:
34
diff
changeset
|
69 for page in source.getPages(): |
430
21e26ed867b6
internal: Create full route metadata in one place.
Ludovic Chabant <ludovic@chabant.com>
parents:
287
diff
changeset
|
70 route_metadata = create_route_metadata(page) |
21e26ed867b6
internal: Create full route metadata in one place.
Ludovic Chabant <ludovic@chabant.com>
parents:
287
diff
changeset
|
71 route = self.app.getRoute(source.name, route_metadata) |
21e26ed867b6
internal: Create full route metadata in one place.
Ludovic Chabant <ludovic@chabant.com>
parents:
287
diff
changeset
|
72 uri = route.getUri(route_metadata) |
34 | 73 |
74 t = page.datetime.timestamp() | |
75 sm_cfg = page.config.get('sitemap') | |
76 | |
77 args = {'url': uri, 'lastmod': strftime_iso8601(t)} | |
78 if sm_cfg: | |
234
1c4078ec3011
sitemap: Fixed typo bug.
Ludovic Chabant <ludovic@chabant.com>
parents:
147
diff
changeset
|
79 args.update(sm_cfg) |
34 | 80 |
81 self._writeEntry(args, fp) | |
82 | |
83 def _writeEntry(self, args, fp): | |
84 fp.write(SITEURL_HEADER) | |
85 fp.write(SITEURL_LOC % args['url']) | |
86 if 'lastmod' in args: | |
87 fp.write(SITEURL_LASTMOD % args['lastmod']) | |
88 if 'changefreq' in args: | |
89 fp.write(SITEURL_CHANGEFREQ % args['changefreq']) | |
90 if 'priority' in args: | |
91 fp.write(SITEURL_PRIORITY % args['priority']) | |
92 fp.write(SITEURL_FOOTER) | |
93 | |
94 | |
95 def strftime_iso8601(t): | |
96 return time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(t)) | |
97 |