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.
 
 
 

159 lines
6.2 KiB

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