importer.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. import ipdb; ipdb.set_trace()
  40. def _read_file(self):
  41. """read the csv file, remove empty lines and the header"""
  42. with open(self.args.import_file) as fh:
  43. csv_f = csv.reader(fh, delimiter=';')
  44. lines = [line for line in csv_f]
  45. lines = list(filter(None, lines))
  46. return lines[1:]
  47. def _create_node(self, row):
  48. """create a node object with encrypted properties"""
  49. n = {'clear_text': True,
  50. 'username': row[0], 'password': row[2], 'url': row[1], '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 runner(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, invoke=CSVImporter):
  79. self.runner = invoke()
  80. def run(self):
  81. self.runner()