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.
 
 
 

151 lines
5.9 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] is not empty" % sitepath)
  50. layout = Layout.find_layout(args.layout)
  51. logger.info(
  52. "Creating site at [%s] with layout [%s]" % (sitepath, layout))
  53. if not layout or not layout.exists:
  54. raise HydeException(
  55. "The given layout is invalid. Please check if you have the"
  56. " `layout` in the right place and the environment variable(%s)"
  57. " has been setup properly if you are using custom path for"
  58. " layouts" % HYDE_DATA)
  59. layout.copy_contents_to(args.sitepath)
  60. logger.info("Site creation complete")
  61. @subcommand('gen', help='Generate the site')
  62. @store('-c', '--config-path', default='site.yaml', dest='config',
  63. help='The configuration used to generate the site')
  64. @store('-d', '--deploy-path', dest='deploy', default=None,
  65. help='Where should the site be generated?')
  66. @true('-r', '--regen', dest='regen', default=False,
  67. help='Only process changed files')
  68. def gen(self, args):
  69. """
  70. The generate command. Generates the site at the given
  71. deployment directory.
  72. """
  73. self.main(args)
  74. site = self.make_site(args.sitepath, args.config, args.deploy)
  75. from hyde.generator import Generator
  76. gen = Generator(site)
  77. incremental = True
  78. if args.regen:
  79. logger.info("Regenerating the site...")
  80. incremental = False
  81. gen.generate_all(incremental=incremental)
  82. @subcommand('serve', help='Serve the website')
  83. @store('-a', '--address', default='localhost', dest='address',
  84. help='The address where the website must be served from.')
  85. @store('-p', '--port', type=int, default=8080, dest='port',
  86. help='The port where the website must be served from.')
  87. @store('-c', '--config-path', default='site.yaml', dest='config',
  88. help='The configuration used to generate the site')
  89. @store('-d', '--deploy-path', dest='deploy', default=None,
  90. help='Where should the site be generated?')
  91. def serve(self, args):
  92. """
  93. The serve command. Serves the site at the given
  94. deployment directory, address and port. Regenerates
  95. the entire site or specific files based on ths request.
  96. """
  97. self.main(args)
  98. sitepath = Folder(Folder(args.sitepath).fully_expanded_path)
  99. config_file = sitepath.child(args.config)
  100. site = self.make_site(args.sitepath, args.config, args.deploy)
  101. from hyde.server import HydeWebServer
  102. server = HydeWebServer(site, args.address, args.port)
  103. logger.info("Starting webserver at [%s]:[%d]", args.address, args.port)
  104. try:
  105. server.serve_forever()
  106. except KeyboardInterrupt, SystemExit:
  107. logger.info("Received shutdown request. Shutting down...")
  108. server.shutdown()
  109. logger.info("Server successfully stopped")
  110. exit()
  111. @subcommand('publish', help='Publish the website')
  112. @store('-c', '--config-path', default='site.yaml', dest='config',
  113. help='The configuration used to generate the site')
  114. @store('-p', '--publisher', dest='publisher', required=True,
  115. help='Points to the publisher configuration.')
  116. @store('-m', '--message', dest='message',
  117. help='Optional message.')
  118. def publish(self, args):
  119. """
  120. Publishes the site based on the configuration from the `target`
  121. parameter.
  122. """
  123. self.main(args)
  124. site = self.make_site(args.sitepath, args.config)
  125. from hyde.publisher import Publisher
  126. publisher = Publisher.load_publisher(site,
  127. args.publisher,
  128. args.message)
  129. publisher.publish()
  130. def make_site(self, sitepath, config, deploy=None):
  131. """
  132. Creates a site object from the given sitepath and the config file.
  133. """
  134. sitepath = Folder(Folder(sitepath).fully_expanded_path)
  135. config = Config(sitepath, config_file=config)
  136. if deploy:
  137. config.deploy_root = deploy
  138. return Site(sitepath, config)