Skip to main content

便捷的环境文件读取

Project description

envx

介绍

环境信息管理模块,适用于跨设备区分环境类型的情况

安装教程

  1. pip安装
pip3 install envx
  1. pip安装(使用淘宝镜像加速)
pip3 install envx -i https://mirrors.aliyun.com/pypi/simple

使用说明

  1. 环境文件目录(如果没有对应目录,请创建对应的目录)
  • Windows 系统

    使用目录:C:\env\
    
  • macOS 系统

    使用目录:/Users/env/
    
  • Linux 系统

    使用目录:/env/
    
  1. 环境文件案例
  • 文件名:local.any.env
  • 文件内容:
    HOST=192.168.0.1
    PORT=6379
    
  • 读取结果:
    {
      "HOST": "192.168.0.1", 
      "PORT": "6379"
    }
    
  1. 项目中使用代码
import envx
local_any_env = envx.read('local.any.env')
  • 建议使用环境类型标记文件:DEFAULT_ENV.env
一般用来描述当前的环境信息,可以用来标记当前的环境类型,
然后后续使用时先去读当前默认的环境类型,再根据设定选取不同的环境文件,
即,会先从当前环境中读取DEFAULT_ENV.env中设定的环境类型,再读取settings.ini的具体设定,
然后根据当前环境类型取对应的环境文件;
注意:settings.ini文件必须和使用此代码的文件在同一目录,若为其他目录,需修改文件路径
  • settings.ini
[DEFAULT]
env=DEV
msg=在这里的env中设置将要使用哪个具体的环境

[DEV]
env_name=DEV-开发
env_file_name_mysql=dev.mysql.env
env_file_name_mongo=dev.mongo.env
silence_mysql=False

[TEST]
env_name=TEST-测试
env_file_name_mysql=test.mysql.env
env_file_name_mongo=test.mongo.env
silence_mysql=False

[PROD]
env_name=PROD-生产
env_file_name_mysql=prod.mysql.env
env_file_name_mongo=prod.mongo.env
silence_mysql=False
  • 项目代码
# ----------------- 读取配置信息 -----------------
import configparser
import platform
import os

if platform.system() == 'Windows':
    path_separator = '\\'
else:
    path_separator = '/'
config_dir = (os.path.dirname(os.path.realpath(__file__))) + path_separator + 'settings.ini'

config = configparser.ConfigParser()  # 类实例化
config.read(config_dir, encoding='utf-8')
default_config = config['DEFAULT']
default_env = default_config['env']
config_config = config[default_env]

# 从这里开始写需要读取为内容
env_file_name_mysql = config_config['env_file_name_mysql']
env_file_name_mongo = config_config['env_file_name_mongo']
silence_mysql = eval(config_config['silence_mysql'])
# ----------------- 读取配置信息 -----------------

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envx-1.0.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

envx-1.0.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file envx-1.0.2.tar.gz.

File metadata

  • Download URL: envx-1.0.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for envx-1.0.2.tar.gz
Algorithm Hash digest
SHA256 1da846eec083f882d8cbf9c39e0c816553c68b854cf4962d3a57e1b7a7b5e6cc
MD5 9e1366e3001dbd830a4222601d67558f
BLAKE2b-256 62ec90d816503e8b8e414d414068c6c0248bfedac16da25676a1adf0d4c35f31

See more details on using hashes here.

File details

Details for the file envx-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: envx-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for envx-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a5308d729730209fc11b41d6ef672fef7665977164613a4bfc9ad971b056f9a3
MD5 12432199f51da44c3a1ab835fbdb4ccc
BLAKE2b-256 299dc412d3efbeae14df9dc9d50f1db24bac27e0c473a5021a08e2cda520f51b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page