OLD | NEW |
---|---|
(Empty) | |
1 #!/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.
| |
2 | |
3 import optparse | |
4 import os | |
5 import xml.etree.cElementTree as ET | |
6 | |
7 from util import build_utils | |
8 | |
9 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.
| |
10 | |
11 s = ('paddingLeft', | |
12 '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.
| |
13 'layout_alignLeft', | |
14 'layout_marginLeft', | |
15 'layout_alignParentLeft', | |
16 'layout_toLeftOf', | |
17 'paddingRight', | |
18 'drawableRight', | |
19 'layout_alignRight', | |
20 'layout_marginRight', | |
21 'layout_alignParentRight', | |
22 'layout_toRightOf', | |
23 'gravity', | |
24 'layout_gravity') | |
25 | |
26 s = set((pre + i for i in s)) | |
27 | |
28 def reverse_xml(input_filename, output_filename): | |
29 """ | |
30 """ | |
31 tree = ET.parse(input_filename) | |
32 for element in tree.iter(): | |
33 directional_attributes = s.intersection(element.keys()) | |
34 new_attributes_to_override = {} | |
35 for directional_attribute in directional_attributes: | |
36 if 'Left' in directional_attribute: | |
37 new_attributes_to_override[directional_attribute.replace('Left', 'Right' )] = element.attrib[directional_attribute] | |
38 elif 'Right' in directional_attribute: | |
39 new_attributes_to_override[directional_attribute.replace('Right', 'Left' )] = element.attrib[directional_attribute] | |
40 else: # Either gravity or layout_gravity | |
41 new_attributes_to_override[directional_attribute] = \ | |
42 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
| |
43 element.attrib.update(new_attributes_to_override) | |
44 output_dir = os.path.dirname(output_filename) | |
45 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.
| |
46 os.makedirs(output_dir) | |
47 tree.write(output_filename, encoding='utf-8', xml_declaration=True) | |
48 | |
49 def reverse_all_xml_in_dir(input_dir, output_dir): | |
50 """ | |
51 """ | |
52 for root, dirs, files in os.walk(input_dir, followlinks=True): | |
53 relative_dir = os.path.relpath(root, input_dir) | |
54 for f in files: | |
55 if f.lower().endswith('.xml'): | |
56 input_filename = os.path.join(input_dir, relative_dir, f) | |
57 output_filename = os.path.join(output_dir, relative_dir, f) | |
58 reverse_xml(input_filename, output_filename) | |
59 | |
60 def ParseArgs(): | |
61 """Parses command line options. | |
62 | |
63 Returns: | |
64 An options object as from optparse.OptionsParser.parse_args() | |
65 """ | |
66 parser = optparse.OptionParser() | |
67 parser.add_option('--res-dir', | |
68 help='directory containing resources to be reversed') | |
69 parser.add_option('--res-ldrtl-dir', | |
70 help='directory containing resources to be generated') | |
71 | |
72 (options, args) = parser.parse_args() | |
73 | |
74 if args: | |
75 parser.error('No positional arguments should be given.') | |
76 | |
77 # Check that required options have been provided. | |
78 required_options = ('res_dir', 'res_ldrtl_dir') | |
79 build_utils.CheckOptions(options, parser, required=required_options) | |
80 | |
81 return options | |
82 | |
83 def main(): | |
84 options = ParseArgs() | |
85 res_sub_dirs = os.walk(options.res_dir).next()[1] | |
86 | |
87 for res_sub_dir in res_sub_dirs: | |
88 if '-ldrtl' in res_sub_dir: | |
89 continue | |
90 if (not res_sub_dir.startswith('layout') and \ | |
91 not res_sub_dir.startswith('xml')): | |
92 continue | |
93 res_sub_dir_split = res_sub_dir.split('-') | |
94 res_sub_dir_split.insert(1, 'ldrtl') | |
95 res_ldrtl_sub_dir = '-'.join(res_sub_dir_split) | |
96 reverse_all_xml_in_dir(os.path.join(options.res_dir, res_sub_dir), \ | |
97 os.path.join(options.res_ldrtl_dir, res_ldrtl_sub_dir )) | |
98 | |
99 if __name__ == '__main__': | |
100 main() | |
OLD | NEW |