Browse Source

Reset the plugin lifecycle to always generate everything during the first run

main
Lakshmi Vyasarajan 14 years ago
parent
commit
5d5c72fddf
2 changed files with 88 additions and 16 deletions
  1. +13
    -4
      hyde/generator.py
  2. +75
    -12
      hyde/tests/test_plugin.py

+ 13
- 4
hyde/generator.py View File

@@ -23,6 +23,7 @@ class Generator(object):
def __init__(self, site): def __init__(self, site):
super(Generator, self).__init__() super(Generator, self).__init__()
self.site = site self.site = site
self.generated_once = False
self.__context__ = dict(site=site) self.__context__ = dict(site=site)
self.template = None self.template = None
Plugin.load_all(site) Plugin.load_all(site)
@@ -48,8 +49,10 @@ class Generator(object):
res = function(*args) res = function(*args)
if res: if res:
targs = list(args) targs = list(args)
last = targs.pop()
targs.append(res if res else last)
last = None
if len(targs):
last = targs.pop()
targs.append(res if res else last)
args = tuple(targs) args = tuple(targs)
return res return res


@@ -123,12 +126,15 @@ class Generator(object):
self.__generate_node__(self.site.content) self.__generate_node__(self.site.content)
self.events.site_complete() self.events.site_complete()
self.finalize() self.finalize()
self.generated_once = True


def generate_node_at_path(self, node_path=None): def generate_node_at_path(self, node_path=None):
""" """
Generates a single node. If node_path is non-existent or empty, Generates a single node. If node_path is non-existent or empty,
generates the entire site. generates the entire site.
""" """
if not self.generated_once:
return self.generate_all()
self.load_template_if_needed() self.load_template_if_needed()
self.load_site_if_needed() self.load_site_if_needed()
node = None node = None
@@ -141,7 +147,7 @@ class Generator(object):
Generates the given node. If node is invalid, empty or Generates the given node. If node is invalid, empty or
non-existent, generates the entire website. non-existent, generates the entire website.
""" """
if not node:
if not node or not self.generated_once:
return self.generate_all() return self.generate_all()


self.load_template_if_needed() self.load_template_if_needed()
@@ -159,6 +165,9 @@ class Generator(object):
Generates a single resource. If resource_path is non-existent or empty, Generates a single resource. If resource_path is non-existent or empty,
generats the entire website. generats the entire website.
""" """
if not self.generated_once:
return self.generate_all()

self.load_template_if_needed() self.load_template_if_needed()
self.load_site_if_needed() self.load_site_if_needed()
resource = None resource = None
@@ -171,7 +180,7 @@ class Generator(object):
Generates the given resource. If resource is invalid, empty or Generates the given resource. If resource is invalid, empty or
non-existent, generates the entire website. non-existent, generates the entire website.
""" """
if not resource:
if not resource or not self.generated_once:
return self.generate_all() return self.generate_all()


self.load_template_if_needed() self.load_template_if_needed()


+ 75
- 12
hyde/tests/test_plugin.py View File

@@ -123,20 +123,39 @@ class TestPlugins(object):
gen.generate_all() gen.generate_all()
assert begin_site_stub.call_count == 1 assert begin_site_stub.call_count == 1


def test_generator_template_begin_site_not_called_for_single_resource(self):
def test_generator_template_begin_site_called_for_single_resource(self):
with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub: with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub:
gen = Generator(self.site) gen = Generator(self.site)
path = self.site.content.source_folder.child('about.html') path = self.site.content.source_folder.child('about.html')
gen.generate_resource_at_path(path) gen.generate_resource_at_path(path)
assert begin_site_stub.call_count == 0
assert begin_site_stub.call_count == 1

def test_generator_template_begin_site_not_called_for_single_resource_second_time(self):
with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub:
gen = Generator(self.site)
gen.generate_all()
assert begin_site_stub.call_count == 1
path = self.site.content.source_folder.child('about.html')
gen.generate_resource_at_path(path)
assert begin_site_stub.call_count == 1

def test_generator_template_begin_site_called_for_single_node(self):
with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub:
gen = Generator(self.site)
path = self.site.content.source_folder
gen.generate_node_at_path(path)

assert begin_site_stub.call_count == 1


def test_generator_template_begin_site_not_called_for_single_node(self):
def test_generator_template_begin_site_not_called_for_single_node_second_time(self):
with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub: with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub:
gen = Generator(self.site) gen = Generator(self.site)
gen.generate_all()
assert begin_site_stub.call_count == 1
path = self.site.content.source_folder path = self.site.content.source_folder
gen.generate_node_at_path(path) gen.generate_node_at_path(path)


assert begin_site_stub.call_count == 0
assert begin_site_stub.call_count == 1


def test_generator_template_site_complete_called(self): def test_generator_template_site_complete_called(self):
with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub: with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub:
@@ -145,23 +164,44 @@ class TestPlugins(object):
assert site_complete_stub.call_count == 1 assert site_complete_stub.call_count == 1




def test_generator_template_site_complete_not_called_for_single_resource(self):
def test_generator_template_site_complete_called_for_single_resource(self):


with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub: with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub:
gen = Generator(self.site) gen = Generator(self.site)
path = self.site.content.source_folder.child('about.html') path = self.site.content.source_folder.child('about.html')
gen.generate_resource_at_path(path) gen.generate_resource_at_path(path)


