Skip to content

move_files

Move a corpus file from oldpath to newpath.

compute_movenames(oldpath, newpath)

Make CorpusPath pairs for the files that needs to move.

Parameters:

Name Type Description Default
oldpath Path

path to the old file

required
newpath Path

path to the new file, with normalised name

required

Returns:

Type Description
list[tuple]

List of tuples of the files that needs to be moved

Source code in /home/anders/projects/CorpusTools/corpustools/move_files.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def compute_movenames(oldpath, newpath):
    """Make CorpusPath pairs for the files that needs to move.

    Args:
        oldpath (Path): path to the old file
        newpath (Path): path to the new file, with normalised name

    Returns:
        (list[tuple]): List of tuples of the files that needs to be moved
    """
    filepairs = [
        (oldpath, newpath),
    ]
    filepairs.extend(compute_parallel_movenames(oldpath, newpath))

    return filepairs

compute_parallel_movenames(old_corpuspath, new_corpuspath)

Compute pairs of CorpusPaths for parallel files.

Parameters:

Name Type Description Default
old_corpuspath CorpusPath

the existing file

required
new_corpuspath CorpusPath

the new name of the file

required

Returns:

Type Description
list[tuple]

List of tuples of the parallel files that needs to be moved

Source code in /home/anders/projects/CorpusTools/corpustools/move_files.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def compute_parallel_movenames(old_corpuspath, new_corpuspath):
    """Compute pairs of CorpusPaths for parallel files.

    Args:
        old_corpuspath (CorpusPath): the existing file
        new_corpuspath (CorpusPath): the new name of the file

    Returns:
        (list[tuple]): List of tuples of the parallel files that needs to
            be moved
    """

    return (
        [
            (
                corpuspath.make_corpus_path(
                    old_corpuspath.name(
                        corpus_lang=para_lang,
                        filepath=old_corpuspath.filepath.with_name(para_name),
                    )
                ),
                corpuspath.make_corpus_path(
                    new_corpuspath.name(
                        corpus_lang=para_lang,
                        filepath=new_corpuspath.filepath.with_name(para_name),
                    )
                ),
            )
            for (
                para_lang,
                para_name,
            ) in old_corpuspath.metadata.get_parallel_texts().items()
        ]
        if is_parallel_move_needed(old_corpuspath.filepath, new_corpuspath.filepath)
        else []
    )

main()

Move a file.

Source code in /home/anders/projects/CorpusTools/corpustools/move_files.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def main():
    """Move a file."""
    args = mover_parse_args()
    if args.oldpath == args.newpath:
        print(
            f"{args.oldpath} and {args.newpath} are the same file",
            file=sys.stderr,
        )
    else:
        oldpath = Path(args.oldpath)
        newpath = Path(args.newpath)

        if not newpath.suffix:
            newpath = newpath / oldpath.name

        try:
            mover(
                corpuspath.make_corpus_path(oldpath),
                corpuspath.make_corpus_path(namechanger.compute_new_basename(newpath)),
            )
        except UserWarning as e:
            print("Can not move file:", str(e), file=sys.stderr)

move_corpuspath(old_corpuspath, new_corpuspath)

Move a set of corpus files to a new location.

Source code in /home/anders/projects/CorpusTools/corpustools/move_files.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def move_corpuspath(old_corpuspath, new_corpuspath):
    """Move a set of corpus files to a new location."""
    orig_vcs = versioncontrol.vcs(old_corpuspath.orig_corpus_dir)
    conv_vcs = versioncontrol.vcs(old_corpuspath.converted_corpus_dir)

    new_corpuspath.orig.parent.mkdir(exist_ok=True, parents=True)
    orig_vcs.move(old_corpuspath.orig, new_corpuspath.orig)
    orig_vcs.move(old_corpuspath.xsl, new_corpuspath.xsl)

    if old_corpuspath.converted.exists():
        new_corpuspath.converted.parent.mkdir(exist_ok=True, parents=True)
        try:
            conv_vcs.move(old_corpuspath.converted, new_corpuspath.converted)
        except GitCommandError:
            old_corpuspath.converted.unlink()

    if not old_corpuspath.metadata.get_variable("translated_from"):
        for lang in old_corpuspath.metadata.get_parallel_texts():
            if old_corpuspath.tmx(lang).exists():
                old_corpuspath.tmx(lang).mkdir(exist_ok=True, parents=True)
                conv_vcs.move(old_corpuspath.tmx(lang), new_corpuspath.tmx(lang))

