Skip to content

Instantly share code, notes, and snippets.

@DavidLGoldberg
Created July 17, 2012 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidLGoldberg/3127455 to your computer and use it in GitHub Desktop.
Save DavidLGoldberg/3127455 to your computer and use it in GitHub Desktop.
Not sure this is useful. Turns directories that start with _m_ and contain mustache templates into a module. Meant to be used at build time.
import pystache
import os, glob
#TODO: use argparse or some config vars
ACTIONS_DIR_PREFIX = '_m_'
MAIN_TEMPLATE_NAME = 'main.mustache'
START_WALK = os.getcwd()
ENV = {}
for root, directories, files in os.walk(START_WALK):
for directory in directories:
if directory.startswith(ACTIONS_DIR_PREFIX):
actions = ''
mustache_module = open(
os.path.join(root, directory.split(ACTIONS_DIR_PREFIX)[1] + '.py'), 'w+')
main_file_name = os.path.join(directory, MAIN_TEMPLATE_NAME)
main_template = open(main_file_name, 'r').read()
compiled_actions = '';
for f in os.listdir(directory):
if f.endswith('.mustache') and f != MAIN_TEMPLATE_NAME:
action_template = open(os.path.join(directory, f), 'r').read()
compiled_actions += pystache.render(action_template, ENV) + '\n'
mustache_module.write(pystache.render(main_template, {'actions': compiled_actions}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment