Read and write .netrc files in Python.
tinynetrc
uses the netrc
module from the standard library under the hood and adds a few
improvements:
- Adds write functionality.
- Fixes a std lib bug with formatting a .netrc file.*
- Parses .netrc into dictionary values rather than tuples.
*This bug is fixed in newer versions of Python.
pip install tinynetrc
tinynetrc
supports Python >= 2.7 or >= 3.5.
from tinynetrc import Netrc
netrc = Netrc() # parse ~/.netrc
# Get credentials
netrc['api.heroku.com']['login']
netrc['api.heroku.com']['password']
# Modify an existing entry
netrc['api.heroku.com']['password'] = 'newpassword'
netrc.save() # writes to ~/.netrc
# Add a new entry
netrc['surge.surge.sh'] = {
'login': 'sloria1@gmail.com',
'password': 'secret'
}
netrc.save()
# Removing an new entry
del netrc['surge.surge.sh']
netrc.save()
You can also use Netrc
as a context manager, which will automatically save
~/.netrc
.
from tinynetrc import Netrc
with Netrc() as netrc:
netrc['api.heroku.com']['password'] = 'newpassword'
assert netrc.is_dirty is True
# saved!
MIT licensed. See the bundled LICENSE file for more details.