| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
# libkysdk-system's Library |
| 4 |
# |
| 5 |
# Copyright (C) 2023, KylinSoft Co., Ltd. |
| 6 |
# |
| 7 |
# This library is free software; you can redistribute it and/or |
| 8 |
# modify it under the terms of the GNU Lesser General Public |
| 9 |
# License as published by the Free Software Foundation; either |
| 10 |
# version 3 of the License, or (at your option) any later version. |
| 11 |
# |
| 12 |
# This library is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
# Lesser General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
# Authors: tianshaoshuai <[email protected]> |
| 21 |
# |
| 22 |
# |
| 23 |
|
| 24 |
import yaml, json |
| 25 |
import sys, os |
| 26 |
import argparse |
| 27 |
|
| 28 |
def retrofit_the_structure(src:dict, dest:dict): |
| 29 |
if not isinstance(src, dict): |
| 30 |
# dest.update({'value':str(src)}) |
| 31 |
print(f"{src}'s root node not a dict") |
| 32 |
return |
| 33 |
for key, value in src.items(): |
| 34 |
if isinstance(value, dict): |
| 35 |
dest[key] = {} |
| 36 |
retrofit_the_structure(src[key], dest[key]) |
| 37 |
else: |
| 38 |
dest[key] = {} |
| 39 |
if isinstance(value, list): |
| 40 |
dest[key]['_type'] = 'as' |
| 41 |
dest[key]['_default'] = str(src[key]) |
| 42 |
else: |
| 43 |
dest[key]['_type'] = 's' |
| 44 |
dest[key]['_default'] = src[key] |
| 45 |
|
| 46 |
def convert_one_file(src:str): |
| 47 |
with open(src, 'r') as file: |
| 48 |
data = json.loads(file.read()) |
| 49 |
base_name = os.path.basename(src) |
| 50 |
name = os.path.splitext(base_name)[0] |
| 51 |
result = {name : {'2.0.0.0-0k0.0': {}}} |
| 52 |
retrofit_the_structure(data, result[name]['2.0.0.0-0k0.0']) |
| 53 |
|
| 54 |
home = os.getenv('HOME') |
| 55 |
if not os.path.exists(f'{home}/yaml'): |
| 56 |
os.mkdir(f'{home}/yaml') |
| 57 |
with open(f'{home}/yaml/{base_name[:-5]}.yaml', 'w') as yaml_file: |
| 58 |
yaml_file.write(yaml.safe_dump(result, allow_unicode = True)) |
| 59 |
|
| 60 |
# 参数传入.json结尾的json格式配置文件或目录路径 |
| 61 |
# 将传入的所有配置文件或目录下的所有配置文件转换为yaml格式,生成在~/yaml路径下 |
| 62 |
if __name__ == '__main__': |
| 63 |
parser = argparse.ArgumentParser( |
| 64 |
description='Convert JSON configuration files to YAML format for conf2', |
| 65 |
epilog='Example: json2yaml -o ~/yaml /path/to/file.json /path/to/dir' |
| 66 |
) |
| 67 |
parser.add_argument('sources', nargs='+', |
| 68 |
help='JSON files or directories containing JSON files') |
| 69 |
parser.add_argument('--output', '-o', default='~/yaml', |
| 70 |
help='Output directory (default: ~/yaml)') |
| 71 |
args = sys.argv[1:] |
| 72 |
for arg in args: |
| 73 |
if os.path.isdir(arg): |
| 74 |
for root, dirs, files in os.walk(arg): |
| 75 |
for file in files: |
| 76 |
if file.endswith('.json'): |
| 77 |
|
| 78 |
convert_one_file(f'{root}/{file}') |
| 79 |
else: |
| 80 |
if arg.endswith('.json'): |
| 81 |
convert_one_file(arg) |
| 82 |
else: |
| 83 |
print(f'argument{arg} is not a json file') |