def add_item(prefix, uid, attrs=None, document=None, request_next_number=None): """Create a Doorstop document from existing document information. :param prefix: previously imported document's prefix :param uid: existing item's UID :param attrs: dictionary of Doorstop and custom attributes :param document: explicit document to add the item :param request_next_number: server method to get a document's next number :return: imported Item """ if document: # Get an explicit tree tree = document.tree assert tree # tree should be set internally else: # Get an implicit tree and document tree = _get_tree(request_next_number=request_next_number) document = tree.find_document(prefix) # Add an item using the specified UID log.info("importing item '{}'...".format(uid)) item = Item.new(tree, document, document.path, document.root, uid, auto=False) for key, value in (attrs or {}).items(): item.set(key, value) item.save() log.info("imported: {}".format(item)) return item
def create_document(prefix, path, parent=None, tree=None): """Create a Doorstop document from existing document information. :param prefix: existing document's prefix (for new items) :param path: new directory path to store this document's items :param parent: parent document's prefix (if one will exist) :param tree: explicit tree to add the document :return: imported Document """ if not tree: tree = _get_tree() # Attempt to create a document with the given parent log.info("importing document '{}'...".format(prefix)) try: document = tree.create_document(path, prefix, parent=parent) except DoorstopError as exc: if not parent: raise exc from None # Create the document despite an unavailable parent document = Document.new(tree, path, tree.root, prefix, parent=parent) log.warning(exc) _documents.append(document) # TODO: attempt to place unplaced documents? log.info("imported: {}".format(document)) return document
def test_import_xlsx(self): """Verify items can be imported from an XLSX file.""" path = os.path.join(self.temp, 'exported.xlsx') core.exporter.export(self.document, path) _path = os.path.join(self.temp, 'imports', 'req') _tree = _get_tree() document = _tree.create_document(_path, 'REQ') # Act core.importer.import_file(path, document) # Assert expected = [item.data for item in self.document.items] actual = [item.data for item in document.items] log_data(expected, actual) self.assertListEqual(expected, actual)
def test_import_xlsx_huge(self): """Verify huge XLSX files are handled.""" path = os.path.join(FILES, 'exported-huge.xlsx') _path = os.path.join(self.temp, 'imports', 'req') _tree = _get_tree() document = _tree.create_document(_path, 'REQ') # Act with warnings.catch_warnings(record=True) as warns: core.importer.import_file(path, document) # Assert self.assertEqual(1, len(warns)) self.assertIn("maximum number of rows", str(warns[-1].message)) expected = [] actual = [item.data for item in document.items] log_data(expected, actual) self.assertListEqual(expected, actual)