comparison piecrust/commands/builtin/publishing.py @ 758:6abb436fea5b

publish: Make publisher more powerful and better exposed on the command line. * Make the `chef publish` command have one sub-command per publish target. * Add custom argument parsing per publisher to have strong extra arguments available per publish target. * Make publish targets a first class citizen of the `PieCrust` app class.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 25 Jun 2016 17:03:29 -0700
parents 8f9c0bdb3724
children 71a755512eb8
comparison
equal deleted inserted replaced
757:7147b06670fd 758:6abb436fea5b
1 import logging 1 import logging
2 import urllib.parse 2 import urllib.parse
3 from piecrust.commands.base import ChefCommand 3 from piecrust.commands.base import ChefCommand
4 from piecrust.pathutil import SiteNotFoundError
4 from piecrust.publishing.publisher import Publisher, find_publisher_name 5 from piecrust.publishing.publisher import Publisher, find_publisher_name
5 6
6 7
7 logger = logging.getLogger(__name__) 8 logger = logging.getLogger(__name__)
8 9
15 self.name = 'publish' 16 self.name = 'publish'
16 self.description = "Publishes you website to a specific target." 17 self.description = "Publishes you website to a specific target."
17 18
18 def setupParser(self, parser, app): 19 def setupParser(self, parser, app):
19 parser.add_argument( 20 parser.add_argument(
20 '-l', '--list',
21 action='store_true',
22 help="List available publish targets for the current site.")
23 parser.add_argument(
24 '--log-publisher', 21 '--log-publisher',
25 metavar='LOG_FILE', 22 metavar='LOG_FILE',
26 help="Log the publisher's output to a given file.") 23 help="Log the publisher's output to a given file.")
27 parser.add_argument( 24 parser.add_argument(
28 '--preview', 25 '--preview',
29 action='store_true', 26 action='store_true',
30 help="Only preview what the publisher would do.") 27 help="Only preview what the publisher would do.")
31 parser.add_argument(
32 'target',
33 nargs='?',
34 default='default',
35 help="The publish target to use.")
36 28
37 def run(self, ctx): 29 subparsers = parser.add_subparsers()
38 if ctx.args.list: 30 for pub in app.publishers:
39 pub_cfg = ctx.app.config.get('publish') 31 p = subparsers.add_parser(
40 if not pub_cfg: 32 pub.target,
41 logger.info("No available publish targets.") 33 help="Publish using target '%s'." % pub.target)
42 return 34 pub.setupPublishParser(p, app)
35 p.set_defaults(sub_func=self._doPublish)
36 p.set_defaults(target=pub.target)
43 37
44 for name, cfg in pub_cfg.items(): 38 if not app.publishers:
45 if isinstance(cfg, dict): 39 parser.epilog = (
46 pub_type = cfg.get('type') 40 "No publishers have been defined. You can define publishers "
47 if pub_type: 41 "through the `publish` configuration settings. "
48 desc = cfg.get('description') 42 "For more information see: "
49 bake_first = cfg.get('bake', True) 43 "https://bolt80.com/piecrust/en/latest/docs/publishing/")
50 msg = '%s (%s)' % (name, pub_type) 44
51 if not bake_first: 45 def checkedRun(self, ctx):
52 msg += ' (no local baking)' 46 if ctx.app.root_dir is None:
53 if desc: 47 raise SiteNotFoundError(theme=ctx.app.theme_site)
54 msg += ': ' + desc 48
55 logger.info(msg) 49 if not hasattr(ctx.args, 'sub_func'):
56 else: 50 ctx.parser.parse_args(['publish', '--help'])
57 logger.error(
58 "%s (unknown type '%s')" % (name, pub_type))
59 elif isinstance(cfg, str):
60 comps = urllib.parse.urlparse(str(cfg))
61 pub_name = find_publisher_name(ctx.app, comps.scheme)
62 if pub_name:
63 logger.info("%s (%s)" % (name, pub_name))
64 else:
65 logger.error(
66 "%s (unknown scheme '%s')" %
67 (name, comps.scheme))
68 else:
69 logger.error(
70 "%s (incorrect configuration)" % name)
71 return 51 return
52 ctx.args.sub_func(ctx)
72 53
54 def _doPublish(self, ctx):
73 pub = Publisher(ctx.app) 55 pub = Publisher(ctx.app)
74 pub.run( 56 pub.run(
75 ctx.args.target, 57 ctx.args.target,
76 preview=ctx.args.preview, 58 preview=ctx.args.preview,
77 log_file=ctx.args.log_publisher) 59 extra_args=ctx.args,
60 log_file=ctx.args.log_publisher,
61 applied_config_variant=ctx.config_variant,
62 applied_config_values=ctx.config_values)
78 63