Erik Grinaker is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againRevelation / src / lib / datahandler / netrc.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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #
# Revelation - a password manager for GNOME 2
# http://oss.codepoet.no/revelation/
# $Id$
#
# Module for handling .netrc files
#
#
# Copyright (c) 2003-2006 Erik Grinaker
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
import base
from revelation import data, entry
import shlex, StringIO, time
class NetRC(base.DataHandler):
"Data handler for .netrc data"
name = "netrc"
importer = True
exporter = True
encryption = False
def export_data(self, entrystore, password = None):
"Converts data from an entrystore to netrc data"
netrc = ""
iter = entrystore.iter_nth_child(None, 0)
while iter is not None:
e = entrystore.get_entry(iter)
try:
if "" in ( e[entry.HostnameField], e[entry.UsernameField], e[entry.PasswordField] ):
raise ValueError
if e.name != "":
netrc += "# %s\n" % e.name
if e.description != "":
netrc += "# %s\n" % e.description
netrc += "machine %s\n" % e[entry.HostnameField]
netrc += " login %s\n" % e[entry.UsernameField]
netrc += " password %s\n" % e[entry.PasswordField]
netrc += "\n"
except ( entry.EntryFieldError, ValueError ):
pass
iter = entrystore.iter_traverse_next(iter)
return netrc
def import_data(self, netrc, password = None):
"Imports data from a netrc stream to an entrystore"
entrystore = data.EntryStore()
# set up a lexical parser
datafp = StringIO.StringIO(netrc)
lexer = shlex.shlex(datafp)
lexer.wordchars += r"!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
while 1:
# look for a machine, default or macdef top-level keyword
tt = lexer.get_token()
if not tt:
break
elif tt == "machine":
name = lexer.get_token()
elif tt == "default":
name = "default"
# skip macdef entries
elif tt == "macdef":
lexer.whitespace = ' \t'
while 1:
line = lexer.instream.readline()
if not line or line == '\012':
lexer.whitespace = ' \t\r\n'
break
continue
else:
raise base.FormatError
# we're looking at an entry, so fetch data
e = entry.GenericEntry()
e.name = name
e.updated = time.time()
if name != "default":
e[entry.HostnameField] = name
while 1:
tt = lexer.get_token()
# if we find a new entry, break out of current field-collecting loop
if tt == "" or tt == "machine" or tt == "default" or tt == "macdef":
entrystore.add_entry(e)
lexer.push_token(tt)
break
elif tt == "login" or tt == "user":
e[entry.UsernameField] = lexer.get_token()
elif tt == "account":
lexer.get_token()
elif tt == "password":
e[entry.PasswordField] = lexer.get_token()
else:
raise base.FormatError
datafp.close()
return entrystore
|