importer.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2014 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. '''
  20. A module to hold the importer class
  21. '''
  22. import csv
  23. from pwman.data.nodes import Node
  24. from pwman.util.crypto_engine import CryptoEngine
  25. from pwman.ui.tools import CLICallback
  26. class BaseImporter(object):
  27. """
  28. The base class which defines the action needed to import data
  29. to pwman database
  30. """
  31. def __init__(self):
  32. pass
  33. class CSVImporter(BaseImporter):
  34. """
  35. A reference implementation which imports a CSV to the pwman database
  36. """
  37. def __init__(self, args, config, db):
  38. self.args = args
  39. self.config = config
  40. self._db = db
  41. def _read_file(self):
  42. """read the csv file, remove empty lines and the header"""
  43. fh = self.args.import_file
  44. import ipdb; ipdb.set_trace() # XXX BREAKPOINT
  45. csv_f = csv.reader(fh, delimiter=';')
  46. lines = [line for line in csv_f]
  47. lines = list(filter(None, lines))
  48. return lines[1:]
  49. def _create_node(self, row):
  50. """create a node object with encrypted properties"""
  51. n = {'clear_text': True,
  52. 'username': row[0], 'password': row[2], 'url': row[1],
  53. 'notes': row[3],
  54. 'tags': row[4].split(',')}
  55. node = Node(**n)
  56. return node
  57. def _insert_node(self, node):
  58. "insert the node object to the database"
  59. self._db.add_node(node)
  60. def _open_db(self):
  61. """
  62. open existing db or create a new db
  63. This will expect a CRYPTO table!
  64. Hence if not CRYPTO table one should create it...
  65. """
  66. self._db._open()
  67. self._db._create_tables()
  68. self._db._con.commit()
  69. self._db.open()
  70. def run(self):
  71. enc = CryptoEngine.get()
  72. enc.callback = CLICallback()
  73. self._open_db()
  74. for row in self._read_file():
  75. node = self._create_node(row)
  76. self._insert_node(node)
  77. self._db.close()
  78. class Importer(object):
  79. """
  80. The actual job runner which by default runs a csv importer instance.
  81. This could be changes by calling other instance which for example import
  82. from KeePass XML or other formats.
  83. """
  84. def __init__(self, args, invoke=CSVImporter):
  85. self.importer = invoke(*args)
  86. def run(self):
  87. self.importer.run()