comparison 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
comparison
equal deleted inserted replaced
2:40fa08b261b9 3:f485ba500df3
1 import os 1 import os
2 import os.path 2 import os.path
3 import shutil
3 import codecs 4 import codecs
4 import logging 5 import logging
5 import yaml 6 import yaml
6 from piecrust.app import CONFIG_PATH 7 from piecrust.app import CONFIG_PATH
7 from piecrust.commands.base import ChefCommand 8 from piecrust.commands.base import ChefCommand
9 from piecrust.sources.base import IPreparingSource, MODE_CREATING
8 10
9 11
10 logger = logging.getLogger(__name__) 12 logger = logging.getLogger(__name__)
11 13
12 14
15 super(InitCommand, self).__init__() 17 super(InitCommand, self).__init__()
16 self.name = 'init' 18 self.name = 'init'
17 self.description = "Creates a new empty PieCrust website." 19 self.description = "Creates a new empty PieCrust website."
18 self.requires_website = False 20 self.requires_website = False
19 21
20 def setupParser(self, parser): 22 def setupParser(self, parser, app):
21 parser.add_argument('destination', 23 parser.add_argument('destination',
22 help="The destination directory in which to create the website.") 24 help="The destination directory in which to create the website.")
23 25
24 def run(self, ctx): 26 def run(self, ctx):
25 destination = ctx.args.destination 27 destination = ctx.args.destination
26 if destination is None: 28 if destination is None:
27 destination = os.getcwd() 29 destination = os.getcwd()
28 30
29 if not os.path.isdir(destination): 31 if not os.path.isdir(destination):
30 os.makedirs(destination, 0755) 32 os.makedirs(destination, 0755)
31 33
32 config_path = os.path.join(destination, CONFIG_PATH) 34 config_path = os.path.join(destination, CONFIG_PATH)
33 if not os.path.isdir(os.path.dirname(config_path)): 35 if not os.path.isdir(os.path.dirname(config_path)):
34 os.makedirs(os.path.dirname(config_path)) 36 os.makedirs(os.path.dirname(config_path), 0755)
35 37
36 config_text = yaml.dump({ 38 config_text = yaml.dump({
37 'site': { 39 'site': {
38 'title': "My New Website", 40 'title': "My New Website",
39 'description': "A website recently generated with PieCrust", 41 'description': "A website recently generated with PieCrust",
44 } 46 }
45 }, 47 },
46 default_flow_style=False) 48 default_flow_style=False)
47 with codecs.open(config_path, 'w', 'utf-8') as fp: 49 with codecs.open(config_path, 'w', 'utf-8') as fp:
48 fp.write(config_text) 50 fp.write(config_text)
49 51
52
53 class PurgeCommand(ChefCommand):
54 def __init__(self):
55 super(PurgeCommand, self).__init__()
56 self.name = 'purge'
57 self.description = "Purges the website's cache."
58
59 def setupParser(self, parser, app):
60 pass
61
62 def run(self, ctx):
63 cache_dir = ctx.app.cache_dir
64 if os.path.isdir(cache_dir):
65 logger.info("Purging cache: %s" % cache_dir)
66 shutil.rmtree(cache_dir)
67
68
69 class PrepareCommand(ChefCommand):
70 def __init__(self):
71 super(PrepareCommand, self).__init__()
72 self.name = 'prepare'
73 self.description = "Prepares new content for your website."
74
75 def setupParser(self, parser, app):
76 subparsers = parser.add_subparsers()
77 for src in app.sources:
78 if not isinstance(src, IPreparingSource):
79 logger.debug("Skipping source '%s' because it's not preparable.")
80 continue
81 p = subparsers.add_parser(src.name)
82 src.setupPrepareParser(p, app)
83 p.set_defaults(source=src)
84
85 def run(self, ctx):
86 app = ctx.app
87 source = ctx.args.source
88 metadata = source.buildMetadata(ctx.args)
89 page_path = source.findPagePath(metadata, MODE_CREATING)
90 name, ext = os.path.splitext(page_path)
91 if ext == '.*':
92 page_path = '%s.%s' % (name,
93 app.config.get('site/default_auto_format'))
94 if os.path.exists(page_path):
95 raise Exception("'%s' already exists." % page_path)
96
97 logger.info("Creating page: %s" % os.path.relpath(page_path, app.root_dir))
98 if not os.path.exists(os.path.dirname(page_path)):
99 os.makedirs(os.path.dirname(page_path), 0755)
100 with open(page_path, 'w') as f:
101 f.write('---\n')
102 f.write('title: %s\n' % 'Unknown title')
103 f.write('---\n')
104 f.write("This is a new page!\n")
105