Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 # Copyright 2014 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 import json | |
| 8 import io | |
| 9 import optparse | |
| 10 import os | |
| 11 import sys | |
| 12 | |
| 13 '''Generate an extension manifest based on a template.''' | |
| 14 | |
| 15 | |
| 16 def processJinjaTemplate(input_file, output_file, context): | |
| 17 jinja2_path = os.path.normpath( | |
| 18 os.path.join(os.path.abspath(__file__), | |
| 19 *[os.path.pardir] * 7 + ['third_party/jinja2'])) | |
| 20 sys.path.append(os.path.split(jinja2_path)[0]) | |
|
Peter Lundblad
2014/05/23 16:04:27
Why are you including a path component that you la
Peter Lundblad
2014/05/23 16:04:27
I recommend prepending so we pick up our own jinja
David Tseng
2014/05/23 19:16:37
Took this code from remoting; revised.
| |
| 21 import jinja2 | |
| 22 (template_path, template_name) = os.path.split(input_file) | |
| 23 env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path)) | |
| 24 template = env.get_template(template_name) | |
| 25 rendered = template.render(context) | |
| 26 io.open(output_file, 'w', encoding='utf-8').write(rendered).close() | |
|
Peter Lundblad
2014/05/23 16:04:27
nit: Why io.open?
Peter Lundblad
2014/05/23 16:04:27
Ho would you feel about quickly loading the result
David Tseng
2014/05/23 19:16:37
Ditto; no I suspect it's because open doesn't let
David Tseng
2014/05/23 19:16:37
Sure. sgtm.
| |
| 27 | |
| 28 | |
| 29 def main(): | |
| 30 parser = optparse.OptionParser(description=__doc__) | |
| 31 parser.usage = '%prog [options] <template_manifest_path>' | |
| 32 parser.add_option( | |
| 33 '-o', '--output_manifest', action='store', metavar='OUTPUT_MANIFEST', | |
| 34 help='File to place generated manifest') | |
| 35 parser.add_option( | |
| 36 '-g', '--is_guest_manifest', action='store', metavar='GUEST_MANIFEST', | |
| 37 help='Generate a guest mode capable manifest') | |
| 38 | |
| 39 options, args = parser.parse_args() | |
| 40 if len(args) != 1: | |
| 41 print >>sys.stderr, 'Expected exactly one argument' | |
| 42 sys.exit(1) | |
| 43 processJinjaTemplate(args[0], options.output_manifest, parser.values.__dict__) | |
|
Peter Lundblad
2014/05/23 16:04:27
What is parser.values.__dict__ for?
David Tseng
2014/05/23 19:16:37
Allows us to pass the jinja2 "renderer" all option
| |
| 44 | |
| 45 if __name__ == '__main__': | |
| 46 main() | |
| OLD | NEW |