source: mps2ois/trunk/mypasswordsafe_to_oisafe.py @ 26

Last change on this file since 26 was 14, checked in by retracile, 16 years ago

Add copyright notice and license

File size: 3.0 KB
RevLine 
[13]1#!/usr/bin/python
2"""This script will take the plaintext export file from MyPasswordSafe (named
3'mypasswordsafe.txt' in the current directory) and convert it to the CSV import
4format expected by OI Safe (for the Android phone platform) and write it to
5'oisafe.csv'.
[14]6
7Copyright 2009, Eli Carter
8GPLv2 or later
[13]9"""
10
11class PasswordData(object):
12    """Holds all information about the passwords.
13
14    Can load from MyPasswordSafe exports, and save to a form OISafe can import.
15    """
16    def __init__(self):
17        self.master_password = None
18        self.passwords = None
19
20    def load_mps(self, filename):
21        """Expects a plain text export from mypasswordsafe as input
22        That file is kind of a tab-delimitted file, with the master password as
23        the only content of the first line.
24        The fields are:
25        name, user, password, created, modified, accessed, lifetime, uuid
26        notes
27
28        Where the notes field is the content of the second line, with \n indicating newlines.
29        """
30        lines = open(filename).readlines()
31        self.master_password = lines[:-1]
32        self.passwords = []
33        field_names = ['name', 'user', 'password', 'group', 'created',
34                       'modified', 'accessed', 'lifetime', 'uuid']
35        for i in range(1, len(lines), 2):
36            entry = {}
37            fields = lines[i].split('\t')
38            entry['notes'] = lines[i+1].replace('\\n', '\n')
39            for fname, fvalue in zip(field_names, fields):
40                #print fname, fvalue
41                entry[fname] = fvalue
42            self.passwords.append(entry)
43           
44    def save_oisafe(self, filename):
45        """Writes out the password information stored in this object to the
46        named file in a format that OI Safe can import.
47        """
48        out = open(filename, 'w')
49        out.write('"Category","Description","Website","Username","Password",'
50                  '"Notes"\n')
51        for entry in self.passwords:
52            category = csv_escape_quotes(entry['group'].lstrip('/'))
53            description = csv_escape_quotes(entry['name'])
54            website = ''
55            username = csv_escape_quotes(entry['user'])
56            password = csv_escape_quotes(entry['password'])
57            full_notes = entry['notes'].strip('\n') + '\n\n'
58            for extra_field in ['created', 'modified', 'accessed', 'lifetime',
59                                'uuid']:
60                full_notes += '%s: %s\n' % (extra_field, entry[extra_field])
61            notes = csv_escape_quotes(full_notes.rstrip('\n'))
62
63            line = '","'.join([category, description, website, username,
64                               password, notes])
65            out.write('"%s"\n' % line)
66
67
68def csv_escape_quotes(text):
69    """Quote the " characters in the provided text by doubling them.
70    """
71    return text.replace('"', '""')
72
73
74def main():
75    """Converts a MyPasswordSafe export to an OI Safe import.
76    """
77    database = PasswordData()
78    database.load_mps('mypasswordsafe.txt')
79    database.save_oisafe('oisafe.csv')
80
81
82if __name__ == '__main__':
83    main()
84
Note: See TracBrowser for help on using the repository browser.