Skip to content

packagetmx

Add tmx files to a zipfile.

PackageTmx

A class to package tmx files into a zip file.

Source code in /home/anders/projects/CorpusTools/corpustools/packagetmx.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class PackageTmx:
    """A class to package tmx files into a zip file."""

    def __init__(self, packagename):
        """Set the counter on which filenames are based."""
        self.fileId = 1
        self.date = time.strftime("%Y-%m-%d", time.localtime())
        self.packagename = packagename
        self.zipname = self.packagename + "-" + self.date + ".zip"
        self.zipfile = zipfile.ZipFile(self.zipname, mode="w")

    def __del__(self):
        """Close the zipfile."""
        print(f"{self.packagename} tmx files are in {self.zipname}")
        self.zipfile.close()

    def find_tmx_files(self, tmxdir):
        """Find the tmx files in dirname, return them as a list."""
        return (
            path.as_posix()
            for path in corpuspath.collect_files([tmxdir], suffix=".tmx")
        )

    def generate_filename(self):
        """Generate a new file name. Return the new filename."""
        name = "".join([self.packagename, "-", self.date, f"-{self.fileId:06d}", ".tmx"])
        self.fileId += 1

        return name

    def write_new_file(self, tmxFile):
        """Write the file to the zipfile with a new filename."""
        self.zipfile.write(
            tmxFile,
            arcname=self.generate_filename(),
            compress_type=zipfile.ZIP_DEFLATED,
        )

__del__()

Close the zipfile.

Source code in /home/anders/projects/CorpusTools/corpustools/packagetmx.py
40
41
42
43
def __del__(self):
    """Close the zipfile."""
    print(f"{self.packagename} tmx files are in {self.zipname}")
    self.zipfile.close()

__init__(packagename)

Set the counter on which filenames are based.

Source code in /home/anders/projects/CorpusTools/corpustools/packagetmx.py
32
33
34
35
36
37
38
def __init__(self, packagename):
    """Set the counter on which filenames are based."""
    self.fileId = 1
    self.date = time.strftime("%Y-%m-%d", time.localtime())
    self.packagename = packagename
    self.zipname = self.packagename + "-" + self.date + ".zip"
    self.zipfile = zipfile.ZipFile(self.zipname, mode="w")

find_tmx_files(tmxdir)

Find the tmx files in dirname, return them as a list.

Source code in /home/anders/projects/CorpusTools/corpustools/packagetmx.py
45
46
47
48
49
50
def find_tmx_files(self, tmxdir):
    """Find the tmx files in dirname, return them as a list."""
    return (
        path.as_posix()
        for path in corpuspath.collect_files([tmxdir], suffix=".tmx")
    )

generate_filename()

Generate a new file name. Return the new filename.

Source code in /home/anders/projects/CorpusTools/corpustools/packagetmx.py
52
53
54
55
56
57
def generate_filename(self):
    """Generate a new file name. Return the new filename."""
    name = "".join([self.packagename, "-", self.date, f"-{self.fileId:06d}", ".tmx"])
    self.fileId += 1

    return name

write_new_file(tmxFile)

Write the file to the zipfile with a new filename.

Source code in /home/anders/projects/CorpusTools/corpustools/packagetmx.py
59
60
61
62
63
64
65
def write_new_file(self, tmxFile):
    """Write the file to the zipfile with a new filename."""
    self.zipfile.write(
        tmxFile,
        arcname=self.generate_filename(),
        compress_type=zipfile.ZIP_DEFLATED,
    )

main()

Make a package containing the tmx files.

Source code in /home/anders/projects/CorpusTools/corpustools/packagetmx.py
78
79
80
81
82
83
84
85
86
87
def main():
    """Make a package containing the tmx files."""
    args = parse_options()

    corpusdir = Path(args.corpusdir).resolve()
    lang1 = corpusdir.parts[-1].split("-")[1]
    for tmxdir in corpusdir.glob("tmx/*"):
        packagetmx = PackageTmx(f"{lang1}2{tmxdir.name}")
        for filename in packagetmx.find_tmx_files(tmxdir):
            packagetmx.write_new_file(filename)

parse_options()

Parse the command line. No arguments expected.

Source code in /home/anders/projects/CorpusTools/corpustools/packagetmx.py
68
69
70
71
72
73
74
75
def parse_options():
    """Parse the command line. No arguments expected."""
    parser = argparse.ArgumentParser(
        description="Package tmx files found in a corpus directory into zipfiles."
    )
    parser.add_argument("corpusdir", help="A corpus directory where tmx files exist")

    return parser.parse_args()