importer.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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], 'notes': row[3],
  50. 'tags': row[4].split(',')}
  51. node = Node(**n)
  52. return node
  53. def _insert_node(self, node):
  54. "insert the node object to the database"
  55. self._db.add_node(node)
  56. def _open_db(self):
  57. """
  58. open existing db or create a new db
  59. This will expect a CRYPTO table!
  60. Hence if not CRYPTO table one should create it...
  61. """
  62. self._db._open()
  63. self._db._create_tables()
  64. self._db._con.commit()
  65. self._db.open()
  66. def runner(self):
  67. self._open_db()
  68. for row in self._read_file():
  69. node = self._create_node(row)
  70. self._insert_node(node)
  71. class Importer(object):
  72. """
  73. The actual job runner which by default runs a csv importer instance.
  74. This could be changes by calling other instance which for example import
  75. from KeePass XML or other formats.
  76. """
  77. def __init__(self, invoke=CSVImporter):
  78. self.runner = invoke()
  79. def run(self):
  80. self.runner()