def test_01(self): """TestDictTreeIadd: test_10(): simple +=.""" myDt1 = DictTree.DictTree() myDt1.add(range(2), 'two') myDt1.add(range(3), 'three') myDt2 = DictTree.DictTree() myDt2.add(range(4), 'four') myDt2.add(range(5), 'five') myDt2 += myDt1 self.assertEqual( [ list(range(2)), list(range(3)), list(range(4)), list(range(5)), ], myDt2.keys(), ) self.assertEqual( [ 'two', 'three', 'four', 'five', ], myDt2.values(), ) self.assertEqual(4, len(myDt2)) self.assertTrue('spam' not in myDt2) self.assertFalse('spam' in myDt2) self.assertTrue(range(4) in myDt2) self.assertEqual('four', myDt2.value(range(4))) self.assertEqual(5, myDt2.depth())
def writeHTML(self, theFilePath, theDesc): """Write the index.html table.""" lenCmnPref = os.path.commonprefix([t[0] for t in self._plotS]).rfind( os.sep) + 1 # Put the plot summary data into a DictTree myTree = DictTree.DictTreeHtmlTable() for aPlt in self._plotS: key = (aPlt[0][lenCmnPref:], ) + aPlt[1:3] myTree.add([str(s) for s in key], aPlt[3]) # Write CSS with open( os.path.join(os.path.dirname(theFilePath), self.CSS_FILE_PATH), 'w') as f: f.write(CSS_CONTENT_INDEX) # Write the index with XmlWrite.XhtmlStream(open(theFilePath, 'w')) as myS: with XmlWrite.Element(myS, 'head'): with XmlWrite.Element( myS, 'link', { 'href': self.CSS_FILE_PATH, 'type': "text/css", 'rel': "stylesheet", }): pass with XmlWrite.Element(myS, 'title'): myS.characters('LIS plots in SVG') with XmlWrite.Element(myS, 'h1'): myS.characters('PlotLogPasses: {:s}'.format(theDesc)) with XmlWrite.Element(myS, 'table', {'border': '1'}): self._writeHTMLTh(myS) self._writeIndexTableRows(myS, myTree, theFilePath)
def setUp(self): self._dt = DictTree.DictTreeHtmlTable('list') self.assertEqual([], self._dt.keys()) self.assertEqual([], self._dt.values()) self.assertEqual(0, len(self._dt)) self.assertTrue('spam' not in self._dt) self.assertFalse('spam' in self._dt)
def test_01(self): """TestDictTreeCtor: test_01(): constructor fails.""" try: DictTree.DictTree(valIterable='int') self.fail('DictTree.ExceptionDictTree not raised.') except DictTree.ExceptionDictTree: pass
def setUp(self): self._dt = DictTree.DictTree() self.assertEqual([], self._dt.keys()) self.assertEqual([], self._dt.values()) self.assertEqual(0, len(self._dt)) self.assertTrue('spam' not in self._dt) self.assertFalse('spam' in self._dt) self.assertEqual(0, self._dt.depth())
def test_02(self): """TestDictTreeCtor: test_02(): constructor OK, change valIterable and fail.""" myDt = DictTree.DictTree() myDt._vI = 'int' try: myDt.add(range(1), 'one') self.fail('DictTree.ExceptionDictTree not raised.') except DictTree.ExceptionDictTree: pass
def writeFileListTrippleAsTable(theS, theFileLinkS, tableAttrs, includeKeyTail): """Writes a list of file names as an HTML table looking like a directory structure. theFileLinkS is a list of triples (file_name, href, nav_text).""" #print 'TRACE: theFileLinkS', theFileLinkS myDict = DictTree.DictTreeHtmlTable('list') for f, h, n in theFileLinkS: keyList = pathSplit(f) myDict.add(keyList, (h, n)) #print 'TRACE: myDict.keys():', myDict.keys() #print 'TRACE: myDict.values():', myDict.values() writeDictTreeAsTable(theS, myDict, tableAttrs, includeKeyTail)
def writeFileListAsTable(theS, theFileLinkS, tableAttrs, includeKeyTail): """Writes a list of file names as an HTML table looking like a directory structure. theFileLinkS is a list of pairs (file_path, href). The navigation text in the cell will be the basename of the file_path.""" #myList = [(f, h, os.path.basename(f)) for f, h in theFileLinkS] #writeFileListTrippleAsTable(theS, myList, tableAttrs, includeKeyTail) #print 'TRACE: theFileLinkS', theFileLinkS myDict = DictTree.DictTreeHtmlTable(None) for f, h in theFileLinkS: keyList = pathSplit(f) myDict.add(keyList, (h, os.path.basename(f))) writeDictTreeAsTable(theS, myDict, tableAttrs, includeKeyTail)
def writeFilePathsAsTable(valueType, theS, theKvS, tableStyle, fnTd): """Writes file paths as a table, for example as a directory structure. valueType - The type of the value: None | 'list' | 'set' theKvS - A list of pairs (file_path, value). tableStyle - The style used for the table. fnTd - A callback function that is executed for a <td> element when there is a non-None value. This is called with the following arguments: * theS - The HTML stream. * attrs - A map of attrs that include the rowspan/colspan for the <td> * k - The key as a list of path components. * v - The value given by the caller. """ myDict = DictTree.DictTreeHtmlTable(valueType) for k, v in theKvS: myDict.add(pathSplit(k), v) # Propagate table class attribute with XmlWrite.Element(theS, 'table', {'class': tableStyle}): for anEvent in myDict.genColRowEvents(): if anEvent == myDict.ROW_OPEN: # Write out the '<tr>' element theS.startElement('tr', {}) elif anEvent == myDict.ROW_CLOSE: # Write out the '</tr>' element theS.endElement('tr') else: #print 'TRACE: anEvent', anEvent k, v, r, c = anEvent # Write '<td rowspan="%d" colspan="%d">%s</td>' % (r, c, txt[-1]) myTdAttrs = {'class': tableStyle} if r > 1: myTdAttrs['rowspan'] = "%d" % r if c > 1: myTdAttrs['colspan'] = "%d" % c if v is not None: fnTd(theS, myTdAttrs, k, v) else: with XmlWrite.Element(theS, 'td', myTdAttrs): # Write out part of the file name theS.characters(k[-1])
def _write_top_level_index(path_out: str, index_map: typing.Dict[str, HTMLResult]) -> None: # Create a DictTree from the paths. dict_tree = DictTree.DictTreeHtmlTable(None) for k in index_map: branch = k.split(os.sep) dict_tree.add(branch, index_map[k]) index_file_path = os.path.join(path_out, INDEX_FILE) logging.info(f'_write_top_level_index(): to "{index_file_path}"') with open(index_file_path, 'w') as fout: with XmlWrite.XhtmlStream(fout) as xhtml_stream: with XmlWrite.Element(xhtml_stream, 'head'): with XmlWrite.Element( xhtml_stream, 'meta', { 'charset': "UTF-8", 'name': "viewport", 'content': "width=device-width, initial-scale=1", }): pass with XmlWrite.Element(xhtml_stream, 'title'): xhtml_stream.charactersWithBr(f'RP66V1 Scan of {path_out}') with XmlWrite.Element(xhtml_stream, 'style'): xhtml_stream.literal(CSS_RP66V1_INDEX) with XmlWrite.Element(xhtml_stream, 'body'): with XmlWrite.Element(xhtml_stream, 'h1'): xhtml_stream.charactersWithBr( f'Index of RP66V1 Scan: {path_out}') with XmlWrite.Element(xhtml_stream, 'p'): xhtml_stream.characters('Command:') with XmlWrite.Element(xhtml_stream, 'p'): with XmlWrite.Element(xhtml_stream, 'pre'): xhtml_stream.characters(' '.join(sys.argv)) with XmlWrite.Element(xhtml_stream, 'table', {'class': 'filetable'}): _write_top_level_index_table_body(index_file_path, dict_tree, xhtml_stream)
def test_00(self): """TestDictTreeCtor: test_00(): constructor.""" DictTree.DictTree() self.assertTrue(True)