# HG changeset patch # User Ludovic Chabant # Date 1436683862 25200 # Node ID 456db44dcc5391c318adc13f716b96fc2013bdfb # Parent b6e7974637987ddfc4ff9611e628eab71f9ebd3a bake: Pass the config variants and values from the CLI to the baker. TODO: add support for that for the processor pipeline. diff -r b6e797463798 -r 456db44dcc53 piecrust/app.py --- 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) + diff -r b6e797463798 -r 456db44dcc53 piecrust/baking/baker.py --- 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, diff -r b6e797463798 -r 456db44dcc53 piecrust/baking/worker.py --- 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 diff -r b6e797463798 -r 456db44dcc53 piecrust/commands/base.py --- 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): diff -r b6e797463798 -r 456db44dcc53 piecrust/commands/builtin/baking.py --- 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 diff -r b6e797463798 -r 456db44dcc53 piecrust/main.py --- 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 diff -r b6e797463798 -r 456db44dcc53 tests/bakes/test_variant.yaml --- /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.' + diff -r b6e797463798 -r 456db44dcc53 tests/conftest.py --- 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: