Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #!python
  2. # coding: utf-8
  3. """Generate content according to INI and template."""
  4.  
  5. import sys
  6. import os
  7. import codecs
  8. from argparse import ArgumentParser
  9. from ConfigParser import ConfigParser
  10. from collections import OrderedDict
  11. from mako.template import Template
  12.  
  13.  
  14. class Ini2Tmpl(object):
  15. """Generate context according to given INI file and template file."""
  16. def __init__(self, ini=None, tmpl=None):
  17. """Parser test."""
  18. self.ini = ini
  19. self.tmpl = tmpl
  20.  
  21. def make(self, out=sys.stdout):
  22. """Make content from INI and template."""
  23. ctx = ConfigParser(dict_type=OrderedDict)
  24. ctx.readfp(codecs.open(self.ini, 'r', 'utf8'))
  25. env = os.environ
  26. env['ini'] = self.ini
  27. env['tmpl'] = self.tmpl
  28. env['out'] = out
  29. if isinstance(out, (str, unicode)):
  30. with open(out, 'w') as fd:
  31. fd.write(Template(filename=self.tmpl).render_unicode(ctx=ctx, env=env))
  32. else:
  33. out.write(Template(filename=self.tmpl).render_unicode(ctx=ctx, env=env))
  34.  
  35.  
  36. def main():
  37. """Main entry."""
  38. parser = ArgumentParser(description='Generate content according to INI and template.')
  39. parser.add_argument('-i', '--ini', dest='ini', help='INI file.')
  40. parser.add_argument('-t', '--tmpl', dest='tmpl', help='Template file.')
  41. parser.add_argument('-o', '--out', dest='out', help='Output file.')
  42. args = parser.parse_args()
  43.  
  44. Ini2Tmpl(ini=args.ini, tmpl=args.tmpl).make(out=args.out)
  45.  
  46. if __name__ == '__main__':
  47. sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement