Coverage for /usr/lib/python3/dist-packages/fontTools/misc/cliTools.py: 22%
18 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
1"""Collection of utilities for command-line interfaces and console scripts."""
2import os
3import re
6numberAddedRE = re.compile(r"#\d+$")
9def makeOutputFileName(
10 input, outputDir=None, extension=None, overWrite=False, suffix=""
11):
12 """Generates a suitable file name for writing output.
14 Often tools will want to take a file, do some kind of transformation to it,
15 and write it out again. This function determines an appropriate name for the
16 output file, through one or more of the following steps:
18 - changing the output directory
19 - appending suffix before file extension
20 - replacing the file extension
21 - suffixing the filename with a number (``#1``, ``#2``, etc.) to avoid
22 overwriting an existing file.
24 Args:
25 input: Name of input file.
26 outputDir: Optionally, a new directory to write the file into.
27 suffix: Optionally, a string suffix is appended to file name before
28 the extension.
29 extension: Optionally, a replacement for the current file extension.
30 overWrite: Overwriting an existing file is permitted if true; if false
31 and the proposed filename exists, a new name will be generated by
32 adding an appropriate number suffix.
34 Returns:
35 str: Suitable output filename
36 """
37 dirName, fileName = os.path.split(input)
38 fileName, ext = os.path.splitext(fileName)
39 if outputDir:
40 dirName = outputDir
41 fileName = numberAddedRE.split(fileName)[0]
42 if extension is None:
43 extension = os.path.splitext(input)[1]
44 output = os.path.join(dirName, fileName + suffix + extension)
45 n = 1
46 if not overWrite:
47 while os.path.exists(output):
48 output = os.path.join(
49 dirName, fileName + suffix + "#" + repr(n) + extension
50 )
51 n += 1
52 return output