通用配置文件分析器

cincoconfig的Python项目详细描述


Cinco配置

Build StatusCoverage Status

下一代通用配置文件分析器。配置文件结构已定义 通过编程和表达式,不需要创建类和继承。

让我们直奔主题:

# app_config.pyimportgetpassfromcincoconfigimport*# first, define the configuration's schema -- the fields available that# customize the application's or library's behaviorschema=Schema()schema.mode=ApplicationModeField(default='production')# nested configurations are built on the fly# http is now a subconfigschema.http.port=PortField(default=8080,required=True)# each field has its own validation rules that are run anytime the config# value is loaded from disk or modified by the user.# here, this field only accepts IPv4 network addresses and the user is# required to define this field in the configuration file.schema.http.address=IPv4AddressField(default='127.0.0.1',required=True)schema.http.ssl.enabled=BoolField(default=False)schema.http.ssl.cafile=FilenameField()schema.http.ssl.keyfile=FilenameField()schema.http.ssl.certfile=FilenameField()schema.db.host=HostnameField(allow_ipv4=True,required=True,default='localhost')schema.db.port=PortField(default=27017,required=True)schema.db.name=StringField(default='my_app',required=True)schema.db.user=StringField(default='admin')# some configuration values are sensitive, such as credentials, so# cincoconfig provides config value encryption when the value is# saved to disk via the SecureFieldschema.db.password=SecureField()# once a schema is defined, build the actual configuration object# that can load config files from disk and interact with the valuesconfig=schema()# print the set http portprint(config.http.port)# >>> 8080config.db.password=getpass.getpass("Enter Password: ")# < 'password'# set a config value manuallyifconfig.mode=='production':config.db.name=config.db.name+'_production'print(config.dumps(format='json',pretty=True).decode())# {#   "mode": "production",#   "http": {#     "port": 8080,#     "address": "127.0.0.1"#     "ssl": {#       "enabled": false#     }#   },#   "db": {#     "host": "localhost",#     "port": 27017,#     "name": "my_app_production",#     "user": "admin",#     "password": {#       "method": "best",#       "ciphertext": "<ciphertext>"#     }#   }# }

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
IE中的java跨域cookie问题   重复java中已经满足的循环   编译java RMI服务器时出错   JavaServlet POST中作为参数传递的javascript大型JSON数组数据为空   java片段未每次刷新/调用   java无法编译。错误消息   java如何构造大型类?   java Hibernate:TableThingsDB。事情并不存在   java如何操作从匹配项创建的数组。发现   循环以搜索和显示数组Java的某些部分   加载或注册SQLite JDBC驱动程序时出现java问题   活动和服务之间的java连接   JavaGWTG2D:ie8中的drawImage   java在安卓中设置hessian阈值   在Tomcat中使用Logback时发生java错误