Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3....
File: netrc.py
"""An object-oriented interface to .netrc files."""
[0] Fix | Delete
[1] Fix | Delete
# Module and documentation by Eric S. Raymond, 21 Dec 1998
[2] Fix | Delete
[3] Fix | Delete
import os, shlex, stat
[4] Fix | Delete
[5] Fix | Delete
__all__ = ["netrc", "NetrcParseError"]
[6] Fix | Delete
[7] Fix | Delete
[8] Fix | Delete
class NetrcParseError(Exception):
[9] Fix | Delete
"""Exception raised on syntax errors in the .netrc file."""
[10] Fix | Delete
def __init__(self, msg, filename=None, lineno=None):
[11] Fix | Delete
self.filename = filename
[12] Fix | Delete
self.lineno = lineno
[13] Fix | Delete
self.msg = msg
[14] Fix | Delete
Exception.__init__(self, msg)
[15] Fix | Delete
[16] Fix | Delete
def __str__(self):
[17] Fix | Delete
return "%s (%s, line %s)" % (self.msg, self.filename, self.lineno)
[18] Fix | Delete
[19] Fix | Delete
[20] Fix | Delete
class netrc:
[21] Fix | Delete
def __init__(self, file=None):
[22] Fix | Delete
default_netrc = file is None
[23] Fix | Delete
if file is None:
[24] Fix | Delete
try:
[25] Fix | Delete
file = os.path.join(os.environ['HOME'], ".netrc")
[26] Fix | Delete
except KeyError:
[27] Fix | Delete
raise OSError("Could not find .netrc: $HOME is not set")
[28] Fix | Delete
self.hosts = {}
[29] Fix | Delete
self.macros = {}
[30] Fix | Delete
with open(file) as fp:
[31] Fix | Delete
self._parse(file, fp, default_netrc)
[32] Fix | Delete
[33] Fix | Delete
def _parse(self, file, fp, default_netrc):
[34] Fix | Delete
lexer = shlex.shlex(fp)
[35] Fix | Delete
lexer.wordchars += r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
[36] Fix | Delete
lexer.commenters = lexer.commenters.replace('#', '')
[37] Fix | Delete
while 1:
[38] Fix | Delete
# Look for a machine, default, or macdef top-level keyword
[39] Fix | Delete
saved_lineno = lexer.lineno
[40] Fix | Delete
toplevel = tt = lexer.get_token()
[41] Fix | Delete
if not tt:
[42] Fix | Delete
break
[43] Fix | Delete
elif tt[0] == '#':
[44] Fix | Delete
if lexer.lineno == saved_lineno and len(tt) == 1:
[45] Fix | Delete
lexer.instream.readline()
[46] Fix | Delete
continue
[47] Fix | Delete
elif tt == 'machine':
[48] Fix | Delete
entryname = lexer.get_token()
[49] Fix | Delete
elif tt == 'default':
[50] Fix | Delete
entryname = 'default'
[51] Fix | Delete
elif tt == 'macdef': # Just skip to end of macdefs
[52] Fix | Delete
entryname = lexer.get_token()
[53] Fix | Delete
self.macros[entryname] = []
[54] Fix | Delete
lexer.whitespace = ' \t'
[55] Fix | Delete
while 1:
[56] Fix | Delete
line = lexer.instream.readline()
[57] Fix | Delete
if not line or line == '\012':
[58] Fix | Delete
lexer.whitespace = ' \t\r\n'
[59] Fix | Delete
break
[60] Fix | Delete
self.macros[entryname].append(line)
[61] Fix | Delete
continue
[62] Fix | Delete
else:
[63] Fix | Delete
raise NetrcParseError(
[64] Fix | Delete
"bad toplevel token %r" % tt, file, lexer.lineno)
[65] Fix | Delete
[66] Fix | Delete
# We're looking at start of an entry for a named machine or default.
[67] Fix | Delete
login = ''
[68] Fix | Delete
account = password = None
[69] Fix | Delete
self.hosts[entryname] = {}
[70] Fix | Delete
while 1:
[71] Fix | Delete
tt = lexer.get_token()
[72] Fix | Delete
if (tt.startswith('#') or
[73] Fix | Delete
tt in {'', 'machine', 'default', 'macdef'}):
[74] Fix | Delete
if password:
[75] Fix | Delete
self.hosts[entryname] = (login, account, password)
[76] Fix | Delete
lexer.push_token(tt)
[77] Fix | Delete
break
[78] Fix | Delete
else:
[79] Fix | Delete
raise NetrcParseError(
[80] Fix | Delete
"malformed %s entry %s terminated by %s"
[81] Fix | Delete
% (toplevel, entryname, repr(tt)),
[82] Fix | Delete
file, lexer.lineno)
[83] Fix | Delete
elif tt == 'login' or tt == 'user':
[84] Fix | Delete
login = lexer.get_token()
[85] Fix | Delete
elif tt == 'account':
[86] Fix | Delete
account = lexer.get_token()
[87] Fix | Delete
elif tt == 'password':
[88] Fix | Delete
if os.name == 'posix' and default_netrc:
[89] Fix | Delete
prop = os.fstat(fp.fileno())
[90] Fix | Delete
if prop.st_uid != os.getuid():
[91] Fix | Delete
import pwd
[92] Fix | Delete
try:
[93] Fix | Delete
fowner = pwd.getpwuid(prop.st_uid)[0]
[94] Fix | Delete
except KeyError:
[95] Fix | Delete
fowner = 'uid %s' % prop.st_uid
[96] Fix | Delete
try:
[97] Fix | Delete
user = pwd.getpwuid(os.getuid())[0]
[98] Fix | Delete
except KeyError:
[99] Fix | Delete
user = 'uid %s' % os.getuid()
[100] Fix | Delete
raise NetrcParseError(
[101] Fix | Delete
("~/.netrc file owner (%s) does not match"
[102] Fix | Delete
" current user (%s)") % (fowner, user),
[103] Fix | Delete
file, lexer.lineno)
[104] Fix | Delete
if (prop.st_mode & (stat.S_IRWXG | stat.S_IRWXO)):
[105] Fix | Delete
raise NetrcParseError(
[106] Fix | Delete
"~/.netrc access too permissive: access"
[107] Fix | Delete
" permissions must restrict access to only"
[108] Fix | Delete
" the owner", file, lexer.lineno)
[109] Fix | Delete
password = lexer.get_token()
[110] Fix | Delete
else:
[111] Fix | Delete
raise NetrcParseError("bad follower token %r" % tt,
[112] Fix | Delete
file, lexer.lineno)
[113] Fix | Delete
[114] Fix | Delete
def authenticators(self, host):
[115] Fix | Delete
"""Return a (user, account, password) tuple for given host."""
[116] Fix | Delete
if host in self.hosts:
[117] Fix | Delete
return self.hosts[host]
[118] Fix | Delete
elif 'default' in self.hosts:
[119] Fix | Delete
return self.hosts['default']
[120] Fix | Delete
else:
[121] Fix | Delete
return None
[122] Fix | Delete
[123] Fix | Delete
def __repr__(self):
[124] Fix | Delete
"""Dump the class data in the format of a .netrc file."""
[125] Fix | Delete
rep = ""
[126] Fix | Delete
for host in self.hosts.keys():
[127] Fix | Delete
attrs = self.hosts[host]
[128] Fix | Delete
rep += f"machine {host}\n\tlogin {attrs[0]}\n"
[129] Fix | Delete
if attrs[1]:
[130] Fix | Delete
rep += f"\taccount {attrs[1]}\n"
[131] Fix | Delete
rep += f"\tpassword {attrs[2]}\n"
[132] Fix | Delete
for macro in self.macros.keys():
[133] Fix | Delete
rep += f"macdef {macro}\n"
[134] Fix | Delete
for line in self.macros[macro]:
[135] Fix | Delete
rep += line
[136] Fix | Delete
rep += "\n"
[137] Fix | Delete
return rep
[138] Fix | Delete
[139] Fix | Delete
if __name__ == '__main__':
[140] Fix | Delete
print(netrc())
[141] Fix | Delete
[142] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function