Created
November 17, 2016 02:39
-
-
Save blakev/8ee7beea178f75b95d6ff5171027d871 to your computer and use it in GitHub Desktop.
cookiecutter helper function that will replace |FILENAME,filter,filter,..| patterns with the contents of FILENAME, each line being applied with filters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
RE_CONTENT = re.compile(r'(\|[\w\.\,]+\|)', re.I) | |
fns = { | |
'blockquote': lambda s: '> ' + s, | |
'code': lambda s: ' ' + s, | |
'comment': lambda s: '# {}'.format(s).rstrip() + '\n', | |
'list': lambda s: ' - ' + s | |
} | |
def inject_file_contents(from_folder, edit_fns=None, verbose=False): | |
""" Inject's a file's contents into other files inside ``from_folder``. | |
Args: | |
from_folder (str) | |
edit_fns (dict[str, Function]) | |
Returns: | |
None | |
""" | |
ROOT = from_folder | |
if not edit_fns: | |
edit_fns = {} | |
assert os.path.exists(ROOT) | |
for key, fn in edit_fns.items(): | |
assert callable(fn), '%s is not callable' % key | |
# read all our input file's content, so we can do a find-replace | |
# on all the output files one at a time, and inject the content | |
read_contents = {} | |
mark_contents = set() | |
mark_edits = [] | |
for root, dirs, files in os.walk(ROOT, topdown=False): | |
for ins in files: | |
f = os.path.join(root, ins) | |
with open(f, 'r') as ins_file: | |
lines = ins_file.readlines() | |
for x in lines: | |
# if we have a content-replace marker in | |
# this file, then we need to save it for | |
# afterwards | |
match = RE_CONTENT.match(x) | |
if match: | |
mark_contents.add(match.group().strip('|').split(',')[0]) | |
mark_edits.append(f) | |
# only read the contents of the files we need | |
for root, dirs, files in os.walk(ROOT, topdown=False): | |
for outs in files: | |
if outs not in mark_contents: | |
continue | |
with open(os.path.join(root, outs), 'r') as ins_file: | |
read_contents[outs] = ins_file.readlines() | |
# do the find-replace on the files we marked for editing | |
for f in mark_edits: | |
if verbose: | |
print '..', f | |
with open(f, 'r+') as out_file: | |
contents = out_file.read() | |
for m in RE_CONTENT.search(contents).groups(): | |
rc_name = m.strip('|').split(',', 1) | |
text = read_contents.get(rc_name[0], None) | |
if text is None: | |
# continue instead of replacing because | |
# we might want to do a second pass... | |
continue | |
if len(rc_name) > 1: | |
# get the function names to apply | |
filters = rc_name[1].split(',') | |
for fn in filters: | |
if fn not in edit_fns: | |
print 'err: no filter named %s' % fn | |
continue | |
text = map(edit_fns[fn], text) | |
contents = contents.replace(m, ''.join(text).strip()) | |
# write it out and flush | |
out_file.seek(0) | |
out_file.write(contents) | |
out_file.truncate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USAGE EXAMPLE
tasks.py (invoke)
Turning the {{ cookiecutter.project_name }}/README.md
Into..