Ejemplo n.º 1
0
def run_export(args, cwd, error, catch=True, auto=False, _tree=None):
    """Process arguments and run the `doorstop export` subcommand.

    :param args: Namespace of CLI arguments
    :param cwd: current working directory
    :param error: function to call for CLI errors
    :param catch: catch and log :class:`~doorstop.common.DoorstopError`

    :param auto: include placeholders for new items on import

    """
    whole_tree = args.prefix == 'all'
    ext = utilities.get_ext(args, error, '.yml', '.csv', whole_tree=whole_tree)

    # Get the tree or document
    with utilities.capture(catch=catch) as success:

        exporter.check(ext)
        tree = _tree or _get_tree(args, cwd, load=whole_tree)
        if not whole_tree:
            document = tree.find_document(args.prefix)

    if not success:
        return False

    # Write to output file(s)
    if args.path:
        if whole_tree:
            msg = "exporting tree to '{}'...".format(args.path)
            utilities.show(msg, flush=True)
            path = exporter.export(tree, args.path, ext, auto=auto)
        else:
            msg = "exporting document {} to '{}'...".format(
                document, args.path)
            utilities.show(msg, flush=True)
            path = exporter.export(document, args.path, ext, auto=auto)
        if path:
            utilities.show("exported: {}".format(path))

    # Or, display to standard output
    else:
        if whole_tree:
            error("only single documents can be displayed")
        for line in exporter.export_lines(document, ext):
            utilities.show(line)

    return True
Ejemplo n.º 2
0
def run_export(args, cwd, error, catch=True, auto=False, _tree=None):
    """Process arguments and run the `doorstop export` subcommand.

    :param args: Namespace of CLI arguments
    :param cwd: current working directory
    :param error: function to call for CLI errors
    :param catch: catch and log :class:`~doorstop.common.DoorstopError`

    :param auto: include placeholders for new items on import

    """
    whole_tree = args.prefix == 'all'
    ext = utilities.get_ext(args, error, '.yml', '.csv', whole_tree=whole_tree)

    # Get the tree or document
    with utilities.capture(catch=catch) as success:

        exporter.check(ext)
        tree = _tree or _get_tree(args, cwd, load=whole_tree)
        if not whole_tree:
            document = tree.find_document(args.prefix)

    if not success:
        return False

    # Write to output file(s)
    if args.path:
        if whole_tree:
            msg = "exporting tree to '{}'...".format(args.path)
            utilities.show(msg, flush=True)
            path = exporter.export(tree, args.path, ext, auto=auto)
        else:
            msg = "exporting document {} to '{}'...".format(document,
                                                            args.path)
            utilities.show(msg, flush=True)
            path = exporter.export(document, args.path, ext, auto=auto)
        if path:
            utilities.show("exported: {}".format(path))

    # Or, display to standard output
    else:
        if whole_tree:
            error("only single documents can be displayed")
        for line in exporter.export_lines(document, ext):
            utilities.show(line)

    return True
Ejemplo n.º 3
0
 def test_export_tree(self, mock_open, mock_makedirs):
     """Verify a tree can be exported."""
     dirpath = os.path.join('mock', 'directory')
     # Act
     dirpath2 = exporter.export(self.mock_tree, dirpath)
     # Assert
     self.assertIs(dirpath, dirpath2)
     self.assertEqual(1, mock_makedirs.call_count)
     self.assertEqual(1, mock_open.call_count)
Ejemplo n.º 4
0
 def test_export_tree(self, mock_open, mock_makedirs):
     """Verify a tree can be exported."""
     dirpath = os.path.join('mock', 'directory')
     # Act
     dirpath2 = exporter.export(self.mock_tree, dirpath)
     # Assert
     self.assertIs(dirpath, dirpath2)
     self.assertEqual(1, mock_makedirs.call_count)
     self.assertEqual(1, mock_open.call_count)
