Exemplo n.º 1
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
Exemplo n.º 2
0
 def test_literal_eval_invalid_err(self):
     """Verify an invalid literal calls the error function."""
     error = Mock()
     utilities.literal_eval("1/", error=error)
     self.assertEqual(1, error.call_count)
Exemplo n.º 3
0
 def test_literal_eval_invalid_log(self, mock_log):
     """Verify an invalid literal logs an error."""
     utilities.literal_eval("1/")
     self.assertEqual(1, mock_log.call_count)
Exemplo n.º 4
0
 def test_literal_eval(self):
     """Verify a string can be evaluated as a Python literal."""
     self.assertEqual(42.0, utilities.literal_eval("42.0"))
Exemplo n.º 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
Exemplo n.º 6
0
 def test_literal_eval_invalid_log(self, mock_log):
     """Verify an invalid literal logs an error."""
     utilities.literal_eval("1/")
     self.assertEqual(1, mock_log.call_count)
Exemplo n.º 7
0
 def test_literal_eval_invalid_err(self):
     """Verify an invalid literal calls the error function."""
     error = Mock()
     utilities.literal_eval("1/", error=error)
     self.assertEqual(1, error.call_count)
Exemplo n.º 8
0
 def test_literal_eval(self):
     """Verify a string can be evaluated as a Python literal."""
     self.assertEqual(42.0, utilities.literal_eval("42.0"))