importer.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. import sys
  24. from pwman.data.nodes import Node
  25. from pwman.util.crypto_engine import CryptoEngine
  26. from pwman.ui.tools import CLICallback
  27. class BaseImporter(object): # pragma: no cover
  28. """
  29. The base class which defines the action needed to import data
  30. to pwman database
  31. """
  32. def __init__(self):
  33. pass
  34. class CSVImporter(BaseImporter):
  35. """
  36. A reference implementation which imports a CSV to the pwman database
  37. """
  38. def __init__(self, args, config, db):
  39. self.args = args
  40. self.config = config
  41. self._db = db
  42. def _read_file(self):
  43. """read the csv file, remove empty lines and the header"""
  44. try:
  45. fh, delim = open(self.args.file_delim[0]), self.args.file_delim[1]
  46. csv_f = csv.reader(fh, delimiter=delim)
  47. except FileNotFoundError:
  48. fh, delim = open(self.args.file_delim[1]), self.args.file_delim[0]
  49. csv_f = csv.reader(fh, delimiter=delim)
  50. lines = [line for line in csv_f]
  51. lines = list(filter(None, lines))
  52. fh.close()
  53. return lines[1:]
  54. def _create_node(self, row):
  55. """create a node object with encrypted properties"""
  56. try:
  57. n = {'clear_text': True,
  58. 'username': row[0], 'password': row[2], 'url': row[1],
  59. 'notes': row[3],
  60. 'tags': row[4].split(',')}
  61. node = Node(**n)
  62. except IndexError as err:
  63. print('{}\nDid you specify the correct delimiter?'.format(err))
  64. sys.exit(1)
  65. return node
  66. def _insert_node(self, node):
  67. "insert the node object to the database"
  68. self._db.add_node(node)
  69. def _open_db(self):
  70. """
  71. open existing db or create a new db
  72. This will expect a CRYPTO table!
  73. Hence if not CRYPTO table one should create it...
  74. """
  75. self._db._open()
  76. self._db._create_tables()
  77. self._db._con.commit()
  78. self._db.open()
  79. def run(self, callback=CLICallback):
  80. enc = CryptoEngine.get()
  81. enc.callback = callback()
  82. self._open_db()
  83. for row in self._read_file():
  84. node = self._create_node(row)
  85. self._insert_node(node)
  86. self._db.close()
  87. class Importer(object):
  88. """
  89. The actual job runner which by default runs a csv importer instance.
  90. This could be changes by calling other instance which for example import
  91. from KeePass XML or other formats.
  92. """
  93. def __init__(self, args, invoke=CSVImporter):
  94. self.importer = invoke(*args)
  95. def run(self): # pragma: no cover
  96. self.importer.run()