-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=_§ion_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|是|环境变量文本数据 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |