Skip to content

Commit

Permalink
[Fix #2] Implement lifecycle hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
funkybob committed Mar 7, 2017
1 parent 1a4d0de commit 96e7605
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions graaf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def run(self):

self.context = Context({})

for generator in self.generators:
generator.start(self)

for root, dirs, files in os.walk(self.srcdir):
print "Scanning: %r (%d files)" % (root, len(files))
dest_root = os.path.join(self.destdir, root[len(self.srcdir):])
Expand All @@ -37,6 +40,9 @@ def run(self):
except OSError:
pass

for generator in self.generators:
generator.enter_dir(root, dirs, files)

self.context.push(**get_yaml(os.path.join(root, '_.yml')))

for filename in files:
Expand All @@ -51,6 +57,12 @@ def run(self):

self.context.pop()

for generator in self.generators:
generator.leave_dir(root, dirs, files)

for generator in self.generators:
generator.finish(self)

def render(self, template_name, extra_context):
'''
Helper function to render a template with the current context, and extra
Expand Down
19 changes: 19 additions & 0 deletions graaf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ def write_file(self, dest_dir, filename, content):
with io.open(os.path.join(dest_dir, filename), mode='w', encoding='utf-8') as fout:
fout.write(content)

def start(self, processor):
'''
Hook to alow Generators to react to start of processing.
'''
pass

def enter_dir(self, src_dir, dirs, files):
'''
Hook to allow Generators to react to start of processing a dir.
'''
pass

def leave_dir(self, src_dir, dirs, files):
'''
Hook to allow Generators to react to end of processing a dir.
'''
pass



def get_yaml(src):
'''
Expand Down

0 comments on commit 96e7605

Please sign in to comment.