-
Notifications
You must be signed in to change notification settings - Fork 7
/
tinynetrc.py
109 lines (85 loc) · 2.91 KB
/
tinynetrc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
"""Read and write .netrc files."""
import netrc
import os
from collections import defaultdict
try:
from collections.abc import MutableMapping
except ImportError:
from collections import MutableMapping
__version__ = '1.3.1'
def dictify_hosts(hosts):
ret = defaultdict(lambda: {
'login': None,
'account': None,
'password': None,
})
for machine, info in hosts.items():
ret[machine] = {
'login': info[0],
'account': info[1],
'password': info[2],
}
return ret
def dedictify_machines(machines):
return {
machine: (info.get('login'), info.get('account'), info.get('password'))
for machine, info
in machines.items()
}
class Netrc(MutableMapping):
def __init__(self, file=None):
if file is None:
file = os.path.join(os.path.expanduser('~'), '.netrc')
self.file = file
self._netrc = netrc.netrc(file)
self.machines = dictify_hosts(self._netrc.hosts)
def authenticators(self, host):
return self._netrc.authenticators(host)
@property
def hosts(self):
return self._netrc.hosts
##### dict-like interface implementation #####
def __getitem__(self, key):
return self.machines[key]
def __setitem__(self, key, value):
self.machines[key] = value
def __delitem__(self, key):
del self.machines[key]
def __iter__(self):
return iter(self.machines)
def __len__(self):
return len(self.machines)
#### end dict-like interface implementation #####
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
if not exc_type and self.is_dirty:
self.save()
def __repr__(self):
return repr(dict(self.machines))
@property
def is_dirty(self):
return self.machines != dictify_hosts(self._netrc.hosts)
# Adapted from https://github.com/python/cpython/blob/master/Lib/netrc.py
# to support Python 2
def format(self):
"""Dump the class data in the format of a .netrc file."""
self._netrc.hosts = dedictify_machines(self.machines)
rep = ""
for host in self._netrc.hosts.keys():
attrs = self._netrc.hosts[host]
rep += "machine {host}\n\tlogin {attrs[0]}\n".format(host=host,
attrs=attrs)
if attrs[1]:
rep += "\taccount {attrs[1]}\n".format(attrs=attrs)
rep += "\tpassword {attrs[2]}\n".format(attrs=attrs)
for macro in self._netrc.macros.keys():
rep += "macdef {macro}\n".format(macro=macro)
for line in self._netrc.macros[macro]:
rep += line
rep += "\n"
return rep
def save(self):
with open(self.file, 'w') as fp:
fp.write(self.format())