/
engine.py
153 lines (140 loc) · 6.03 KB
/
engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
"""
Implements the hyde entry point commands
"""
from commando import *
from hyde.exceptions import HydeException
from hyde.fs import File, Folder
from hyde.layout import Layout, HYDE_DATA
from hyde.model import Config
from hyde.site import Site
from hyde.version import __version__
from hyde.util import getLoggerWithConsoleHandler
import codecs
import os
import yaml
HYDE_LAYOUTS = "HYDE_LAYOUTS"
logger = getLoggerWithConsoleHandler('hyde')
class Engine(Application):
"""
The Hyde Application
"""
@command(description='hyde - a python static website generator',
epilog='Use %(prog)s {command} -h to get help on individual commands')
@true('-v', '--verbose', help="Show detailed information in console")
@version('--version', version='%(prog)s ' + __version__)
@store('-s', '--sitepath', default='.', help="Location of the hyde site")
def main(self, args):
"""
Will not be executed. A sub command is required. This function exists
to provide common parameters for the subcommands and some generic stuff
like version and metadata
"""
if args.verbose:
import logging
logger.setLevel(logging.DEBUG)
@subcommand('create', help='Create a new hyde site')
@store('-l', '--layout', default='basic', help='Layout for the new site')
@true('-f', '--force', default=False, dest='overwrite',
help='Overwrite the current site if it exists')
def create(self, args):
"""
The create command. Creates a new site from the template at the given
sitepath.
"""
self.main(args)
sitepath = Folder(Folder(args.sitepath).fully_expanded_path)
if sitepath.exists and not args.overwrite:
raise HydeException(
"The given site path [%s] already exists."
" Use -f to overwrite." % sitepath)
layout = Layout.find_layout(args.layout)
logger.info(
"Creating site at [%s] with layout [%s]" % (sitepath, layout))
if not layout or not layout.exists:
raise HydeException(
"The given layout is invalid. Please check if you have the"
" `layout` in the right place and the environment variable(%s)"
" has been setup properly if you are using custom path for"
" layouts" % HYDE_DATA)
layout.copy_contents_to(args.sitepath)
logger.info("Site creation complete")
@subcommand('gen', help='Generate the site')
@store('-c', '--config-path', default='site.yaml', dest='config',
help='The configuration used to generate the site')
@store('-d', '--deploy-path', dest='deploy', default=None,
help='Where should the site be generated?')
@true('-r', '--regen', dest='regen', default=False,
help='Only process changed files')
def gen(self, args):
"""
The generate command. Generates the site at the given
deployment directory.
"""
self.main(args)
site = self.make_site(args.sitepath, args.config, args.deploy)
from hyde.generator import Generator
gen = Generator(site)
incremental = True
if args.regen:
logger.info("Regenerating the site...")
incremental = False
gen.generate_all(incremental=incremental)
logger.info("Generation complete.")
@subcommand('serve', help='Serve the website')
@store('-a', '--address', default='localhost', dest='address',
help='The address where the website must be served from.')
@store('-p', '--port', type=int, default=8080, dest='port',
help='The port where the website must be served from.')
@store('-c', '--config-path', default='site.yaml', dest='config',
help='The configuration used to generate the site')
@store('-d', '--deploy-path', dest='deploy', default=None,
help='Where should the site be generated?')
def serve(self, args):
"""
The serve command. Serves the site at the given
deployment directory, address and port. Regenerates
the entire site or specific files based on ths request.
"""
self.main(args)
sitepath = Folder(Folder(args.sitepath).fully_expanded_path)
config_file = sitepath.child(args.config)
site = self.make_site(args.sitepath, args.config, args.deploy)
from hyde.server import HydeWebServer
server = HydeWebServer(site, args.address, args.port)
logger.info("Starting webserver at [%s]:[%d]", args.address, args.port)
try:
server.serve_forever()
except KeyboardInterrupt, SystemExit:
logger.info("Received shutdown request. Shutting down...")
server.shutdown()
logger.info("Server successfully stopped")
exit()
@subcommand('publish', help='Publish the website')
@store('-c', '--config-path', default='site.yaml', dest='config',
help='The configuration used to generate the site')
@store('-p', '--publisher', dest='publisher', required=True,
help='Points to the publisher configuration.')
@store('-m', '--message', dest='message',
help='Optional message.')
def publish(self, args):
"""
Publishes the site based on the configuration from the `target`
parameter.
"""
self.main(args)
site = self.make_site(args.sitepath, args.config)
from hyde.publisher import Publisher
publisher = Publisher.load_publisher(site,
args.publisher,
args.message)
publisher.publish()
def make_site(self, sitepath, config, deploy=None):
"""
Creates a site object from the given sitepath and the config file.
"""
sitepath = Folder(Folder(sitepath).fully_expanded_path)
config = Config(sitepath, config_file=config)
if deploy:
config.deploy_root = deploy
return Site(sitepath, config)