Chromium Code Reviews| Index: build/android/gyp/mirror_resources.py |
| diff --git a/build/android/gyp/mirror_resources.py b/build/android/gyp/mirror_resources.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..b074401e7534a5eb7d84744fe1aaf8c556a6d265 |
| --- /dev/null |
| +++ b/build/android/gyp/mirror_resources.py |
| @@ -0,0 +1,139 @@ |
| +#!/usr/bin/env python |
| +# |
| +# Copyright 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import optparse |
| +import os |
| +import re |
| +import sys |
| +import xml.etree.cElementTree as ET |
| + |
| +from util import build_utils |
| + |
| + |
| +ATTRIBUTE_PREFIX = '{http://schemas.android.com/apk/res/android}' |
| + |
| +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.
|
| + 'drawableLeft', |
| + 'layout_alignLeft', |
| + 'layout_marginLeft', |
| + 'layout_alignParentLeft', |
| + 'layout_toLeftOf', |
| + 'paddingRight', |
| + 'drawableRight', |
| + 'layout_alignRight', |
| + 'layout_marginRight', |
| + 'layout_alignParentRight', |
| + 'layout_toRightOf', |
| + 'gravity', |
| + 'layout_gravity'] |
| + |
| +ATTRIBUTES_TO_MIRROR = set([ATTRIBUTE_PREFIX + i for i in ATTRIBUTES_TO_MIRROR]) |
| + |
| + |
| +def mirror_resource(input_filename, output_filename): |
| + """Convert LTR resource to RTL android resource. (or the other way around) |
| + |
| + For example, |
| + from paddingLeft="10dp" to paddingRight="10dp" |
| + from gravity="right|center_vertical" to gravity="left|center_vertical" |
| + """ |
| + |
| + tree = ET.parse(input_filename) |
| + for element in tree.iter(): |
| + attribute_names = ATTRIBUTES_TO_MIRROR.intersection(element.keys()) |
| + mirrored_attributes = {} |
| + for attribute_name in attribute_names: |
| + if 'Left' in attribute_name: |
| + mirrored_attribute_name = attribute_name.replace('Left', 'Right') |
| + mirrored_attribute_value = element.attrib[attribute_name] |
| + elif 'Right' in attribute_name: |
| + mirrored_attribute_name = attribute_name.replace('Right', 'Left') |
| + mirrored_attribute_value = element.attrib[attribute_name] |
| + else: |
| + # This is either gravity or layout_gravity |
| + mirrored_attribute_name = attribute_name |
| + mirrored_attribute_value = re.sub('left|right', |
| + lambda x: 'right' if x.group(0) == 'left' else 'left', |
| + element.attrib[directional_attribute]) |
| + |
| + mirrored_attributes[mirrored_attribute_name] = mirrored_attribute_value |
| + # Delete the attribute that we are going to mirror. |
| + del element.attrib[attribute_name] |
| + |
| + # Update the parsed xml with the mirrored attributes. |
| + element.attrib.update(mirrored_attributes) |
| + |
| + build_utils.MakeDirectory(os.path.dirname(output_filename)) |
| + tree.write(output_filename, encoding='utf-8', xml_declaration=True) |
| + |
| + |
| +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.
|
| + for root, dirs, files in os.walk(input_dir, followlinks=True): |
| + relative_dir = os.path.relpath(root, input_dir) |
| + for file_name in files: |
| + if file_name.lower().endswith('.xml'): |
| + input_filename = os.path.join(input_dir, relative_dir, file_name) |
| + output_filename = os.path.join(output_dir, relative_dir, file_name) |
| + mirror_resource(input_filename, output_filename) |
| + |
| + |
| +def ParseArgs(): |
| + """Parses command line options. |
| + |
| + Returns: |
| + An options object as from optparse.OptionsParser.parse_args() |
| + """ |
| + parser = optparse.OptionParser() |
| + parser.add_option('--res-dir', |
| + help='directory containing resources to be mirrored to RTL') |
| + parser.add_option('--res-mirrored-dir', |
| + help='directory containing RTL resources to be generated') |
| + |
| + (options, args) = parser.parse_args() |
| + |
| + if args: |
| + parser.error('No positional arguments should be given.') |
| + |
| + # Check that required options have been provided. |
| + required_options = ('res_dir', 'res_mirrored_dir') |
| + build_utils.CheckOptions(options, parser, required=required_options) |
| + return options |
| + |
| + |
| +def main(argv): |
| + """Convert Android LTR xml resources to RTL and save it to -ldrtl directories. |
| + |
| + Please refer to the function mirror_resource's comment on how the files |
| + are mirrored. |
| + |
| + ldrtl directory is explained at |
| + http://developer.android.com/guide/topics/resources/providing-resources.html |
| + """ |
| + options = ParseArgs() |
| + |
| + res_sub_dirs = os.walk(options.res_dir).next()[1] |
| + |
| + for res_sub_dir in res_sub_dirs: |
| + # If the directory has already RTL resources, we don't need to mirror. Skip. |
| + 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
|
| + continue |
| + |
| + # We only mirror resources under layout*/ or xml*/. |
| + if (res_sub_dir.startswith('layout') or res_sub_dir.startswith('xml')): |
| + # Insert ldrtl to the dir name. |
| + # For example, from layout-hdpi to layout-ldrtl-hdpi. |
| + res_sub_dir_split = res_sub_dir.split('-') |
| + 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.
|
| + res_mirrored_sub_dir = '-'.join(res_sub_dir_split) |
| + # Mirror all the resource files. |
| + mirror_resources_in_dir( |
| + os.path.join(options.res_dir, res_sub_dir), |
| + os.path.join(options.res_mirrored_dir, res_mirrored_sub_dir)) |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main(sys.argv)) |
| + |