1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import json 17import os 18import argparse 19 20 21class SysResource: 22 def __init__(self): 23 self.records = {} 24 self.json_records = None 25 26 def read_json_file(self, json_file): 27 with open(os.path.join(json_file), 'r') as fp: 28 self.json_records = json.load(fp)["record"] 29 30 def process_ids(self): 31 for item in self.json_records: 32 if ("flags" in item) and item["flags"].find("private") > -1: 33 continue 34 35 item_type = item["type"] 36 if item_type != "color" \ 37 and item_type != "float" \ 38 and item_type != "media" \ 39 and item_type != "string" \ 40 and item_type != "symbol" \ 41 and item_type != "plural": 42 continue 43 44 item_name = item["name"] 45 item_id = item["order"] + 0x7800000 46 if item_type not in self.records: 47 self.records[item_type] = dict() 48 self.records[item_type][item_name] = item_id 49 50 def write_to_file(self, js_file): 51 output = open(js_file, 'w+') 52 output.write("module.exports.sys = {\n") 53 first_item = True 54 for (item_type, dataset) in self.records.items(): 55 if first_item: 56 first_item = False 57 output.write(" %s: {\n" % (item_type)) 58 else: 59 output.write(",\n %s: {\n" % (item_type)) 60 first_line = True 61 for (res_name, res_id) in dataset.items(): 62 if first_line: 63 first_line = False 64 output.write(" %s: %d" % (res_name, res_id)) 65 else: 66 output.write(",\n %s: %d" % (res_name, res_id)) 67 output.write("\n }") 68 output.write("\n}\n") 69 output.close() 70 71if __name__ == "__main__": 72 parser = argparse.ArgumentParser() 73 parser.add_argument('--input-json', help='input path to id_defined.json') 74 parser.add_argument('--output-js', help='output path to sysResource.js') 75 options = parser.parse_args() 76 77 processor = SysResource() 78 processor.read_json_file(options.input_json) 79 processor.process_ids() 80 processor.write_to_file(options.output_js) 81 82