Index: build/android/gyp/XmlLayoutCopyMirror.py |
diff --git a/build/android/gyp/XmlLayoutCopyMirror.py b/build/android/gyp/XmlLayoutCopyMirror.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..ab98e966cee01a9556a6d5078e9440fa40fde23f |
--- /dev/null |
+++ b/build/android/gyp/XmlLayoutCopyMirror.py |
@@ -0,0 +1,100 @@ |
+#!/usr/bin/env python |
newt (away)
2013/04/24 23:34:38
copyright
also, this filename should be in unders
Kibeom Kim (inactive)
2013/04/25 00:04:39
Done.
|
+ |
+import optparse |
+import os |
+import xml.etree.cElementTree as ET |
+ |
+from util import build_utils |
+ |
+pre = '{http://schemas.android.com/apk/res/android}' |
newt (away)
2013/04/24 23:34:38
constants should be in ALL_CAPS. also, I'd give th
Kibeom Kim (inactive)
2013/04/25 00:04:39
Done.
|
+ |
+s = ('paddingLeft', |
+ 'drawableLeft', |
newt (away)
2013/04/24 23:34:38
add more spaces to line these up with 'paddingLeft
Kibeom Kim (inactive)
2013/04/25 00:04:39
Done.
|
+ 'layout_alignLeft', |
+ 'layout_marginLeft', |
+ 'layout_alignParentLeft', |
+ 'layout_toLeftOf', |
+ 'paddingRight', |
+ 'drawableRight', |
+ 'layout_alignRight', |
+ 'layout_marginRight', |
+ 'layout_alignParentRight', |
+ 'layout_toRightOf', |
+ 'gravity', |
+ 'layout_gravity') |
+ |
+s = set((pre + i for i in s)) |
+ |
+def reverse_xml(input_filename, output_filename): |
+ """ |
+ """ |
+ tree = ET.parse(input_filename) |
+ for element in tree.iter(): |
+ directional_attributes = s.intersection(element.keys()) |
+ new_attributes_to_override = {} |
+ for directional_attribute in directional_attributes: |
+ if 'Left' in directional_attribute: |
+ new_attributes_to_override[directional_attribute.replace('Left', 'Right')] = element.attrib[directional_attribute] |
+ elif 'Right' in directional_attribute: |
+ new_attributes_to_override[directional_attribute.replace('Right', 'Left')] = element.attrib[directional_attribute] |
+ else: # Either gravity or layout_gravity |
+ new_attributes_to_override[directional_attribute] = \ |
+ element.attrib[directional_attribute].replace('left', 'XXX').replace('right', 'left').replace('XXX', 'right') |
newt (away)
2013/04/24 23:34:38
it'd be nice to avoid using XXX like this, e.g.:
Kibeom Kim (inactive)
2013/04/25 00:04:39
I haven't used regex much, but this simple thing l
|
+ element.attrib.update(new_attributes_to_override) |
+ output_dir = os.path.dirname(output_filename) |
+ if not os.path.exists(output_dir): |
newt (away)
2013/04/24 23:34:38
build_utils.MakeDirectory(output_dir)
Kibeom Kim (inactive)
2013/04/25 00:04:39
Done.
|
+ os.makedirs(output_dir) |
+ tree.write(output_filename, encoding='utf-8', xml_declaration=True) |
+ |
+def reverse_all_xml_in_dir(input_dir, output_dir): |
+ """ |
+ """ |
+ for root, dirs, files in os.walk(input_dir, followlinks=True): |
+ relative_dir = os.path.relpath(root, input_dir) |
+ for f in files: |
+ if f.lower().endswith('.xml'): |
+ input_filename = os.path.join(input_dir, relative_dir, f) |
+ output_filename = os.path.join(output_dir, relative_dir, f) |
+ reverse_xml(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 reversed') |
+ parser.add_option('--res-ldrtl-dir', |
+ help='directory containing 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_ldrtl_dir') |
+ build_utils.CheckOptions(options, parser, required=required_options) |
+ |
+ return options |
+ |
+def main(): |
+ options = ParseArgs() |
+ res_sub_dirs = os.walk(options.res_dir).next()[1] |
+ |
+ for res_sub_dir in res_sub_dirs: |
+ if '-ldrtl' in res_sub_dir: |
+ continue |
+ if (not res_sub_dir.startswith('layout') and \ |
+ not res_sub_dir.startswith('xml')): |
+ continue |
+ res_sub_dir_split = res_sub_dir.split('-') |
+ res_sub_dir_split.insert(1, 'ldrtl') |
+ res_ldrtl_sub_dir = '-'.join(res_sub_dir_split) |
+ reverse_all_xml_in_dir(os.path.join(options.res_dir, res_sub_dir), \ |
+ os.path.join(options.res_ldrtl_dir, res_ldrtl_sub_dir)) |
+ |
+if __name__ == '__main__': |
+ main() |