importer.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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): # pragma: no cover
  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. csv_f = csv.reader(fh, delimiter=';')
  45. lines = [line for line in csv_f]
  46. lines = list(filter(None, lines))
  47. return lines[1:]
  48. def _create_node(self, row):
  49. """create a node object with encrypted properties"""
  50. n = {'clear_text': True,
  51. 'username': row[0], 'password': row[2], 'url': row[1],
  52. 'notes': row[3],
  53. 'tags': row[4].split(',')}
  54. node = Node(**n)
  55. return node
  56. def _insert_node(self, node):
  57. "insert the node object to the database"
  58. self._db.add_node(node)
  59. def _open_db(self):
  60. """
  61. open existing db or create a new db
  62. This will expect a CRYPTO table!
  63. Hence if not CRYPTO table one should create it...
  64. """
  65. self._db._open()
  66. self._db._create_tables()
  67. self._db._con.commit()
  68. self._db.open()
  69. def run(self, callback=CLICallback):
  70. enc = CryptoEngine.get()
  71. enc.callback = callback()
  72. self._open_db()
  73. for row in self._read_file():
  74. node = self._create_node(row)
  75. self._insert_node(node)
  76. self._db.close()
  77. class Importer(object):
  78. """
  79. The actual job runner which by default runs a csv importer instance.
  80. This could be changes by calling other instance which for example import
  81. from KeePass XML or other formats.
  82. """
  83. def __init__(self, args, invoke=CSVImporter):
  84. self.importer = invoke(*args)
  85. def run(self): # pragma: no cover
  86. self.importer.run()