changeset 7:343d08ef5668

More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 16 Aug 2014 15:07:22 -0700
parents f5ca5c5bed85
children ff07f6d23450
files piecrust/baking/baker.py piecrust/baking/records.py piecrust/data/debug.py piecrust/data/provider.py piecrust/main.py piecrust/page.py piecrust/plugins/base.py piecrust/processing/less.py piecrust/records.py piecrust/rendering.py piecrust/serving.py piecrust/sources/base.py piecrust/templating/jinjaengine.py
diffstat 13 files changed, 45 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/baking/baker.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/baking/baker.py	Sat Aug 16 15:07:22 2014 -0700
@@ -192,7 +192,7 @@
         if not os.path.isdir(out_dir):
             os.makedirs(out_dir, 0o755)
 
-        with codecs.open(out_path, 'w', 'utf-8') as fp:
+        with codecs.open(out_path, 'w', 'utf8') as fp:
             fp.write(rp.content)
 
         return ctx, rp
@@ -235,7 +235,8 @@
         # Load/create the bake record.
         record = TransitionalBakeRecord()
         record_cache = self.app.cache.getCache('bake_r')
-        record_name = hashlib.md5(self.out_dir).hexdigest() + '.record'
+        record_name = (hashlib.md5(self.out_dir.encode('utf8')).hexdigest() +
+                '.record')
         if not self.force and record_cache.has(record_name):
             t = time.clock()
             record.loadPrevious(record_cache.getCachePath(record_name))
--- a/piecrust/baking/records.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/baking/records.py	Sat Aug 16 15:07:22 2014 -0700
@@ -65,7 +65,14 @@
         self.current.entry_added += self._onCurrentEntryAdded
 
     def loadPrevious(self, previous_path):
-        self.previous = BakeRecord.load(previous_path)
+        try:
+            self.previous = BakeRecord.load(previous_path)
+        except Exception as ex:
+            logger.debug("Error loading previous record: %s" % ex)
+            logger.debug("Will reset to an empty one.")
+            self.previous = BakeRecord()
+            return
+
         for e in self.previous.entries:
             self.transitions[e.transition_key] = (e, None)
 
--- a/piecrust/data/debug.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/data/debug.py	Sat Aug 16 15:07:22 2014 -0700
@@ -175,9 +175,7 @@
             self._write('<span style="%s">%4.2f</span>' % (CSS_VALUE, data))
             return
 
-        if data_type in (str, str):
-            if data_type == str:
-                data = data.decode('utf8')
+        if data_type is str:
             if len(data) > DebugDataRenderer.MAX_VALUE_LENGTH:
                 data = data[:DebugDataRenderer.MAX_VALUE_LENGTH - 5]
                 data += '[...]'
--- a/piecrust/data/provider.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/data/provider.py	Sat Aug 16 15:07:22 2014 -0700
@@ -1,5 +1,4 @@
 import time
-import itertools
 from piecrust.data.iterators import PageIterator
 from piecrust.sources.base import ArraySource
 
@@ -18,7 +17,10 @@
 
     def __getattr__(self, name):
         if self._user_data is not None:
-            return self._user_data[name]
+            try:
+                return self._user_data[name]
+            except KeyError:
+                pass
         raise AttributeError()
 
     def __getitem__(self, name):
@@ -170,6 +172,8 @@
         for fac in self._source.getPageFactories():
             post = fac.buildPage()
             tax_values = post.config.get(tax_name)
+            if tax_values is None:
+                continue
             if not isinstance(tax_values, list):
                 tax_values = [tax_values]
             for val in tax_values:
--- a/piecrust/main.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/main.py	Sat Aug 16 15:07:22 2014 -0700
@@ -135,7 +135,7 @@
     parser.add_argument('--log', help="Send log messages to the specified file.")
 
     commands = sorted(app.plugin_loader.getCommands(),
-            lambda a, b: cmp(a.name, b.name))
+            key=lambda c: c.name)
     subparsers = parser.add_subparsers()
     for c in commands:
         p = subparsers.add_parser(c.name, help=c.description)
--- a/piecrust/page.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/page.py	Sat Aug 16 15:07:22 2014 -0700
@@ -172,7 +172,7 @@
 
     # Check the cache first.
     cache = app.cache.getCache('pages')
-    cache_path = "%s.json" % hashlib.md5(path).hexdigest()
+    cache_path = "%s.json" % hashlib.md5(path.encode('utf8')).hexdigest()
     page_time = os.path.getmtime(path)
     if cache.isValid(cache_path, page_time):
         exec_info.was_cache_valid = True
--- a/piecrust/plugins/base.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/plugins/base.py	Sat Aug 16 15:07:22 2014 -0700
@@ -105,7 +105,7 @@
     def _loadPlugin(self, plugin_dir):
         pass
 
-    def _getPluginComponents(self, name, initialize=False, order_cmp=None, order_key=None):
+    def _getPluginComponents(self, name, initialize=False, order_key=None):
         if name in self._componentCache:
             return self._componentCache[name]
 
@@ -117,8 +117,8 @@
                 for comp in plugin_components:
                     comp.initialize(self.app)
 
-        if order_cmp is not None or order_key is not None:
-            all_components.sort(cmp=order_cmp, key=order_key)
+        if order_key is not None:
+            all_components.sort(key=order_key)
 
         self._componentCache[name] = all_components
         return all_components
--- a/piecrust/processing/less.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/processing/less.py	Sat Aug 16 15:07:22 2014 -0700
@@ -71,7 +71,7 @@
                             "must be an array of arguments.")
 
     def _getMapPath(self, path):
