Python程序Spring YAML 转换为Properties文件

GitHub - up2code/yaml2props: Convert YAML to Properties for Spring configurationConvert YAML to Properties for Spring configuration - GitHub - up2code/yaml2props: Convert YAML to Properties for Spring configurationhttps://github.com/up2code/yaml2props

import argparse
import os.path
import pyperclip
import re
import os

TAB_SIZE = 2
USE_SPACES = True

parser = argparse.ArgumentParser(
    prog = 'YAML2Properties',
    description = 'Simple script for convert YAML to Properties file format.'
)
parser.add_argument('file', help = 'Path of YAML file')
parser.add_argument('--output', help = 'Output path. Default is same path of input file by replace extension to .properties')

args = parser.parse_args()

print('YAML File : %s' % args.file)

output_file_path = args.output

if(not output_file_path):
    output_file_path = os.path.splitext(args.file)[0]+'.properties'

if not os.path.isfile(args.file):
    raise ValueError(args.file + ' is not file')

formatted = ''

with open(args.file) as f:
    lines = f.readlines()

prop = []
output = ''
array_object = False

for line in lines:
    ignore = re.search(r'^\s*#', line)
    array_line = re.search(r'^\s*-\s', line) is not None

    if not array_line:
        array_index = -1

    if ignore or not line.strip() or not ':' in line and not array_line:
        output += '\n' if len(output) else ''
        continue

    tabs = re.findall(r'(' + (TAB_SIZE * '\s') + ')', line.split(':')[0])

    index = len(tabs) if tabs else 0

    result_prop = re.search(r'.+(?=:\s)', line)
    
    if index is 0:
        prop = []
        prop.append(result_prop.group().strip())
    else:
        if array_line:
            array_index += 1
        else:
            prop_name = result_prop.group(0).strip()

        while array_index < 0 and prop and index < len(prop):
            prop.pop()

        if array_index < 0:
            prop.append(prop_name)

    value = re.search(r'(?<=:).+', line) if array_index < 0 else re.search(r'(?<=-\s).+', line)

    if value and value.group().strip():
        p = '%(key)s%(idx)s%(spc)s=%(spc)s%(value)s\n' % {
            'key': '.'.join(prop),
            'idx': ('[%d]' % array_index) if array_index >= 0 else '',
            'spc': ' ' if USE_SPACES else '',
            'value': value.group().strip()
        }
        output += p

print('\nSave to file : ' + output_file_path)
 
 # Write file
file_props = open(output_file_path,'w+')
file_props.write(re.sub(r'\n\n\n+', '\n\n', output))
file_props.close()

print('Done!')

yaml2props.py application.yml

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值