Exemple #1
0
    def test_util_file_create_if_necessary__file_read(self):
        if self._test_exec("test_file_create_if_necessary"):
            Prg = self.Prg
            Fname = os.path.join(Prg["DirWork"],
                                 "test_file_create_if_necessary.txt")
            util.file_del(Fname)

            Content = "Cloud\nRain\nSun\r\nSnow   "
            Created = util.file_create_if_necessary(Prg,
                                                    Fname,
                                                    ContentDefault=Content)
            self.assertTrue(Created)

            RetRead, ContentReadAll = util.file_read_all(Prg, Fname)
            self.assertEqual(ContentReadAll, Content)

            ContentReadLines = util.file_read_lines(Prg, Fname, Lower=True)
            LinesWanted = ["cloud\n", "rain\n", "sun\r\n", "snow   "]
            self.assertEqual(LinesWanted, ContentReadLines)

            ContentReadLines = util.file_read_lines(Prg, Fname, Strip=True)
            LinesWanted = ["Cloud", "Rain", "Sun", "Snow"]
            self.assertEqual(LinesWanted, ContentReadLines)

            util.file_del(Fname)
Exemple #2
0
 def test_util_file_read_lines(self):
     if self._test_exec("test_file_read_lines"):
         Prg = self.Prg
         Fname = os.path.join(Prg["DirWork"], "test_file_read_lines.txt")
         util.file_write(Prg, Fname=Fname, Content="cat\ndog\nelephant")
         Lines = util.file_read_lines(Prg, Fname, Strip=True)
         self.assertEqual(Lines, ["cat", "dog", "elephant"])
         util.file_del(Fname)
Exemple #3
0
 def test_fun_pdf_to_text_converter(self):
     if self._test_exec("test_fun_pdf_to_text_converter"):
         Prg = self.Prg
         FileTxt = os.path.join(Prg["DirWork"],
                                "test_converted_from_pdf.txt")
         util.file_del(FileTxt)
         FilePdf = os.path.join(Prg["DirTestFiles"],
                                "test_pdf_conversion.pdf")
         Prg["ConverterPdfToText"](Prg, FilePdf, FileTxt)
         FileLines = util.file_read_lines(Prg, FileTxt, Strip=True)
         self.assertEqual(FileLines[0], "This is new document.")
         util.file_del(FileTxt)
Exemple #4
0
    def test_collect_docs_from_working_dir(self):
        if self._test_exec("test_collect_docs_from_working_dir"):
            Prg = self.Prg

            FileName = "test_file_document_example.txt"
            FilePath = os.path.join(Prg["DirDocuments"], FileName)
            util.file_del(FilePath)
            util.file_write(Prg, Fname=FilePath, Content="example text")
            DocumentsAvailable = document.document_objects_collect_from_dir_documents(
                Prg)

            self.assertIn(util.filename_without_extension(FileName),
                          DocumentsAvailable)
            util.file_del(FilePath)
Exemple #5
0
def doc_objects_delete__basename(Prg, BaseNameNoExt):
    if DocObj := Prg["DocumentObjectsLoaded"].pop(BaseNameNoExt, None):
        util.file_del(DocObj["FileOrigPathAbs"])
        util.file_del(DocObj["FileTextPathAbs"])
        util.file_del(DocObj["FileIndex"])
        util.file_del(DocObj["FileSentences"])
        util_json_obj.doc_source_webpages_update_in_file_and_Prg(
            Prg, BaseNameNoExtRemove=BaseNameNoExt)
Exemple #6
0
    def test_docs_copy_samples_into_dir(self):
        if self._test_exec("test_collect_docs_from_working_dir"):
            Prg = self.Prg

            DirTestBaseName = "test_" + str(int(time.time()))
            DirTest = os.path.join(Prg["DirWork"], DirTestBaseName)
            util.dir_create_if_necessary(Prg, DirTest)

            document.docs_copy_samples_into_dir(Prg, DirTest)
            Files = util.files_abspath_collect_from_dir(DirTest)
            FileNamesInOneLine = " ".join(Files)
            Wanted = "DanielDefoe__LifeAdventuresRobinsonCrusoe__gutenberg_org_521-0.txt"
            self.assertIn(Wanted, FileNamesInOneLine)

            for FileAbsPath in Files:
                util.file_del(FileAbsPath)

            DelRes = util.dir_delete_if_exist(Prg, DirTest)
            self.assertEqual("deleted", DelRes)
