This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.py
95 lines (74 loc) · 3.25 KB
/
files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import subprocess
import pathlib
import json
def tryDirectory(absolutePath, dirOrFile, dirName, tryMakeFolder=False):
"""
Function for checking if a directory exists and/or fulfils certain requirements.
WIll raise an Exception or ValueError if it doesn't meet these expectations.
Error printouts designed to reflect internal file operations.
"""
if not absolutePath.exists():
if not tryMakeFolder:
raise ValueError(f"The {dirName} ({absolutePath}) doesn't exist.")
else:
try:
absolutePath.mkdir(parents=True, exist_ok=True)
except Exception as e:
raise Exception(f"Couldn't make the {dirName} ({absolutePath}). ({e})" )
else:
if dirOrFile == "file" and absolutePath.is_dir():
raise ValueError(f"{dirName} ({absolutePath}) is a folder, not a file.")
elif dirOrFile == "dir" and absolutePath.is_file():
raise ValueError(f"{dirName} ({absolutePath}) is a file, not a folder.")
def tryUserDirectory(absolutePath, dirOrFile, dirName, tryMakeFolder=False):
"""
Function for checking if a directory exists and/or fulfils certain requirements.
WIll raise an Exception or ValueError if it doesn't meet these expectations.
With error printouts designed to reflect end-user inputs.
"""
if not absolutePath.exists():
if not tryMakeFolder:
raise ValueError(f"The {dirName} you gave ({absolutePath}) doesn't exist.")
else:
try:
absolutePath.mkdir(parents=True, exist_ok=True)
except Exception as e:
raise Exception(f"Couldn't make the {dirName} ({absolutePath}). ({e})" )
else:
if dirOrFile == "file" and absolutePath.is_dir():
raise ValueError(f"The {dirName} you gave ({absolutePath}) is a folder, not a file.")
elif dirOrFile == "dir" and absolutePath.is_file():
raise ValueError(f"The {dirName} you gave ({absolutePath}) is a file, not a folder.")
def loadJson(jsonPath, fileName):
"""
Repetitive function for attempting to load a JSON file.
"""
try:
with open(jsonPath, "r") as read_file:
return json.load(read_file)
except Exception as e:
raise ValueError(f"Loading the {fileName} file failed! ({e})")
def writeFile(path, contents, exceptionString):
"""
A basic repetitive function that tries to write something to a file.
"""
try:
with open(path, 'wb') as file:
file.write(contents)
except Exception:
raise Exception(exceptionString)
def compileTTX(input, output):
"""
Invokes the TTX compiler and attempts to compile a font with it.
(this can be for multiple purposes, either compiling a font by TTX or
just for using the TTX compiler as an extra testing mechanism.)
"""
# feed the assembled TTX as input to the ttx command line tool.
cmd_ttx = ['ttx', '-q', '-o', output, input]
# try to export temporary PNG
try:
r = subprocess.run(cmd_ttx, stdout=subprocess.DEVNULL).returncode
except Exception as e:
raise Exception('TTX compiler invocation failed: ' + str(e))
if r:
raise Exception('TTX compiler returned error code: ' + str(r))