-        map_name = "%s.map" % hashlib.md5(path).hexdigest()
+        map_name = "%s.map" % hashlib.md5(path.encode('utf8')).hexdigest()
         map_path = os.path.join(self._map_dir, map_name)
         return map_path
 
--- a/piecrust/records.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/records.py	Sat Aug 16 15:07:22 2014 -0700
@@ -35,7 +35,7 @@
         if not os.path.isdir(path_dir):
             os.makedirs(path_dir, 0o755)
 
-        with open(path, 'w') as fp:
+        with open(path, 'wb') as fp:
             pickle.dump(self, fp, pickle.HIGHEST_PROTOCOL)
 
     def __getstate__(self):
@@ -46,6 +46,6 @@
     @staticmethod
     def load(path):
         logger.debug("Loading bake record from: %s" % path)
-        with open(path, 'r') as fp:
+        with open(path, 'rb') as fp:
             return pickle.load(fp)
 
--- a/piecrust/rendering.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/rendering.py	Sat Aug 16 15:07:22 2014 -0700
@@ -1,6 +1,5 @@
 import re
 import os.path
-import codecs
 import logging
 from piecrust.data.builder import (DataBuildingContext, build_page_data,
         build_layout_data)
@@ -98,7 +97,7 @@
 
         rp = RenderedPage(page, ctx.uri, ctx.page_num)
         rp.data = page_data
-        rp.content = codecs.encode(output, 'utf8')
+        rp.content = output
         rp.execution_info = eis.current_page_info
         return rp
     finally:
--- a/piecrust/serving.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/serving.py	Sat Aug 16 15:07:22 2014 -0700
@@ -23,8 +23,6 @@
 logger = logging.getLogger(__name__)
 
 
-
-
 class Server(object):
     def __init__(self, root_dir, host='localhost', port='8080',
                  debug=False, static_preview=True):
@@ -122,7 +120,7 @@
         pipeline.run(asset_in_path)
 
         logger.debug("Serving %s" % asset_out_path)
-        wrapper = wrap_file(environ, open(asset_out_path))
+        wrapper = wrap_file(environ, open(asset_out_path, 'rb'))
         response = Response(wrapper)
         _, ext = os.path.splitext(rel_req_path)
         response.mimetype = self._mimetype_map.get(
@@ -138,7 +136,7 @@
             return None
 
         logger.debug("Serving %s" % full_path)
-        wrapper = wrap_file(environ, open(full_path))
+        wrapper = wrap_file(environ, open(full_path, 'rb'))
         response = Response(wrapper)
         _, ext = os.path.splitext(full_path)
         response.mimetype = self._mimetype_map.get(
@@ -197,10 +195,17 @@
         rendered_page = render_page(render_ctx)
         rp_content = rendered_page.content
 
+        if app.debug:
+            now_time = time.clock()
+            timing_info = ('%8.1f ms' %
+                    ((now_time - app.env.start_time) * 1000.0))
+            rp_content = rp_content.replace('__PIECRUST_TIMING_INFORMATION__',
+                    timing_info)
+
         # Start response.
         response = Response()
 
-        etag = hashlib.md5(rp_content).hexdigest()
+        etag = hashlib.md5(rp_content.encode('utf8')).hexdigest()
         if not app.debug and etag in request.if_none_match:
             response.status_code = 304
             return response
@@ -227,29 +232,18 @@
         if mimetype:
             response.mimetype = mimetype
 
-        if app.debug:
-            now_time = time.clock()
-            timing_info = ('%8.1f ms' %
-                    ((now_time - app.env.start_time) * 1000.0))
-            rp_content = rp_content.replace('__PIECRUST_TIMING_INFORMATION__',
-                    timing_info)
-
         if ('gzip' in request.accept_encodings and
                 app.config.get('site/enable_gzip')):
             try:
-                gzip_buffer = io.StringIO()
-                gzip_file = gzip.GzipFile(
-                        mode='wb',
-                        compresslevel=9,
-                        fileobj=gzip_buffer)
-                gzip_file.write(rp_content)
-                gzip_file.close()
-                rp_content = gzip_buffer.getvalue()
-                response.content_encoding = 'gzip'
+                with io.BytesIO() as gzip_buffer:
+                    with gzip.open(gzip_buffer, mode='wt',
+                                   encoding='utf8') as gzip_file:
+                        gzip_file.write(rp_content)
+                    rp_content = gzip_buffer.getvalue()
+                    response.content_encoding = 'gzip'
             except Exception:
                 logger.exception("Error compressing response, "
                                  "falling back to uncompressed.")
-                rp_content = rendered_page.content
         response.set_data(rp_content)
 
         return response
--- a/piecrust/sources/base.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/sources/base.py	Sat Aug 16 15:07:22 2014 -0700
@@ -274,7 +274,7 @@
         return None
 
 
-class ArraySource(PageSource):
+class ArraySource(PageSource, SimplePaginationSourceMixin):
     def __init__(self, app, inner_source, name='array', config=None):
         super(ArraySource, self).__init__(app, name, config or {})
         self.inner_source = inner_source
--- a/piecrust/templating/jinjaengine.py	Sat Aug 16 08:15:30 2014 -0700
+++ b/piecrust/templating/jinjaengine.py	Sat Aug 16 15:07:22 2014 -0700
@@ -231,7 +231,7 @@
         # we only listen to ``'pccache'`` so this will be a name token with
         # `pccache` as value.  We get the line number so that we can give
         # that line number to the nodes we create by hand.
-        lineno = parser.stream.next().lineno
+        lineno = next(parser.stream).lineno
 
         # now we parse a single expression that is used as cache key.
         args = [parser.parse_expression()]