changeset 466:456db44dcc53

bake: Pass the config variants and values from the CLI to the baker. TODO: add support for that for the processor pipeline.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 11 Jul 2015 23:51:02 -0700
parents b6e797463798
children a8028bf329a2
files piecrust/app.py piecrust/baking/baker.py piecrust/baking/worker.py piecrust/commands/base.py piecrust/commands/builtin/baking.py piecrust/main.py tests/bakes/test_variant.yaml tests/conftest.py
diffstat 8 files changed, 73 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/app.py	Sat Jul 11 20:33:55 2015 -0700
+++ b/piecrust/app.py	Sat Jul 11 23:51:02 2015 -0700
@@ -607,3 +607,14 @@
 
         return dirs
 
+
+def apply_variant_and_values(app, config_variant=None, config_values=None):
+    if config_variant is not None:
+        logger.debug("Applying configuration variant '%s'." % config_variant)
+        app.config.applyVariant('variants/' + config_variant)
+
+    if config_values is not None:
+        for name, value in config_values:
+            logger.debug("Setting configuration '%s' to: %s" % (name, value))
+            app.config.set(name, value)
+
--- a/piecrust/baking/baker.py	Sat Jul 11 20:33:55 2015 -0700
+++ b/piecrust/baking/baker.py	Sat Jul 11 23:51:02 2015 -0700
@@ -2,7 +2,6 @@
 import os.path
 import hashlib
 import logging
