A fork of hyde, the static site generation. Some patches will be pushed upstream.
 
 
 

325 lines
9.8 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Parses & holds information about the site to be generated.
  4. """
  5. from hyde.exceptions import HydeException
  6. from hyde.fs import FS, File, Folder
  7. from hyde.model import Config
  8. import logging
  9. from logging import NullHandler
  10. logger = logging.getLogger('hyde.engine')
  11. logger.addHandler(NullHandler())
  12. class Processable(object):
  13. """
  14. A node or resource.
  15. """
  16. def __init__(self, source):
  17. super(Processable, self).__init__()
  18. self.source = FS.file_or_folder(source)
  19. self.is_processable = True
  20. @property
  21. def name(self):
  22. """
  23. The resource name
  24. """
  25. return self.source.name
  26. def __repr__(self):
  27. return self.path
  28. @property
  29. def path(self):
  30. """
  31. Gets the source path of this node.
  32. """
  33. return self.source.path
  34. class Resource(Processable):
  35. """
  36. Represents any file that is processed by hyde
  37. """
  38. def __init__(self, source_file, node):
  39. super(Resource, self).__init__(source_file)
  40. self.source_file = source_file
  41. if not node:
  42. raise HydeException("Resource cannot exist without a node")
  43. if not source_file:
  44. raise HydeException("Source file is required"
  45. " to instantiate a resource")
  46. self.node = node
  47. self.site = node.site
  48. self._relative_deploy_path = None
  49. @property
  50. def relative_path(self):
  51. """
  52. Gets the path relative to the root folder (Content, Media, Layout)
  53. """
  54. return self.source_file.get_relative_path(self.node.root.source_folder)
  55. def get_relative_deploy_path(self):
  56. """
  57. Gets the path where the file will be created
  58. after its been processed.
  59. """
  60. return self._relative_deploy_path \
  61. if self._relative_deploy_path \
  62. else self.relative_path
  63. def set_relative_deploy_path(self, path):
  64. """
  65. Sets the path where the file ought to be created
  66. after its been processed.
  67. """
  68. self._relative_deploy_path = path
  69. relative_deploy_path = property(get_relative_deploy_path, set_relative_deploy_path)
  70. class Node(Processable):
  71. """
  72. Represents any folder that is processed by hyde
  73. """
  74. def __init__(self, source_folder, parent=None):
  75. super(Node, self).__init__(source_folder)
  76. if not source_folder:
  77. raise HydeException("Source folder is required"
  78. " to instantiate a node.")
  79. self.root = self
  80. self.module = None
  81. self.site = None
  82. self.source_folder = Folder(str(source_folder))
  83. self.parent = parent
  84. if parent:
  85. self.root = self.parent.root
  86. self.module = self.parent.module if self.parent.module else self
  87. self.site = parent.site
  88. self.child_nodes = []
  89. self.resources = []
  90. def contains_resource(self, resource_name):
  91. """
  92. Returns True if the given resource name exists as a file
  93. in this node's source folder.
  94. """
  95. return File(self.source_folder.child(resource_name)).exists
  96. def get_resource(self, resource_name):
  97. """
  98. Gets the resource if the given resource name exists as a file
  99. in this node's source folder.
  100. """
  101. if self.contains_resource(resource_name):
  102. return self.root.resource_from_path(
  103. self.source_folder.child(resource_name))
  104. return None
  105. def add_child_node(self, folder):
  106. """
  107. Creates a new child node and adds it to the list of child nodes.
  108. """
  109. if folder.parent != self.source_folder:
  110. raise HydeException("The given folder [%s] is not a"
  111. " direct descendant of [%s]" %
  112. (folder, self.source_folder))
  113. node = Node(folder, self)
  114. self.child_nodes.append(node)
  115. return node
  116. def add_child_resource(self, afile):
  117. """
  118. Creates a new resource and adds it to the list of child resources.
  119. """
  120. if afile.parent != self.source_folder:
  121. raise HydeException("The given file [%s] is not"
  122. " a direct descendant of [%s]" %
  123. (afile, self.source_folder))
  124. resource = Resource(afile, self)
  125. self.resources.append(resource)
  126. return resource
  127. def walk(self):
  128. """
  129. Walks the node, first yielding itself then
  130. yielding the child nodes depth-first.
  131. """
  132. yield self
  133. for child in self.child_nodes:
  134. for node in child.walk():
  135. yield node
  136. def walk_resources(self):
  137. """
  138. Walks the resources in this hierarchy.
  139. """
  140. for node in self.walk():
  141. for resource in node.resources:
  142. yield resource
  143. @property
  144. def relative_path(self):
  145. """
  146. Gets the path relative to the root folder (Content, Media, Layout)
  147. """
  148. return self.source_folder.get_relative_path(self.root.source_folder)
  149. class RootNode(Node):
  150. """
  151. Represents one of the roots of site: Content, Media or Layout
  152. """
  153. def __init__(self, source_folder, site):
  154. super(RootNode, self).__init__(source_folder)
  155. self.site = site
  156. self.node_map = {}
  157. self.resource_map = {}
  158. def node_from_path(self, path):
  159. """
  160. Gets the node that maps to the given path.
  161. If no match is found it returns None.
  162. """
  163. if Folder(path) == self.source_folder:
  164. return self
  165. return self.node_map.get(str(Folder(path)), None)
  166. def node_from_relative_path(self, relative_path):
  167. """
  168. Gets the content node that maps to the given relative path.
  169. If no match is found it returns None.
  170. """
  171. return self.node_from_path(
  172. self.source_folder.child(str(relative_path)))
  173. def resource_from_path(self, path):
  174. """
  175. Gets the resource that maps to the given path.
  176. If no match is found it returns None.
  177. """
  178. return self.resource_map.get(str(File(path)), None)
  179. def resource_from_relative_path(self, relative_path):
  180. """
  181. Gets the content resource that maps to the given relative path.
  182. If no match is found it returns None.
  183. """
  184. return self.resource_from_path(
  185. self.source_folder.child(str(relative_path)))
  186. def add_node(self, a_folder):
  187. """
  188. Adds a new node to this folder's hierarchy.
  189. Also adds to to the hashtable of path to node associations
  190. for quick lookup.
  191. """
  192. folder = Folder(a_folder)
  193. node = self.node_from_path(folder)
  194. if node:
  195. logger.info("Node exists at [%s]" % node.relative_path)
  196. return node
  197. if not folder.is_descendant_of(self.source_folder):
  198. raise HydeException("The given folder [%s] does not"
  199. " belong to this hierarchy [%s]" %
  200. (folder, self.source_folder))
  201. p_folder = folder
  202. parent = None
  203. hierarchy = []
  204. while not parent:
  205. hierarchy.append(p_folder)
  206. p_folder = p_folder.parent
  207. parent = self.node_from_path(p_folder)
  208. hierarchy.reverse()
  209. node = parent if parent else self
  210. for h_folder in hierarchy:
  211. node = node.add_child_node(h_folder)
  212. self.node_map[str(h_folder)] = node
  213. logger.info("Added node [%s] to [%s]" % (
  214. node.relative_path, self.source_folder))
  215. return node
  216. def add_resource(self, a_file):
  217. """
  218. Adds a file to the parent node. Also adds to to the
  219. hashtable of path to resource associations for quick lookup.
  220. """
  221. afile = File(a_file)
  222. resource = self.resource_from_path(afile)
  223. if resource:
  224. logger.info("Resource exists at [%s]" % resource.relative_path)
  225. if not afile.is_descendant_of(self.source_folder):
  226. raise HydeException("The given file [%s] does not reside"
  227. " in this hierarchy [%s]" %
  228. (afile, self.source_folder))
  229. node = self.node_from_path(afile.parent)
  230. if not node:
  231. node = self.add_node(afile.parent)
  232. resource = node.add_child_resource(afile)
  233. self.resource_map[str(afile)] = resource
  234. logger.info("Added resource [%s] to [%s]" %
  235. (resource.relative_path, self.source_folder))
  236. return resource
  237. def load(self):
  238. """
  239. Walks the `source_folder` and loads the sitemap.
  240. Creates nodes and resources, reads metadata and injects attributes.
  241. This is the model for hyde.
  242. """
  243. if not self.source_folder.exists:
  244. raise HydeException("The given source folder[%s]"
  245. " does not exist" % self.source_folder)
  246. with self.source_folder.walker as walker:
  247. @walker.folder_visitor
  248. def visit_folder(folder):
  249. self.add_node(folder)
  250. @walker.file_visitor
  251. def visit_file(afile):
  252. self.add_resource(afile)
  253. class Site(object):
  254. """
  255. Represents the site to be generated.
  256. """
  257. def __init__(self, sitepath=None, config=None):
  258. super(Site, self).__init__()
  259. self.sitepath = Folder(str(sitepath))
  260. self.config = config if config else Config(self.sitepath)
  261. self.content = RootNode(self.config.content_root_path, self)
  262. self.plugins = []
  263. def load(self):
  264. """
  265. Walks the content and media folders to load up the sitemap.
  266. """
  267. self.content.load()