"Fossies" - the Fresh Open Source Software Archive

Member "revelation-0.5.4/src/lib/datahandler/splashid.py" (4 Oct 2020, 3659 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 "splashid.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.5.3_vs_0.5.4.

    1 #
    2 # Revelation - a password manager for GNOME 2
    3 # http://oss.codepoet.no/revelation/
    4 # $Id$
    5 #
    6 # Module for importing data from CSV files.
    7 #
    8 # Copyright (c) 2006 Devan Goodwin <dgoodwin@dangerouslyinc.com>
    9 #
   10 # This program is free software; you can redistribute it and/or
   11 # modify it under the terms of the GNU General Public License
   12 # as published by the Free Software Foundation; either version 2
   13 # of the License, or (at your option) any later version.
   14 #
   15 # This program is distributed in the hope that it will be useful,
   16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18 # GNU General Public License for more details.
   19 #
   20 # You should have received a copy of the GNU General Public License
   21 # along with this program; if not, write to the Free Software
   22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   23 #
   24 
   25 import csv, time
   26 
   27 from . import base
   28 from revelation import data, entry
   29 
   30 
   31 class SplashIDCSV(base.DataHandler):
   32     """
   33     Data handler for CSV files generated by SplashID.
   34 
   35     While SplashID CSV files were used as the basis for this parser,
   36     by arranging data in the format similar to what SplashID generates
   37     (i.e. use a spreadsheet and export a CSV with the proper format)
   38     one should be able to import any CSV file.
   39 
   40     SplashID's CSV data looks like the following:
   41 
   42     Type,Field 1,Field 2,Field 3,Field 4,Field 5,Field 6,Notes,Category
   43 
   44     The mapping to a Revelation generic entry takes place as follows:
   45 
   46     Type:       Ignored
   47     Field 1:    Name
   48     Field 2:    Username
   49     Field 3:    Password
   50     Field 4:    Hostname
   51     Field 5:    Added to Description
   52     Field 6:    Added to Description
   53     Notes:      Added to Description
   54     Category:   Folder
   55     """
   56 
   57     name        = "SplashID CSV"
   58     importer    = True
   59     exporter    = False
   60     encryption  = False
   61 
   62     def import_data(self, input, password):
   63         " Import data from a file into the entry store"
   64 
   65         # Replace any vertical tabs with spaces, SplashID seems to use
   66         # these to seperate lines within a Notes field:
   67         if input.count(b'\x0b'):
   68             input = input.replace(b'\x0b', b' ')
   69 
   70         entrystore = data.EntryStore()
   71 
   72         # Maintain a hash of folder names to folder entries so we
   73         # can use each category encountered to create a new folder
   74         # by that name, or use an existing one if we've already
   75         # created it:
   76         folders = {}
   77 
   78         for line in input.splitlines():
   79             for row in csv.reader([line.decode()]):
   80 
   81                 # Raise FormatError if we don't have all 9 fields
   82                 if len(row) != 9:
   83                     raise base.FormatError
   84 
   85                 # Import the entry
   86                 e           = entry.GenericEntry()
   87                 e.name          = row[1]
   88                 e.description       = " / ".join([ desc.strip() for desc in row[5:8] if desc.strip() != "" ])
   89                 e.updated       = time.time()
   90 
   91                 e[entry.UsernameField]  = row[2]
   92                 e[entry.PasswordField]  = row[3]
   93                 e[entry.HostnameField]  = row[4]
   94 
   95                 # Create and/or add to folder based on category:
   96                 if row[8] in folders:
   97                     parent = folders[category]
   98 
   99                 else:
  100                     folder      = entry.FolderEntry()
  101                     folder.name = category
  102                     parent      = entrystore.add_entry(new_folder)
  103                     folders[row[8]] = parent
  104 
  105                 # Add the entry
  106                 entrystore.add_entry(e, parent)
  107 
  108         return entrystore