def testWrite(self): """ This method tests :meth:`foundations.io.File.write` method. """ ioFile = File(tempfile.mkstemp()[1]) self.assertIsInstance(ioFile.content, list) ioFile.content = FILE_CONTENT writeSuccess = ioFile.write() self.assertTrue(writeSuccess) ioFile.read() self.assertListEqual(ioFile.content, FILE_CONTENT) os.remove(ioFile.file)
def textileToHtml( fileIn, fileOut, title ): ''' This Definition Outputs A Textile File To HTML. @param fileIn: File To Convert. ( String ) @param fileOut: Output File. ( String ) @param title: HTML File Title. ( String ) ''' LOGGER.info( "{0} | Converting '{1}' Textile File To HTML !".format( textileToHtml.__name__, fileIn ) ) file = File( fileIn ) file.read() output = [] output.append( "<html>\n\t<head>\n" ) output.append( "\t\t<title>{0}</title>\n".format( title ) ) output.append( """\t\t<style type="text/css"> body { text-align: justify; font-size: 10pt; margin: 10px 10px 10px 10px; background-color: rgb(192, 192, 192); color: rgb(50, 50, 50); } A:link { text-decoration: none; color: rgb(50, 85, 125); } A:visited { text-decoration: none; color: rgb(50, 85, 125); } A:active { text-decoration: none; color: rgb(50, 85, 125); } A:hover { text-decoration: underline; color: rgb(50, 85, 125); } </style>\n""" ) output.append( "\t</head>\n\t<body>\n\t" ) output.append( "\n\t".join( [line for line in textile.textile( "".join( file.content ) ).split( "\n" ) if line] ) ) output.append( "\t\t</span>\n" ) output.append( "\t</body>\n</html>" ) file = File( fileOut ) file.content = output file.write()
def testAppend(self): """ This method tests :meth:`foundations.io.File.append` method. """ ioFile = File(tempfile.mkstemp()[1]) self.assertIsInstance(ioFile.content, list) ioFile.content = FILE_CONTENT ioFile.write() append = ioFile.append() self.assertTrue(append) ioFile.read() self.assertListEqual(ioFile.content, FILE_CONTENT + FILE_CONTENT) os.remove(ioFile.file)
def bleach(file): """ Sanitizes given python module. :param file: Python module file. :type file: unicode :return: Definition success. :rtype: bool """ LOGGER.info("{0} | Sanitizing '{1}' python module!".format(__name__, file)) sourceFile = File(file) content = sourceFile.read() for pattern in STATEMENT_SUBSTITUTE: matches = [match for match in re.finditer(pattern, content, re.DOTALL)] offset = 0 for match in matches: start, end = match.start("bleach"), match.end("bleach") substitution = "{0}{1}".format(STATEMENT_UPDATE_MESSAGE, re.sub("\n", "\n{0}".format(STATEMENT_UPDATE_MESSAGE), match.group("bleach"))) content = "".join((content[0: start + offset], substitution, content[end + offset:])) offset += len(substitution) - len(match.group("bleach")) sourceFile.content = [content] sourceFile.write() return True
def bleach(file): """ Sanitizes given python module. :param file: Python module file. :type file: unicode :return: Definition success. :rtype: bool """ LOGGER.info("{0} | Sanitizing '{1}' python module!".format(__name__, file)) sourceFile = File(file) content = sourceFile.read() for pattern in STATEMENT_SUBSTITUTE: matches = [match for match in re.finditer(pattern, content, re.DOTALL)] offset = 0 for match in matches: start, end = match.start("bleach"), match.end("bleach") substitution = "{0}{1}".format(STATEMENT_UPDATE_MESSAGE, re.sub("\n", "\n{0}".format(STATEMENT_UPDATE_MESSAGE), match.group("bleach"))) content = "".join((content[0: start + offset], substitution, content[end + offset:])) offset += len(substitution) - len(match.group("bleach")) sourceFile.content = [content] sourceFile.write() return True
def testRead(self): """ Tests :meth:`foundations.io.File.read` method. """ ioFile = File(TEXT_FILE) self.assertIsInstance(ioFile.content, list) content = ioFile.read() self.assertIsInstance(ioFile.content, list) self.assertEqual(content, "".join(FILE_CONTENT))
def testRead(self): """ This method tests :meth:`foundations.io.File.read` method. """ ioFile = File(TEST_FILE) self.assertIsInstance(ioFile.content, list) readSuccess = ioFile.read() self.assertTrue(readSuccess) self.assertIsInstance(ioFile.content, list) self.assertListEqual(ioFile.content, FILE_CONTENT)
def reStructuredTextToHtml(fileIn, fileOut): """ This definition outputs a reStructuredText file to html. :param fileIn: File to convert. ( String ) :param fileOut: Output file. ( String ) """ LOGGER.info("{0} | Converting '{1}' reStructuredText file to html!".format(reStructuredTextToHtml.__name__, fileIn)) os.system("{0} --stylesheet-path='{1}' '{2}' > '{3}'".format(RST2HTML, os.path.join(os.path.dirname(__file__), CSS_FILE), fileIn, fileOut)) LOGGER.info("{0} | Formatting html file!".format("Tidy")) os.system("tidy -config {0} -m '{1}'".format(os.path.join(os.path.dirname(__file__), TIDY_SETTINGS_FILE), fileOut)) file = File(fileOut) file.read() LOGGER.info("{0} | Replacing spaces with tabs!".format(reStructuredTextToHtml.__name__)) file.content = [line.replace(" " * 4, "\t") for line in file.content] file.write()