SelectorMapGen.py (2012B)
1 #!/usr/bin/env python 2 # 3 # This Source Code Form is subject to the terms of the Mozilla Public 4 # License, v. 2.0. If a copy of the MPL was not distributed with this 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 7 import re 8 9 10 def write_map(fd, name, text): 11 matches = re.findall(r"^//\s(AX\w+)\n-\s?\(.*?\)([\w:]+)", text, re.MULTILINE) 12 entries = [' @"%s" : @"%s"' % (a, s) for [a, s] in matches] 13 14 fd.write("NSDictionary* %s() {\n" % name) 15 fd.write(" // Create an autoreleased NSDictionary object once, and leak it.\n") 16 fd.write(" static NSDictionary* s%s = [@{\n" % name) 17 fd.write(",\n".join(entries)) 18 fd.write("\n } retain];\n\n") 19 fd.write(" return s%s;\n" % name) 20 fd.write("}\n\n") 21 22 23 def gen_mm(fd, protocol_file): 24 protocol = open(protocol_file).read() 25 fd.write("/* THIS FILE IS AUTOGENERATED - DO NOT EDIT */\n\n") 26 fd.write("#import <Foundation/Foundation.h>\n\n") 27 fd.write("namespace mozilla {\nnamespace a11y {\nnamespace mac {\n\n") 28 29 sections = re.findall( 30 r"#pragma mark - (\w+)\n(.*?)(?=(?:#pragma mark|@end))", protocol, re.DOTALL 31 ) 32 for name, text in sections: 33 write_map(fd, name, text) 34 35 fd.write("}\n}\n}\n") 36 37 38 def gen_h(fd, protocol_file): 39 protocol = open(protocol_file).read() 40 sections = re.findall( 41 r"#pragma mark - (\w+)\n(.*?)(?=(?:#pragma mark|@end))", protocol, re.DOTALL 42 ) 43 44 fd.write("/* THIS FILE IS AUTOGENERATED - DO NOT EDIT */\n\n") 45 fd.write("#ifndef _MacSelectorMap_H_\n") 46 fd.write("#define _MacSelectorMap_H_\n") 47 fd.write("\n@class NSDictionary;\n") 48 fd.write("\nnamespace mozilla {\nnamespace a11y {\nnamespace mac {\n\n") 49 for name, _ in sections: 50 fd.write("NSDictionary* %s();\n\n" % name) 51 fd.write("}\n}\n}\n") 52 fd.write("\n#endif\n") 53 54 55 # For debugging 56 if __name__ == "__main__": 57 import sys 58 59 gen_mm(sys.stdout, "accessible/mac/MOXAccessibleProtocol.h") 60 61 gen_h(sys.stdout, "accessible/mac/MOXAccessibleProtocol.h")