41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
# Copyright (c) 2021 Huawei Device Co., Ltd.
|
||
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
|
# you may not use this file except in compliance with the License.
|
||
|
|
# You may obtain a copy of the License at
|
||
|
|
#
|
||
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
#
|
||
|
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
|
# See the License for the specific language governing permissions and
|
||
|
|
# limitations under the License.
|
||
|
|
|
||
|
|
import os
|
||
|
|
import json
|
||
|
|
|
||
|
|
|
||
|
|
def read_json_file(input_file):
|
||
|
|
if not os.path.exists(input_file):
|
||
|
|
print("file '{}' doesn't exist.".format(input_file))
|
||
|
|
return None
|
||
|
|
|
||
|
|
data = None
|
||
|
|
try:
|
||
|
|
with open(input_file, 'r') as input_f:
|
||
|
|
data = json.load(input_f)
|
||
|
|
except json.decoder.JSONDecodeError:
|
||
|
|
print("The file '{}' format is incorrect.".format(input_file))
|
||
|
|
raise
|
||
|
|
return data
|
||
|
|
|
||
|
|
|
||
|
|
def write_json_file(output_file, content):
|
||
|
|
file_dir = os.path.dirname(os.path.abspath(output_file))
|
||
|
|
if not os.path.exists(file_dir):
|
||
|
|
os.makedirs(file_dir, exist_ok=True)
|
||
|
|
with open(output_file, 'w') as output_f:
|
||
|
|
json.dump(content, output_f, indent=2)
|