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 != "plural": 41 continue 42 43 item_name = item["name"] 44 item_id = item["order"] + 0x7800000 45 if item_type not in self.records: 46 self.records[item_type] = dict() 47 self.records[item_type][item_name] = item_id 48 49 def write_to_file(self, js_file): 50 output = open(js_file, 'w+') 51 output.write("module.exports.sys = {\n") 52 first_item = True 53 for (item_type, dataset) in self.records.items(): 54 if first_item: 55 first_item = False 56 output.write(" %s: {\n" % (item_type)) 57 else: 58 output.write(",\n %s: {\n" % (item_type)) 59 first_line = True 60 for (res_name, res_id) in dataset.items(): 61 if first_line: 62 first_line = False 63 output.write(" %s: %d" % (res_name, res_id)) 64 else: 65 output.write(",\n %s: %d" % (res_name, res_id)) 66 output.write("\n }") 67 output.write("\n}\n") 68 output.close() 69 70if __name__ == "__main__": 71 parser = argparse.ArgumentParser() 72 parser.add_argument('--input-json', help='input path to id_defined.json') 73 parser.add_argument('--output-js', help='output path to sysResource.js') 74 options = parser.parse_args() 75 76 processor = SysResource() 77 processor.read_json_file(options.input_json) 78 processor.process_ids() 79 processor.write_to_file(options.output_js) 80 81