importer.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. class BaseImporter(object):
  25. """
  26. The base class which defines the action needed to import data
  27. to pwman database
  28. """
  29. def __init__(self):
  30. pass
  31. class CSVImporter(BaseImporter):
  32. """
  33. A reference implementation which imports a CSV to the pwman database
  34. """
  35. def __init__(self, args, config, db):
  36. self.args = args
  37. self.config = config
  38. self._db = db
  39. def _read_file(self):
  40. """read the csv file, remove empty lines and the header"""
  41. with open(self.args.import_file) as fh:
  42. csv_f = csv.reader(fh, delimiter=';')
  43. lines = [line for line in csv_f]
  44. lines = list(filter(None, lines))
  45. return lines[1:]
  46. def _create_node(self, row):
  47. """create a node object with encrypted properties"""
  48. n = {'clear_text': True,
  49. 'username': row[0], 'password': row[2], 'url': row[1],
  50. 'notes': row[3],
  51. 'tags': row[4].split(',')}
  52. node = Node(**n)
  53. return node
  54. def _insert_node(self, node):
  55. "insert the node object to the database"
  56. self._db.add_node(node)
  57. def _open_db(self):
  58. """
  59. open existing db or create a new db
  60. This will expect a CRYPTO table!
  61. Hence if not CRYPTO table one should create it...
  62. """
  63. self._db._open()
  64. self._db._create_tables()
  65. self._db._con.commit()
  66. self._db.open()
  67. def run(self):
  68. self._open_db()
  69. for row in self._read_file():
  70. node = self._create_node(row)
  71. self._insert_node(node)
  72. class Importer(object):
  73. """
  74. The actual job runner which by default runs a csv importer instance.
  75. This could be changes by calling other instance which for example import
  76. from KeePass XML or other formats.
  77. """
  78. def __init__(self, args, invoke=CSVImporter):
  79. self.importer = invoke(*args)
  80. def run(self):
  81. self.importer.run()