Skip to content

Commit

Permalink
feat: env to ini
Browse files Browse the repository at this point in the history
  • Loading branch information
ztj1993 committed Oct 25, 2019
1 parent e6ce0e1 commit 6533896
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
18 changes: 18 additions & 0 deletions App/ApiBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,21 @@ def cursor_init():
MemoryData.set_cursor_expire(cursor_id, expire)

return str(cursor_id)

@staticmethod
@FlaskApp.route('/env_to_ini', methods=['GET', 'POST'])
def env_to_ini():
"""初始化游标"""
prefix = flask.request.values.get('prefix')
delimiter = str(flask.request.values.get('delimiter', '_'))
section_lower = bool(flask.request.values.get('section_lower', False))
key_lower = bool(flask.request.values.get('key_lower', False))
env_str = str(flask.request.get_data().decode())

return Libs.env_to_ini(
env_str,
prefix=prefix,
delimiter=delimiter,
section_lower=section_lower,
key_lower=key_lower
)
2 changes: 2 additions & 0 deletions App/Env.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import yaml

from . import Excepts
from . import Libs
from .MemoryData import MemoryData

json = json
Expand All @@ -23,5 +24,6 @@

MemoryData = MemoryData()
Excepts = Excepts
Libs = Libs

FlaskApp = flask.Flask('APP')
41 changes: 41 additions & 0 deletions App/Libs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
# Intro: 库模块
# Author: Ztj
# Email: [email protected]

import configparser
from io import StringIO

from dotenv import dotenv_values


def env_to_ini(env_str, prefix=None, delimiter='_', section_lower=False, key_lower=False):
"""环境变量文本转 INI 文本"""
ini_parser = configparser.ConfigParser()
# 解析环境变量
file_stream = StringIO(env_str)
file_stream.seek(0)
items = dotenv_values(stream=file_stream)
for item in items:
# 提取关键元素
words = item.split(delimiter)
if len(words) < 3:
continue
if prefix is not None:
# 校验前缀
item_prefix = words.pop(0)
if not item_prefix == prefix:
continue
section = words.pop(0)
key = delimiter.join(words)
value = items.get(item)
# 设置关键元素
section = section.lower() if section_lower else section
key = key.lower() if key_lower else key
if not ini_parser.has_section(section):
ini_parser.add_section(section)
ini_parser.set(section, key, value)
# 输出 INI 文本
output_stream = StringIO()
ini_parser.write(output_stream)
return output_stream.getvalue()
46 changes: 46 additions & 0 deletions Docs/EnvToIni.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 环境变量转 INI 配置接口

## 应用示例
```
$ export FRP_COMMON_SERVER_ADDR=0.0.0.0
$ export FRP_COMMON_BIND_PORT=7000
$ export FRP_SSH_TYPE=tcp
$ export FRP_SSH_LOCAL_IP=127.0.0.1
$ export FRP_SSH_LOCAL_PORT=22
$ export FRP_SSH_REMOTE_PORT=6000
$ data=$(env | grep FRP) && echo "${data}"
FRP_COMMON_SERVER_ADDR=0.0.0.0
FRP_COMMON_BIND_PORT=7000
FRP_SSH_TYPE=tcp
FRP_SSH_LOCAL_IP=127.0.0.1
FRP_SSH_LOCAL_PORT=22
FRP_SSH_REMOTE_PORT=6000
$ query_args="prefix=FRP&delimiter=_&section_lower=1&key_lower=1"
$ curl -H "Content-Type: text/plain" http://127.0.0.1:5000/env_to_ini?${query_args} -d "${data}"
[common]
server_addr = 0.0.0.0
bind_port = 7000
[ssh]
local_ip = 127.0.0.1
remote_port = 6000
local_port = 22
type = tcp
```

## 接口文档
- 接口路径:/env_to_ini
- 返回数据:INI 配置文件文本

参数名|类型|必填|说明
---|---|---|---
prefix|query|否|前缀,只处理指定前缀的环境变量
delimiter|query|否|分隔符,默认下划线,用于分割前缀、配置组和配置键
section_lower|query|否|配置组是否转为小写
key_lower|query|否|配置键是否转为小写
-|data|是|环境变量文本数据
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Flask==1.1.1
PyYAML==5.1.2
python-dotenv==0.10.3
py-ztj-registry==0.0.2

0 comments on commit 6533896

Please sign in to comment.