Exemple #7
0
    def test_file_create_sentences__create_index(self):
        self.maxDiff = None
        if self._test_exec("test_file_create_sentences__create_index"):
            Prg = self.Prg

            FileSentences = os.path.join(Prg["DirWork"],
                                         "test_file_create_sentences.txt")
            util.file_del(FileSentences)

            Sample = 'He is my friend. "This is \n the next - city, London -- here, in London, the sky is nice." Is this the third line, or a Book about London?'

            seeker.file_sentence_create(Prg, FileSentences, Sample)
            Wanted = [
                "He is my friend. \n",  # detect London only once from this sentence:
                '"This is the next - city, London -- here, in London, the sky is nice." \n',
                "Is this the third line, or a Book about London?"
            ]

            LinesFromFile = util.file_read_lines(Prg, FileSentences)
            self.assertEqual(Wanted, LinesFromFile)

            FileIndex = os.path.join(Prg["DirWork"],
                                     "test_file_create_index.txt")
            util.file_del(FileIndex)
            seeker.file_index_create(Prg,
                                     FileIndex,
                                     FileSentences,
                                     ForcedWrite=True)
            #seeker.file_index_create(Prg, "/tmp/index.txt", FileSentences)
            # print(util.file_read_all(Prg, FileIndex))

            MultiSub = Prg["SubSentenceMultiplier"]
            MultiSubAndWord = Prg["SubSentenceMultiplier"] * Prg[
                "WordPositionMultiplier"]
            _Status, WordPosition = util_json_obj.obj_from_file(FileIndex)

            self.assertEqual(set(WordPosition["london"]),
                             set([10100, 10201, 20104]))

            util.file_del(FileSentences)
            util.file_del(FileIndex)
Exemple #8
0
def doc_objects_delete__file_abspath(Prg, FileAbsPathWithExt):
    BaseName = os.path.basename(FileAbsPathWithExt)
    BaseNameNoExt = util.filename_without_extension(BaseName)
    doc_objects_delete__basename(Prg, BaseNameNoExt)
    util.file_del(FileAbsPathWithExt
                  )  # del orig file in every case, if DocObj doesn't exis
Exemple #9
0
    def test_util_file_write_append_del(self):
        if self._test_exec("test_file_write_append_del"):
            Prg = self.Prg

            Ret = util.file_write(Prg, Fname="")
            self.assertFalse(Ret)

            Content = "apple "
            Fname = os.path.join(Prg["DirWork"], "test_file_write.txt")
            RetWrite = util.file_write(Prg, Fname=Fname, Content=Content)
            RetAppend = util.file_append(Prg, Fname=Fname, Content="tree")
            self.assertTrue(RetWrite)
            self.assertTrue(RetAppend)
            RetRead, ContentRead = util.file_read_all(Prg, Fname)

            self.assertTrue(RetRead)
            self.assertEqual(ContentRead, "apple tree")

            FileState, FileGzipped = util.file_is_gzipped(Prg, Fname)
            self.assertEqual("file_exists", FileState)
            self.assertEqual("not_gzipped", FileGzipped)

            Sample = "Árvíztűrő tükörfúrógép"
            RetWriteGz = util.file_write(Prg,
                                         Fname=Fname,
                                         Content=Sample,
                                         Gzipped=True)
            self.assertTrue(RetWriteGz)
            RetReadGz, ContentReadGz = util.file_read_all(Prg,
                                                          Fname,
                                                          Gzipped=True)
            self.assertTrue(RetReadGz)
            self.assertEqual(ContentReadGz, Sample)

            FileState, FileGzipped = util.file_is_gzipped(Prg, Fname)
            self.assertEqual("file_exists", FileState)
            self.assertEqual("gzipped", FileGzipped)

            BinWanted = Sample.encode()
            util.file_write_utf8_error_avoid(Prg, Fname, Sample)
            BinFromFile = util.file_read_all_simple(Fname, "rb")
            # print("\n######### >>" + util.file_read_all(Prg, Fname)[1] + "<<")
            # print("\n######### >>", Sample.encode(), "<<")
            self.assertEqual(BinWanted, BinFromFile)

            FileWriteRet = util.file_write_with_check(Prg, Fname, Sample)
            TxtFromFile = util.file_read_all_simple(Fname)
            self.assertEqual(TxtFromFile, Sample)
            self.assertTrue(FileWriteRet)

            util.file_write_with_check(Prg, Fname,
                                       "")  # clear the content of the file

            # writing is unsuccessful because writer fun doesn't do anything
            def empty_writer_fun(Prg, Fname, Sample):
                pass

            FileWriteRet = util.file_write_with_check(
                Prg, Fname, Sample, WriterFun=empty_writer_fun)
            self.assertFalse(FileWriteRet)

            RetDel1 = util.file_del(Fname)
            RetDel2 = util.file_del(Fname)
            self.assertTrue(RetDel1)
            self.assertFalse(RetDel2)

            FileState, FileGzipped = util.file_is_gzipped(Prg, Fname)
            self.assertEqual("file_not_found", FileState)
            self.assertEqual("", FileGzipped)