| 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 configparser |
| 25 |
import yaml |
| 26 |
import sys, os |
| 27 |
import argparse |
| 28 |
|
| 29 |
file_help = "指定一个ini文件或一个文件夹。ini文件扩展名应为'.ini','.cf','.conf'"\ |
| 30 |
"工具会将这个文件或文件夹内全部ini格式的文件转化为yaml文件,并生成到输出目录" |
| 31 |
app_help = "指定生成yaml文件的应用名, 不指定则为'app',需要手动修改文件" |
| 32 |
version_help = "指定生成yaml文件的配置版本,如果不指定版本则默认版本为1.0.0" |
| 33 |
path_help = "指定yaml文件的生成目录,如果不指定生成在${HOME}/yaml目录中" |
| 34 |
|
| 35 |
def convert_one_file(file, name, version, path): |
| 36 |
# 读取.ini或.conf配置文件 |
| 37 |
config = configparser.ConfigParser(interpolation=None) |
| 38 |
config.read(file) |
| 39 |
|
| 40 |
# 将配置文件数据转换为字典 |
| 41 |
data = {name:{version:{}}} |
| 42 |
version = data[name][version] |
| 43 |
for section in config.sections(): |
| 44 |
version[section] = {} |
| 45 |
for option in config.options(section): |
| 46 |
value = config.get(section, option) |
| 47 |
value = value.strip() |
| 48 |
value = value.strip("'") |
| 49 |
value = value.strip('"') |
| 50 |
version[section][option] = {} |
| 51 |
version[section][option]["_default"] = value |
| 52 |
version[section][option]["_type"] = 's' |
| 53 |
|
| 54 |
if not os.path.exists(path): |
| 55 |
os.makedirs(path) |
| 56 |
# 将字典转换为YAML格式并写入文件 |
| 57 |
with open(f'{path}/{name}.yaml', 'w') as yaml_file: |
| 58 |
yaml.dump(data, yaml_file, allow_unicode = True) |
| 59 |
|
| 60 |
# 参数传入.conf或.ini结尾的ini格式配置文件或目录路径 |
| 61 |
# 将传入的所有配置文件或目录下的所有配置文件转换为yaml格式,生成在~/yaml路径下 |
| 62 |
if __name__ == "__main__": |
| 63 |
parser = argparse.ArgumentParser(description='这是一个将ini文件转化为yaml格式的程序, 例如: python3 gschema_xml2yaml.py -f /etc/dput.cf -n testconf -v 1.0.0 -p /home/kylin') |
| 64 |
parser.add_argument('--file', '-f', type=str, help=file_help) |
| 65 |
parser.add_argument('--name', '-n', type=str, help=app_help) |
| 66 |
parser.add_argument('--version', '-v', type=str, help=version_help) |
| 67 |
parser.add_argument('--path', '-p', type=str, help=path_help) |
| 68 |
|
| 69 |
args = parser.parse_args() |
| 70 |
src_file = args.file |
| 71 |
if not src_file: |
| 72 |
print("文件是必填项") |
| 73 |
|
| 74 |
version = args.version if args.version else "1.0.0" |
| 75 |
home = os.getenv('HOME') |
| 76 |
path = args.path if args.path else f'{home}/yaml' |
| 77 |
|
| 78 |
if os.path.isdir(src_file): |
| 79 |
for root, dirs, files in os.walk(src_file): |
| 80 |
for file in files: |
| 81 |
if file.endswith('.conf') or file.endswith('.ini') or file.endswith('.cf'): |
| 82 |
name = file.rsplit('.', maxsplit=1)[0] |
| 83 |
convert_one_file(f'{root}/{file}', name, version, path) |
| 84 |
else: |
| 85 |
if src_file.endswith('.conf') or src_file.endswith('.ini') or src_file.endswith('.cf'): |
| 86 |
name = args.name if args.name else os.path.basename(src_file).rsplit('.', maxsplit=1)[0] |
| 87 |
convert_one_file(src_file, name, version, path) |
| 88 |
else: |
| 89 |
print(f'argument{src_file} is not a conf file') |