Giter VIP home page Giter VIP logo

tinynetrc's Introduction

tinynetrc

pypi badge travis-ci status

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.

Get it now

pip install tinynetrc

tinynetrc supports Python >= 2.7 or >= 3.5.

Usage

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': '[email protected]',
    '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!

License

MIT licensed. See the bundled LICENSE file for more details.

tinynetrc's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar pyup-bot avatar sloria avatar tirkarthi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tinynetrc's Issues

Deprecation warning regarding `collections.abc` in python 3.9

python3.8/site-packages/tinynetrc-1.3.0-py3.8.egg/tinynetrc.py:5: 

DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working 

from collections import MutableMapping, defaultdict

Create .netrc file

There doesn't seem to be any way to create a new .netrc file when none already exists. What's the recommendation?

One user-side workaround is to create an empty .netrc if one doesn't already exist, but that's pretty ugly - we also have to replicate the internal default-file stuff you do in your __init__ method:

# When .netrc exists:
with tinynetrc.Netrc() as netrc:
    netrc[host] = {'login': username, 'password': password}

# When .netrc might not exist:
import os
fname = os.path.join(os.path.expanduser('~'), '.netrc')
with open(fname, 'a'):
    pass

with tinynetrc.Netrc(fname) as netrc:
    netrc[host] = {'login': username, 'password': password}

Create new .netrc file when not present

When .netrc file is not present in the user's home directory, trying to save a new entry triggers FileNotFoundError.

Can this library automatically create one when .netrc is not found?

Preserve comments

Calling Netrc#save will overwrite ~/.netrc and remove comments. It would be great if comments were preserved.

Fail silently

Currently if I use Travis for testing then using this library makes tests fail as it does not gracfully fail if it does not find the .netrc file.

==================================== ERRORS ====================================
___________________ ERROR collecting tests/test_siteship.py ____________________
.tox/py36/lib/python3.6/site-packages/tinynetrc.py:38: in __init__
    file = os.path.join(os.environ['HOME'], ".netrc")
.tox/py36/lib/python3.6/os.py:669: in __getitem__
    raise KeyError(key) from None
E   KeyError: 'HOME'
During handling of the above exception, another exception occurred:
tests/test_siteship.py:11: in <module>
    from siteship import cli
siteship/cli.py:14: in <module>
    netrc = Netrc()
.tox/py36/lib/python3.6/site-packages/tinynetrc.py:40: in __init__
    raise OSError("Could not find .netrc: $HOME is not set")
E   OSError: Could not find .netrc: $HOME is not set

https://travis-ci.org/siteship/siteship-cli

Context manager API?

Using a context manager to manage opening and saving of the netrc might be a more idiomatic API.

with Netrc() as netrc:
    netrc['surge.surge.sh'] = {
        'login': '[email protected]',
        'password': 'secret'
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.