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.
 
 
 

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