Beispiel #1
0
 def test_import_file(self, mock_file_csv):
     """Verify an extension is parsed from the import path."""
     mock_path = 'path/to/file.csv'
     mock_document = Mock()
     importer.FORMAT_FILE['.csv'] = mock_file_csv
     importer.import_file(mock_path, mock_document)
     mock_file_csv.assert_called_once_with(mock_path, mock_document, mapping=None)
Beispiel #2
0
def run_import(args, cwd, error, catch=True, _tree=None):
    """Process arguments and run the `doorstop import` 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`

    """
    document = item = None
    attrs = utilities.literal_eval(args.attrs, error)
    mapping = utilities.literal_eval(args.map, error)
    if args.path:
        if not args.prefix:
            error("when [path] specified, [prefix] is also required")
        elif args.document:
            error("'--document' cannot be used with [path] [prefix]")
        elif args.item:
            error("'--item' cannot be used with [path] [prefix]")
        ext = utilities.get_ext(args, error, None, None)
    elif not (args.document or args.item):
        error("specify [path], '--document', or '--item' to import")

    with utilities.capture(catch=catch) as success:

        if args.path:

            # get the document
            request_next_number = _request_next_number(args)
            tree = _tree or _get_tree(args, cwd,
                                      request_next_number=request_next_number)
            document = tree.find_document(args.prefix)

            # import items into it
            msg = "importing '{}' into document {}...".format(args.path,
                                                              document)
            utilities.show(msg, flush=True)
            importer.import_file(args.path, document, ext, mapping=mapping)

        elif args.document:
            prefix, path = args.document
            document = importer.create_document(prefix, path,
                                                parent=args.parent)
        elif args.item:
            prefix, uid = args.item
            request_next_number = _request_next_number(args)
            item = importer.add_item(prefix, uid, attrs=attrs,
                                     request_next_number=request_next_number)
    if not success:
        return False

    if document:
        utilities.show("imported document: {} ({})".format(document.prefix,
                                                           document.relpath))
    else:
        assert item
        utilities.show("imported item: {} ({})".format(item.uid, item.relpath))

    return True
Beispiel #3
0
 def test_import_file(self, mock_file_csv):
     """Verify an extension is parsed from the import path."""
     mock_path = 'path/to/file.csv'
     mock_document = Mock()
     importer.FORMAT_FILE['.csv'] = mock_file_csv
     importer.import_file(mock_path, mock_document)
     mock_file_csv.assert_called_once_with(mock_path, mock_document,
                                           mapping=None)
Beispiel #4
0
 def test_import_file_custom_ext(self, mock_check):
     """Verify a custom extension can be specified for import."""
     mock_path = 'path/to/file.ext'
     mock_document = Mock()
     importer.import_file(mock_path, mock_document, ext='.custom')
     mock_check.assert_called_once_with('.custom')
Beispiel #5
0
def run_import(args, cwd, error, catch=True, _tree=None):
    """Process arguments and run the `doorstop import` 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`

    """
    document = item = None
    attrs = utilities.literal_eval(args.attrs, error)
    mapping = utilities.literal_eval(args.map, error)
    if args.path:
        if not args.prefix:
            error("when [path] specified, [prefix] is also required")
        elif args.document:
            error("'--document' cannot be used with [path] [prefix]")
        elif args.item:
            error("'--item' cannot be used with [path] [prefix]")
        ext = utilities.get_ext(args, error, None, None)
    elif not (args.document or args.item):
        error("specify [path], '--document', or '--item' to import")

    with utilities.capture(catch=catch) as success:

        if args.path:

            # get the document
            request_next_number = _request_next_number(args)
            tree = _tree or _get_tree(
                args, cwd, request_next_number=request_next_number)
            document = tree.find_document(args.prefix)

            # import items into it
            msg = "importing '{}' into document {}...".format(
                args.path, document)
            utilities.show(msg, flush=True)
            importer.import_file(args.path, document, ext, mapping=mapping)

        elif args.document:
            prefix, path = args.document
            document = importer.create_document(prefix,
                                                path,
                                                parent=args.parent)
        elif args.item:
            prefix, uid = args.item
            request_next_number = _request_next_number(args)
            item = importer.add_item(prefix,
                                     uid,
                                     attrs=attrs,
                                     request_next_number=request_next_number)
    if not success:
        return False

    if document:
        utilities.show("imported document: {} ({})".format(
            document.prefix, document.relpath))
    else:
        assert item
        utilities.show("imported item: {} ({})".format(item.uid, item.relpath))

    return True
Beispiel #6
0
 def test_import_file_custom_ext(self, mock_check):
     """Verify a custom extension can be specified for import."""
     mock_path = 'path/to/file.ext'
     mock_document = Mock()
     importer.import_file(mock_path, mock_document, ext='.custom')
     mock_check.assert_called_once_with('.custom')