def _export_import(args, cwd, error, document, ext): """Edit a document by calling export followed by import. :param args: Namespace of CLI arguments :param cwd: current working directory :param error: function to call for CLI errors :param document: :class:`~doorstop.core.document.Document` to edit :param ext: extension for export format """ # Export the document to file args.prefix = document.prefix path = "{}-{}{}".format(args.prefix, int(time.time()), ext) args.path = path get('export')(args, cwd, error, catch=False, auto=True, _tree=document.tree) # Open the exported file editor.edit(path, tool=args.tool) # Import the file to the same document if utilities.ask("import from '{}'?".format(path)): args.attrs = {} args.map = {} get('import')(args, cwd, error, catch=False, _tree=document.tree) common.delete(path) else: utilities.show("import canceled") if utilities.ask("delete '{}'?".format(path)): common.delete(path) else: msg = "to manually import: doorstop import {0}".format(path) utilities.show(msg)
def run_reorder(args, cwd, error, catch=True, _tree=None): """Process arguments and run the `doorstop reorder` 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` """ reordered = False with utilities.capture(catch=catch) as success: # get the document tree = _tree or _get_tree(args, cwd) document = tree.find_document(args.prefix) if not success: return False with utilities.capture(catch=catch) as success: # automatically order if args.auto: msg = "reordering document {}...".format(document) utilities.show(msg, flush=True) document.reorder(manual=False) reordered = True # or, reorder from a previously updated index elif document.index: relpath = os.path.relpath(document.index, cwd) if utilities.ask("reorder from '{}'?".format(relpath)): msg = "reordering document {}...".format(document) utilities.show(msg, flush=True) document.reorder(automatic=not args.manual) reordered = True else: del document.index # or, create a new index to update else: document.index = True # create index relpath = os.path.relpath(document.index, cwd) editor.edit(relpath, tool=args.tool) get('reorder')(args, cwd, error, catch=False, _tree=tree) if not success: msg = "after fixing the error: doorstop reorder {}".format(document) utilities.show(msg) return False if reordered: utilities.show("reordered document: {}".format(document)) return True
def test_ask_bad(self): """Verify a bad response re-prompts.""" with patch('builtins.input', Mock(side_effect=['maybe', 'yes'])): response = utilities.ask("?") self.assertTrue(response)
def test_ask_no(self): """Verify 'no' maps to False.""" with patch('builtins.input', Mock(return_value='no')): response = utilities.ask("?") self.assertFalse(response)
def test_ask_yes(self): """Verify 'yes' maps to True.""" with patch('builtins.input', Mock(return_value='yes')): response = utilities.ask("?") self.assertTrue(response)