Skip to main content

Manage your configuration files.

Project description

Config File

Manage and manipulate your configuration files

Python Verisons Version Style Build Status Codecov

This python package is currently a work in progress and is in a pre-alpha phase. The API is liable to break until v1.

Config File allows you to use the same simple API for manipulating INI, JSON, YAML, and TOML configuration files. For the time being, it only supports INI.


$ pip install config_file


The config_file package exposes ConfigFile, ParsingError, and BaseParser to the public API.

Sample Configuration File


today = monday
start_week_on_sunday = false
today_index = 0
quarter_hours_passed = 0.25

Create a ConfigFile

from config_file import ConfigFile

config = ConfigFile("~/.config/test/config.ini")

Output your config file as a string

>>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 0\nquarter_hours_passed = 0.25\n\n'

Retrieve values or sections

A section.key format is used for retrieving and setting values.

# Values from the config file are automatically parsed
>>> False

# Unless you don't want them to be parsed
config.get("calendar.start_week_on_sunday", parse_type=False)
>>> 'false'

>>> {'today': 'monday', 'start_week_on_sunday': False, 'today_index': 0, 'quarter_hours_passed': 0.25}

Set values

config.set("calendar.today_index", 20)
>>> True
>>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 20\nquarter_hours_passed = 0.25\n\n'

# If you specify a section that isn't in your config file, the section and the key are added for you.
config.set("week.tuesday_index", 2)
>>> True
>>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 20\nquarter_hours_passed = 0.25\n\n[week]\ntuesday_index = 2\n\n'

Delete sections or key/value pairs.

>>> True
>>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 20\nquarter_hours_passed = 0.25\n\n'

>>> True
>>> '[calendar]\nstart_week_on_sunday = false\ntoday_index = 20\nquarter_hours_passed = 0.25\n\n'

Check whether you have a particular section or key

>>> True

>>> False

>>> True

Save when you're done

Write the contents of the file back out. set() and delete() both modify the contents of the file and require a call to save() to write those changes back out.
>>> True

Restore the file back to its original

The current configuration file would be deleted and replaced by a copy of the original. By default, since our passed in config file was at path ~/.config/test/config.ini, restore_original() will look for ~/.config/test/config.original.ini.

>>> True

# But you can also specify the original config file explicitly.
>>> True

Using your own parser

You can still use config file, even if you don't use one of our supported configuration formats. The ConfigFile object swaps in the parser it needs based on the file format. However, the constructor takes in an optional parser argument that you can use to supply your own custom parser. The only requirement is that the parser must be a concrete implementation of BaseParser.

Project details

Download files

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

Files for config-file, version 0.7.0
Filename, size File type Python version Upload date Hashes
Filename, size config_file-0.7.0-py3-none-any.whl (9.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size config_file-0.7.0.tar.gz (9.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page