Created
June 29, 2011 17:56
-
-
Save robcowie/1054427 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import argparse | |
from os import path | |
import re | |
replacements = {} | |
pattern = '<<([^>]*)>>' | |
def user_replace(match): | |
## Pull from replacements dict or prompt | |
placeholder = match.group(1) | |
if placeholder in replacements: | |
return replacements[placeholder] | |
## .setdefault(key, value) returns the value if present, else sets it then returns | |
return replacements.setdefault(placeholder, raw_input('%s? ' % placeholder)) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('infile', type=argparse.FileType('r')) | |
parser.add_argument('outfile', type=argparse.FileType('w')) | |
args = parser.parse_args() | |
matcher = re.compile(pattern) | |
for line in args.infile: | |
new_line = matcher.sub(user_replace, line) | |
args.outfile.write(new_line) | |
args.infile.close() | |
args.outfile.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment