files.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import subprocess
  2. import pathlib
  3. import json
  4. def tryDirectory(absolutePath, dirOrFile, dirName, tryMakeFolder=False):
  5. """
  6. Function for checking if a directory exists and/or fulfils certain requirements.
  7. WIll raise an Exception or ValueError if it doesn't meet these expectations.
  8. Error printouts designed to reflect internal file operations.
  9. """
  10. if not absolutePath.exists():
  11. if not tryMakeFolder:
  12. raise ValueError(f"The {dirName} ({absolutePath}) doesn't exist.")
  13. else:
  14. try:
  15. absolutePath.mkdir(parents=True, exist_ok=True)
  16. except Exception as e:
  17. raise Exception(f"Couldn't make the {dirName} ({absolutePath}). ({e})" )
  18. else:
  19. if dirOrFile == "file" and absolutePath.is_dir():
  20. raise ValueError(f"{dirName} ({absolutePath}) is a folder, not a file.")
  21. elif dirOrFile == "dir" and absolutePath.is_file():
  22. raise ValueError(f"{dirName} ({absolutePath}) is a file, not a folder.")
  23. def tryUserDirectory(absolutePath, dirOrFile, dirName, tryMakeFolder=False):
  24. """
  25. Function for checking if a directory exists and/or fulfils certain requirements.
  26. WIll raise an Exception or ValueError if it doesn't meet these expectations.
  27. With error printouts designed to reflect end-user inputs.
  28. """
  29. if not absolutePath.exists():
  30. if not tryMakeFolder:
  31. raise ValueError(f"The {dirName} you gave ({absolutePath}) doesn't exist.")
  32. else:
  33. try:
  34. absolutePath.mkdir(parents=True, exist_ok=True)
  35. except Exception as e:
  36. raise Exception(f"Couldn't make the {dirName} ({absolutePath}). ({e})" )
  37. else:
  38. if dirOrFile == "file" and absolutePath.is_dir():
  39. raise ValueError(f"The {dirName} you gave ({absolutePath}) is a folder, not a file.")
  40. elif dirOrFile == "dir" and absolutePath.is_file():
  41. raise ValueError(f"The {dirName} you gave ({absolutePath}) is a file, not a folder.")
  42. def loadJson(jsonPath, fileName):
  43. """
  44. Repetitive function for attempting to load a JSON file.
  45. """
  46. try:
  47. with open(jsonPath, "r") as read_file:
  48. return json.load(read_file)
  49. except Exception as e:
  50. raise ValueError(f"Loading the {fileName} file failed! ({e})")
  51. def writeFile(path, contents, exceptionString):
  52. """
  53. A basic repetitive function that tries to write something to a file.
  54. """
  55. try:
  56. with open(path, 'wb') as file:
  57. file.write(contents)
  58. except Exception:
  59. raise Exception(exceptionString)
  60. def compileTTX(input, output):
  61. """
  62. Invokes the TTX compiler and attempts to compile a font with it.
  63. (this can be for multiple purposes, either compiling a font by TTX or
  64. just for using the TTX compiler as an extra testing mechanism.)
  65. """
  66. # feed the assembled TTX as input to the ttx command line tool.
  67. cmd_ttx = ['ttx', '-q', '-o', output, input]
  68. # try to export temporary PNG
  69. try:
  70. r = subprocess.run(cmd_ttx, stdout=subprocess.DEVNULL).returncode
  71. except Exception as e:
  72. raise Exception('TTX compiler invocation failed: ' + str(e))
  73. if r:
  74. raise Exception('TTX compiler returned error code: ' + str(r))