diff piecrust/commands/builtin/util.py @ 3:f485ba500df3

Gigantic change to basically make PieCrust 2 vaguely functional. - Serving works, with debug window. - Baking works, multi-threading, with dependency handling. - Various things not implemented yet.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 10 Aug 2014 23:43:16 -0700
parents aaa8fb7c8918
children 474c9882decf
line wrap: on
line diff
--- a/piecrust/commands/builtin/util.py	Wed Dec 25 22:16:46 2013 -0800
+++ b/piecrust/commands/builtin/util.py	Sun Aug 10 23:43:16 2014 -0700
@@ -1,10 +1,12 @@
 import os
 import os.path
+import shutil
 import codecs
 import logging
 import yaml
 from piecrust.app import CONFIG_PATH
 from piecrust.commands.base import ChefCommand
+from piecrust.sources.base import IPreparingSource, MODE_CREATING
 
 
 logger = logging.getLogger(__name__)
@@ -17,7 +19,7 @@
         self.description = "Creates a new empty PieCrust website."
         self.requires_website = False
 
-    def setupParser(self, parser):
+    def setupParser(self, parser, app):
         parser.add_argument('destination',
                 help="The destination directory in which to create the website.")
 
@@ -28,10 +30,10 @@
 
         if not os.path.isdir(destination):
             os.makedirs(destination, 0755)
-        
+
         config_path = os.path.join(destination, CONFIG_PATH)
         if not os.path.isdir(os.path.dirname(config_path)):
-            os.makedirs(os.path.dirname(config_path))
+            os.makedirs(os.path.dirname(config_path), 0755)
 
         config_text = yaml.dump({
                 'site': {
@@ -46,4 +48,58 @@
                 default_flow_style=False)
         with codecs.open(config_path, 'w', 'utf-8') as fp:
             fp.write(config_text)
-        
+
+
+class PurgeCommand(ChefCommand):
+    def __init__(self):
+        super(PurgeCommand, self).__init__()
+        self.name = 'purge'
+        self.description = "Purges the website's cache."
+
+    def setupParser(self, parser, app):
+        pass
+
+    def run(self, ctx):
+        cache_dir = ctx.app.cache_dir
+        if os.path.isdir(cache_dir):
+            logger.info("Purging cache: %s" % cache_dir)
+            shutil.rmtree(cache_dir)
+
+
+class PrepareCommand(ChefCommand):
+    def __init__(self):
+        super(PrepareCommand, self).__init__()
+        self.name = 'prepare'
+        self.description = "Prepares new content for your website."
+
+    def setupParser(self, parser, app):
+        subparsers = parser.add_subparsers()
+        for src in app.sources:
+            if not isinstance(src, IPreparingSource):
+                logger.debug("Skipping source '%s' because it's not preparable.")
+                continue
+            p = subparsers.add_parser(src.name)
+            src.setupPrepareParser(p, app)
+            p.set_defaults(source=src)
+
+    def run(self, ctx):
+        app = ctx.app
+        source = ctx.args.source
+        metadata = source.buildMetadata(ctx.args)
+        page_path = source.findPagePath(metadata, MODE_CREATING)
+        name, ext = os.path.splitext(page_path)
+        if ext == '.*':
+            page_path = '%s.%s' % (name,
+                    app.config.get('site/default_auto_format'))
+        if os.path.exists(page_path):
+            raise Exception("'%s' already exists." % page_path)
+
+        logger.info("Creating page: %s" % os.path.relpath(page_path, app.root_dir))
+        if not os.path.exists(os.path.dirname(page_path)):
+            os.makedirs(os.path.dirname(page_path), 0755)
+        with open(page_path, 'w') as f:
+            f.write('---\n')
+            f.write('title: %s\n' % 'Unknown title')
+            f.write('---\n')
+            f.write("This is a new page!\n")
+