Created
December 27, 2014 02:25
-
-
Save bluebear94/8fde3cf9e99a15ca209b to your computer and use it in GitHub Desktop.
A tool to autogenerate text files.
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 argparse, re, io | |
template_pattern = re.compile("<%%(.*?)%%>") | |
def autogen(template, config): | |
exec(config) | |
loc = locals() | |
def replace(match): | |
nonlocal loc | |
expression = match.group(1) | |
return eval(expression, None, loc) | |
return re.sub(template_pattern, replace, template) | |
parser = argparse.ArgumentParser(description='Autogeneration based on templates') | |
parser.add_argument('--input', metavar='I', type=str, nargs=1, | |
help='the template file to input') | |
parser.add_argument('--output', metavar='O', type=str, nargs=1, | |
help='the file to output') | |
parser.add_argument('--config', metavar='C', type=str, nargs=1, | |
help='any statements to be executed') | |
args = parser.parse_args() | |
input_file = open(args.input[0], 'r', newline='\n') | |
output_file = open(args.output[0], 'w', newline='\n') | |
output_file.writelines(map(lambda s: s + '\n', autogen(''.join(input_file.readlines()), args.config[0]).split('\n'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment