"Fossies" - the Fresh Open Source Software Archive 
Member "revelation-0.5.4/src/lib/datahandler/keepassxc.py" (4 Oct 2020, 3596 Bytes) of package /linux/privat/revelation-0.5.4.tar.xz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "keepassxc.py" see the
Fossies "Dox" file reference documentation.
1 #
2 # Revelation
3 # Module for importing data from KeepassXC CSV export file
4 #
5 # Copyright (c) 2020 Mikel Olasagasti Uranga <mikel@olasagasti.info>
6 # Copyright (c) 2006 Devan Goodwin <dgoodwin@dangerouslyinc.com>
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #
22
23 import csv
24 import time
25
26 from revelation import data, entry
27 from . import base
28
29
30 class KeepassXCCSV(base.DataHandler):
31 """
32 Data handler for CSV files generated by KeepassXC.
33
34 CSV format is defined in KeepassXC's CsvExporter.cpp
35
36 https://github.com/keepassxreboot/keepassxc/blob/develop/src/format/CsvExporter.cpp
37
38 CSV field description:
39
40 Type: Ignored
41 Field 1: Group
42 Field 2: Title
43 Field 3: Username
44 Field 4: Password
45 Field 5: URL
46 Field 6: Notes
47 # since KeepassXC-2.6.2
48 Field 7: TOTP
49 Field 8: Icon
50 Field 9: Last Modified
51 Field 10: Created
52 """
53
54 name = "KeepassXC CSV"
55 importer = True
56 exporter = False
57 encryption = False
58
59 def import_data(self, input, password):
60 " Import data from a file into the entry store"
61
62 entrystore = data.EntryStore()
63
64 # Maintain a hash of folder names to folder entries so we
65 # can use each category encountered to create a new folder
66 # by that name, or use an existing one if we've already
67 # created it:
68 folders = {}
69
70 for line in input.splitlines()[1:]:
71 f_csv = csv.reader([line.decode()])
72 for row in f_csv:
73
74 # Raise FormatError if we don't have all 9 fields
75 # KeepasXC 2.5 to 2.6.1 has 6 fields
76 # KeepasXC 2.6.2 has 10 fields
77 if len(row) != 6 and len(row) != 10:
78 raise base.FormatError
79
80 # If URL is present create WebEntry
81 if row[4]:
82 new_entry = entry.WebEntry()
83 new_entry[entry.URLField] = row[4]
84 else:
85 new_entry = entry.GenericEntry()
86
87 new_entry.name = row[1]
88 new_entry[entry.UsernameField] = row[2]
89 new_entry[entry.PasswordField] = row[3]
90 new_entry.notes = row[5]
91
92 # TODO As Last modified and creationtime are from newer
93 # keepassXC releases, set to current time for now
94 new_entry.updated = time.time()
95
96 # Create and/or add to folder
97 # TODO split folder name and correctly group them
98 if row[0] in folders:
99 parent = folders[row[0]]
100
101 else:
102 folder = entry.FolderEntry()
103 folder.name = row[0]
104 parent = entrystore.add_entry(folder)
105 folders[row[0]] = parent
106
107 # Add the entry
108 entrystore.add_entry(new_entry, parent)
109
110 return entrystore