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.
 
 
 

110 lines
4.4 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 os
  14. import yaml
  15. HYDE_LAYOUTS = "HYDE_LAYOUTS"
  16. logger = getLoggerWithConsoleHandler('hyde')
  17. class Engine(Application):
  18. """
  19. The Hyde Application
  20. """
  21. @command(description='hyde - a python static website generator',
  22. epilog='Use %(prog)s {command} -h to get help on individual commands')
  23. @version('-v', '--version', version='%(prog)s ' + __version__)
  24. @store('-s', '--sitepath', default='.', help="Location of the hyde site")
  25. def main(self, args):
  26. """
  27. Will not be executed. A sub command is required. This function exists
  28. to provide common parameters for the subcommands and some generic stuff
  29. like version and metadata
  30. """
  31. pass
  32. @subcommand('create', help='Create a new hyde site')
  33. @store('-l', '--layout', default='basic', help='Layout for the new site')
  34. @true('-f', '--force', default=False, dest='overwrite',
  35. help='Overwrite the current site if it exists')
  36. def create(self, args):
  37. """
  38. The create command. Creates a new site from the template at the given
  39. sitepath.
  40. """
  41. sitepath = Folder(Folder(args.sitepath).fully_expanded_path)
  42. if sitepath.exists and not args.overwrite:
  43. raise HydeException(
  44. "The given site path[%s] is not empty" % sitepath)
  45. layout = Layout.find_layout(args.layout)
  46. logger.info(
  47. "Creating site at [%s] with layout [%s]" % (sitepath, layout))
  48. if not layout or not layout.exists:
  49. raise HydeException(
  50. "The given layout is invalid. Please check if you have the"
  51. " `layout` in the right place and the environment variable(%s)"
  52. " has been setup properly if you are using custom path for"
  53. " layouts" % HYDE_DATA)
  54. layout.copy_contents_to(args.sitepath)
  55. logger.info("Site creation complete")
  56. @subcommand('gen', help='Generate the site')
  57. @store('-c', '--config-path', default='site.yaml', dest='config',
  58. help='The configuration used to generate the site')
  59. @store('-d', '--deploy-path', default='deploy',
  60. help='Where should the site be generated?')
  61. def gen(self, args):
  62. """
  63. The generate command. Generates the site at the given
  64. deployment directory.
  65. """
  66. site = self.make_site(args.sitepath, args.config)
  67. from hyde.generator import Generator
  68. gen = Generator(site)
  69. gen.generate_all()
  70. @subcommand('serve', help='Serve the website')
  71. @store('-a', '--address', default='localhost', dest='address',
  72. help='The address where the website must be served from.')
  73. @store('-p', '--port', type=int, default=8080, dest='port',
  74. help='The port where the website must be served from.')
  75. @store('-c', '--config-path', default='site.yaml', dest='config',
  76. help='The configuration used to generate the site')
  77. @store('-d', '--deploy-path', default='deploy',
  78. help='Where should the site be generated?')
  79. def serve(self, args):
  80. """
  81. The serve command. Serves the site at the given
  82. deployment directory, address and port. Regenerates
  83. the entire site or specific files based on ths request.
  84. """
  85. sitepath = Folder(Folder(args.sitepath).fully_expanded_path)
  86. config_file = sitepath.child(args.config)
  87. site = self.make_site(args.sitepath, args.config)
  88. from hyde.server import HydeWebServer
  89. server = HydeWebServer(site, args.address, args.port)
  90. server.serve_forever()
  91. def make_site(self, sitepath, config):
  92. """
  93. Creates a site object from the given sitepath and the config file.
  94. """
  95. sitepath = Folder(Folder(sitepath).fully_expanded_path)
  96. config_file = sitepath.child(config)
  97. logger.info("Reading site configuration from [%s]", config_file)
  98. conf = {}
  99. with open(config_file) as stream:
  100. conf = yaml.load(stream)
  101. return Site(sitepath, Config(sitepath, conf))