OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 """ |
| 4 |
| 5 The goal of this script is to integrate and dump interface node information to j
son file. |
| 6 """ |
| 7 |
| 8 import os |
| 9 import sys |
| 10 import json |
| 11 |
| 12 from blink_idl_parser import parse_file, BlinkIDLParser |
| 13 |
| 14 |
| 15 def load_filepaths(path_file): |
| 16 """ |
| 17 Args: |
| 18 path_file: text file |
| 19 Return: |
| 20 path: str, absolute file path |
| 21 """ |
| 22 for line in open(path_file, 'r'): |
| 23 path = line.strip() |
| 24 yield path |
| 25 |
| 26 |
| 27 def get_interfaces(path_file): |
| 28 """ |
| 29 Args: |
| 30 path_file: text file |
| 31 Return: |
| 32 definition: node class object, interface node objects |
| 33 """ |
| 34 parser = BlinkIDLParser(debug=False) |
| 35 class_name = 'Interface' |
| 36 for node_path in load_filepaths(path_file): |
| 37 definitions = parse_file(parser, node_path) |
| 38 for definition in definitions.GetChildren(): |
| 39 if definition.GetClass() == class_name: |
| 40 yield definition |
| 41 |
| 42 |
| 43 def get_filepath(interface_node): |
| 44 """ |
| 45 Args: |
| 46 interface_node: interface node class object |
| 47 Return: |
| 48 os.path.relpath(filename): str, interface_node's file path |
| 49 """ |
| 50 filename = interface_node.GetProperty('FILENAME') |
| 51 return os.path.relpath(filename) |
| 52 |
| 53 |
| 54 def get_partial(interface_node_list): |
| 55 """ |
| 56 Args: |
| 57 interface_node_list: generator, interface node class object |
| 58 Return: |
| 59 interface_node: generator, interface node class object |
| 60 """ |
| 61 for interface_node in interface_node_list: |
| 62 if interface_node.GetProperty('Partial', default=False): |
| 63 yield interface_node |
| 64 |
| 65 |
| 66 def get_non_partial(interface_node_list): |
| 67 """ |
| 68 Args: |
| 69 interface_node_list: generator interface node class object |
| 70 Return: |
| 71 interface_node: generator, interface node class object |
| 72 """ |
| 73 for interface_node in interface_node_list: |
| 74 if not interface_node.GetProperty('Partial', default=False): |
| 75 yield interface_node |
| 76 |
| 77 |
| 78 def get_attributes(interface_node): |
| 79 """ |
| 80 Args: |
| 81 interface_node: interface node object |
| 82 Return: |
| 83 interface_node.GetListOf('Attribute'): list, Attribute object list |
| 84 """ |
| 85 return interface_node.GetListOf('Attribute') |
| 86 |
| 87 |
| 88 def get_type(node): |
| 89 """ |
| 90 Args: |
| 91 node: interface node object |
| 92 """ |
| 93 return node.GetListOf('Type')[0].GetChildren()[0].GetName() |
| 94 |
| 95 |
| 96 def get_extattirbutes(node): |
| 97 """ |
| 98 Args: |
| 99 node: interface node object |
| 100 Return: |
| 101 extattribute_list: list, extattribute object list generator |
| 102 """ |
| 103 for extattributes in node.GetListOf('ExtAttributes'): |
| 104 for extattribute_list in extattributes.GetChildren(): |
| 105 yield extattribute_list |
| 106 |
| 107 |
| 108 def extattr_dict(node): |
| 109 """ |
| 110 Args: |
| 111 node: interface node object |
| 112 Return: |
| 113 {'Name': extattribute.GetName()}: dict, extattribute dictionary generator |
| 114 """ |
| 115 for extattribute in get_extattirbutes(node): |
| 116 yield { |
| 117 'Name': extattribute.GetName() |
| 118 } |
| 119 |
| 120 |
| 121 def attributes_dict(interface_node): |
| 122 """ |
| 123 Args: |
| 124 interface_node: interface node object |
| 125 Return: |
| 126 attr_dict: dict, dictionary of attribite information generator |
| 127 """ |
| 128 for attribute in get_attributes(interface_node): |
| 129 attr_dict = {} |
| 130 attr_dict['Name'] = attribute.GetName() |
| 131 attr_dict['Type'] = get_type(attribute) |
| 132 attr_dict['ExtAttributes'] = [extattr for extattr in extattr_dict(attrib
ute)] |
| 133 yield attr_dict |
| 134 |
| 135 |
| 136 def get_operations(interface_node): |
| 137 """ |
| 138 Args: |
| 139 interface_node: interface node object |
| 140 Return: |
| 141 interface_node.GetListOf('Operation'): list, list of oparation object |
| 142 """ |
| 143 return interface_node.GetListOf('Operation') |
| 144 |
| 145 |
| 146 def get_arguments(operation): |
| 147 """ |
| 148 Args: |
| 149 operation: interface node object |
| 150 Return: |
| 151 argument_node.GetListOf('Argument'): list, list of argument object |
| 152 """ |
| 153 argument_node = operation.GetListOf('Arguments')[0] |
| 154 return argument_node.GetListOf('Argument') |
| 155 |
| 156 |
| 157 def argument_dict(argument): |
| 158 """ |
| 159 Args: |
| 160 argument: interface node object |
| 161 Return: |
| 162 arg_dict: dict, generator of argument information's dictionary |
| 163 """ |
| 164 for arg_name in get_arguments(argument): |
| 165 arg_dict = {} |
| 166 arg_dict['Name'] = arg_name.GetName() |
| 167 arg_dict['Type'] = get_type(arg_name) |
| 168 yield arg_dict |
| 169 |
| 170 |
| 171 def get_operation_name(operation): |
| 172 """ |
| 173 Args: |
| 174 operation: operation object in interface node object |
| 175 Return: |
| 176 str, operation's name |
| 177 """ |
| 178 if operation.GetProperty('GETTER', default=None): |
| 179 return '__getter__' |
| 180 elif operation.GetProperty('SETTER', default=None): |
| 181 return '__setter__' |
| 182 elif operation.GetProperty('DELETER', default=None): |
| 183 return '__deleter__' |
| 184 else: |
| 185 return operation.GetName() |
| 186 |
| 187 |
| 188 def operation_dict(interface_node): |
| 189 """ |
| 190 Args: |
| 191 interface_node: interface node object |
| 192 Return: |
| 193 operate_dict: generator of operation dictionary |
| 194 """ |
| 195 for operation in get_operations(interface_node): |
| 196 operate_dict = {} |
| 197 operate_dict['Name'] = get_operation_name(operation) |
| 198 operate_dict['Argument'] = [args for args in argument_dict(operation)] |
| 199 operate_dict['Type'] = get_type(operation) |
| 200 operate_dict['ExtAttributes'] = [extattr for extattr in extattr_dict(ope
ration)] |
| 201 yield operate_dict |
| 202 |
| 203 |
| 204 def get_consts(interface_node): |
| 205 """ |
| 206 Args: |
| 207 interface_node: interface node object |
| 208 Return: |
| 209 interface_node.GetListOf('Const'): list, list of constant object |
| 210 """ |
| 211 return interface_node.GetListOf('Const') |
| 212 |
| 213 |
| 214 def get_const_type(node): |
| 215 """ |
| 216 Args: |
| 217 node: interface node's attribute or operation object |
| 218 Return: |
| 219 node.GetChildren()[0].GetName(): str, constant object's name |
| 220 """ |
| 221 return node.GetChildren()[0].GetName() |
| 222 |
| 223 |
| 224 def get_const_value(node): |
| 225 """ |
| 226 Args: |
| 227 node: interface node's attribute or operation object |
| 228 Return: |
| 229 node.GetChildren()[1].GetName(): list, list of oparation object |
| 230 """ |
| 231 return node.GetChildren()[1].GetName() |
| 232 |
| 233 |
| 234 def const_dict(interface_node): |
| 235 """ |
| 236 Args: |
| 237 interface_node: interface node object |
| 238 Return: |
| 239 {}: generator, dict of constant object information |
| 240 """ |
| 241 for const in get_consts(interface_node): |
| 242 yield { |
| 243 'Name': const.GetName(), |
| 244 'Type': get_const_type(const), |
| 245 'Value': get_const_value(const) |
| 246 } |
| 247 |
| 248 |
| 249 def format_interface_dict(interface_node): |
| 250 """ |
| 251 Args: |
| 252 interface_node: interface node object |
| 253 Return: |
| 254 interface_dict: dict, dictionary of interface node information |
| 255 """ |
| 256 interface_dict = {} |
| 257 interface_dict['Name'] = interface_node.GetName() |
| 258 interface_dict['FilePath'] = get_filepath(interface_node) |
| 259 interface_dict['Attribute'] = [attr for attr in attributes_dict(interface_no
de)] |
| 260 interface_dict['Operation'] = [operation for operation in operation_dict(int
erface_node)] |
| 261 interface_dict['ExtAttributes'] = [extattr for extattr in extattr_dict(inter
face_node)] |
| 262 interface_dict['Constant'] = [const for const in const_dict(interface_node)
if const] |
| 263 return interface_dict |
| 264 |
| 265 |
| 266 def merge_partial_interface(interface_dict_list, partial_dict_list): |
| 267 """ |
| 268 Args: |
| 269 interface_dict_list: list |
| 270 partial_dict_list: list |
| 271 Return: |
| 272 interface_dict_list: list, list of interface node's dictionry merged with
partial interface node |
| 273 """ |
| 274 for partial in partial_dict_list: |
| 275 for interface in interface_dict_list: |
| 276 if interface['Name'] == partial['Name']: |
| 277 interface['Attribute'].append(partial['Attribute']) |
| 278 interface['Operation'].append(partial['Operation']) |
| 279 interface['ExtAttributes'].append(partial['ExtAttributes']) |
| 280 interface.setdefault('Partial_FilePath', []).append(partial['Fil
ePath']) |
| 281 if interface['Constant']: |
| 282 interface.setdefault('Constant', []).append(partial['Constan
t']) |
| 283 return interface_dict_list |
| 284 |
| 285 |
| 286 def format_dictionary(dictionary_list): |
| 287 """ |
| 288 Args: |
| 289 dictirary_list: list, list of interface node dictionary |
| 290 Return: |
| 291 dictionary: dict, {interface_node name: interface node dictionary} |
| 292 """ |
| 293 dictionary = {} |
| 294 for interface_dict in dictionary_list: |
| 295 dictionary.setdefault(interface_dict['Name'], interface_dict) |
| 296 return dictionary |
| 297 |
| 298 |
| 299 def export_jsonfile(dictionary, json_file): |
| 300 filename = json_file |
| 301 indent_size = 4 |
| 302 f = open(filename, 'w') |
| 303 json.dump(dictionary, f, sort_keys=True, indent=indent_size) |
| 304 f.close() |
| 305 |
| 306 |
| 307 def main(args): |
| 308 path_file = args[0] |
| 309 json_file = args[1] |
| 310 interface_dict_list = [format_interface_dict(interface_node) for interface_n
ode in get_non_partial(get_interfaces(path_file))] |
| 311 partial_dict_list = [format_interface_dict(interface_node) for interface_nod
e in get_partial(get_interfaces(path_file))] |
| 312 dictionary_list = merge_partial_interface(interface_dict_list, partial_dict_
list) |
| 313 dictionary = format_dictionary(dictionary_list) |
| 314 export_jsonfile(dictionary, json_file) |
| 315 |
| 316 |
| 317 if __name__ == '__main__': |
| 318 main(sys.argv[1:]) |
OLD | NEW |