mover(oldpath, newpath)

Move filepairs and update metadata.

Source code in /home/anders/projects/CorpusTools/corpustools/move_files.py
127
128
129
130
131
132
def mover(oldpath, newpath):
    """Move filepairs and update metadata."""
    filepairs = compute_movenames(oldpath, newpath)
    update_metadata(filepairs)
    for filepair in filepairs:
        move_corpuspath(old_corpuspath=filepair[0], new_corpuspath=filepair[1])

mover_parse_args()

Parse the commandline options.

Returns:

Type Description
argparse.Namespace

the parsed commandline arguments

Source code in /home/anders/projects/CorpusTools/corpustools/move_files.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
def mover_parse_args():
    """Parse the commandline options.

    Returns:
        (argparse.Namespace): the parsed commandline arguments
    """
    parser = argparse.ArgumentParser(
        parents=[argparse_version.parser],
        description="Program to move or rename a file inside the corpus.",
    )

    parser.add_argument("oldpath", help="The path of the old file.")
    parser.add_argument(
        "newpath",
        help="The place to move the file to. newpath can "
        "be either a filename or a directory",
    )

    return parser.parse_args()

remove_main()

Remove a file.

Source code in /home/anders/projects/CorpusTools/corpustools/move_files.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
def remove_main():
    """Remove a file."""
    args = remover_parse_args()
    try:
        old_corpuspath = corpuspath.make_corpus_path(args.oldpath)
        remove_metadata(old_corpuspath)

        orig_vcs = versioncontrol.vcs(old_corpuspath.orig_corpus_dir)
        orig_vcs.remove(old_corpuspath.orig)
        orig_vcs.remove(old_corpuspath.xsl)

        conv_vcs = versioncontrol.vcs(old_corpuspath.converted_corpus_dir)

        if Path(old_corpuspath.converted).exists():
            conv_vcs.remove(old_corpuspath.converted)

        for lang in old_corpuspath.metadata.get_parallel_texts():
            tmx_path = Path(old_corpuspath.tmx(lang))
            if tmx_path.exists():
                conv_vcs.remove(tmx_path.as_posix())

    except UserWarning as e:
        print("Can not remove file:", str(e), file=sys.stderr)

remove_metadata(remove_path)

Remove parallel info about remove_path.

Source code in /home/anders/projects/CorpusTools/corpustools/move_files.py
180
181
182
183
184
185
186
187
188
189
190
191
192
def remove_metadata(remove_path):
    """Remove parallel info about remove_path."""
    for para_lang, para_name in remove_path.metadata.get_parallel_texts().items():
        para_path = corpuspath.make_corpus_path(
            remove_path.name(
                corpus_lang=para_lang,
                filepath=remove_path.filepath.with_name(para_name),
            )
        )
        para_path.metadata.set_parallel_text(language=remove_path.lang, location="")
        para_path.metadata.write_file()
        parallel_vcs = versioncontrol.vcs(para_path.orig_corpus_dir)
        parallel_vcs.add(para_path.xsl)

remover_parse_args()

Parse the commandline options.

Returns:

Type Description
argparse.Namespace

the parsed commandline arguments

Source code in /home/anders/projects/CorpusTools/corpustools/move_files.py
195
196
197
198
199
200
201
202
203
204
205
206
207
208
def remover_parse_args():
    """Parse the commandline options.

    Returns:
        (argparse.Namespace): the parsed commandline arguments
    """
    parser = argparse.ArgumentParser(
        parents=[argparse_version.parser],
        description="Program to remove a file from the corpus.",
    )

    parser.add_argument("oldpath", help="The path of the old file.")

    return parser.parse_args()

update_metadata(filepairs)

Update parallel info for all filepairs.

Source code in /home/anders/projects/CorpusTools/corpustools/move_files.py
29
30
31
32
33
34
35
36
37
38
39
40
41
def update_metadata(filepairs):
    """Update parallel info for all filepairs."""
    for filepair in filepairs:
        if filepair[0].filepath.name != filepair[1].filepath.name:
            for parallel_name in filepair[0].parallels():
                parallel_path = corpuspath.make_corpus_path(parallel_name)
                parallel_path.metadata.set_parallel_text(
                    language=filepair[1].lang,
                    location=filepair[1].filepath.name,
                )
                parallel_path.metadata.write_file()
                parallel_vcs = versioncontrol.vcs(parallel_path.orig_corpus_dir)
                parallel_vcs.add(parallel_path.xsl)