-import multiprocessing
 from piecrust.baking.records import (
         BakeRecordEntry, TransitionalBakeRecord, TaxonomyInfo)
 from piecrust.baking.worker import (
@@ -19,11 +18,15 @@
 
 
 class Baker(object):
-    def __init__(self, app, out_dir, force=False):
+    def __init__(self, app, out_dir, force=False,
+                 applied_config_variant=None,
+                 applied_config_values=None):
         assert app and out_dir
         self.app = app
         self.out_dir = out_dir
         self.force = force
+        self.applied_config_variant = applied_config_variant
+        self.applied_config_values = applied_config_values
 
         # Remember what taxonomy pages we should skip
         # (we'll bake them repeatedly later with each taxonomy term)
@@ -546,6 +549,8 @@
         ctx = BakeWorkerContext(
                 self.app.root_dir, self.app.cache.base_dir, self.out_dir,
                 previous_record_path=previous_record_path,
+                config_variant=self.applied_config_variant,
+                config_values=self.applied_config_values,
                 force=self.force, debug=self.app.debug)
         pool = WorkerPool(
                 worker_count=worker_count,
--- a/piecrust/baking/worker.py	Sat Jul 11 20:33:55 2015 -0700
+++ b/piecrust/baking/worker.py	Sat Jul 11 23:51:02 2015 -0700
@@ -1,6 +1,6 @@
 import time
 import logging
-from piecrust.app import PieCrust
+from piecrust.app import PieCrust, apply_variant_and_values
 from piecrust.baking.records import BakeRecord, _get_transition_key
 from piecrust.baking.single import PageBaker, BakingError
 from piecrust.environment import AbortedSourceUseError
@@ -17,11 +17,14 @@
 class BakeWorkerContext(object):
     def __init__(self, root_dir, sub_cache_dir, out_dir,
                  previous_record_path=None,
+                 config_variant=None, config_values=None,
                  force=False, debug=False):
         self.root_dir = root_dir
         self.sub_cache_dir = sub_cache_dir
         self.out_dir = out_dir
         self.previous_record_path = previous_record_path
+        self.config_variant = config_variant
+        self.config_values = config_values
         self.force = force
         self.debug = debug
         self.app = None
@@ -42,6 +45,8 @@
         app.env.registerTimer("BakeWorker_%d_Total" % self.wid)
         app.env.registerTimer("BakeWorkerInit")
         app.env.registerTimer("JobReceive")
+        apply_variant_and_values(app, self.ctx.config_variant,
+                                 self.ctx.config_values)
         self.ctx.app = app
 
         # Load previous record
--- a/piecrust/commands/base.py	Sat Jul 11 20:33:55 2015 -0700
+++ b/piecrust/commands/base.py	Sat Jul 11 23:51:02 2015 -0700
@@ -12,6 +12,8 @@
         self.app = app
         self.parser = parser
         self.args = args
+        self.config_variant = None
+        self.config_values = None
 
 
 class ChefCommand(object):
--- a/piecrust/commands/builtin/baking.py	Sat Jul 11 20:33:55 2015 -0700
+++ b/piecrust/commands/builtin/baking.py	Sat Jul 11 23:51:02 2015 -0700
@@ -98,7 +98,9 @@
             ctx.app.config.set('baker/batch_size', ctx.args.batch_size)
         baker = Baker(
                 ctx.app, out_dir,
-                force=ctx.args.force)
+                force=ctx.args.force,
+                applied_config_variant=ctx.config_variant,
+                applied_config_values=ctx.config_values)
         record = baker.bake()
         _merge_timers(record.timers, ctx.timers)
         return record.success
--- a/piecrust/main.py	Sat Jul 11 20:33:55 2015 -0700
+++ b/piecrust/main.py	Sat Jul 11 23:51:02 2015 -0700
@@ -6,7 +6,8 @@
 import argparse
 import colorama
 from piecrust import APP_VERSION
-from piecrust.app import PieCrust, PieCrustConfiguration
+from piecrust.app import (
+        PieCrust, PieCrustConfiguration, apply_variant_and_values)
 from piecrust.chefutil import (
         format_timed, log_friendly_exception, print_help_item)
 from piecrust.commands.base import CommandContext
@@ -184,15 +185,16 @@
     # Build a hash for a custom cache directory.
     cache_key = 'default'
 
-    # Handle a configuration variant.
+    # Handle custom configurations.
+    if pre_args.config_variant is not None and not root:
+        raise SiteNotFoundError("Can't apply any variant.")
+    apply_variant_and_values(pre_args.config_variant,
+                             pre_args.config_values)
+
+    # Adjust the cache key.
     if pre_args.config_variant is not None:
-        if not root:
-            raise SiteNotFoundError("Can't apply any variant.")
-        app.config.applyVariant('variants/' + pre_args.config_variant)
         cache_key += ',variant=%s' % pre_args.config_variant
     for name, value in pre_args.config_values:
-        logger.debug("Setting configuration '%s' to: %s" % (name, value))
-        app.config.set(name, value)
         cache_key += ',%s=%s' % (name, value)
 
     # Setup the arg parser.
@@ -265,6 +267,9 @@
 
     # Run the command!
     ctx = CommandContext(app, parser, result)
+    ctx.config_variant = pre_args.config_variant
+    ctx.config_values = pre_args.config_values
+
     exit_code = result.func(ctx)
     if exit_code is None:
         return 0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/bakes/test_variant.yaml	Sat Jul 11 23:51:02 2015 -0700
@@ -0,0 +1,21 @@
+---
+config:
+    what: not good
+    variants:
+        test:
+            what: awesome
+config_variant: test
+in:
+    pages/_index.md: 'This is {{what}}.'
+out:
+    index.html: 'This is awesome.'
+---
+config:
+    what: not good
+config_values:
+    what: awesome
+in:
+    pages/_index.md: 'This is {{what}}.'
+out:
+    index.html: 'This is awesome.'
+
--- a/tests/conftest.py	Sat Jul 11 20:33:55 2015 -0700
+++ b/tests/conftest.py	Sat Jul 11 23:51:02 2015 -0700
@@ -7,6 +7,7 @@
 import pytest
 import yaml
 import colorama
+from piecrust.app import apply_variant_and_values
 from piecrust.configuration import merge_dicts
 from .mockutil import mock_fs, mock_fs_scope
 
@@ -174,7 +175,16 @@
         with mock_fs_scope(fs):
             out_dir = fs.path('kitchen/_counter')
             app = fs.getApp()
-            baker = Baker(app, out_dir)
+
+            variant = self.spec.get('config_variant')
+            values = self.spec.get('config_values')
+            if values is not None:
+                values = list(values.items())
+            apply_variant_and_values(app, variant, values)
+
+            baker = Baker(app, out_dir,
+                          applied_config_variant=variant,
+                          applied_config_values=values)
             record = baker.bake()
 
             if not record.success: