def test_put_file_contents_str(self): s, fpath = "123", os.path.join(self.resources, "foo.bar") self.assertTrue(not os.path.isfile(fpath)) self.assertTrue(Doraemon.put_file_contents(fpath, s)) self.assertTrue(os.path.isfile(fpath)) self.assertEqual(1, len(Doraemon.get_file_contents(fpath))) self.assertEqual(s, Doraemon.get_file_contents(fpath)[0]) if os.path.isfile(fpath): os.remove(fpath) self.assertTrue(not os.path.isfile(fpath))
def test_put_file_contents_list(self): _l, fpath = range(0, 10), os.path.join(self.resources, "foo.bar") self.assertTrue(not os.path.isfile(fpath)) self.assertTrue(Doraemon.put_file_contents(fpath, _l)) self.assertTrue(os.path.isfile(fpath)) self.assertEqual(10, len(Doraemon.get_file_contents(fpath))) self.assertEqual(map(lambda num: str(num), _l), Doraemon.get_file_contents(fpath)) if os.path.isfile(fpath): os.remove(fpath) self.assertTrue(not os.path.isfile(fpath))
def test_get_file_filter(self): self.assertEqual( 0, len( Doraemon.get_files(self.resources, file_filter=lambda fpath: ".foobar" == fpath .split(".")[-1])))
def save_transferred(self, raw_fname, transferred, output_dir=None): output_fpath = "transferred_{0}.txt".format( os.path.splitext(os.path.basename(raw_fname))[0]) if output_dir is not None: if not os.path.isdir(output_dir): os.makedirs(output_dir) output_fpath = os.path.join(output_dir, output_fpath) return (Doraemon.put_file_contents(output_fpath, transferred), output_fpath)
def test_save_transferred(self): tvaf = TransferVoiceAnnotationFiles() tag_file = os.path.join(self.resources, "test.TAG") transferred = tvaf.transfer_tagfile(tag_file) is_saved, output_fpath = tvaf.save_transferred(tag_file, transferred) self.assertTrue(is_saved) self.assertTrue(os.path.isfile(output_fpath)) self.assertEqual(transferred, Doraemon.get_file_contents(output_fpath)) if os.path.isfile(output_fpath): os.remove(output_fpath) self.assertTrue(not os.path.isfile(output_fpath))
def transfer_tagfile(self, tagfile_path, in_charset=None, out_charset=None): transferred = [] lines = Doraemon.get_file_contents(tagfile_path, in_charset=in_charset, out_charset=out_charset) for line in lines: line = line.split(" ") try: if len(line) < 4 or line[1].startswith("0"): continue except Exception: continue transferred.append(line[-1]) return transferred
def test_get_files_by_suffix_lower(self): self.assertTrue( len(Doraemon.get_files_by_suffix(self.resources, ".TXT")) > 0)
def test_get_files_by_suffix(self): self.assertEqual( 0, len(Doraemon.get_files_by_suffix(self.resources, ".foobar")))
def test_get_file_by_get_files(self): filepath = os.path.join(self.resources, "none_contents_file.txt") files = Doraemon.get_files(filepath) self.assertEqual(files, [filepath])
def test_get_files(self): files = Doraemon.get_files(self.resources) self.assertTrue( os.path.join(self.resources, "d", "e", "2.txt") in files)
def test_get_none_contents_file_contents(self): fpath = os.path.join(self.resources, "none_contents_file.txt") self.assertEqual(0, len(Doraemon.get_file_contents(fpath)))
# coding=utf-8 import os import sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) if __name__ == "__main__": from doraemon import Doraemon fpath = sys.argv[1] contents = Doraemon.get_file_contents(fpath, in_charset="gbk", out_charset="utf-8") Doraemon.put_file_contents("utf_8_{0}".format(os.path.basename(fpath)), contents)
def get_tagfiles(self, input_dir): return Doraemon.get_files_by_suffix(input_dir, ".tag")