Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!python
- # coding: utf-8
- """Generate content according to INI and template."""
- import sys
- import os
- import codecs
- from argparse import ArgumentParser
- from ConfigParser import ConfigParser
- from collections import OrderedDict
- from mako.template import Template
- class Ini2Tmpl(object):
- """Generate context according to given INI file and template file."""
- def __init__(self, ini=None, tmpl=None):
- """Parser test."""
- self.ini = ini
- self.tmpl = tmpl
- def make(self, out=sys.stdout):
- """Make content from INI and template."""
- ctx = ConfigParser(dict_type=OrderedDict)
- ctx.readfp(codecs.open(self.ini, 'r', 'utf8'))
- env = os.environ
- env['ini'] = self.ini
- env['tmpl'] = self.tmpl
- env['out'] = out
- if isinstance(out, (str, unicode)):
- with open(out, 'w') as fd:
- fd.write(Template(filename=self.tmpl).render_unicode(ctx=ctx, env=env))
- else:
- out.write(Template(filename=self.tmpl).render_unicode(ctx=ctx, env=env))
- def main():
- """Main entry."""
- parser = ArgumentParser(description='Generate content according to INI and template.')
- parser.add_argument('-i', '--ini', dest='ini', help='INI file.')
- parser.add_argument('-t', '--tmpl', dest='tmpl', help='Template file.')
- parser.add_argument('-o', '--out', dest='out', help='Output file.')
- args = parser.parse_args()
- Ini2Tmpl(ini=args.ini, tmpl=args.tmpl).make(out=args.out)
- if __name__ == '__main__':
- sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement