A fork of hyde, the static site generation. Some patches will be pushed upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

180 lines
6.7 KiB

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