Ejemplo n.º 5
0
def run_export(args, cwd, error, catch=True):
    """Process arguments and run the `doorstop export` subcommand.

    :param args: Namespace of CLI arguments
    :param cwd: current working directory
    :param error: function to call for CLI errors
    :param catch: catch and log :class:`~doorstop.common.DoorstopError`

    """
    # Parse arguments
    whole_tree = args.prefix == 'all'
    ext = utilities.get_ext(args, '.yml', '.csv', whole_tree, error)

    # Export documents
    with utilities.capture(catch=catch) as success:
        exporter.check(ext)
        tree = build(cwd=cwd, root=args.project)
        if not whole_tree:
            document = tree.find_document(args.prefix)
    if not success:
        return False

    # Write to output file(s)
    if args.path:
        if whole_tree:
            show("exporting tree to '{}'...".format(args.path), flush=True)
            path = exporter.export(tree, args.path, ext)
        else:
            msg = "exporting document {} to '{}'...".format(document,
                                                            args.path)
            show(msg, flush=True)
            path = exporter.export(document, args.path, ext)
        if path:
            show("exported: {}".format(path))

    # Display to standard output
    else:
        if whole_tree:
            error("only single documents can be displayed")
        for line in exporter.export_lines(document, ext):
            show(line)

    return True
Ejemplo n.º 6
0
 def test_export_document_lines(self, mock_write_lines, mock_makedirs):
     """Verify a document can be exported (lines to file)."""
     dirpath = os.path.join('mock', 'directory')
     path = os.path.join(dirpath, 'exported.custom')
     # Act
     path2 = exporter.export(self.document, path, ext='.yml')
     # Assert
     self.assertIs(path, path2)
     mock_makedirs.assert_called_once_with(dirpath)
     mock_write_lines.assert_called_once()
Ejemplo n.º 7
0
 def test_export_document(self, mock_export_file, mock_makedirs):
     """Verify a document can be exported."""
     dirpath = os.path.join('mock', 'directory')
     path = os.path.join(dirpath, 'exported.xlsx')
     # Act
     path2 = exporter.export(self.document, path)
     # Assert
     self.assertIs(path, path2)
     mock_makedirs.assert_called_once_with(dirpath)
     mock_export_file.assert_called_once_with(self.document, path, '.xlsx')
Ejemplo n.º 8
0
 def test_export_document_lines(self, mock_write_lines, mock_makedirs):
     """Verify a document can be exported (lines to file)."""
     dirpath = os.path.join('mock', 'directory')
     path = os.path.join(dirpath, 'exported.custom')
     # Act
     path2 = exporter.export(self.document, path, ext='.yml')
     # Assert
     self.assertIs(path, path2)
     mock_makedirs.assert_called_once_with(dirpath)
     self.assertEqual(1, mock_write_lines.call_count)
Ejemplo n.º 9
0
 def test_export_document(self, mock_export_file, mock_makedirs):
     """Verify a document can be exported."""
     dirpath = os.path.join('mock', 'directory')
     path = os.path.join(dirpath, 'exported.xlsx')
     # Act
     path2 = exporter.export(self.document, path)
     # Assert
     self.assertIs(path, path2)
     mock_makedirs.assert_called_once_with(dirpath)
     mock_export_file.assert_called_once_with(self.document, path, '.xlsx')
Ejemplo n.º 10
0
 def test_export_tree_no_documents(self, mock_open, mock_makedirs):
     """Verify a tree can be exported."""
     dirpath = os.path.join('mock', 'directory')
     mock_tree = MagicMock()
     mock_tree.documents = []
     # Act
     dirpath2 = exporter.export(mock_tree, dirpath)
     # Assert
     self.assertIs(None, dirpath2)
     self.assertEqual(0, mock_makedirs.call_count)
     self.assertEqual(0, mock_open.call_count)
Ejemplo n.º 11
0
 def test_export_tree_no_documents(self, mock_open, mock_makedirs):
     """Verify a tree can be exported."""
     dirpath = os.path.join('mock', 'directory')
     mock_tree = MagicMock()
     mock_tree.documents = []
     # Act
     dirpath2 = exporter.export(mock_tree, dirpath)
     # Assert
     self.assertIs(None, dirpath2)
     self.assertEqual(0, mock_makedirs.call_count)
     self.assertEqual(0, mock_open.call_count)