Package: localconfig
localconfigA simplified interface toConfigParserusing dot notion with data type / comment support.Feature SummarySimple access to config using dot notion and iteratorsFull compatibility withConfigParserini formats (as that is used as the backend)Data type support by intelligently guessing the data types based on value on read.Multiple config source input (read from string, file pointer, file, or list of them)Full comment support / retention on saveLazy reading of config sources for performance (only read when a config value is accessed)Quick Start TutorialTo install:pip install localconfigLet’s say we have a script namedprogramwith the following config in~/.config/program:[Web Server]# Server hosthost=0.0.0.0# Server portport=8080# Debug loggingdebug=offTo read the config, simply do:fromlocalconfigimportconfigstart_server(config.web_server.host,config.web_server.port,config.web_server.debug)# Or use get method:# start_server(config.get('Web Server', 'host'),#              config.get('Web Server', 'port'),#              config.get('web_server', 'debug'))  # Yes, 'web_server' also works here!## Or if the config is in docstring, read from it:# config.read(__doc__)## Or if the config file is elsewhere:# config.read('/etc/path/to/config.ini')  # Non-existing file is ignored## Or read from a list of sources# config.read(['string config', file_path, file_pointer, io.StringIO('config')])## Or create another instance for another config:# from localconfig import LocalConfig# config2 = LocalConfig('/etc/path/to/another/config.ini')Configs are read in the order they are called usingconfig.read(), but the config file passed to theLocalConfig()constructor (defaults to~/.config/$script_name) will be read last before the first access to config values, which
allows us to read configs from various locations, like default configs from a string that is checked in with the code,
while allowing them to be overrriden from the config file that is passed to the constructor.Now, let’s do some inspection:# Iterate over sections and their keys/valuesforsectioninconfig:print(section)# Web Serverforkey,valueinconfig.items(section):print(key,value,type(value))# host 0.0.0.0 <type 'str'># port 8080 <type 'int'># debug False <type 'bool'>sections=list(config)# ['Web Server']# Iterate over keys/valuesforkey,valueinconfig.web_server:print(key,value,type(value))# Same output as above config.items()items=list(config.web_server)# [('host', '0.0.0.0'), ('port', 8080), ('debug', False)]items=dict(config.web_server)# {'host': '0.0.0.0', 'port': 8080, 'debug': False}# Check if a section or key is set - any non-existing section or key defaults to None.ifconfig.web_serverorconfig.no_such_section:passifconfig.web_serverand(config.web_server.portorconfig.web_server.no_such_key):passTo add a section and set a value:config.add_section('App Server',comment='Settings for application server')config.app_server.host='localhost'# Use `set` if you want to set a commentconfig.set('App Server','port',9090,comment='App server port')# Set value for the DEFAULT section (default value for all other sections)config.env='prod'To write the config:config.save()# Or simply get the config as a string:# config_str = str(config)## Or save to a different location:# config.save('/path/to/save/to.ini')If we open~/.config/programnow, we would see:[DEFAULT]

env = prod


[Web Server]

# Server host
host = 0.0.0.0

# Server port
port = 8080

# Debug logging
debug = off


# Settings for application server
[App Server]

host = localhost

# App server port
port = 9090Supported Data TypesData type is guessed based on the value and converted on read.The following types are supported:TypeExample Valueint1float2.0booltrue false yes no on off (case insensitive)Nonenone (case insensitive)strAny other value not matched by aboveRemote ConfigCheck out:https://pypi.python.org/pypi/remoteconfigMoreDocumentation:http://localconfig.readthedocs.org/PyPI Package:https://pypi.python.org/pypi/localconfigGitHub Source:https://github.com/maxzheng/localconfigReport Issues/Bugs:https://github.com/maxzheng/localconfig/issuesConnect:https://www.linkedin.com/in/maxzhengContact: maxzheng.os @t gmail.com