assert site_complete_stub.call_count == 0
assert site_complete_stub.call_count == 1

def test_generator_template_site_complete_not_called_for_single_resource_second_time(self):

with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub:
gen = Generator(self.site)
gen.generate_all()
assert site_complete_stub.call_count == 1
path = self.site.content.source_folder.child('about.html')
gen.generate_resource_at_path(path)

assert site_complete_stub.call_count == 1


def test_generator_template_site_complete_not_called_for_single_node(self):
def test_generator_template_site_complete_called_for_single_node(self):


with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub: with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub:
gen = Generator(self.site) gen = Generator(self.site)
path = self.site.content.source_folder path = self.site.content.source_folder
gen.generate_node_at_path(path) gen.generate_node_at_path(path)


assert site_complete_stub.call_count == 0
assert site_complete_stub.call_count == 1

def test_generator_template_site_complete_not_called_for_single_node_second_time(self):

with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub:
gen = Generator(self.site)
gen.generate_all()
path = self.site.content.source_folder
gen.generate_node_at_path(path)

assert site_complete_stub.call_count == 1


def test_generator_template_begin_node_called(self): def test_generator_template_begin_node_called(self):


@@ -173,12 +213,22 @@ class TestPlugins(object):
called_with_nodes = sorted([arg[0][0].path for arg in begin_node_stub.call_args_list]) called_with_nodes = sorted([arg[0][0].path for arg in begin_node_stub.call_args_list])
assert called_with_nodes == self.content_nodes assert called_with_nodes == self.content_nodes


def test_generator_template_begin_node_not_called_for_single_resource(self):
def test_generator_template_begin_node_called_for_single_resource(self):


with patch.object(PluginLoaderStub, 'begin_node') as begin_node_stub: with patch.object(PluginLoaderStub, 'begin_node') as begin_node_stub:
gen = Generator(self.site) gen = Generator(self.site)
gen.generate_resource_at_path(self.site.content.source_folder.child('about.html')) gen.generate_resource_at_path(self.site.content.source_folder.child('about.html'))
assert begin_node_stub.call_count == 0
assert begin_node_stub.call_count == len(self.content_nodes)


def test_generator_template_begin_node_not_called_for_single_resource_second_time(self):

with patch.object(PluginLoaderStub, 'begin_node') as begin_node_stub:
gen = Generator(self.site)
gen.generate_all()
assert begin_node_stub.call_count == len(self.content_nodes)
gen.generate_resource_at_path(self.site.content.source_folder.child('about.html'))
assert begin_node_stub.call_count == len(self.content_nodes) # No extra calls




def test_generator_template_node_complete_called(self): def test_generator_template_node_complete_called(self):
@@ -191,12 +241,21 @@ class TestPlugins(object):
called_with_nodes = sorted([arg[0][0].path for arg in node_complete_stub.call_args_list]) called_with_nodes = sorted([arg[0][0].path for arg in node_complete_stub.call_args_list])
assert called_with_nodes == self.content_nodes assert called_with_nodes == self.content_nodes


def test_generator_template_node_complete_not_called_for_single_resource(self):
def test_generator_template_node_complete_called_for_single_resource(self):

with patch.object(PluginLoaderStub, 'node_complete') as node_complete_stub:
gen = Generator(self.site)
gen.generate_resource_at_path(self.site.content.source_folder.child('about.html'))
assert node_complete_stub.call_count == len(self.content_nodes)

def test_generator_template_node_complete_not_called_for_single_resource_second_time(self):


with patch.object(PluginLoaderStub, 'node_complete') as node_complete_stub: with patch.object(PluginLoaderStub, 'node_complete') as node_complete_stub:
gen = Generator(self.site) gen = Generator(self.site)
gen.generate_all()
assert node_complete_stub.call_count == len(self.content_nodes)
gen.generate_resource_at_path(self.site.content.source_folder.child('about.html')) gen.generate_resource_at_path(self.site.content.source_folder.child('about.html'))
assert node_complete_stub.call_count == 0
assert node_complete_stub.call_count == len(self.content_nodes) # No extra calls


def test_generator_template_begin_text_resource_called(self): def test_generator_template_begin_text_resource_called(self):


@@ -214,6 +273,8 @@ class TestPlugins(object):
with patch.object(PluginLoaderStub, 'begin_text_resource') as begin_text_resource_stub: with patch.object(PluginLoaderStub, 'begin_text_resource') as begin_text_resource_stub:
begin_text_resource_stub.return_value = '' begin_text_resource_stub.return_value = ''
gen = Generator(self.site) gen = Generator(self.site)
gen.generate_all()
begin_text_resource_stub.reset_mock()
path = self.site.content.source_folder.child('about.html') path = self.site.content.source_folder.child('about.html')
gen.generate_resource_at_path(path) gen.generate_resource_at_path(path)


@@ -235,6 +296,8 @@ class TestPlugins(object):


with patch.object(PluginLoaderStub, 'begin_binary_resource') as begin_binary_resource_stub: with patch.object(PluginLoaderStub, 'begin_binary_resource') as begin_binary_resource_stub:
gen = Generator(self.site) gen = Generator(self.site)
gen.generate_all()
begin_binary_resource_stub.reset_mock()
path = self.site.content.source_folder.child('favicon.ico') path = self.site.content.source_folder.child('favicon.ico')
gen.generate_resource_at_path(path) gen.generate_resource_at_path(path)




Loading…
Cancel
Save