def run_publish(args, cwd, error, catch=True): """Process arguments and run the `doorstop publish` 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` """ whole_tree = args.prefix == 'all' ext = utilities.get_ext(args, error, '.txt', '.html', whole_tree) # Get the tree or document with utilities.capture(catch=catch) as success: publisher.check(ext) tree = _get_tree(args, cwd, load=whole_tree) if not whole_tree: document = tree.find_document(args.prefix) if not success: return False # Set publishing arguments kwargs = {} if args.width: kwargs['width'] = args.width # Write to output file(s) if args.path: path = os.path.abspath(os.path.join(cwd, args.path)) if whole_tree: msg = "publishing tree to '{}'...".format(path) utilities.show(msg, flush=True) published_path = publisher.publish(tree, path, ext, template=args.template, **kwargs) else: msg = "publishing document {} to '{}'...".format(document, path) utilities.show(msg, flush=True) published_path = publisher.publish(document, path, ext, template=args.template, **kwargs) if published_path: utilities.show("published: {}".format(published_path)) # Or, display to standard output else: if whole_tree: error("only single documents can be displayed") for line in publisher.publish_lines(document, ext, **kwargs): utilities.show(line) return True
def publishtestdoc(self, tree, items, selecteduid): path = tree.root pathtodoc = Path(path, "tests-for-" + selecteduid) activedict = {} textdict = {} for doc in tree.documents: for item in doc.items: activedict[str(item)] = item.active textdict[str(item)] = item.text product = self.db.find(self.currentuid) for doc in tree.documents: for item in doc.items: if item in items: newitemtext = item.text item.active = True if self.INPUTVARIABLES in product.data: for inputvar in product.data[self.INPUTVARIABLES]: varname = inputvar[0] try: varvalue = inputvar[1] except IndexError: varvalue = '' newitemtext = re.sub(r"\b%s\b" % varname, varvalue, newitemtext) newitemtext = re.sub( '', '', newitemtext ) # Seems like the text can't be saved without this if self.EXPECTEDRESULTS in product.data: for pair in product.data[self.EXPECTEDRESULTS]: if str(pair[0]) == str(item.uid): newitemtext = newitemtext + '\n\n Expected results: \n\n' + pair[ 1] item.text = newitemtext item.save() else: item.active = False ''' Says in the doorstop documentation that the publish method can take a list of items. Didn't manage to get it to work because at a point it gets to the method 'iter_documents' which returns it wrong. The workaround is to add text to all chosen items and set the other to inactive. //Samuel ''' publisher.publish(tree, pathtodoc, extensions=EXTENSIONS) for doc in tree.documents: for item in sorted(i for i in doc._iter()): if item.active: item._data['text'] = textdict[str(item)] item.save() if str(item) in activedict: item.active = activedict[str(item)]
def run_publish(args, cwd, error, catch=True): """Process arguments and run the `doorstop publish` 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, '.txt', '.html', whole_tree, error) # Publish documents with utilities.capture(catch=catch) as success: publisher.check(ext) tree = build(cwd=cwd, root=args.project) if not whole_tree: document = tree.find_document(args.prefix) if not success: return False # Set publishing arguments kwargs = {} if args.width: kwargs['width'] = args.width # Write to output file(s) if args.path: if whole_tree: show("publishing tree to '{}'...".format(args.path), flush=True) path = publisher.publish(tree, args.path, ext, **kwargs) else: msg = "publishing document {} to '{}'...".format(document, args.path) show(msg, flush=True) path = publisher.publish(document, args.path, ext, **kwargs) if path: show("published: {}".format(path)) # Display to standard output else: if whole_tree: error("only single documents can be displayed") for line in publisher.publish_lines(document, ext, **kwargs): show(line) return True
def test_publish_tree_no_documents(self, mock_index): """Verify a tree can be published with no documents.""" dirpath = os.path.join('mock', 'directory') mock_tree = MagicMock() mock_tree.documents = [] # Act path2 = publisher.publish(mock_tree, dirpath, index=False) # Assert self.assertIs(None, path2) self.assertEqual(0, mock_index.call_count)
def test_publish_tree_no_index(self, mock_open, mock_makedirs, mock_index): """Verify a tree can be published.""" dirpath = os.path.join('mock', 'directory') # Act dirpath2 = publisher.publish(self.mock_tree, dirpath, index=False) # Assert self.assertIs(dirpath, dirpath2) self.assertEqual(1, mock_makedirs.call_count) self.assertEqual(2, mock_open.call_count) self.assertEqual(0, mock_index.call_count)
def test_publish_tree(self, mock_open, mock_makedirs, mock_index): """Verify a tree can be published.""" dirpath = os.path.join('mock', 'directory') # Act dirpath2 = publisher.publish(self.mock_tree, dirpath) # Assert self.assertIs(dirpath, dirpath2) self.assertEqual(1, mock_makedirs.call_count) self.assertEqual(2, mock_open.call_count) mock_index.assert_called_once_with(dirpath, tree=self.mock_tree)
def test_publish_document(self, mock_open, mock_makedirs): """Verify a document can be published.""" dirpath = os.path.join('mock', 'directory') path = os.path.join(dirpath, 'published.html') self.document.items = [] # Act path2 = publisher.publish(self.document, path) # Assert self.assertIs(path, path2) mock_makedirs.assert_called_once_with(os.path.join(dirpath, Document.ASSETS)) mock_open.assert_called_once_with(path, 'wb')
def test_publish_tree(self, mock_open, mock_index, mock_makedirs): """Verify a tree can be published.""" dirpath = os.path.join('mock', 'directory') mock_open.side_effect = lambda *args, **kw: mock.mock_open(read_data="$body").return_value expected_calls = [call(os.path.join('mock', 'directory', 'MOCK.html'), 'wb')] # Act dirpath2 = publisher.publish(self.mock_tree, dirpath) # Assert self.assertIs(dirpath, dirpath2) self.assertEqual(expected_calls, mock_open.call_args_list) mock_index.assert_called_once_with(dirpath, tree=self.mock_tree)
def test_publish_document(self, mock_open, mock_makedirs): """Verify a document can be published.""" dirpath = os.path.join('mock', 'directory') path = os.path.join(dirpath, 'published.html') self.document.items = [] # Act path2 = publisher.publish(self.document, path) # Assert self.assertIs(path, path2) mock_makedirs.assert_called_once_with(dirpath) mock_open.assert_called_once_with(path, 'wb')
def test_publish_document_html(self, mock_lines, mock_open, mock_makedirs): """Verify a (mock) HTML file can be created.""" dirpath = os.path.join('mock', 'directory') path = os.path.join(dirpath, 'published.custom') # Act path2 = publisher.publish(self.document, path, '.html') # Assert self.assertIs(path, path2) mock_makedirs.assert_called_once_with(dirpath) mock_open.assert_called_once_with(path, 'wb') mock_lines.assert_called_once_with(self.document, '.html', linkify=False)
def test_publish_document_copies_assets(self, mock_open, mock_makedirs, mock_copyassets): """Verify that assets are published""" dirpath = os.path.join('mock', 'directory') assets_path = os.path.join(dirpath, 'assets') path = os.path.join(dirpath, 'published.custom') document = MockDocument('/some/path') mock_open.side_effect = lambda *args, **kw: mock.mock_open(read_data="$body").return_value # Act path2 = publisher.publish(document, path, '.html') # Assert self.assertIs(path, path2) mock_makedirs.assert_called_once_with(os.path.join(dirpath, Document.ASSETS)) mock_copyassets.assert_called_once_with(assets_path)
def test_publish_tree(self, mock_open, mock_index, mock_makedirs): """Verify a tree can be published.""" dirpath = os.path.join('mock', 'directory') mock_open.side_effect = lambda *args, **kw: mock.mock_open( read_data="$body").return_value expected_calls = [ call(os.path.join('mock', 'directory', 'MOCK.html'), 'wb') ] # Act dirpath2 = publisher.publish(self.mock_tree, dirpath) # Assert self.assertIs(dirpath, dirpath2) self.assertEqual(expected_calls, mock_open.call_args_list) mock_index.assert_called_once_with(dirpath, tree=self.mock_tree)
def test_publish_document_copies_assets(self, mock_open, mock_makedirs, mock_copyassets): """Verify that assets are published""" dirpath = os.path.join('mock', 'directory') assets_path = os.path.join(dirpath, 'assets') path = os.path.join(dirpath, 'published.custom') document = MockDocument('/some/path') mock_open.side_effect = lambda *args, **kw: mock.mock_open( read_data="$body").return_value # Act path2 = publisher.publish(document, path, '.html') # Assert self.assertIs(path, path2) mock_makedirs.assert_called_once_with( os.path.join(dirpath, Document.ASSETS)) mock_copyassets.assert_called_once_with(assets_path)
def test_publish_document_deletes_the_contents_of_assets_folder(self, mock_lines, mock_open, mock_glob, mock_rm): """Verify that the contents of an assets directory next to the published file is deleted""" dirpath = os.path.abspath(os.path.join('mock', 'directory')) path = os.path.join(dirpath, 'published.custom') assets = [os.path.join(dirpath, Document.ASSETS, dir) for dir in ['css', 'logo.png']] mock_glob.return_value = assets # Act path2 = publisher.publish(self.document, path, '.html') # Assert self.assertIs(path, path2) mock_open.assert_called_once_with(path, 'wb') mock_lines.assert_called_once_with(self.document, '.html', template=publisher.HTMLTEMPLATE, toc=True, linkify=False) calls = [call(assets[0]), call(assets[1])] self.assertEqual(calls, mock_rm.call_args_list)
def test_publish_document_deletes_the_contents_of_assets_folder( self, mock_lines, mock_open, mock_glob, mock_rm): """Verify that the contents of an assets directory next to the published file is deleted""" dirpath = os.path.abspath(os.path.join('mock', 'directory')) path = os.path.join(dirpath, 'published.custom') assets = [ os.path.join(dirpath, Document.ASSETS, dir) for dir in ['css', 'logo.png'] ] mock_glob.return_value = assets # Act path2 = publisher.publish(self.document, path, '.html') # Assert self.assertIs(path, path2) mock_open.assert_called_once_with(path, 'wb') mock_lines.assert_called_once_with(self.document, '.html', template=publisher.HTMLTEMPLATE, toc=True, linkify=False) calls = [call(assets[0]), call(assets[1])] self.assertEqual(calls, mock_rm.call_args_list)
def publishdocs(self): publisher.publish(self.db.root, Path(self.db.root.root, "public"))