Chromium Code Reviews| 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 | |
|
newt (away)
2013/04/26 18:46:26
remove this
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done.
| |
| 12 import xml.dom.minidom as minidom | |
| 13 | |
| 14 from util import build_utils | |
| 15 | |
| 16 | |
| 17 ATTRIBUTE_NAMESPACE = u'http://schemas.android.com/apk/res/android' | |
| 18 | |
| 19 # The list of the attributes were taken from go/android-rtl | |
| 20 ATTRIBUTES_TO_MAP = {'paddingStart' : 'paddingLeft', | |
| 21 'drawableStart' : 'drawableLeft', | |
| 22 'layout_alignStart' : 'layout_alignLeft', | |
| 23 'layout_marginStart' : 'layout_marginLeft', | |
| 24 'layout_alignParentStart' : 'layout_alignParentLeft', | |
| 25 'layout_toStartOf' : 'layout_toLeftOf', | |
| 26 'paddingEnd' : 'paddingRight', | |
| 27 'drawableEnd' : 'drawableRight', | |
| 28 'layout_alignEnd' : 'layout_alignRight', | |
| 29 'layout_marginEnd' : 'layout_marginRight', | |
| 30 'layout_alignParentEnd' : 'layout_alignParentRight', | |
| 31 'layout_toEndOf' : 'layout_toRightOf'} | |
| 32 | |
| 33 ATTRIBUTES_TO_MAP = {(ATTRIBUTE_NAMESPACE, k) : (ATTRIBUTE_NAMESPACE , v) | |
|
newt (away)
2013/04/26 18:46:26
dict comprehensions are python 2.7+. not sure if
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done.
| |
| 34 for k, v in ATTRIBUTES_TO_MAP.items()} | |
| 35 | |
| 36 GRAVITY_ATTRIBUTES = ('gravity', 'layout_gravity') | |
| 37 | |
| 38 GRAVITY_ATTRIBUTES = [(ATTRIBUTE_NAMESPACE, i) for i in GRAVITY_ATTRIBUTES] | |
| 39 | |
| 40 | |
| 41 def iterate_xml_elements(node): | |
| 42 """minidom helper function that iterates all the element nodes. Depth-first. | |
| 43 """ | |
| 44 if node.nodeType == node.ELEMENT_NODE: | |
| 45 yield node | |
| 46 for child_node in node.childNodes: | |
| 47 for child_node_element in iterate_xml_elements(child_node): | |
| 48 yield child_node_element | |
| 49 | |
| 50 | |
| 51 def generate_pre_v17_resource(input_filename, output_filename): | |
| 52 """Convert resource to API 14 compatible resource. | |
| 53 | |
| 54 It's mostly a simple replacement, s/Start/Left s/End/Right, | |
| 55 on the attribute names. | |
| 56 """ | |
| 57 dom = minidom.parse(input_filename) | |
| 58 | |
| 59 for element in iterate_xml_elements(dom): | |
| 60 all_names = element.attributes.keysNS() | |
| 61 | |
| 62 # Iterate all the attributes to find attributes to convert. | |
| 63 # Note that name variable is actually a tuple that has namespace and name. | |
| 64 # For example, | |
| 65 # name == ('http://schemas.android.com/apk/res/android', 'paddingStart') | |
| 66 for name, value in list(element.attributes.itemsNS()): | |
| 67 # For gravity attributes, replace start/end to left/right. | |
| 68 # For example, gravity="start" to gravity="end" | |
| 69 # This is actually not necessary because start/end is backward-compatible. | |
|
newt (away)
2013/04/26 18:46:26
then why do it? :)
Kibeom Kim (inactive)
2013/04/26 19:37:44
yeah.. actually I was on the edge, because it didn
| |
| 70 if name in GRAVITY_ATTRIBUTES: | |
| 71 replaced_value = re.sub('start|end', | |
| 72 lambda x: 'left' if x.group(0) == 'start' else 'right', value) | |
| 73 element.setAttributeNS(name[0], name[1], replaced_value) | |
| 74 # Convert any other API 17 Start/End attributes to Left/Right attributes. | |
| 75 # For example, from paddingStart="10dp" to paddingLeft="10dp" | |
| 76 elif name in ATTRIBUTES_TO_MAP.keys(): | |
|
newt (away)
2013/04/26 18:46:26
don't need ".keys()"
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done.
| |
| 77 mapped_name = ATTRIBUTES_TO_MAP[name] | |
| 78 | |
| 79 # If, for example, paddingLeft and paddingStart are used | |
| 80 # but have different values, then it is definitely not right. | |
| 81 if mapped_name in all_names: | |
| 82 if value != element.getAttributeNS(*mapped_name): | |
|
newt (away)
2013/04/26 18:46:26
I'd skip this check. We can just make it a rule t
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done.
| |
| 83 error_message = '[ERROR]' + input_filename + '\n' | |
|
newt (away)
2013/04/26 18:46:26
I'd write the error like this:
Error: some_layout
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done. (Removed)
| |
| 84 error_message += ('These Attributes are defined' | |
| 85 'but the values are different.\n') | |
| 86 error_message += name[1] + ' = ' | |
| 87 error_message += element.getAttributeNS(*name) + '\n' | |
| 88 error_message += mapped_name[1] + ' = ' | |
| 89 error_message += element.getAttributeNS(*mapped_name) + '\n' | |
| 90 print >> sys.stderr, error_message | |
| 91 sys.exit(1) | |
| 92 | |
| 93 # Add the new mapped attribute and remove the original attribute. | |
| 94 # For example, add paddingLeft and remove paddingStart. | |
| 95 element.setAttributeNS(mapped_name[0], mapped_name[1], value) | |
| 96 del element.attributes[name] | |
| 97 | |
|
newt (away)
2013/04/26 18:46:26
I think it makes sense to check for non-RTL-friend
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done.
| |
| 98 build_utils.MakeDirectory(os.path.dirname(output_filename)) | |
| 99 dom.writexml(open(output_filename,"w"), ' ', '\n') | |
| 100 | |
| 101 | |
| 102 def mirror_resources_in_dir(input_dir, output_dir): | |
| 103 """Convert resources to API 14 compatible resources in the directory | |
| 104 recursively. | |
| 105 """ | |
| 106 for root, dirs, files in os.walk(input_dir, followlinks=True): | |
|
newt (away)
2013/04/26 18:46:26
I'd remove followlinks=True. actually, this doesn'
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done.
| |
| 107 relative_dir = os.path.relpath(root, input_dir) | |
| 108 for file_name in files: | |
| 109 if file_name.lower().endswith('.xml'): | |
| 110 input_filename = os.path.join(input_dir, relative_dir, file_name) | |
| 111 output_filename = os.path.join(output_dir, relative_dir, file_name) | |
| 112 generate_pre_v17_resource(input_filename, output_filename) | |
| 113 | |
| 114 | |
| 115 def ParseArgs(): | |
| 116 """Parses command line options. | |
| 117 | |
| 118 Returns: | |
| 119 An options object as from optparse.OptionsParser.parse_args() | |
| 120 """ | |
| 121 parser = optparse.OptionParser() | |
| 122 parser.add_option('--res-dir', | |
| 123 help='directory containing resources to be mirrored to RTL') | |
|
newt (away)
2013/04/26 18:46:26
these help messages need updating
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done.
| |
| 124 parser.add_option('--res-mirrored-dir', | |
| 125 help='directory containing RTL resources to be generated') | |
| 126 | |
| 127 (options, args) = parser.parse_args() | |
| 128 | |
| 129 if args: | |
| 130 parser.error('No positional arguments should be given.') | |
| 131 | |
| 132 # Check that required options have been provided. | |
| 133 required_options = ('res_dir', 'res_mirrored_dir') | |
| 134 build_utils.CheckOptions(options, parser, required=required_options) | |
| 135 return options | |
| 136 | |
| 137 | |
| 138 def main(argv): | |
| 139 """Convert Android xml resources to API 14 compatible. | |
|
newt (away)
2013/04/26 18:46:26
I'd mention the issue with Galaxy Note 2 (or whate
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done.
| |
| 140 | |
| 141 """ | |
| 142 options = ParseArgs() | |
| 143 | |
| 144 res_sub_dir_names = os.walk(options.res_dir).next()[1] | |
|
newt (away)
2013/04/26 18:46:26
add a comment explaining what this non-obvious cal
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done. replaced to listdir(...)
| |
| 145 | |
| 146 for res_sub_dir_name in res_sub_dir_names: | |
| 147 dir_pieces = res_sub_dir_name.split('-') | |
| 148 resource_type = dir_pieces[0] | |
| 149 qualifiers = dir_pieces[1:] | |
| 150 | |
|
newt (away)
2013/04/26 18:46:26
only one blank line here
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done.
| |
| 151 | |
| 152 # We only convert resources under layout*/ and xml*/. | |
| 153 if resource_type in ('layout', 'xml'): | |
| 154 res_sub_dir = os.path.join(options.res_dir, res_sub_dir_name) | |
| 155 to_path = os.path.join(options.res_mirrored_dir, | |
| 156 res_sub_dir_name + '-v17') | |
| 157 | |
| 158 # Android pre-v17 API doesn't support RTL. Skip. | |
| 159 if 'ldrtl' in qualifiers: | |
|
newt (away)
2013/04/26 18:46:26
move this to the top of the if block
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done. (I just made the above if resource_type... t
| |
| 160 continue | |
| 161 | |
| 162 # Convert all the resource files. | |
| 163 mirror_resources_in_dir(res_sub_dir, | |
| 164 os.path.join(options.res_mirrored_dir, | |
|
newt (away)
2013/04/26 18:46:26
maybe assign this to a variable and pass in the va
Kibeom Kim (inactive)
2013/04/26 19:37:44
Done.
| |
| 165 res_sub_dir_name)) | |
| 166 | |
| 167 | |
| 168 if __name__ == '__main__': | |
| 169 sys.exit(main(sys.argv)) | |
| 170 | |
| OLD | NEW |