OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # | |
3 # Copyright 2013 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 optparse | |
8 import os | |
9 import re | |
10 import sys | |
11 import xml.etree.cElementTree as ET | |
12 | |
13 from util import build_utils | |
14 | |
15 | |
16 ATTRIBUTE_PREFIX = '{http://schemas.android.com/apk/res/android}' | |
17 | |
18 ATTRIBUTES_TO_MIRROR = ['paddingLeft', | |
newt (away)
2013/04/25 00:41:24
completely optional / just an idea.
you could mak
Kibeom Kim (inactive)
2013/04/25 01:49:35
Done.
| |
19 'drawableLeft', | |
20 'layout_alignLeft', | |
21 'layout_marginLeft', | |
22 'layout_alignParentLeft', | |
23 'layout_toLeftOf', | |
24 'paddingRight', | |
25 'drawableRight', | |
26 'layout_alignRight', | |
27 'layout_marginRight', | |
28 'layout_alignParentRight', | |
29 'layout_toRightOf', | |
30 'gravity', | |
31 'layout_gravity'] | |
32 | |
33 ATTRIBUTES_TO_MIRROR = set([ATTRIBUTE_PREFIX + i for i in ATTRIBUTES_TO_MIRROR]) | |
34 | |
35 | |
36 def mirror_resource(input_filename, output_filename): | |
37 """Convert LTR resource to RTL android resource. (or the other way around) | |
38 | |
39 For example, | |
40 from paddingLeft="10dp" to paddingRight="10dp" | |
41 from gravity="right|center_vertical" to gravity="left|center_vertical" | |
42 """ | |
43 | |
44 tree = ET.parse(input_filename) | |
45 for element in tree.iter(): | |
46 attribute_names = ATTRIBUTES_TO_MIRROR.intersection(element.keys()) | |
47 mirrored_attributes = {} | |
48 for attribute_name in attribute_names: | |
49 if 'Left' in attribute_name: | |
50 mirrored_attribute_name = attribute_name.replace('Left', 'Right') | |
51 mirrored_attribute_value = element.attrib[attribute_name] | |
52 elif 'Right' in attribute_name: | |
53 mirrored_attribute_name = attribute_name.replace('Right', 'Left') | |
54 mirrored_attribute_value = element.attrib[attribute_name] | |
55 else: | |
56 # This is either gravity or layout_gravity | |
57 mirrored_attribute_name = attribute_name | |
58 mirrored_attribute_value = re.sub('left|right', | |
59 lambda x: 'right' if x.group(0) == 'left' else 'left', | |
60 element.attrib[directional_attribute]) | |
61 | |
62 mirrored_attributes[mirrored_attribute_name] = mirrored_attribute_value | |
63 # Delete the attribute that we are going to mirror. | |
64 del element.attrib[attribute_name] | |
65 | |
66 # Update the parsed xml with the mirrored attributes. | |
67 element.attrib.update(mirrored_attributes) | |
68 | |
69 build_utils.MakeDirectory(os.path.dirname(output_filename)) | |
70 tree.write(output_filename, encoding='utf-8', xml_declaration=True) | |
71 | |
72 | |
73 def mirror_resources_in_dir(input_dir, output_dir): | |
newt (away)
2013/04/25 00:41:24
doc string here. mention that it's recursive.
Kibeom Kim (inactive)
2013/04/25 01:49:35
Done.
| |
74 for root, dirs, files in os.walk(input_dir, followlinks=True): | |
75 relative_dir = os.path.relpath(root, input_dir) | |
76 for file_name in files: | |
77 if file_name.lower().endswith('.xml'): | |
78 input_filename = os.path.join(input_dir, relative_dir, file_name) | |
79 output_filename = os.path.join(output_dir, relative_dir, file_name) | |
80 mirror_resource(input_filename, output_filename) | |
81 | |
82 | |
83 def ParseArgs(): | |
84 """Parses command line options. | |
85 | |
86 Returns: | |
87 An options object as from optparse.OptionsParser.parse_args() | |
88 """ | |
89 parser = optparse.OptionParser() | |
90 parser.add_option('--res-dir', | |
91 help='directory containing resources to be mirrored to RTL') | |
92 parser.add_option('--res-mirrored-dir', | |
93 help='directory containing RTL resources to be generated') | |
94 | |
95 (options, args) = parser.parse_args() | |
96 | |
97 if args: | |
98 parser.error('No positional arguments should be given.') | |
99 | |
100 # Check that required options have been provided. | |
101 required_options = ('res_dir', 'res_mirrored_dir') | |
102 build_utils.CheckOptions(options, parser, required=required_options) | |
103 return options | |
104 | |
105 | |
106 def main(argv): | |
107 """Convert Android LTR xml resources to RTL and save it to -ldrtl directories. | |
108 | |
109 Please refer to the function mirror_resource's comment on how the files | |
110 are mirrored. | |
111 | |
112 ldrtl directory is explained at | |
113 http://developer.android.com/guide/topics/resources/providing-resources.html | |
114 """ | |
115 options = ParseArgs() | |
116 | |
117 res_sub_dirs = os.walk(options.res_dir).next()[1] | |
118 | |
119 for res_sub_dir in res_sub_dirs: | |
120 # If the directory has already RTL resources, we don't need to mirror. Skip. | |
121 if '-ldrtl' in res_sub_dir: | |
newt (away)
2013/04/25 00:41:24
I'd get the resource type and list of qualifiers h
Kibeom Kim (inactive)
2013/04/25 01:49:35
I like that much better. Thanks. Done.
On 2013/04
| |
122 continue | |
123 | |
124 # We only mirror resources under layout*/ or xml*/. | |
125 if (res_sub_dir.startswith('layout') or res_sub_dir.startswith('xml')): | |
126 # Insert ldrtl to the dir name. | |
127 # For example, from layout-hdpi to layout-ldrtl-hdpi. | |
128 res_sub_dir_split = res_sub_dir.split('-') | |
129 res_sub_dir_split.insert(1, 'ldrtl') | |
newt (away)
2013/04/25 00:41:24
I'd add a comment here about why it's OK to insert
Kibeom Kim (inactive)
2013/04/25 01:49:35
Done.
| |
130 res_mirrored_sub_dir = '-'.join(res_sub_dir_split) | |
131 # Mirror all the resource files. | |
132 mirror_resources_in_dir( | |
133 os.path.join(options.res_dir, res_sub_dir), | |
134 os.path.join(options.res_mirrored_dir, res_mirrored_sub_dir)) | |
135 | |
136 | |
137 if __name__ == '__main__': | |
138 sys.exit(main(sys.argv)) | |
139 | |
OLD | NEW |