changeset 719:a066f4ac9094

rendering: Use `fastpickle` serialization before JSON. This is because JSON loses information about stuff like tuples.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 28 May 2016 15:43:24 -0700
parents d4408fbbbc7d
children 3e188d88a9ac
files piecrust/cache.py piecrust/rendering.py tests/bakes/test_simple_tags.yaml
diffstat 3 files changed, 43 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/cache.py	Sat May 28 15:41:51 2016 -0700
+++ b/piecrust/cache.py	Sat May 28 15:43:24 2016 -0700
@@ -201,9 +201,7 @@
                 logger.debug("'%s' found in file-system cache." %
                              key)
                 item_raw = self.fs_cache.read(fs_key)
-                item = json.loads(
-                        item_raw,
-                        object_pairs_hook=collections.OrderedDict)
+                item = json.loads(item_raw)
                 self.cache.put(key, item)
                 self._hits += 1
                 return item
--- a/piecrust/rendering.py	Sat May 28 15:41:51 2016 -0700
+++ b/piecrust/rendering.py	Sat May 28 15:43:24 2016 -0700
@@ -7,6 +7,7 @@
         DataBuildingContext, build_page_data, build_layout_data)
 from piecrust.data.filters import (
         PaginationFilter, SettingFilterClause, page_value_accessor)
+from piecrust.fastpickle import _pickle_object, _unpickle_object
 from piecrust.sources.base import PageSource
 from piecrust.templating.base import TemplateNotFoundError, TemplatingError
 
@@ -93,27 +94,6 @@
         return self._custom_info.get(key, default)
 
 
-    def _toJson(self):
-        data = {
-                'used_source_names': list(self.used_source_names),
-                'used_pagination': self.used_pagination,
-                'pagination_has_more': self.pagination_has_more,
-                'used_assets': self.used_assets,
-                'custom_info': self._custom_info}
-        return data
-
-    @staticmethod
-    def _fromJson(data):
-        assert data is not None
-        rpi = RenderPassInfo()
-        rpi.used_source_names = set(data['used_source_names'])
-        rpi.used_pagination = data['used_pagination']
-        rpi.pagination_has_more = data['pagination_has_more']
-        rpi.used_assets = data['used_assets']
-        rpi._custom_info = data['custom_info']
-        return rpi
-
-
 class PageRenderingContext(object):
     def __init__(self, qualified_page, page_num=1,
                  force_render=False, is_from_request=False):
@@ -218,11 +198,11 @@
         rp = RenderedPage(page, ctx.uri, ctx.page_num)
         rp.data = page_data
         rp.content = layout_result['content']
-        rp.render_info[PASS_FORMATTING] = RenderPassInfo._fromJson(
-                    render_result['pass_info'])
+        rp.render_info[PASS_FORMATTING] = _unpickle_object(
+                render_result['pass_info'])
         if layout_result['pass_info'] is not None:
-            rp.render_info[PASS_RENDERING] = RenderPassInfo._fromJson(
-                layout_result['pass_info'])
+            rp.render_info[PASS_RENDERING] = _unpickle_object(
+                    layout_result['pass_info'])
         return rp
     except Exception as ex:
         if ctx.app.debug:
@@ -260,7 +240,7 @@
 
     rs = RenderedSegments(
             render_result['segments'],
-            RenderPassInfo._fromJson(render_result['pass_info']))
+            _unpickle_object(render_result['pass_info']))
     return rs
 
 
@@ -319,7 +299,7 @@
     pass_info = cpi.render_ctx.render_passes[PASS_FORMATTING]
     res = {
             'segments': formatted_segments,
-            'pass_info': pass_info._toJson()}
+            'pass_info': _pickle_object(pass_info)}
     return res
 
 
@@ -351,7 +331,7 @@
         raise Exception(msg) from ex
 
     pass_info = cpi.render_ctx.render_passes[PASS_RENDERING]
-    res = {'content': output, 'pass_info': pass_info._toJson()}
+    res = {'content': output, 'pass_info': _pickle_object(pass_info)}
     return res
 
 
--- a/tests/bakes/test_simple_tags.yaml	Sat May 28 15:41:51 2016 -0700
+++ b/tests/bakes/test_simple_tags.yaml	Sat May 28 15:43:24 2016 -0700
@@ -55,4 +55,38 @@
         whatever.html: |
           Pages in whatever
           Post 02
+---
+in:
+    posts/2016-06-01_post01.md: |
+        ---
+        title: Post 01
+        tags: [foo, bar]
+        ---
+    posts/2016-06-02_post02.md: |
+        ---
+        title: Post 02
+        tags: [bar, foo]
+        ---
+    pages/_tag.md: |
+        Pages in {{tags|join(', ')}}
+        {% for p in pagination.posts -%}
+        {{p.title}}
+        {% endfor %}
+    pages/blah.md: |
+        Link to: {{pctagurl('foo', 'bar')}}
+outfiles:
+    blah.html: |
+        Link to: /tag/foo/bar.html
+    tag/foo.html: |
+        Pages in foo
+        Post 02
+        Post 01
+    tag/bar.html: |
+        Pages in bar
+        Post 02
+        Post 01
+    tag/foo/bar.html: |
+        Pages in foo, bar
+        Post 02
+        Post 01