[MD settings] moving attached() code
[chromium-blink-merge.git] / tools / polymer / txt_to_polymer_grdp.py
blob02e275a8cac59b959d4f4c2a378feaf14207c6d5
1 #!/usr/bin/env python
2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 from __future__ import with_statement
7 import os
8 import string
9 import sys
12 FILE_TEMPLATE = \
13 """<?xml version="1.0" encoding="utf-8"?>
14 <!--
15 This file is generated.
16 Please use 'src/tools/polymer/polymer_grdp_to_txt.py' and
17 'src/tools/polymer/txt_to_polymer_grdp.py' to modify it, if possible.
19 'polymer_grdp_to_txt.py' converts 'polymer_resources.grdp' to a plane list of
20 used Polymer components:
21 ...
22 iron-iron-iconset/iron-iconset-extracted.js
23 iron-iron-iconset/iron-iconset.html
24 ...
26 'txt_to_polymer_grdp.py' converts list back to GRDP file.
28 Usage:
29 $ polymer_grdp_to_txt.py polymer_resources.grdp > /tmp/list.txt
30 $ vim /tmp/list.txt
31 $ txt_to_polymer_grdp.py /tmp/list.txt > polymer_resources.grdp
32 -->
33 <grit-part>
34 <!-- Polymer 1.0 -->
35 %(v_1_0)s
36 <structure name="IDR_POLYMER_1_0_WEB_ANIMATIONS_JS_WEB_ANIMATIONS_NEXT_LITE_MIN_JS"
37 file="../../../third_party/web-animations-js/sources/web-animations-next-lite.min.js"
38 type="chrome_html" />
39 </grit-part>
40 """
43 DEFINITION_TEMPLATE_1_0 = \
44 """ <structure name="%s"
45 file="../../../third_party/polymer/v1_0/components-chromium/%s"
46 type="chrome_html" />"""
49 def PathToGritId(path):
50 table = string.maketrans(string.lowercase + '/.-', string.uppercase + '___')
51 return 'IDR_POLYMER_1_0_' + path.translate(table)
54 def SortKey(record):
55 return (record, PathToGritId(record))
58 def ParseRecord(record):
59 return record.strip()
62 class FileNotFoundException(Exception):
63 pass
66 _HERE = os.path.dirname(os.path.realpath(__file__))
67 _POLYMER_DIR = os.path.join(_HERE, os.pardir, os.pardir,
68 'third_party', 'polymer', 'v1_0', 'components-chromium')
71 def main(argv):
72 with open(argv[1]) as f:
73 records = [ParseRecord(r) for r in f if not r.isspace()]
74 lines = { 'v_1_0': [] }
75 for path in sorted(set(records), key=SortKey):
76 full_path = os.path.normpath(os.path.join(_POLYMER_DIR, path))
77 if not os.path.exists(full_path):
78 raise FileNotFoundException('%s not found' % full_path)
80 template = DEFINITION_TEMPLATE_1_0
81 lines['v_1_0'].append(
82 template % (PathToGritId(path), path))
83 print FILE_TEMPLATE % { 'v_1_0': '\n'.join(lines['v_1_0']) }
85 if __name__ == '__main__':
86 sys.exit(main(sys.argv))