def test_01(self): """Test_retHtmlFileName.test_01(): retHtmlFileName() - basic functionality.""" self.assertEqual('_85815d8fcaf4100c893e42ecf798ee68.html', HtmlUtils.retHtmlFileName('')) self.assertEqual('foo.lis_a2264fb27d01482d85fb07d540d99d29.html', HtmlUtils.retHtmlFileName('foo.lis')) myPathStr = 'a very long path that goes on and on and on and you think that it will never ever stop spam.lis' myPath = os.path.join(*myPathStr.split()) self.assertEqual('a/very/long/path/that/goes/on/and/on/and/on/and/you/think/that/it/will/never/ever/stop/spam.lis', myPath) self.assertEqual( 'spam.lis_eb53aeb1072ab90b9ba0304c5e2b6fd9.html', HtmlUtils.retHtmlFileName(myPath), )
def test_01(self): """Test_writeFileListTrippleAsTable.test_01(): writeFileListTrippleAsTable() - Single file list""" myFileNameS = [ '0eggs.lis', '1chips.lis', '2beans.lis', ] myFileLinkS = [(f, HtmlUtils.retHtmlFileName(f), 'Link text {:d}'.format(i)) for i, f in enumerate(myFileNameS)] myF = io.StringIO() with XmlWrite.XhtmlStream(myF) as myS: HtmlUtils.writeFileListTrippleAsTable(myS, myFileLinkS, {}, False) # print() # print(myF.getvalue()) # self.maxDiff = None # print(myFileLinkS) self.assertEqual(myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> <table> <tr> <td> <a href="0eggs.lis_4656d29aa36ca0c168a10dd3ffe2c9fa.html">Link text 0</a></td> </tr> <tr> <td> <a href="1chips.lis_6cb5a1758e21dad3dba0cc4a53d91c31.html">Link text 1</a></td> </tr> <tr> <td> <a href="2beans.lis_961d8a2ab0ecf325e12f11c8bdbbf817.html">Link text 2</a></td> </tr> </table> </html> """)
def test_02(self): """Test_writeFileListTrippleAsTable.test_02(): writeFileListTrippleAsTable() - Single directory list""" myFileNameS = [ 'spam/beans.lis', 'spam/chips.lis', 'spam/eggs.lis', ] myFileLinkS = [(f, HtmlUtils.retHtmlFileName(f), 'Link text {:d}'.format(i)) for i, f in enumerate(myFileNameS)] myF = io.StringIO() with XmlWrite.XhtmlStream(myF) as myS: HtmlUtils.writeFileListTrippleAsTable(myS, myFileLinkS, {}, False) # print() # print(myF.getvalue()) # self.maxDiff = None self.assertEqual(myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> <table> <tr> <td rowspan="3">spam/</td> <td> <a href="beans.lis_2cd7aad2a1a03013720d9cc5d83b860c.html">Link text 0</a></td> </tr> <tr> <td> <a href="chips.lis_78c24a9e68057387b3f7c7de7f57385d.html">Link text 1</a></td> </tr> <tr> <td> <a href="eggs.lis_afc193e8de9a2e06454b4bf92d5fefd8.html">Link text 2</a></td> </tr> </table> </html> """)
def test_01(self): """Test_writeFileListTrippleAsTable.test_01(): writeFileListTrippleAsTable() - Single file list""" myFileNameS = [ '0eggs.lis', '1chips.lis', '2beans.lis', ] myFileLinkS = [(f, HtmlUtils.retHtmlFileName(f), 'Link text {:d}'.format(i)) for i, f in enumerate(myFileNameS)] myF = io.StringIO() with XmlWrite.XhtmlStream(myF) as myS: HtmlUtils.writeFileListTrippleAsTable(myS, myFileLinkS, {}, False) # print() # print(myF.getvalue()) # self.maxDiff = None # print(myFileLinkS) self.assertEqual( myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> <table> <tr> <td> <a href="0eggs.lis_55d1c28c70fa11242791f484339eb1be.html">Link text 0</a></td> </tr> <tr> <td> <a href="1chips.lis_9d8f17f73fd3f595dcdc0d6cb43efee2.html">Link text 1</a></td> </tr> <tr> <td> <a href="2beans.lis_c191c76d1f99127750d4f05f0d47e101.html">Link text 2</a></td> </tr> </table> </html> """)
def __init__(self, theItu, theHtmlDir, keepGoing=False, macroRefMap=None, cppCondMap=None, ituToTuLineSet=None): """Takes an input source file and an output directory. theItu - The original source file path (or file like object for the input). theHtmlDir - The output directory for the HTML or a file-like object for the output keepGoing - Bool, if True raise on error. macroRefMap - Map of {identifier : href_text, ...) to link to macro definitions. ituToTuLineSet - Set of integer line numbers which are lines that can be linked to the translation unit representation.""" try: # Assume string or unicode first self._fpIn = theItu self._ituFileObj = open(self._fpIn) except TypeError: self._fpIn = 'Unknown' self._ituFileObj = theItu if isinstance(theHtmlDir, str): if not os.path.exists(theHtmlDir): os.makedirs(theHtmlDir) self._fOut = open(os.path.join(theHtmlDir, HtmlUtils.retHtmlFileName(self._fpIn)), 'w') else: self._fOut = theHtmlDir self._keepGoing = keepGoing # Map of {identifier : href_text, ...) to link to macro definitions. self._macroRefMap = macroRefMap or {} self._cppCondMap = cppCondMap self._ituToTuLineSet = ituToTuLineSet # Start at 0 as this gets incremented before write self._lineNum = 0 self._convert()
def test_01(self): """Test_retHtmlFileName.test_01(): retHtmlFileName() - basic functionality.""" # exp = hashlib.md5(bytes(os.path.abspath(''), 'ascii')).hexdigest() # print(hashlib.md5(bytes(os.path.abspath(''), 'ascii')).hexdigest()) # print(hashlib.md5(bytes(os.path.normpath(''), 'ascii')).hexdigest()) self.assertEqual('_5058f1af8388633f609cadb75a75dc9d.html', HtmlUtils.retHtmlFileName('')) self.assertEqual('foo.lis_7cc7c6edbd4c065f2406358e9211d9eb.html', HtmlUtils.retHtmlFileName('foo.lis')) myPathStr = 'a very long path that goes on and on and on and you think that it will never ever stop spam.lis' myPath = os.path.join(*myPathStr.split()) self.assertEqual( 'a/very/long/path/that/goes/on/and/on/and/on/and/you/think/that/it/will/never/ever/stop/spam.lis', myPath) self.assertEqual( 'spam.lis_899456b1d7f9e35f3f5338b5883ae262.html', HtmlUtils.retHtmlFileName(myPath), )
def __init__(self, theItu, theHtmlDir, keepGoing=False, macroRefMap=None, cppCondMap=None, ituToTuLineSet=None): """Takes an input source file and an output directory. :param theItu: The original source file path (or file like object for the input). :type theItu: ``str`` :param theHtmlDir: The output directory for the HTML or a file-like object for the output. :type theHtmlDir: ``str`` :param keepGoing: If ``True`` keep going as far as possible. :type keepGoing: ``bool`` :param macroRefMap: Map of ``{identifier : href_text, ...)`` to link to macro definitions. :type macroRefMap: ``dict({str : [list([tuple([<class 'str'>, <class 'int'>, str])]), list([tuple([<class 'str'>, int, str])]), list([tuple([str, int, str])])]})`` :param cppCondMap: Conditional compilation map. :type cppCondMap: :py:class:`cpip.core.CppCond.CppCondGraphVisitorConditionalLines` :param ituToTuLineSet: Set of integer line numbers which are lines that can be linked to the translation unit representation. :type ituToTuLineSet: ``NoneType, set([int])`` :returns: ``NoneType`` """ try: # Assume string or unicode first self._fpIn = theItu self._ituFileObj = open(self._fpIn) except TypeError: self._fpIn = 'Unknown' self._ituFileObj = theItu if isinstance(theHtmlDir, str): if not os.path.exists(theHtmlDir): os.makedirs(theHtmlDir) self._fOut = open( os.path.join(theHtmlDir, HtmlUtils.retHtmlFileName(self._fpIn)), 'w') else: self._fOut = theHtmlDir self._keepGoing = keepGoing # Map of {identifier : href_text, ...) to link to macro definitions. self._macroRefMap = macroRefMap or {} self._cppCondMap = cppCondMap self._ituToTuLineSet = ituToTuLineSet # Start at 0 as this gets incremented before write self._lineNum = 0 self._convert()
def test_03(self): """Test_writeFileListAsTable.test_03(): writeFileListAsTable() - Multiple directory list""" myFileNameS = [ 'spam/eggs.lis', 'spam/chips.lis', 'spam/fishfingers/beans.lis', 'spam/fishfingers/peas.lis', ] myFileLinkS = [(f, HtmlUtils.retHtmlFileName(f)) for f in myFileNameS] myF = io.StringIO() with XmlWrite.XhtmlStream(myF) as myS: HtmlUtils.writeFileListAsTable(myS, myFileLinkS, {}, False) # print() # print(myF.getvalue()) # self.maxDiff = None self.assertEqual( myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> <table> <tr> <td rowspan="4">spam/</td> <td colspan="2"> <a href="chips.lis_cf88bd52fd64d0ee6dd7f9f7d465d22b.html">chips.lis</a> </td> </tr> <tr> <td colspan="2"> <a href="eggs.lis_bc8a04943b99ef250322263f7f263272.html">eggs.lis</a> </td> </tr> <tr> <td rowspan="2">fishfingers/</td> <td> <a href="beans.lis_a50e730e7b0104f5a34f3aca74fd03b0.html">beans.lis</a> </td> </tr> <tr> <td> <a href="peas.lis_08179aa1bc47182a36430dafec259742.html">peas.lis</a> </td> </tr> </table> </html> """)
def test_03(self): """Test_writeFileListAsTable.test_03(): writeFileListAsTable() - Multiple directory list""" myFileNameS = [ 'spam/eggs.lis', 'spam/chips.lis', 'spam/fishfingers/beans.lis', 'spam/fishfingers/peas.lis', ] myFileLinkS = [(f, HtmlUtils.retHtmlFileName(f)) for f in myFileNameS] myF = io.StringIO() with XmlWrite.XhtmlStream(myF) as myS: HtmlUtils.writeFileListAsTable(myS, myFileLinkS, {}, False) # print() # print(myF.getvalue()) # self.maxDiff = None self.assertEqual(myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> <table> <tr> <td rowspan="4">spam/</td> <td colspan="2"> <a href="chips.lis_78c24a9e68057387b3f7c7de7f57385d.html">chips.lis</a> </td> </tr> <tr> <td colspan="2"> <a href="eggs.lis_afc193e8de9a2e06454b4bf92d5fefd8.html">eggs.lis</a> </td> </tr> <tr> <td rowspan="2">fishfingers/</td> <td> <a href="beans.lis_8c05f2bb7fd4e4946ceae7a0f0a658f1.html">beans.lis</a> </td> </tr> <tr> <td> <a href="peas.lis_93ee5d4c29eb90269e47d1e20daa4110.html">peas.lis</a> </td> </tr> </table> </html> """)
def test_02(self): """Test_writeFileListAsTable.test_02(): writeFileListAsTable() - Single directory list""" myFileNameS = [ 'spam/eggs.lis', 'spam/chips.lis', 'spam/beans.lis', ] myFileLinkS = [(f, HtmlUtils.retHtmlFileName(f)) for f in myFileNameS] myF = io.StringIO() with XmlWrite.XhtmlStream(myF) as myS: HtmlUtils.writeFileListAsTable(myS, myFileLinkS, {}, False) # print() # print(myF.getvalue()) # self.maxDiff = None self.assertEqual( myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> <table> <tr> <td rowspan="3">spam/</td> <td> <a href="beans.lis_85ae02d1d72c7fa819c834e55cd4b1c2.html">beans.lis</a> </td> </tr> <tr> <td> <a href="chips.lis_cf88bd52fd64d0ee6dd7f9f7d465d22b.html">chips.lis</a> </td> </tr> <tr> <td> <a href="eggs.lis_bc8a04943b99ef250322263f7f263272.html">eggs.lis</a> </td> </tr> </table> </html> """)
def test_01(self): """Test_writeFileListAsTable.test_01(): writeFileListAsTable() - Single file list""" myFileNameS = [ 'eggs.lis', 'chips.lis', 'beans.lis', ] myFileLinkS = [(f, HtmlUtils.retHtmlFileName(f)) for f in myFileNameS] myF = io.StringIO() with XmlWrite.XhtmlStream(myF) as myS: HtmlUtils.writeFileListAsTable(myS, myFileLinkS, {}, False) # print() # print(myF.getvalue()) # self.maxDiff = None self.assertEqual( myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> <table> <tr> <td> <a href="beans.lis_0041e5194bf9f9efa9f26d5a2139a418.html">beans.lis</a> </td> </tr> <tr> <td> <a href="chips.lis_cb7a31031b69325af0e4d341041c4844.html">chips.lis</a> </td> </tr> <tr> <td> <a href="eggs.lis_66b549ff0dd23f61d7ea822aa9549756.html">eggs.lis</a> </td> </tr> </table> </html> """)
def test_01(self): """Test_writeFileListAsTable.test_01(): writeFileListAsTable() - Single file list""" myFileNameS = [ 'eggs.lis', 'chips.lis', 'beans.lis', ] myFileLinkS = [(f, HtmlUtils.retHtmlFileName(f)) for f in myFileNameS] myF = io.StringIO() with XmlWrite.XhtmlStream(myF) as myS: HtmlUtils.writeFileListAsTable(myS, myFileLinkS, {}, False) # print() # print(myF.getvalue()) # self.maxDiff = None self.assertEqual(myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> <table> <tr> <td> <a href="beans.lis_37fe59d50e21a22f770f96832a3316be.html">beans.lis</a> </td> </tr> <tr> <td> <a href="chips.lis_52ff1ebbb6b180864516ab041e20da65.html">chips.lis</a> </td> </tr> <tr> <td> <a href="eggs.lis_1355f6695f856c9448c806cfa562a4c4.html">eggs.lis</a> </td> </tr> </table> </html> """)
def writeTuIndexHtml(theOutDir, theTuPath, theLexer, theFileCountMap, theTokenCntr, hasIncDot, macroHistoryIndexName): with XmlWrite.XhtmlStream( os.path.join(theOutDir, tuIndexFileName(theTuPath)), mustIndent=INDENT_ML, ) as myS: with XmlWrite.Element(myS, 'head'): with XmlWrite.Element( myS, 'link', { 'href': TokenCss.TT_CSS_FILE, 'type': "text/css", 'rel': "stylesheet", }): pass with XmlWrite.Element(myS, 'title'): myS.characters('CPIP Processing of %s' % theTuPath) with XmlWrite.Element(myS, 'body'): with XmlWrite.Element(myS, 'h1'): myS.characters('CPIP Processing of %s' % theTuPath) with XmlWrite.Element(myS, 'p'): myS.characters("""This has links to individual pages about the pre-processing of this file.""") # ## # Translation unit # ## with XmlWrite.Element(myS, 'h2'): myS.characters('1. Source Code') _writeParagraphWithBreaks(myS, SOURCE_CODE_INTRO) with XmlWrite.Element(myS, 'h3'): # 'p'): myS.characters('The ') with XmlWrite.Element( myS, 'a', { 'href': HtmlUtils.retHtmlFileName(theTuPath), }): myS.characters('source file') myS.characters(' and ') with XmlWrite.Element(myS, 'a', { 'href': tuFileName(theTuPath), }): myS.characters('as a translation unit') # ## # Include graph # ## with XmlWrite.Element(myS, 'h2'): myS.characters('2. Include Graphs') _writeParagraphWithBreaks(myS, INCLUDE_GRAPH_INTRO) with XmlWrite.Element(myS, 'h3'): # 'p'): myS.characters('A ') with XmlWrite.Element( myS, 'a', { 'href': includeGraphFileNameSVG(theTuPath), }): myS.characters('visual #include tree in SVG') # If we have successfully written a .dot file then link to it if hasIncDot: myS.characters(', ') with XmlWrite.Element( myS, 'a', { 'href': includeGraphFileNameDotSVG(theTuPath), }): myS.characters('Dot dependency [SVG]') myS.characters(' or ') with XmlWrite.Element( myS, 'a', { 'href': includeGraphFileNameText(theTuPath), }): myS.characters('as Text') # ## # Conditional compilation # ## with XmlWrite.Element(myS, 'h2'): myS.characters('3. Conditional Compilation') _writeParagraphWithBreaks(myS, CONDITIONAL_COMPILATION_INTRO) with XmlWrite.Element(myS, 'h3'): # 'p'): myS.characters('The ') with XmlWrite.Element( myS, 'a', { 'href': includeGraphFileNameCcg(theTuPath), }): myS.characters('conditional compilation graph') # ## # Macro history # ## with XmlWrite.Element(myS, 'h2'): myS.characters('4. Macros') _writeParagraphWithBreaks(myS, MACROS_INTRO) with XmlWrite.Element(myS, 'h3'): myS.characters('The ') with XmlWrite.Element(myS, 'a', { 'href': macroHistoryIndexName, }): myS.characters('Macro Environment') # ## # Write out token counter as a table # ## with XmlWrite.Element(myS, 'h2'): myS.characters('5. Token Count') _writeParagraphWithBreaks(myS, TOKEN_COUNT_INTRO) with XmlWrite.Element(myS, 'table', {'class': "monospace"}): with XmlWrite.Element(myS, 'tr'): with XmlWrite.Element(myS, 'th', {'class': "monospace"}): myS.characters('Token Type') with XmlWrite.Element(myS, 'th', {'class': "monospace"}): myS.characters('Count') myTotal = 0 for tokType, tokCount in theTokenCntr.tokenTypesAndCounts( isAll=True, allPossibleTypes=True): with XmlWrite.Element(myS, 'tr'): with XmlWrite.Element(myS, 'td', {'class': "monospace"}): myS.characters(tokType) with XmlWrite.Element(myS, 'td', {'class': "monospace"}): # <tt> does not preserve space so force it to myStr = '%10d' % tokCount myStr = myStr.replace(' ', ' ') myS.literal(myStr) myTotal += tokCount with XmlWrite.Element(myS, 'tr'): with XmlWrite.Element(myS, 'td', {'class': "monospace"}): with XmlWrite.Element(myS, 'b'): myS.characters('Total:') with XmlWrite.Element(myS, 'td', {'class': "monospace"}): with XmlWrite.Element(myS, 'b'): # <tt> does not preserve space so force it to myStr = '%10d' % myTotal myStr = myStr.replace(' ', ' ') myS.literal(myStr) with XmlWrite.Element(myS, 'br'): pass with XmlWrite.Element(myS, 'h2'): myS.characters('6. Files Included and Count') _writeParagraphWithBreaks(myS, FILES_INCLUDED_INTRO) with XmlWrite.Element(myS, 'p'): myS.characters('Total number of unique files: %d' % len(theFileCountMap)) # TODO: Value count # with XmlWrite.Element(myS, 'p'): # myS.characters('Total files processed: %d' % sum(theFileCountMap.values())) myItuFileS = sorted(theFileCountMap.keys()) # Create a list for the DictTree myFileLinkS = [ ( p, # Value is a tripple (href, basename, count) (HtmlUtils.retHtmlFileName(p), os.path.basename(p), theFileCountMap[p]), ) for p in myItuFileS if p != PpLexer.UNNAMED_FILE_NAME ] HtmlUtils.writeFilePathsAsTable(None, myS, myFileLinkS, 'filetable', _tdCallback) with XmlWrite.Element(myS, 'br'): pass # TODO... with XmlWrite.Element(myS, 'p'): myS.characters('Produced by %s version: %s' % ('CPIPMain', __version__)) # Back link with XmlWrite.Element(myS, 'p'): myS.characters('Back to: ') with XmlWrite.Element(myS, 'a', { 'href': 'index.html', }): myS.characters('Index Page')
def tuIndexFileName(theTu): return 'index_' + HtmlUtils.retHtmlFileName(theTu)